function hintHide(id) {
	$("#"+id).parent().find(".hint").hide(200);
}

function sendEmail(targetButton) {
	var elementsRoot = $(targetButton).parent().parent();
	var sendData = {
		emailSender : $(".emailSender", elementsRoot).val(),
		emailFrom : $(".emailFrom", elementsRoot).val(),
		emailTo : $(".emailTo", elementsRoot).val(),
		emailSubject : $(".emailSubject", elementsRoot).val(),
		emailBody : $(".emailBody", elementsRoot).val(),
		emailType : $(".emailType", elementsRoot).val()
	};
	if (
		typeof(sendData) !== 'undefined'
     && typeof(sendData.emailSender) === 'string'
	 && typeof(sendData.emailFrom) === 'string'
	 && typeof(sendData.emailTo) === 'string'
	 && typeof(sendData.emailSubject) === 'string'
	 && typeof(sendData.emailBody) === 'string'
	 && typeof(sendData.emailType) === 'string'
	) {
		elementsRoot.fadeOut(200, function() {
			$(".pleaseWait", elementsRoot.parent()).fadeIn(200, function() {
				$.post(
					'/email-ajax.php',
					sendData,
					function (receiveData) {
						$(".pleaseWait", elementsRoot.parent()).fadeOut(200, function() {
							if (1 === receiveData.status) {
								$(".emailSuccess", elementsRoot.parent()).fadeIn(200);
							}
							else {
								$(".emailError", elementsRoot.parent()).fadeIn(200);
							}
						});
					},
					'json'
				);
			});
		});
	}
}

function trCheck() {
	var evalSelectedTR = $(".mainTable2 tr.selected").length + $("#about-us .mainTable tr.selected").length;
	if (evalSelectedTR > 0) {
		$("#one-estate .button, #about-us .button").removeClass("button-disabled");
		$("#one-estate .addNote").removeClass("addNote-disabled");
	} else {
		$("#one-estate .button, #about-us .button").addClass("button-disabled");
		$("#one-estate .addNote").addClass("addNote-disabled");
	}
}

$(document).ready(function() {
	// inputs hover & focus
	$(".inputText2, .inputPassword2, .inputArea2").hover(function() {
    	$(this).addClass("input2-hover");
	}, function() {
		$(this).removeClass("input2-hover");
    });
	$(".inputText2, .inputPassword2, .inputArea2").focus(function() {
    	$(this).addClass("input2-focus");
	});
		$(".inputText2, .inputPassword2, .inputArea2").blur(function() {
			$(this).removeClass("input2-focus");
		});
	// hovers
	$('.button').hover(function() {
		$(this).addClass("button-over");
	}, function() {
		$(this).removeClass("button-over");
	});
	$('.imgtable-container').hover(function() {
		$(this).addClass("imgtable-container-hover");
	}, function() {
		$(this).removeClass("imgtable-container-hover");
	});
	$('.imgtable-container2').hover(function() {
		$(this).addClass("imgtable-container-hover2");
	}, function() {
		$(this).removeClass("imgtable-container-hover2");
	});
	// level blocks
	promo_height = $("#contentPanel .promo .title").next().height();
	new_height = $("#contentPanel .new .title").next().height();
	recomend_height = $("#contentPanel .recomend .title").next().height();
	highest_block = promo_height;
	if (highest_block < new_height)
		highest_block = new_height;
	if (highest_block < recomend_height)
		highest_block = recomend_height;
	$("#contentPanel .content").height(recomend_height);
	$("#contentPanel .content, .lPanelMenu").corner("5px");
	// exsisto-logo hover
	$(".exsisto").hover(function() {
		$(this).attr("src", "/images/exsisto-logo-over.png");
	}, function() {
		$(this).attr("src", "/images/exsisto-logo.png");
	})
	// round corners
	$("#one-estate .board, .gallery, .vCarousel").corner("5px");
	
	$("#gallery1, #gallery2").jcarousel({
        scroll: 3,
        animation: 800
    });
    $("#vertCarousel").jcarousel({
        scroll: 4,
        animation: 800,
        vertical: "true"
    });
	
    var hintSection = $("#mainmenu .hint");
	var hintShower = $("#mainmenu .hintShower");

	hintShower.hover(function() {
		var hint = $(this).parent().find(".hint");		
		var hintHeight = hint.height();
		var hintShowerXPos = $(this).position().left;
		var hintShowerYPos = $(this).position().top;
		hint.css("left", (hintShowerXPos + 47) + "px");
		hint.css("top", (hintShowerYPos - hintHeight - 7) + "px");
		hint.show(200);
	},
	function() {
		var current_id = $(this).attr("id");
		t = setTimeout("hintHide('"+current_id+"')", 300);
	});
	
	hintSection.hover(function() {
		clearTimeout(t);
	},
	function() {
		$(this).hide(200);
	});
	
	var tableHints = $(".mainTable2 .hint");
	tableHints.css("display", "none");
	var tableHintShower = $(".mainTable2 .col8 img");

	tableHintShower.hover(function() {
		var hint = $(this).parent().find(".hint");
		var hintShowerXPos = $(this).parent().position().left;
		var hintShowerYPos = $(this).parent().position().top;
		var hintWidth = hint.width();
		hint.css("left", (hintShowerXPos - hintWidth + 20) + "px");
		hint.css("top", (hintShowerYPos + 34) + "px");
		hint.show();
	},
	function() {
		var hint = $(this).parent().find(".hint");
		hint.hide();
	});

	$(".mainTable2 tbody tr, #about-us .mainTable tbody tr").hover(function() {
		$(this).addClass("hovered");
	}, function() {
		$(this).removeClass("hovered");
	});
	
	$(".mainTable2 tr, #about-us .mainTable tr").click(function() {
		$(this).toggleClass("selected");
		trCheck();
	});
	
	$(".mainTable2 tr a").click(function() {
		$(this).parents("tr").toggleClass("selected");
		trCheck();
	});
	
	$("#b_question").click(function (e) {
		if (-1 !== $(this).attr("class").indexOf("button-disabled")) {
			return;
		}
		var ap_ids = new Array();
		var ap_names = new Array();
		$(".mainTable2 tr.selected").each(function (i) {
			t = $(this).attr("id");
			ap_ids[i] = t.substring(1, t.length);
			ap_names[i] = $(this).children(".col1").html();
		});
	    $.nyroModalManual({
			content: $("#nyroEmailForm2").html(),
			modal: true
		});
		$(".emailSubject").val('Вопрос по этой теме: ' + location.href);
		$(".emailBody").val('Здравствуйте,\r
\r
Я хотел бы задать следующий вопрос по этой теме:\r
\r
' + location.href + '\r
\r
Апартаменты: ' + ap_names + '\r
\r
');
		$.nyroModalSettings({ width: 290, height: 280 });
	});
	
	$(".askQuestionLink").click(function (e) {
	    $.nyroModalManual({
			content: $("#nyroEmailForm3").html(),
			modal: true
		});
		$(".emailSubject").val('Вопрос по этой теме: ' + location.href);
		$(".emailBody").val('Здравствуйте,\r
\r
Я хотел бы задать следующий вопрос по этой теме:\r
\r
' + location.href);
		$.nyroModalSettings({ width: 290, height: 280 });
	});
	
	$("#b_notepad").click(function () {
		if (-1 !== $(this).attr("class").indexOf("addNote-disabled")) {
			return;
		}
		var ap_ids = new Array();
		var ap_names = new Array();
		$(".mainTable2 tr.selected").each(function (i) {
			t = $(this).attr("id");
			ap_ids[i] = t.substring(1, t.length);
			ap_names[i] = $(this).children(".col1").html();
		});
		$.post("/notepad-ajax.php", { "action" : "add", "type" : "ap", "data" : ap_ids.join("$") }, function (data, textStatus) {
			if (1 === data.status) {
				$(".addedApartments").text('Добавленные апартаментов: ' + data.aps);
			}
			else {
				$(".addedApartments").text('Нет добавленых новые апартаментов!');
			}
    	}, "json");
	});
	
	$(".removeButton").click(function () {
		if (-1 !== $(this).attr("class").indexOf("button-disabled")) {
			return;
		}
		var obj_ids = new Array();
		var ap_ids = new Array();
		var obj_names = new Array();
		var ap_names = new Array();
		$("#about-us .mainTable tr.selected").each(function (i) {
			t = $(this).attr("id");
			obj_ids[i] = t.substring(1, t.length);
			obj_names[i] = $(this).children(".pic").find("a").attr("title");
		});
		$(".mainTable2 tr.selected").each(function (i) {
			t = $(this).attr("id");
			ap_ids[i] = t.substring(1, t.length);
			ap_names[i] = $(this).children(".col1").html();
		});
		$.post("/notepad-ajax.php", { "action" : "delete", "objs" : obj_ids.join("$"), "aps" : ap_ids.join("$") }, function (data, textStatus) {
			if (1 === data.status) {
				$(".mainTable2 tr.selected, #about-us .mainTable tr.selected").fadeOut(200, function() {
					$(this).remove();
					var hiderTopTable = $("#hiderTopTable");
					var hiderBottomTable = $("#hiderBottomTable");
					if ($("#about-us .mainTable tbody tr").length < 1) {
						hiderTopTable.fadeOut(200);
					}
					if ($(".mainTable2 tbody tr").length < 1) {
						hiderBottomTable.fadeOut(200);
					}
					if (($("#about-us .mainTable tbody tr").length < 1) && ($(".mainTable2 tbody tr").length < 1)) {
						$(".hideButtons").fadeOut(200);
						$("#empty").fadeIn(200);
					}
					$(".removeButton").addClass("button-disabled");
				});
			}
			else {
				//alert('Произошла ошибка!');
			}
    	}, "json");
	});
	
	$(".send2friend").click(function() {
	    $.nyroModalManual({
			content: $("#nyroEmailForm").html(),
			modal: true
		});
		$(".emailSubject").val('Объявления опубликовани здесь AlphaEstates.ru');
		$(".emailBody").val('Здравствуй,\r
\r
Я находил и посылаю тебе объявления опубликовани здесь AlphaEstates.ru:\r
\r
' + location.href + '\r
\r
');
		$.nyroModalSettings({ width: 290 });
	});
	
	$(".sendToFriend").click(function() {
		var objects = new Array();
		var objects1 = new Array();
		var objects2 = new Array();
		$("#about-us .mainTable tbody tr").each(function(i) {
			objects[i] = $(this).children(".pic").find("a").attr("title");
		});
		$(".mainTable2 tbody tr").each(function(i) {
			objects1[i] = $(this).children(".col1").html();
			objects2[i] = $(this).children(".col2").html();
		});
	    $.nyroModalManual({
			content: $("#nyroEmailForm").html(),
			modal: true
		});
		$(".emailSubject").val('Объявления опубликовани здесь AlphaEstates.ru');
		var body = 'Здравствуй,\r
\r
Я находил и посылаю тебе объявления опубликовани здесь AlphaEstates.ru:\r
\r
';
		if (objects.length > 0) {
			for (var i = 0; i < objects.length; ++i) {
				body += 'Недвижимость: ' + objects[i] + '\r
\r
';
			}
		}
		if (objects.length > 0 && objects1.length > 0) {
			body += '\r
';
		}
		if (objects1.length > 0) {
			for (var i = 0; i < objects1.length; ++i) {
				body += 'Недвижимость: ' + objects1[i] + '\r
Апартаменты: ' + objects2[i] + '\r
\r
';
			}
		}
		$(".emailBody").val(body);
		$.nyroModalSettings({ width: 290 });
	});
	
	$("#addNotepad").click(function(e) {
		$.post("/notepad-ajax.php", { "action" : "add", "type" : "obj", "data" : $("#obj_id").html() }, function (data, textStatus) {
			if (1 === data.status) {
				$("#addNotepad")
					.unbind("click")
					.click(function(e) {
						e.preventDefault();
					})
					.fadeOut(500, function() {
						$(this)
							.text("Добавлено к блокноте")
							.attr("title", "Добавлено к блокноте")
							.fadeIn(500);
					})
					.addClass("added");
			}
			else {
				//alert('Произошла ошибка добавление!');
			}
    	}, "json");
		e.preventDefault();
	});
	
	/*
$.fn.fancyzoom.defaultsOptions.imgDir='/js/fancyzoom/resources/';
	$('a.fancyZoom').fancyzoom({Speed:500});
*/
	$('a[@rel*=lightbox]').lightBox({
		imageLoading: '/images/lightbox-ico-loading.gif',
		imageBtnClose: '/images/lightbox-btn-close.gif',
		imageBtnPrev: '/images/lightbox-btn-prev.gif',
		imageBtnNext: '/images/lightbox-btn-next.gif',
		imageBlank: '/images/lightbox-blank.gif'
	});
	
	$(".unbindall").unbind();
	
	$(".noAction").click(function (e) {
		e.preventDefault();
	});
	
	$('#faq').find('dd').addClass('faq_answer').hide().end().find('dt').addClass('faq_question').click(function() {
         var answer = $(this).next();
         if (answer.is(':visible')) {
             answer.slideUp();
         } else {
             answer.slideDown();
         }
     });
     
     var vCarouselTitleHeight = $(".vCarouselTitle").height();
     if (vCarouselTitleHeight > 30) {
     	$(".vCarouselTitle").addClass("vCarouselBigTitle");
     }
     
    $("a.blank").attr("target", "_blank");
	
	$('#search_cat')
		.selectChain({
			target: $('#search_loc'),
			url: '/js/selectchain/ajax.php'
		})
		.trigger('change');

});

