var users_ul;
var users_ul_li;
var users_ul_li_width;
var last_width = 0;

function fix_userlist() {
	users_ul = $('.userpage-pages').eq(0);
	if (users_ul.length > 0) {
		users_ul_li = users_ul.children('li');
		users_ul_li_width = 188 + 40;//users_ul_li.eq(0).width() + 40;
		fix_userlist_do();
	}
}
fix_userlist_do = function() {
	var new_width = users_ul.parent().width();
	if (last_width!=new_width) {
		var onrow = Math.floor((new_width-5) / users_ul_li_width);
		var tmp = users_ul_li.length / onrow;
		var onrow_last = Math.round((tmp-Math.floor(tmp))*onrow);
		var margin = Math.round((onrow-onrow_last)*users_ul_li_width/2);

		users_ul.css({'width':(onrow*users_ul_li_width+5)+'px','margin-left':'auto','margin-right':'auto'});
		users_ul_li.css({'margin-left':'0px'});
		users_ul_li.eq(users_ul_li.length-onrow_last).css({'margin-left':margin+'px'});
		last_width = new_width;
	}
	setTimeout(fix_userlist_do,1000);
}

function show_login_form(user,image) {
	$.cms.popup.overlay_toggle(true);
	$('#code-image').css({'background-image' : 'url(\''+image+'.(180)\')'});
	$('#code-login').val(user);
	$('#code').css({
		'left'		: $(document).scrollLeft() + Math.round(($(window).width()-533)/2) + 'px',
		'top'		: $(document).scrollTop() + Math.round(($(window).height()-478)/2) + 'px'
	});
	$('#code').show();
	$('#code-password input').focus();
	return false;
}
function hide_login_form() {
	$.cms.popup.overlay_toggle(false);
	$('#code').hide();
}
function tablefield_chooser(text,type,table) {
	$.cms.popup.show('<div id="tablefield_chooser"><p><strong>'+type+'</strong></p><p>'+text+'</p><p>'+table+'</p></div>');
	$('#tablefield_chooser table tr td.price').each(function() {
		$(this).click(function() {
			$('#field_tf1').val($(this).children('span.price').html());
			$('#field_tf2').val($(this).children('span.type').html());
			$('#field_tf3').val($(this).children('span.time').html());
			$('#field_tf4').val($(this).children('span.date').html());
			$('#field_tf5').val(type);

			$('#row_field_tf1 span.value').html($(this).children('span.price').html());
			$('#row_field_tf2 span.value').html($(this).children('span.type').html());
			$('#row_field_tf3 span.value').html($(this).children('span.time').html());
			$('#row_field_tf4 span.value').html($(this).children('span.date').html());
			$('#row_field_tf5 span.value').html(type);
			$.cms.popup.close();
		});
	});
}
function tablefield_chooser_set(value) {
	$('input[name=field_tf1]').val(value);
	return $.cms.popup.close();
}

$(function() {
	$('.strip hr').before('<p style="text-align: left;"><a href="#" onclick="$(this).parent().nextAll().toggle(); return false;">Подробнеe…</a></p>');
	$('.strip hr').nextAll().andSelf().hide();
	//$('#field_tf1, #field_tf2, #field_tf3, #field_tf4').attr('disabled', 'disabled');
	fix_userlist();
});
