$(document).ready(function() {
	// sociable
	//$("img", "#social").fadeTo(10000, 0.4);

	$("img", "#social").hover(function() {
		$(this).stop().animate({ opacity: 1 }, 1);
	}, function() {
		$(this).stop().animate({ opacity: 0.4 }, 400);
	});
	
	//$("img", "#social2").fadeTo(10000, 0.4);

	$("img", "#social2").hover(function() {
		$(this).stop().animate({ opacity: 1 }, 1);
	}, function() {
		$(this).stop().animate({ opacity: 0.4 }, 400);
	});
	
	$("#lang_arrow").click(toggle_menu);

	portfolio();
	
	$("#search-box input#rent").click(function() {
		if($(this).attr("checked") == true) {
			$("#search-box #sale-price").css("display", "none");
			$("#search-box select#sprice").attr("disabled", "disabled");
			$("#search-box #rent-price").css("display", "block");
			$("#search-box select#rprice").removeAttr("disabled");
			predictCount();
		}
	});
	
	$("#search-box input#sale").click(function() {
		if($(this).attr("checked") == true) {
			$("#search-box #rent-price").css("display", "none");
			$("#search-box select#rprice").attr("disabled", "disabled");
			$("#search-box #sale-price").css("display", "block");
			$("#search-box select#sprice").removeAttr("disabled");
			predictCount();
		}
	});
	
	$("#search-box input#features1").click(function() {
		predictCount();
	});
	$("#search-box input#features2").click(function() {
		predictCount();
	});
	$("#search-box select#location").change(function() {
		getCities();
		predictCount();
	});
	$("#search-box select#city").change(function() {
		predictCount();
	});
	$("#search-box select#sprice").change(function() {
		predictCount();
	});
	$("#search-box select#rprice").change(function() {
		predictCount();
	});
	$("#search-box select#bedrooms").change(function() {
		predictCount();
	});
	$("#search-box select#size").change(function() {
		predictCount();
	});
	$("#search-box input#reset").click(function() {
		$("input#sale").attr("checked", "checked");
		$("input#features1").removeAttr("checked");
		$("input#features2").removeAttr("checked");
		$("#search-box option").removeAttr("selected");
		$("#search-box select").val("all");
		
		$("#search-box select#city option").remove();
		$("#search-box select#city").append($("<option></option>").attr("value", "all").text("All cities"));
		
		$("input#sale").click();
	});
	
	if($("#search-box").length != 0) {
		predictCount();
	}
	
	$("a.inquiry-link").click(function() {
		var overlayDescId = "#" + $(this).attr("class").split(" ").slice(-1);
		var mlsNumber = overlayDescId.substring(14);
		$("#inquiry-div textarea").html($(overlayDescId).html());
		$("#inquiry_mls_number").val(mlsNumber);
		$("#inquiry-div").modal( {
			overlayClose: true,
			opacity: 90,
			onOpen: function (dialog) {
				dialog.overlay.fadeIn('slow', function () {
					dialog.data.hide();
					dialog.container.fadeIn('slow', function () {
						dialog.data.slideDown('slow');
					});
				});
			}
		});
	});
});

function portfolio(){
	var carusel = $('.carousel');
	carusel.each(function(i, _carusel){
		_carusel = $(_carusel);
		var _caruselTab = _carusel.parents('.tab')
		var _btNext = $('span.right', _caruselTab);
		var _btPrev = $('span.left', _caruselTab);
		var _informBlock = $('> li', _carusel);
		var _current = 0;
		var _step = _carusel.parent().width();
		var _next;
		var _duration = 300;
		
		// set paremetr
		if (_informBlock.filter('.active').length) {
			_informBlock.not('.active').css('left',_step);
			_current = _informBlock.index(_informBlock.filter('.active'));
		}
		else {
			_informBlock.not(':eq(0)').css('left',_step);
		}
		
		// generate inform ul
		var _inform = '<ul class="carousel-info">';
		_informBlock.each(function(i, block){
			var _inf = $(block).find('div.info').html();
			_inform += '<li>'+_inf+'</li>';
		});
		_inform += '</ul>';
		_carusel.after(_inform);
		
		var _text = $('ul.carousel-info', _caruselTab);
		var _informText = $('> li', _text);
		var _heightText = _informText.outerHeight();
		
		if (_informBlock.filter('.active').length) _informText.not(':eq('+_current+')').css('top',_heightText);
		else _informText.not(':eq(0)').css('top',_heightText);
		
		// set pager
//		var _pager = $('ul.gallery-btns', _caruselTab);
//		var _li = '';
//		_informBlock.each(function(i, block){
//			_li += '<li><a href="#">button</a></li>';
//		});
//		_pager.html(_li);
//		_pager.find('li:eq('+_current+')').addClass('active');
//		_pager.find('a').click(function(){
//			var _index = _pager.find('li a').index($(this));
//			_next = _index;
//			
//			if (_next > _current) animateNext();
//			else animatePrev();
//			
//			_current = _next;
//			setActive();
//			return false;
//		});
		
		// next/prev
		_btNext.click(function(){
			if (!_informBlock.is(':animated')) {
				_next = _current+1
				if (_next > _informBlock.length-1) _next = 0;
				
				animateNext();			
				_current = _next;
				//setActive();
			}
			return false;
		});
		_btPrev.click(function(){
			if (!_informBlock.is(':animated')) {
				_next = _current-1;
				if (_next < 0) _next = _informBlock.length-1;
				
				animatePrev();			
				_current = _next;
				//setActive();
			}
			return false;
		});
		
		function animateNext() {
			_informBlock.eq(_next).css('left',_step).animate({left:0},{duration:_duration});
			_informBlock.eq(_current).animate({left:-_step},{duration:_duration});
			
			_informText.eq(_next).css('top',-_heightText).animate({top:0},{duration:_duration});
			_informText.eq(_current).animate({top:_heightText},{duration:_duration});
		}
		
		function animatePrev() {
			_informBlock.eq(_next).css('left',-_step).animate({left:0},{duration:_duration});
			_informBlock.eq(_current).animate({left:_step},{duration:_duration});
			
			_informText.eq(_next).css('top',_heightText).animate({top:0},{duration:_duration});
			_informText.eq(_current).animate({top:-_heightText},{duration:_duration});
		}
		
		function setActive(){
			_pager.find('li').removeClass('active');
			_pager.find('li:eq('+_current+')').addClass('active');
		}
	});
}

var menu_open=false;

function toggle_menu() {
	if(!menu_open) {
		$("#icons").animate({"height":$("#social").height()},300,"swing");}
	else{
		$("#icons").animate({"height":"16"},300,"swing",function(){});
	}

	menu_open = !menu_open;
}

function getCities() {
	var county = $("#search-box select#location").val();
	if(county != "all") {
		var ajaxURL = "/pages/ajax/get-cities.php?county=" + county;
		
		$.ajax({
			type: "GET",
			cache: true,
			url: ajaxURL,
			dataType: "json",
			success: function(citiesJSON) {
				$("#search-box select#city option").remove();
				if(!citiesJSON) {
					return;
				}
				$.each(citiesJSON, function(val, text) {
				    $("#search-box select#city").append(
				        $("<option></option>").attr("value", val).text(text)
				    );
				});
			}
		});
	}
}

function predictCount() {
	var location = $("#search-box select#location").val();
	var city = $("#search-box select#city").val();
	var price = false;
	if($("#search-box select#rprice").attr("disabled")) {
		price = $("#search-box select#sprice").val();
	} else if($("#search-box select#sprice").attr("disabled")) {
		price = $("#search-box select#rprice").val();
	}
	var bedrooms = $("#search-box select#bedrooms").val();
	var size = $("#search-box select#size").val();
	var features1 = $("#search-box input:checked#features1").val();
	var features2 = $("#search-box input:checked#features2").val();
	var rent_sale = $("#search-box input[name=rent-sale]:radio:checked").val();
	
	var features = false;
	
	if(features1) {
		features = features1;
	}
	if(features1 && features2) {
		features += "_" + features2;
	}
	else if(features2) {
		features = features2;
	}
	
	var ajaxURL = "/pages/ajax/predict-search-count.php" 
		+ "?location=" + location 
		+ "&price=" + price 
		+ "&bedrooms=" + bedrooms 
		+ "&size=" + size 
		+ "&rent_sale=" + rent_sale
		+ "&city=" + city;
	
	if(features) {
		ajaxURL += "&features=" + features;
	}
	
	$.ajax({
		type: "GET",
		cache: false,
		url: ajaxURL,
		success: function(count) {
			$("#predicted-count").html(count);
			if(count == 1) {
				$(".predict-container span").html("Penthouse found");
			} else {
				$(".predict-container span").html("Penthouses found");
			}
		}
	});
}

function validateInquiryForm() {
	var firstname = $("#inquiry #first_name").val();
	var lastname = $("#inquiry #last_name").val();
	var email = $("#inquiry #email").val();
	
	var isValid = true;
	
	if(!firstname || $.trim(firstname) == null)
	{
		$("#inquiry label[for=first_name]").addClass("error");
		isValid = false;
	}
	else
	{
		$("#inquiry label[for=first_name]").removeClass("error");
	}
	
	if(!lastname || $.trim(lastname) == null)
	{
		$("#inquiry label[for=last_name]").addClass("error");
		isValid = false;
	}
	else
	{
		$("#inquiry label[for=last_name]").removeClass("error");
	}
	
	if(!email || $.trim(email) == null)
	{
		$("#inquiry label[for=email]").addClass("error");
		isValid = false;
	}
	else
	{
		$("#inquiry label[for=email]").removeClass("error");
	}
	
	return isValid;
}

