﻿$(document).ready(function(){
	$.ga.load('UA-11347170-2');
	$('body').addClass('js');
	$("#txtCityZipSearch").blur(function(){
		if ($(this).val().toLowerCase() == "") {
			$("#lblCityZipSearch").fadeIn("fast");
		}
		else {
			$('[id^="aCounty_"].checked').removeClass("checked");
		}
	});
	$("#txtCityZipSearch").focus(function(){
		$("#lblCityZipSearch").fadeOut("fast");
	});
	$("#aSearchPropType").click(function(){
		$(this).toggleClass('active');
		$("#divSearchPropType").slideToggle('fast');
		if ($('#aSearchCounty').hasClass('active')) {
			$('#aSearchCounty').removeClass('active');
			$("#divSearchCounty").slideUp('fast');
		}
		return false;
	});
	$("#aSearchCounty").click(function(){
		$(this).toggleClass('active');
		$("#divSearchCounty").slideToggle('fast');
		if ($('#aSearchPropType').hasClass('active')) {
			$('#aSearchPropType').removeClass('active');
			$("#divSearchPropType").slideUp('fast');
		}
		return false;
	});
	$('[id^="aPropertyType_"]').click(function(){
		if (!$(this).hasClass('checked')) {
			$('[id^="aPropertyType_"].checked').removeClass("checked");
		}
		$(this).toggleClass('checked');
		return false;
	}).focus(function(){
		$(this).addClass('focus');
	}).blur(function(){
		$(this).removeClass('focus');
	});
	$('[id^="aCounty_"]').click(function(){
		if (!$(this).hasClass('checked')) {
			$('[id^="aCounty_"].checked').removeClass("checked");
			$("#txtCityZipSearch").val("");
			$("#lblCityZipSearch").fadeIn("fast");
			$("#txtSubdivisionSearch").val("");
			$("#lblSubdivisionSearch").fadeIn("fast");
		}
		$(this).toggleClass('checked');
		return false;
	}).focus(function(){
		$(this).addClass('focus');
	}).blur(function(){
		$(this).removeClass('focus');
	});
	$("body.js #divFeaturedWrapper").scrollable({
		nextPage: '#aScrollNext',
		prevPage: '#aScrollPrev',
		speed: 1400,
		size: 7,
		loop: false,
		clickable: false
	}).circular().autoscroll({
		autoplay: true,
		autopause: true,
		interval: 6000,
		steps: 7});
	$('body.js select#selPriceRange_min, body.js select#selPriceRange_max').selectToUISlider({
		labels: 0,
		tooltip: false,
		drawScale: false,
		sliderOptions: {}
	});
	$('body.js select#selBedroom_min').selectToUISlider({
		range: "max",
		max: 10,
		labels: 0,
		tooltip: false,
		drawScale: false,
		sliderOptions: {}
	});
	$('body.js select#selBathroom_min').selectToUISlider({
		range: "max",
		max: 10,
		labels: 0,
		tooltip: false,
		drawScale: false,
		sliderOptions: {}
	});
	$('body.js select#selSfRange_min, body.js select#selSfRange_max').selectToUISlider({
		labels: 0,
		tooltip: false,
		drawScale: false,
		sliderOptions: {}
	});
	$('#txtCityZipSearch').autocomplete('/SiteService.svc/GetACCityZip', {
		json: true,
		formatItem: formatItem,
		minChars: 2,
		selectFirst: true,
		autoFill: true,
		maxItemsToShow: 8
	});
	$("#subSignup").click(function(){
		SubmitSignupMessage();
		return false;
	});
	$("#aSearchProp").click(function(){
		SubmitPropSearch();
		return false;
	});
	InitializePage();
});
function InitializePage(){
	var ckSearchRequest = $.readCookie('searchRequest');
	if (ckSearchRequest) {
		var data = JSON.parse(ckSearchRequest);
		if (typeof(data.request.Criteria) != "undefined") {
			var thisElem;
			if (typeof(data.request.Criteria.Mls) != "undefined") {
				$('#txtCityZipSearch').val(data.request.Criteria.Mls);
				$("#lblCityZipSearch").hide();
				$('#aClearResults').show();
				delete data.request.Criteria.Mls;
				var sdata = JSON.stringify(data);
				ckSearchRequest = $.setCookie('searchRequest', sdata, {
					duration: 90, // In days
					path: '/',
					domain: '',
					secure: false
				});
			}
			else 
				if (typeof(data.request.Criteria.City) != "undefined") {
					$('#txtCityZipSearch').val(data.request.Criteria.City);
					$("#lblCityZipSearch").hide();
				}
				else 
					if (typeof(data.request.Criteria.Zip) != "undefined") {
						$('#txtCityZipSearch').val(data.request.Criteria.Zip);
						$("#lblCityZipSearch").hide();
					}
					else 
						if (typeof(data.request.Criteria.County) != "undefined") {
							thisElem = $('#aCounty_' + data.request.Criteria.County.toLowerCase().replace(/ /g, "-"));
							if (!thisElem.hasClass('checked')) {
								$('[id^="aCounty_"].checked').removeClass("checked");
								$("#txtCityZipSearch").val("");
								$("#lblCityZipSearch").fadeIn("fast");
								thisElem.addClass('checked');
							}
						}
			
			if (typeof(data.request.Criteria.PropertyType) != "undefined") {
				thisElem = $('#aPropertyType_' + data.request.Criteria.PropertyType.toLowerCase().replace(/ /g, "-"));
				if (!thisElem.hasClass('checked')) {
					$('[id^="aPropertyType_"].checked').removeClass("checked");
					thisElem.addClass('checked');
				}
			}
			if (typeof(data.request.Criteria.PriceMin) != "undefined") {
				$('#selPriceRange_min').val(data.request.Criteria.PriceMin).change();
			}
			if (typeof(data.request.Criteria.PriceMax) != "undefined") {
				$('#selPriceRange_max').val(data.request.Criteria.PriceMax).change();
			}
			if (typeof(data.request.Criteria.SfMin) != "undefined") {
				$('#selSfRange_min').val(data.request.Criteria.SfMin).change();
			}
			if (typeof(data.request.Criteria.SfMax) != "undefined") {
				$('#selSfRange_max').val(data.request.Criteria.SfMax).change();
			}
			if (typeof(data.request.Criteria.MinBeds) != "undefined") {
				$('#selBedroom_min').val(data.request.Criteria.MinBeds).change();
			}
			if (typeof(data.request.Criteria.MinBaths) != "undefined") {
				$('#selBathroom_min').val(data.request.Criteria.MinBaths).change();
			}
		}
	}
}
function SubmitPropSearch(){
	var criteria;
	var critVal;
	if (!$('#txtCityZipSearch').val().match(/^[a-zA-Z]{1}\d{2,9}?/)) {
		if ($('#txtCityZipSearch').val() != "") {
			critVal = $('#txtCityZipSearch').val();
			if (typeof(criteria) == "undefined") {
				criteria = new Object();
				criteria.__type = "MlsCriteria:#WCF";
			}
			if (critVal.match(/^\d{5}$/)) {
				criteria.Zip = critVal;
			}
			else {
				criteria.City = critVal;
			}
		}
		else 
			if ($('[id^="aCounty_"].checked').length > 0) {
				if (typeof(criteria) == "undefined") {
					criteria = new Object();
					criteria.__type = "MlsCriteria:#WCF";
				}
				criteria.County = $('[id^="aCounty_"].checked').html();
			}
		if ($('[id^="aPropertyType_"].checked').length > 0) {
			if (typeof(criteria) == "undefined") {
				criteria = new Object();
				criteria.__type = "MlsCriteria:#WCF";
			}
			criteria.PropertyType = $('[id^="aPropertyType_"].checked').html();
		}
		var minPrice = $("#selPriceRange_min").val();
		var maxPrice = $("#selPriceRange_max").val();
		if (!((minPrice == "min") && (maxPrice == "max")) &&
		!((minPrice == "min") && (maxPrice == "min")) &&
		!((minPrice == "max") && (maxPrice == "max"))) {
			if (typeof(criteria) == "undefined") {
				criteria = new Object();
				criteria.__type = "MlsCriteria:#WCF";
			}
			criteria.PriceMin = minPrice;
			criteria.PriceMax = maxPrice;
		}
		var minSf = $("#selSfRange_min").val();
		var maxSf = $("#selSfRange_max").val();
		if (!((minSf == "min") && (maxSf == "max")) &&
		!((minSf == "min") && (maxSf == "min")) &&
		!((minSf == "max") && (maxSf == "max"))) {
			if (typeof(criteria) == "undefined") {
				criteria = new Object();
				criteria.__type = "MlsCriteria:#WCF";
			}
			criteria.SfMin = minSf;
			criteria.SfMax = maxSf;
		}
		var minBed = $("#selBedroom_min").val();
		if (minBed != "min") {
			if (typeof(criteria) == "undefined") {
				criteria = new Object();
				criteria.__type = "MlsCriteria:#WCF";
			}
			criteria.MinBeds = minBed;
		}
		var minBath = $("#selBathroom_min").val();
		if (minBath != "min") {
			if (typeof(criteria) == "undefined") {
				criteria = new Object();
				criteria.__type = "MlsCriteria:#WCF";
			}
			criteria.MinBaths = minBath;
		}
		if ($('[id^="aFeatures_"].checked').length > 0) {
			var features = new Array();
			$('[id^="aFeatures_"].checked').each(function(){
				features.push($(this).html());
			});
			if (features.length > 0) {
				if (typeof(criteria) == "undefined") {
					criteria = new Object();
					criteria.__type = "MlsCriteria:#WCF";
				}
				criteria.Features = features.join(",");
			}
		}
	}
	else {
		if ($('#txtCityZipSearch').val() != "") {
			critVal = $('#txtCityZipSearch').val();
			if (typeof(criteria) == "undefined") {
				criteria = new Object();
				criteria.__type = "MlsCriteria:#WCF";
			}
			criteria.Mls = critVal;
		}
	}
	if (typeof(criteria) != "undefined") {
		var sdata;
		var data;
		var ckSearchRequest = $.readCookie('searchRequest');
		if (ckSearchRequest) {
			data = JSON.parse(ckSearchRequest);
			data.request.Criteria = criteria;
		}
		else {
			var request = new Object();
			request.__type = "SearchRequest:#WCF";
			request.Criteria = criteria;
			data = new Object();
			data.request = request;
		}
		sdata = JSON.stringify(data);
		var ckSearchRequest = $.setCookie('searchRequest', sdata, {
			duration: 90, // In days
			path: '/',
			domain: '',
			secure: false
		});
	}
	window.location.href = "/real-estate/fl/";
}
function SubmitSignupMessage() {
	$('[id^="signupLabel_"]').removeClass("labelInvalid");
	$('#signupLabel_Required').remove();
    var sendData = new Object();
    sendData.__type = "ContactSendFields:#WCF";
    sendData.FirstName = $("#txtSignup_name").val();
	sendData.Email = $("#txtSignup_email").val();
    var data = new Object();
    data.sendData = sendData;
    var sdata = JSON.stringify(data);
    $.ajax({
        url: "/SiteService.svc/SubmitSignupMessage",
        type: "POST",
        data: sdata,
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        error: function(xhr, desc, exception) {
			alert(xhr.responseText);
        },
        success: function(obj){
			obj = obj.d;
			if (obj.Status.toLowerCase() == "invalid") {
				$('[id^="signupLabel_"]').remove();
				$("#divSignup").append(obj.Message);
			}
			else 
				if (obj.Status.toLowerCase() == "success") {
					$("#txtSignup_name").val('');
					$("#txtSignup_email").val('');
					$("#main").append(obj.Message);
					setTimeout(function(){ $("#signupLabel_Thanks").fadeOut('slow',function(){
						$(this).remove();
					}); }, 3000);
				}
		}
    });
}
function formatItem(row) {
	return row[0] + " " + row[1];
}
function changePriceRange() {
    $("#spaPrMin").text($("#selPriceRange_min").val());
    $("#spaPrMax").text($("#selPriceRange_max").val());
 }
function getResultsCount(){
	var criteria = new Object();
	criteria.__type = "MlsCriteria:#WCF";
	criteria.BeMin = $("#selBedroom_min").val();
	criteria.BeMax = "";
	criteria.FbMin = $("#selBathroom_min").val();
	criteria.FbMax = "";
	criteria.SfMin = $("#selSfRange_min").val();
	criteria.SfMax = $("#selSfRange_max").val();
	criteria.CityZip = $("#txtCityZipSearch").val();
	criteria.Subdivision = $("#txtSubdivisionSearch").val();
	criteria.County = $("#selCountySearch").val();
	criteria.MlsNumber = $("#txtMlsSearch").val();
	criteria.PriceMin = $("#selPriceRange_min").val();
	criteria.PriceMax = $("#selPriceRange_max").val();
	criteria.PropertyType = $("#chkSearchTS_SFH").attr("checked");
	criteria.ArchitecturalStyle = $("#chkSearchTS_Bglo").attr("checked");
	criteria.Pool = $("#chkSearchF_Pool").attr("checked");
	criteria.Fireplace = $("#chkSearchF_FP").attr("checked");
	criteria.Water = $("#chkSearchF_Water").attr("checked");
	criteria.Garage = $("#chkSearchF_Gara").attr("checked");
	criteria.LandLocation = $("#chkSearchF_CL").attr("checked");
	criteria.PropertyDescription = $("#chkSearchF_1S").attr("checked");
	criteria.AdditionalRooms = $("#chkSearchF_BR").attr("checked");
	var data = new Object();
	data.criteria = criteria;
	var sdata = JSON.stringify(data);
	$.ajax({
		url: "/SiteService.svc/MlsResultsCountMU",
		type: "POST",
		data: sdata,
		dataType: "json",
		contentType: "application/json; charset=utf-8",
		error: function(xhr, desc, exception){
			alert(xhr.responseText);
		},
		success: function(obj){
			obj = obj.d;
			if (obj.Status == "Fail") {
				alert(obj.Message);
			}
			else {
				$("#spaResultsFound").fadeOut("fast").text(obj.Message).fadeIn("fast");
			}
		}
	});
}
