$(document).ready(function() {
	//$("a.lightbox").lightBox();
	var footerHeight;
	footerHeight = $(".footer").height();
	if(footerHeight > 257) {
		$(".main_empty").height(footerHeight + 47);
		$(".main").css({"margin-bottom":"-"+(footerHeight + 47)+"px"});
	}
	
	/*if($.browser.msie) {
		$(".hide_shadow").width($("body").width());
	}*/
	
	$(".menu > ul > li").hover(
		function() {
			$(this).addClass("hover");
			$(this).find("a:first-child").addClass("hover");
			$(this).find("div.submenu").removeClass("hidden");
		}, 
		function() {
			$(this).removeClass("hover");
			$(this).find("a:first-child").removeClass("hover");
			$(this).find("div.submenu").addClass("hidden");
		}
	);
	
	$(".dialog_link").click(function() {
		$($(this).attr("href")).dialog("open");
		return false;
	});
	
	$("#call_me_dialog").dialog({
		autoOpen: false, 
		width: 295, 
		minHeight: 10, 
		modal: false, 
		resizable: false, 
		draggable: true, 
		zIndex: 10000, 
		title: "Перезвоните мне", 
		dialogClass: "dialog", 
		closeOnEscape: true, 
		position: [($("body").width() / 2 + 190), 50]
	});
	
	$("#order_dialog").dialog({
		autoOpen: false, 
		width: 475, 
		minHeight: 10, 
		modal: false, 
		resizable: false, 
		draggable: true, 
		zIndex: 10000, 
		title: "Заявка на бронирование участка", 
		dialogClass: "dialog", 
		closeOnEscape: true
	});
	
	$(".carousel").jCarouselLite({
        btnNext: ".next_carousel", 
        btnPrev: ".prev_carousel", 
		/*auto: 2000, */
		speed: 800, 
		visible: ($(".carousel ul li").length < 4)?$(".carousel ul li").length:4
    });
	
	$(".next_carousel, .prev_carousel").click(function() {
		$(".carousel_zoom img").animate({
			"width": "223", 
			"height": "197", 
			"margin-top": "0", 
			"margin-left": "0"
		}, function() {
			$(this).parent().remove()
		});
	});
	
	$(".carousel img").hover(
		function() {
			$(".carousel_zoom img").animate({
				"width": "223", 
				"height": "197", 
				"margin-top": "0", 
				"margin-left": "0"
			}, function() {
				$(this).parent().remove()
			});
			$(this).parent().clone().appendTo("div.main").css({
				"position": "absolute", 
				"z-index": "10", 
				"top": 0, 
				"left": 0, 
				"margin-top": $(this).offset().top, 
				"margin-left": $(this).offset().left
			}).addClass("carousel_zoom").children("img").css({
				"width": $(this).width(), 
				"height": $(this).height()
			}).animate({
				"width": "280", 
				"height": "248", 
				"margin-top": "-"+((248 - $(this).height()) / 2), 
				"margin-left": "-"+((280 - $(this).width()) / 2)
			}).mouseleave(function() {
				$(this).animate({
					"width": "223", 
					"height": "197", 
					"margin-top": "0", 
					"margin-left": "0"
				}, function() {
					$(this).parent().remove()
				});
			});
		}
	);
	
	$(".photo_gallery .carousel_photo").jCarouselLite({
        btnNext: ".next_carousel_photo", 
        btnPrev: ".prev_carousel_photo", 
		speed: 400, 
		visible: ($(".photo_gallery .carousel_photo ul li").length < 7)?$(".photo_gallery .carousel_photo ul li").length:7
    });
	
	$(".photo_gallery_5 .carousel_photo").jCarouselLite({
        btnNext: ".next_carousel_photo", 
        btnPrev: ".prev_carousel_photo", 
		speed: 400, 
		visible: ($(".photo_gallery_5 .carousel_photo ul li").length < 5)?$(".photo_gallery_5 .carousel_photo ul li").length:5
    });
	
	$(".carousel_photo a").click(function() {
		$(".photo_detail").height($(".photo_detail img").height());
		var photoDetailWidth, photoDetailHeight;
		photoDetailWidth = $(".photo_detail").width();
		photoDetailHeight = $(".photo_detail").height();
		$(".photo_detail").html('<img src="/i/loading.gif" style="margin: '+(photoDetailHeight/2-8)+'px 0 0 '+(photoDetailWidth/2-8)+'px;" class="loading" />');
		myImage = new Image();
		myImage.src = $(this).attr("href");
		myImage.onload  = function() {
			$(".photo_detail").html('<img src="'+myImage.src+'" />').animate({
				"height": $(".photo_detail img").height()
			});
			return false;
		};
		$(".photo_description").text($(this).attr("title"));
		
		$(".carousel_photo ul li").removeClass("selected");
		$(".carousel_photo ul li img.frame").remove();
		
		$(this).parent().addClass("selected");
		$(this).parent().prepend('<img src="/i/frame.gif" class="frame" />');

		return false;
	});
	
	$("#call_me_form, #order_form").submit(function() {
		$(this).find("label.red").removeClass("red");
		$(this).find(".error").addClass("hidden");
		var error;
		error = 0;
		var thisId;
		thisId = $(this).attr("id");
		var $form = $(this);
		
		$(this).find(".required").each(function() {
			if($(this).val() == '') {
				$("#"+thisId+" label[for='"+$(this).attr("id")+"']").addClass("red");
				error++;
			}
		});
		
		if(error == 0) {
			if($(this).attr("id") == "call_me_form") {
				$(this).replaceWith('<p>Мы приняли сообщение и скоро свяжемся с Вами</p>');
				$("#call_me_dialog").dialog( "option" , "title" , "Спасибо" );
			} else if($(this).attr("id") == "order_form") {
				$.post($(this).attr("action"), $(this).serialize(), 
				function(data) {
					if(data == 'Y') {
						$form.replaceWith('<p>Наш менеджер позвонит Вам в ближайшее время.</p>');
						$("#order_dialog").dialog( "option" , "title" , "Спасибо, Ваша заявка отправлена" );
					}
				});
			} else {
				$(this).replaceWith('<p>Мы приняли сообщение и скоро свяжемся с Вами</p>');
			}
			/*$.post("/ajax/object.order.php", $(this).serialize(), 
			function(data) {
				$(".reserve").html(data);
			});*/
		} else {
			$(this).find(".error").removeClass("hidden");
		}
		
		return false;
	});

	$(".cottage_plan area").mouseover(function(emouse) {
		$("div.tooltip_block div.tooltip_content").html($($(this).attr("href")).html());
		$("div.tooltip_block").css({left : emouse.pageX - $("div.tooltip_block").width()/2 + 5, top : emouse.pageY - $("div.tooltip_block").height() - 5});
		$("div.tooltip_block").show();
		return false;
	}).mousemove(function(emouse){
		$("div.tooltip_block").css({left : emouse.pageX - $("div.tooltip_block").width()/2 + 5, top : emouse.pageY - $("div.tooltip_block").height() - 5});
	});
	
	$(".cottage_plan area").hover(
		function() {
		}, 
		function() {
			$("div.tooltip_block").hide();
			return false;
		}
	);
	
	if($(".cottage_plan")) {
		generateBlankPlan($(".cottage_plan"));
	}
	
	$(".cottage_plan img").maphilight({
		fill: true,
		fillColor: 'ffffff',
		fillOpacity: 0.4,
		stroke: false,
		fade: false,
		alwaysOn: false,
		neverOn: false,
		groupBy: false
	});
	
	$(".dataTable thead th").click(function() {
		var up = ' &uarr;';
		var down = ' &darr;';
		
		$(this).parent().parent().parent(".dataTable").find("th.sorting_asc, th.sorting_desc").each(function() {
			$(this).children("span").remove();
		});
		if($(this).hasClass("sorting")) {
			$(this).append('<span>'+down+'</span>');
		} else if($(this).hasClass("sorting_asc")) {
			$(this).append('<span>'+up+'</span>');
		} else if($(this).hasClass("sorting_desc")) {
			$(this).append('<span>'+down+'</span>');
		}
	});
	
	$("#lands_sort_filter").change(function() {
		$(this).parent().parent().submit();
	});
	
	$("a.print").click(function() {
		window.frames[$(this).attr("rel")].focus(); 
		window.frames[$(this).attr("rel")].print(); 
		return false;
	});
});

function initAreaSlider(maxValue) {
	$("#area_slider").slider({
		range: true,
		min: 1,
		max: maxValue,
		values: [$("#S_FROM").val(), $("#S_FOR").val()],
		slide: function( event, ui ) {
			$("#S_FROM").val(ui.values[0]);
			$("#S_FOR").val(ui.values[1]);
		}
	});
	$("#area_slider").append("<ul></ul>");
	var liValue = '';
	var arValue = new Array();
	arValue[0] = Math.floor($("#area_slider").slider("option", "min") / 1000);
	arValue[4] = Math.floor($("#area_slider").slider("option", "max") / 1000);
	for(var i = 0;i < 5;i++) {
		if(i != 0 && i != 4) {
			liValue = $("#area_slider").slider("option", "min") + i * ($("#area_slider").slider("option", "max") - $("#area_slider").slider("option", "min")) / 4;
			liValue = Math.floor(liValue);
			if(liValue == arValue[0] || liValue == arValue[4]) {
				liValue = '';
			}
		}
		if(i == 0) {
			liValue = $("#area_slider").slider("option", "min");
			liValue = Math.floor(liValue);
		}
		if(i == 4) {
			liValue = $("#area_slider").slider("option", "max");
			liValue = Math.floor(liValue);
		}
		$("#area_slider ul").append("<li><span>" + liValue + "</span></li>");
	}
	$("#S_FROM").val($("#area_slider").slider("values", 0));
	$("#S_FOR").val($("#area_slider").slider("values", 1));
}

function initDistSlider(maxValue) {
	$("#distance_slider").slider({
		range: true,
		min: 1,
		max: maxValue,
		values: [$("#D_FROM").val(), $("#D_FOR").val()],
		slide: function( event, ui ) {
			$("#D_FROM").val(ui.values[0]);
			$("#D_FOR").val(ui.values[1]);
		}
	});
	$("#distance_slider").append("<ul></ul>");
	var liValue = '';
	var arValue = new Array();
	arValue[0] = Math.floor($("#distance_slider").slider("option", "min") / 1000);
	arValue[4] = Math.floor($("#distance_slider").slider("option", "max") / 1000);
	for(var i = 0;i < 5;i++) {
		if(i != 0 && i != 4) {
			liValue = $("#distance_slider").slider("option", "min") + i * ($("#distance_slider").slider("option", "max") - $("#distance_slider").slider("option", "min")) / 4;
			liValue = Math.floor(liValue);
			if(liValue == arValue[0] || liValue == arValue[4]) {
				liValue = '';
			}
		}
		if(i == 0) {
			liValue = $("#distance_slider").slider("option", "min");
			liValue = Math.floor(liValue);
		}
		if(i == 4) {
			liValue = $("#distance_slider").slider("option", "max");
			liValue = Math.floor(liValue);
		}
		$("#distance_slider ul").append("<li><span>" + liValue + "</span></li>");
	}
	$("#D_FROM").val($("#distance_slider").slider("values", 0));
	$("#D_FOR").val($("#distance_slider").slider("values", 1));
}

function generateBlankPlan(block) {
	var blockWidth;
	var blockHeight;
	var useMap;
	var blockBg;
	
	blockWidth = block.width();
	blockHeight = block.height();
	blockBg = block.find("img[usemap]").attr("src");
	
	block.css({
		"background-image":'url('+blockBg+')'
	});
	
	useMap = block.find("img[usemap]").attr("usemap");
	block.find("img[usemap]").remove();

	block.prepend('<img src="/i/blank.gif" class="default" usemap="'+useMap+'" width="'+blockWidth+'" height="'+blockHeight+'" />');
}
