/**
 *  Обработчики при загрузке
 */
$(function() {
	// Фотогаллерея
	$("a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed: 'fast', /* fast/slow/normal */
		padding: 16, /* padding for each side of the picture */
		opacity: 0.2, /* Value betwee 0 and 1 */
		showTitle: false, /* true/false */
		allowresize: true, /* true/false */
		theme: 'sandy_rounded' /* light_rounded / dark_rounded / light_square / dark_square */
	});
	
	// Отключает контекстное меню и выделение текста
	$(document).bind("contextmenu", function(e) {
		return false;
	});
	$(document).css({
		'MozUserSelect' : 'none'
	}).bind('selectstart', function() {
		return false;
	}).mousedown(function() {
		return false;
	});
});

whyImg = "/static/images/why.png";
why2Img = "/static/images/why2.png";

/**
 *  Двигает центральную картинку
 */
function showAnswer() {
	// Блоки с картинками
	var el1 = $("#centerpic");
	var el2 = $("#centerpic2");
	var elWhy = $(".why a");

	// Основный константы
	var elwdt = el1.width();
	var picwdt = 909;
	var posCenter = Math.round((elwdt-picwdt)/2);
	var posLeft = -picwdt;
	var posRight = elwdt;

	if (el1[0].pic != 2) {
		// Движение влево
		el1[0].pic = "2";
		elWhy.hide();

		el1.css("backgroundPosition", posCenter+"px 0px");
		el1.animate({backgroundPosition: posLeft+"px 0px"}, 1000);

		el2.css("backgroundPosition", posRight+"px 0px");
		el2.animate({backgroundPosition: posCenter+"px 0px"}, 1000, function() {
			el2.css("backgroundPosition", "50% 0%");
			$("img", elWhy).attr("src", why2Img);
			elWhy.show();
		});
	} else {
		// Движение вправо
		el1[0].pic = "1";
		elWhy.hide();

		el2.css("backgroundPosition", posCenter+"px 0px");
		el2.animate({backgroundPosition: posRight+"px 0px"}, 1000);

		el1.css("backgroundPosition", posLeft+"px 0px");
		el1.animate({backgroundPosition: posCenter+"px 0px"}, 1000, function() {
			el1.css("backgroundPosition", "50% 0%");
			$("img", elWhy).attr("src", whyImg);
			elWhy.show();
		});
	}
}

/**
 * Прокручивает фотографии с недвижимостью
 */
var pagenum = 1;
var timerID = setTimeout('scrollProperties()', 15000);
function scrollProperties() {
	clearTimeout(timerID);
	var elList = $("#top-properties ul");
	elList.animate({marginTop: "-=544px"}, 1000, "swing", function() {
		pagenum++;
		if (pagenum > pagecount) {
			pagenum = 1;
			elList.css('marginTop', '0px');
		}
		timerID = setTimeout('scrollProperties()', 15000);
	});
}