// добавление случайного числа к загружаемым страницам
getRand = function() {
	rand_date = new Date();
	random_date = rand_date.getTime();
	random = Math.round(Math.random()*1000)*random_date;
	return random;
}

// preview
var prev_init = function(e)
{
	obj = $("img.thg_pic");
	$(obj).hover(function() {
		var uni_id = $(this).attr("rel");
		var content = '<div><img src="/data/'+uni_id+'/pic_preview.jpg" alt=""></div>';
		$('#previewContent').html(content);
		$("#previewContent").show();	
	}, function() {
		$("#previewContent").html('');
		$("#previewContent").hide();
	});
	$(obj).bind("mousemove", function(e) {
		// XXX
		var xraz = $(window).width() - e.pageX;
		if (xraz < $("#previewContent").width()+13) xpos = e.pageX - $("#previewContent").width() - 12;
		else	xpos = e.pageX + 12;
		// YYY
		var yraz = $(window).height() + $(document).scrollTop() - e.pageY;
		if (yraz < $("#previewContent").height()+1) ypos = e.pageY - $("#previewContent").height();
		else ypos = e.pageY;
		// styling
		$("#previewContent").css({top: ypos, left: xpos}); 
	});
}

// скрываем/показываем локацию в формах поиска, заказа и т.д.
var citydist = function() {
	if ($('#s_city').val()=="ok") {
		$('#s_dist_n').hide();
		$('#s_dist_k').hide();
		$('#s_dist_ok').show();
		$('#s_dist_metro_n').hide();
		$('#s_dist_metro_o').show();
		$('#s_dist_metro').hide();
	}
	else if ($('#s_city').val()=="k") {
		$('#s_dist_n').hide();
		$('#s_dist_k').show();
		$('#s_dist_ok').hide();
		$('#s_dist_metro_n').hide();
		$('#s_dist_metro_o').hide();
		$('#s_dist_metro').show();
	}
	else {
		$('#s_dist_n').show();
		$('#s_dist_k').hide();
		$('#s_dist_ok').hide();
		$('#s_dist_metro_n').show();
		$('#s_dist_metro_o').hide();
		$('#s_dist_metro').hide();
	}
}

// управление комментариями
var showComments = function(status) {
	if(status=='on' && $('#commentsDiv').html()!='') {
		$('#commentsDiv').show();
		$('#comment_href').html('скрыть отзывы (<b>'+$('#comment_summ').html()+'</b>)');
		$('#comment_href').attr("href", "javascript:showComments('off');");
		$('#comment_icon').attr("href", "javascript:showComments('off');");
	}
	else if(status=='on'  && $('#commentsDiv').html()=='') {
		$.ajax({
			type: "GET",
			url: "/inc/comments.php?page="+$('#comment_id').html()+"&backlink="+$('#backlink').html(),
			cache: false,
			scriptCharset: "windows-1251",
			dataType: "html",
			beforeSend: function() {
					$('#comment_href').html('ожидайте, загружаем...');
					$('#comment_href').attr("href", "javascript:return false();");
					$('#comment_icon').attr("href", "javascript:return false();");
			},
			success: function(transport) {
					var d = $('#commentsDiv');
					d.html(transport);
					d.hide();
					d.show();
					$('#comment_href').html('скрыть отзывы (<b>'+$('#comment_summ').html()+'</b>)');
					$('#comment_href').attr("href", "javascript:showComments('off');");
					$('#comment_icon').attr("href", "javascript:showComments('off');");
			}
		});
	}
	else if(status=='off') {
		$('#commentsDiv').hide();
		$('#comment_href').html('показать отзывы (<b>'+$("#comment_summ").html()+'</b>)');
		$('#comment_href').attr("href", "javascript:showComments('on');");
		$('#comment_icon').attr("href", "javascript:showComments('on');");
	}
}

// обновление каптчи
var refresh_captcha = function() {$('#img_captcha').attr("src", "/engine/antispam/antispam.php?rnd="+getRand());}

// количество анкет в меню
$(function() {
	var obj_link = $("a[rel='msv']");
	obj_link.mouseover(function(){
		var txt = $(this).next().html();
		$(this).append("<span>&nbsp;["+txt+"]</span>");
	});
	obj_link.mouseout(function(){
		$(this).children("span").remove();
	});
});
