// vc_id = "$Id: front_page.js 13421 2011-01-18 21:35:57Z jgiven $"
$(document).ready(function() {
	var mls_focus = 0;
	$("#search_front_mls").focus(function() {
		$("#location_search_btn").fadeOut("fast",function() {
			$("#mls_num_search").css("opacity",1);
			$("#location_search_container").css("opacity",0.5);
			$("#mls_num_search_btn").fadeIn("slow");
		});
	});
	
	$("#search_front_mls").blur(function() {
		$("#mls_num_search_btn").fadeOut("fast",function() {
			$("#mls_num_search").css("opacity",0.5);
			$("#location_search_container").css("opacity","100");
			$("#location_search_btn").fadeIn("slow");
		});
	});
	
	//  START - Front page marketing stuff...since there is no way the content will always match in height let's assure the box borders do.
	if ($("#front_mk_content").height() < $("#front_premier").height()) {
		$("#front_mk_content").css("height",$("#front_premier").height() + "px");
	} else {
		$("#front_premier").css("height",$("#front_mk_content").height() + "px")
	}
	//  END - Front page marketing stuff
	
	// START - Init the search box city auto complete
	//$(".hp_search_box").focus().autocomplete(cities);
	$(".hp_search_box").blur();
	$("#top_un").focus();
	// END - Init the search box city auto complete
	
	// Start - Bind functions for front page search boxes
	$("#hp_search_box").bind("focus",function() {
		$("#search_input").val("location");
		if ($("#hp_search_box").val() == "Enter any state, city, neighborhood, zip code, or address") {
			$("#hp_search_box").val("");
		}
	});
	$("#hp_search_box").bind("blur",function() {
		if ($("#hp_search_box").val() == "") {
			$("#hp_search_box").val("Enter any state, city, neighborhood, zip code, or address");
		}
	});
        $("#hp_search_box,#search_front_mls").keypress(function (e) {
	    if (e.which == 13) {
		triggerSearch(document.hp_search_form);
	    }
	});
	$("#search_front_mls").bind("focus",function() {
		mls_focus = 1;
		$("#search_input").val("mls");
		$("#mls_num_search").css("opacity",100);
		if ($("#search_front_mls").val() == "Enter a unique number listed in Multiple Listing Service") {
			$("#search_front_mls").val("");
		}
	});
	$("#search_front_mls").bind("blur",function() {
		mls_focus = 0;
		if ($("#search_front_mls").val() == "") {
			$("#search_front_mls").val("Enter a unique number listed in Multiple Listing Service");
		}
	});
	// END - Bind functions for front page search boxes
	
	// START - Focus / Opacity for different Searches
	$("#mls_num_search").bind("mouseover",function() {
		$("#mls_num_search").css("opacity",100);
	});
	$("#mls_num_search").bind("mouseout",function() {
		if (!mls_focus) {
			$("#mls_num_search").css("opacity",0.5);
		}
	});
	$("#location_search_container").bind("mouseover",function() {
		$("#location_search_container").css("opacity",100);
	});
	$("#location_search_container").bind("mouseout",function() {
		if (mls_focus) {
			$("#location_search_container").css("opacity",0.5);
		}
	});
	// END - Focus / Opacity for different Searches
	
	// START - Commercial checkbox logic
	$("#cb_com").bind("click", function() {
		var commercial = confirm("Would you like to search commercial listings?  Click \'OK\' to be taken to Windermere\'s commercial web site.");
		if (commercial) {
			window.open('http://www.windermerecommercial.com');
			$("#cb_com").attr('checked', false);
		}
		else {
			return false;
		}	
	});
	// END - Commercial checkbox logic
	
	//Are you an iPad user???  Set default to list-view.
	var isiPad = navigator.userAgent.match(/iPad/i) != null;
	
	if (isiPad) {
		simClick("#radio_list_view");
	}
	
	
	// Autocomplete
	$("#hp_search_box").focus().autocomplete(cities);
	
	
});

// jQuery solution for simulating a mouseclick.
function simClick(obj_id) {
	//var a = $('#search_form_href')[0];
	var a = $(obj_id)[0];
	var e = document.createEvent('MouseEvents');
	e.initEvent( 'click', true, true );
	a.dispatchEvent(e);
}

function toggleAdvanced() {
	$('#advanced_options').slideToggle('slow',function () {
		if ($('#advanced_options').css("display") == "none") {
			$("#arrow").removeClass();
			$("#arrow").addClass("arrowDown");
			$("#more_fewer").html("More");
		} else {
			$("#arrow").removeClass();
			$("#arrow").addClass("arrowUp");
			$("#more_fewer").html("Fewer");
		}
	});
}

function triggerSearch( form ) {
    if ( form.search.value == "location" ) {
	if ( form.location.value != '' ) {
	    LOCATION_GATEWAY.findLocation( form.location.value );
	} else {
	    window.location = 'index.cfm?fuseaction=listing.SearchLanding';
	}
    } else if ( form.search.value == "mls" ) {   
	LOCATION_GATEWAY.targetMls( form.mls_num.value );
    }
}

function getSearchDefault() {
    var ths = getCookie("searchType");
    
    if (ths == 'list') {
	document.hp_search_form.searchType[1].checked = true;
	LOCATION_GATEWAY.setDisplay('list');
    }
}

function getDisambiguationMessage( ) {
    return '<table width="100%" height="100%"><tr><td class="pageTitle" valign="top">We\'re sorry. Either we could not find your location or it falls outside of our service area.  Please <a href="javascript:closeResultDialogue();">try your search again</a>.<br /><br /><span style="color: #660000;">Tips:<br><br> Try to be more specific or start by clicking on a state name below.<br><br>If you are searching by MLS number, please enter the number in the MLS field below.</span></td></tr><tr><td valign="bottom" align="right"><a href="javascript:closeResultDialogue();">Close</a></td></tr></table>';
}

function getAdditionalSearchConstraints( ) {
    return queryStringFromForm( document.hp_search_form );
}


