$(document).ready(function(){

var bitmasks = {
	"feature" : {
		"feature_water_front" : 1,
		"feature_business_center" : 2,
		"feature_24_hour_maintenance" : 4,
		"feature_picnic_area" : 8,
		"feature_on_site_management" : 16,
		"feature_club_house" : 32,
		"feature_fitness_center" : 64,
		"feature_playground" : 128,
		"feature_pool" : 256,
		"feature_racquetball_court" : 512,
		"feature_tennis_court" : 1024,
		"feature_basketball_court" : 2048,
		"feature_water_view" : 4096,
		"feature_controlled_access" : 8192,
		"feature_deck" : 16384,
		"feature_patio" : 32768,
		"feature_spa" : 65536,
		"feature_hot_tub" : 131072,
		"feature_yard" : 262144,
		"feature_fenced_yard" : 524288,
		"feature_balcony" : 1048576,
		"feature_fireplace" : 2097152,
		"feature_central_air" : 4194304,
		"feature_window_ac" : 8388608,
		"feature_elevator" : 16777216 
	},
	"accessibility" : {
		"accessibility_bathrooms" : 1,
		"accessibility_counters" : 2,
		"accessibility_doors" : 4,
		"accessibility_emergency_cords" : 8,
		"accessibility_first_floor" : 16,
		"accessibility_handrails" : 32,
		"accessibility_lights" : 64,
		"accessibility_mods_available" : 128,
		"accessibility_ramp" : 256,
		"accessibility_sink" : 512,
		"accessibility_strobe_alarm" : 1024
	},
	"parking" : {
		"parking_assigned" : 1,
		"parking_off_street" : 2,
		"parking_attached_garage" : 4,
		"parking_detached_garage" : 8,
		"parking_underground" : 16
	},
	"petpolicy" : {
		"petpolicy_dogs_small" : 1,
		"petpolicy_dogs_large" : 2,
		"petpolicy_cats" : 4,
		"petpolicy_fish" : 8,
		"petpolicy_other" : 16
	},
	"utility" : {
		"utility_heat" : 1,
		"utility_hot_water" : 2,
		"utility_gas" : 4,
		"utility_electric" : 8,
		"utility_cable_tv" : 16,
		"utility_cable_tv_available" : 32,
		"utility_satellite_tv" : 64,
		"utility_sewage" : 128,
		"utility_water" : 256,
		"utility_internet" : 512,
		"utility_internet_high_speed" : 1024,
		"utility_internet_wireless" : 2048,
		"utility_trash" : 4096,
		"utility_phone" : 8192,
		"utility_phone_digital" : 16384
	},
	"kitchen" : {
		"kitchen_eat_in" : 1,
		"kitchen_breakfast_nook" : 2,
		"kitchen_dishwasher" : 4,
		"kitchen_garbage_disposal" : 8,
		"kitchen_trash_compactor" : 16,
		"kitchen_refrigerator" : 32,
		"kitchen_microwave" : 64,
		"kitchen_stove_type_offset" : 7,
		"kitchen_stove_type_length" : 4
	},
	"laundry" : {
		"laundry_coin_op" : 1,
		"laundry_hook_up" : 2,
		"laundry_washer_dryer" : 4,
		"laundry_washer" : 8,
		"laundry_clothes_line" : 16
	},
	"subsidy" : {
		"subsidy_call" : 32,
		"subsidy_dss" : 1,
		"subsidy_iq" : 64,
		"subsidy_s8" : 4
	}
};
	$("#min, #max").selectToUISlider({labels: 0, labelSrc: "text", tooltipLocation: "below"});
	$("#minbeds, #maxbeds").selectToUISlider({labels: 0, labelSrc: "text", tooltipLocation: "below"});
	$("#minradius, #maxradius").selectToUISlider({labels: 0, labelSrc: "text", tooltipLocation: "below"});
	$("#searchBtn").bind("click", function(){
			switch ($("#location").val()) {
				case "City and state, ZIP, or place of interest":
				case "":
					showTT("#location");
					$("#location").bind("keyup", function() {
						$(this).unbind("keyup");
						hideTT();
					});
					break;
				default:
					$("#ndbasic").submit();
					break;
			}
		});

	$(".ui-slider").bind("mouseup dragend", function(){ checkLabels(); });

	$("#location").bind("focus", configLocation).bind("blur", checkLocation);
	$("#ndlsno").bind("focus", configNDLS).bind("blur", checkNDLS);

	checkLabels();
	checkLocation();
	checkNDLS();
	
	$("#advSearchOptions").not(".open").hide();
	$("#advSearchOpen").bind("mouseup",function(){advSearchToggle();});
	if (location.hash == "#advsearch") advSearchToggle();
	
	$("#advSearchOptions").find("input").change(function(){
		if ($(this).is(":checked")) $(this).parent().addClass("active");
		else $(this).parent().removeClass("active");
	});
	
	$("#ndls_search").hover(
			function(){$("#ndls_lawnsign").css("background-position", "0 -134px") },
			function(){$("#ndls_lawnsign").css("background-position", "0 0 ");}
		).click(function(){ndls_search();});
		
	//showMsg();
	
	$("#advSearchOptions input[type=checkbox]").live("change", function(){
			var fullName = $(this).attr("name");
			var category = fullName.substring(0, fullName.indexOf("_"));
			var value = parseInt($("#"+category).val());
			var bitval = parseInt(eval("bitmasks."+category+"."+fullName));
			if((bitval & value) == 0) {
				$("#"+category).val(value+bitval);
			} else {
				$("#"+category).val(value-bitval);	
			}
			debug($("#"+category).val());
	});

});
function checkLocation(){ if ($("#location").val() == "") { $("#location").css({"color":"#999"}).val("City and state, ZIP, or place of interest").bind("focus", configLocation);}}
function checkNDLS(){ if($("#ndlsno").val()=="") { $("#ndlsno").css({"color":"#999"}).val("Listing #").bind("focus", configNDLS);}}
function configLocation(){ if ($("#location").val() == "City and state, ZIP, or place of interest") $("#location").val(""); $("#location").css({"color":"#333"}).unbind("focus", configLocation).focus(); }
function configNDLS(){$("#ndlsno").val("");$("#ndlsno").css({"color":"#333"});$("#ndlsno").unbind("focus", configNDLS);$("#ndlsno").focus();}

function checkLabels() {
	var cost = { minval: $("#min").val(), mintext: $("#min :selected").get(0).text, maxval: $("#max").val(), maxtext: $("#max :selected").get(0).text };
	var beds = { minval: $("#minbeds").val(), mintext: $("#minbeds :selected").get(0).text, maxval: $("#maxbeds").val(), maxtext: $("#maxbeds :selected").get(0).text };
	var radius = { minval: $("#minradius").val(), mintext: $("#minradius :selected").get(0).text, maxval: $("#maxradius").val(), maxtext: $("#maxradius :selected").get(0).text };
	var costText, bedText, radiusText = "";

	if (cost["minval"] == 0){
		if (cost["maxval"] == -1) costText = " No cost limits";
		else if (cost["maxval"] > 0) costText = " Up to " + cost["maxtext"];
	} else if (cost["minval"] > 0) {
		if (cost["maxval"] == -1) costText = cost["mintext"] + " or more";
		else if (cost["maxval"] > 0) costText = " " + cost["mintext"] + " to " + cost["maxtext"];
	}
	
	if (beds["minval"] == 7){
		if (beds["maxval"] == 7) bedText = " Rooms only";
		else if (beds["maxval"] == 6) bedText = " No bedroom limit";			
		else bedText = " Up to " + beds["maxtext"];
	} else if (beds["minval"] == 0) { 
		if (beds["maxval"] == 6) bedText = " Studio or larger";
		else bedText = " Studio to " + beds["maxval"] + " BD";
	} else if (beds["minval"] > 0){
		if (beds["maxval"] == 6) bedText = " " + beds["minval"] + "+ BD";
		else bedText = " " + beds["minval"] + " to " + beds["maxval"] + " BD";
	}
	
	if (radius["minval"] == 0) radiusText = "Up to " + radius["maxtext"];
	else if (radius["minval"] > 0) radiusText = radius["minval"] + " to " + radius["maxtext"];

	$("#costspan").text(costText);
	$("#bedspan").text(bedText);
	$("#radiusspan").text(radiusText);
}

function advSearchToggle() {
	if ($("#advSearchOptions").is(":hidden")) {
		if (location.hash != "#advsearch"){
			$("#advSearchOptions").slideDown("slow");
			$.scrollTo(".fp_search",500, {offset:-20});
		}
		$("#advSearchOptions").show();
		$("#advSearchOpen").html("Less Options &uArr;").addClass("open");
		location.hash = "#advsearch";
	} else {
		$("#advSearchOptions").slideUp("slow");
		$("#advSearchOpen").html("More Options &dArr;").removeClass("open");
		$.scrollTo(".fp_search",1000, {offset:-20});
		location.hash = "#";
		if ($("#advSearchOptions input").is(":checked")){
			$("#advSearchOptions input").attr("checked", false).parent().removeClass("active");
		}
	}
}

function showTT(inc){
	var tt = $("#tt");
	$(tt).hide();
	var target = $(inc);
	if (target.length > 0 && $(target).attr("title")) {
		$("#ttcontent").html(target.attr("title"));
		var pos = $(target).offset();
		var leftpos = pos.left;
		var toppos = pos.top;
		leftpos += (($(target).width() - $(tt).width()) / 2);
		toppos -= ( $(tt).height() + 22 );
		$(tt).css({left: leftpos, top: toppos}).show();
	}
}
function hideTT() {
	var tt = $("#tt");
	$(tt).hide();
}

