$(document).ready(function(){
	$("#searchBtn").bind("click", function(){
		switch ($("#location").val()) {
			case "City and state, ZIP, or place of interest":
			case "":
				showTT(getObj("location"));
				$("#location").bind("keyup", function() {
					$(this).unbind("keyup");
					hideTT();
				});
				break;
			default:
				$("#ndbasic").submit();
				break;
		}
	});
	$("#ndls_search").bind("click", function(){ndls_search(this);});
	$("#min, #max").selectToUISlider({labels: 0, labelSrc: "text"}).hide();
	$("#minbeds, #maxbeds").selectToUISlider({labels: 0, labelSrc: "text"}).hide();
	$("#minradius, #maxradius").selectToUISlider({labels: 0, labelSrc: "text"}).hide();
	$(".ui-slider").bind("mouseup dragend", function(){ checkLabels(); });
	
	$("#stbfind").bind("click", function(){contentSwitch("find");});
	$("#stblist").bind("click", function(){contentSwitch("list");});

	$("#location").bind("focus", configLocation);
	$("#location").bind("blur", checkLocation);
	$("#ndlsno").bind("focus", configNDLS);
	$("#ndlsno").bind("blur", checkNDLS);
	
	$('#page0_gallery1').spacegallery({loadingClass: 'loading', perspective: 40, header: "#c1head"});

	checkLabels();
	checkLocation();
	checkNDLS();
	contentSwitch();
	prepForm();
});

function checkLocation(){ if ($("#location").val() == "") { $("#location").css({"color":"#999"});$("#location").val("City and state, ZIP, or place of interest");$("#location").bind("focus", configLocation);}}
function checkNDLS(){ if($("#ndlsno").val()=="") { $("#ndlsno").css({"color":"#999"});$("#ndlsno").val("Listing #");$("#ndlsno").bind("focus", configNDLS);}}
function configLocation(){ $("#location").val("");$("#location").css({"color":"#555"});$("#location").unbind("focus", configLocation);$("#location").focus();}
function configNDLS(){$("#ndlsno").val("");$("#ndlsno").css({"color":"#555"});$("#ndlsno").unbind("focus", configNDLS);$("#ndlsno").focus();}
function contentSwitch(inc) {
	switch(inc) {
		case "list":
			$("#slider").animate({left:"-1000px"});
			$("#stblist").attr("class", "list active");
			$("#stbfind").attr("class", "find inactive");
			$("#wam").find(".wam_list").fadeOut(250);
			$("#wam").find(".wam_find").fadeIn(500);
			$("#wam").attr("class", "find");
			break;
		case "find":
			$("#slider").animate({left:"0px"});
			$("#stbfind").attr("class", "find active");
			$("#stblist").attr("class", "list inactive");
			$("#wam").find(".wam_find").fadeOut(250);
			$("#wam").find(".wam_list").fadeIn(500);
			$("#wam").attr("class", "list");
			break;
		default: 
			$("#wam").bind("click", function(){ $("html, body").animate({scrollTop: "0px"}); contentSwitch($(this).attr("class")); });
			$("#wam").find(".wam_list").fadeIn(500);
			$("#wam").attr("class", "list");
		return;
	}
}

function prepForm() {
	$('#page0_gallery2').spacegallery({loadingClass: 'loading', perspective: 40, header: "#c2head"});
	$("#anon_add").bind("click", function() {
		hideTT();
		if (validateForm(getObj("anonProp"))) {
			debug("doing anon_add_property");
			anon_add_property();
		}
		
	});
	$("#address_postal").bind("keypress", function(event){ return (validateKeypress(event)); });
	$("#stblist").unbind().bind("click", function(){contentSwitch("list"); });	
	$("#property_type").bind("change", function(event){ selectEvent(this); });
	$("#card2 input:radio").bind("change", function(event) { radioEvent(this); });
}

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"] == -1){
		if (beds["maxval"] == -1) bedText = " Rooms only";
		else if (beds["maxval"] == 6) bedText = " No BR limit";			
		else if (beds["maxval"] > 0) bedText = " Up to " + beds["maxtext"];
	} else if (beds["minval"] == 0) { 
		if (beds["maxval"] == 6) bedText = " Std. or larger";
		else if (beds["maxval"] > 0) bedText = " Std. - " + beds["maxval"] + " BDs";
	} else if (beds["minval"] > 0){
		if (beds["maxval"] == 6) bedText = " " + beds["minval"] + "+ BDs";
		else if (beds["maxval"] > 0) bedText = " " + beds["minval"] + " - " + beds["maxval"] + " BDs";
	}
	
	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 showTT(inc){
	var tt = $("#tt");
	$(tt).hide();
	if (inc.title) {
		$("#ttcontent").html(inc.title);
		var pos = findPos(inc);
		var leftpos = pos[0];
		var toppos = pos[1];
		leftpos += (($(inc).width() - $(tt).width()) / 2);
		toppos -= ( $(tt).height() + 32 );
		$(tt).css({left: leftpos, top: toppos}).show();
	}
}
function hideTT() {
	var tt = $("#tt");
	$(tt).hide();
}

function clearForm() {
	var o1 = getObj("overlay");
	var inputs = o1.getElementsByTagName("input");
	clearInputs(inputs);
	var selects = o1.getElementsByTagName("select");
	clearSelects(selects);
	var button = o1.getElementsByTagName("button")[0];
	button.getElementsByTagName("img")[0].setAttribute("src", "/images/nav_right_gray.png");
	button.disabled = true;
}

function selectEvent(inc){
	if (inc.value == null) return false;
	if (inc.value > 6) {
		$("#unit_count_div").empty();
		$("<label for'unit_count'># of units:</label><input type='text' style='width: 50px;' size='7' id='unit_count' name='unit_count' />").appendTo("#unit_count_div");
		$("#listing_type_floorplan").removeAttr("disabled");
		$("#unit_count").bind("keypress", function(event){ return (validateKeypress(event)); });
		if (inc.value > 9) {
			$("#listing_type_floorplan").attr("checked", true);
			$("#floorplan_count_div").html("<div style='margin-left: 22px'><label for='floorplan_count'># of floor plans:</label><input type='text' style='width: 50px;' size='7' id='floorplan_count' name='floorplan_count' title='Please enter the total number of floor plans in this property.'/></div>");
			$("#floorplan_count").bind("keypress", function(event){ return (validateKeypress(event)); });
		}
	} else {
		$("#unit_count_div").empty();
		$("#floorplan_count_div").empty();
		getObj("listing_type_floorplan").disabled = true;   
		$("#listing_type_unit").attr("checked", "checked");
	}
}

function radioEvent(inc){
	switch(inc.name) {
	case "listing_type":
		if (inc.value == "0") $("#floorplan_count_div").empty();
		else {
			$("#floorplan_count_div").html("<div style='margin-left: 22px'><label for='floorplan_count'># of floor plans:</label><input type='text' style='width: 50px;' size='7' id='floorplan_count' name='floorplan_count' title='Please enter the total number of floor plans in this property.'/></div>");
			$("#floorplan_count").bind("keypress", function(event){ return (validateKeypress(event)); });
		}
		break;
	}
}

function validateKeypress(e) {
	if (!e) e = window.event;
	var k = e.keyCode;
	if (!k) k = e.which;
	if (k==8||k==9||k==13||k==16||k==18||k==27|| (k>34 && k < 41) || k==45 || k==46 || (k>47 && k<58)) return true;
	return false;
}

function validateForm(inc) {
	if (!inc) return false;
	for (var x=0; x< inc.elements.length; x++) {
		switch (inc.elements[x].nodeName) {
			case "INPUT":
				switch (inc.elements[x].type) {
					case "text":
						if (inc.elements[x].value == "") {
							showTT(inc.elements[x]);
							return false;
						}
						break;
					default:
						break;
				}
				break;
			case "TEXTAREA":
				break;
			default:
				break;
		}
	}
	return true;
}

function anon_add_property(inc) { e_POST(getObj("anonProp"),"/property/property.php?a=anon_add_property_1", property_response); }
function anon_add_property_2(inc) { e_POST(getObj("anonProp"),"/property/property.php?a=anon_add_property_2", property_response); }

function property_response(inc){
	var tokens = "";
	var code = 0;
	var displaymode = 0;
	var editmode = 0;
	var return_message = null;
	var param = null;
	if(inc.length) {
		tokens = inc.split(":");
		if(tokens[1]) code = parseInt(tokens[1]);
		else code = -1;
		if(tokens[2]) return_message = tokens[2];
		if(tokens[3]) param = tokens[3];
	} else {
		var doc = inc.getElementsByTagName("document")[0];
		displaymode = doc.getAttribute("display");
		editmode = doc.getAttribute("edit");
		doc = null;
		var info = inc.getElementsByTagName("info");
		info = info[0];
		code = parseInt(info.getAttribute("return_code"));
		if(info.getAttribute("return_message")) return_message = info.getAttribute("return_message");
	}
	switch(code){
		case 5:
			alert("Your property and listings have been successfully added to your Properties Panel.\n\nWe're forwarding you there now.");
			window.location = "/index.php?m=properties";
			break;
		case 100:
			var unit = inc.getElementsByTagName("unit")[0];
			var unit_id = unit.getAttribute("unit_id");
			if (unit_id) window.location = "/listing/listing.php?t="+unit_id+"&edit";
			else {
				alert("Oops, we've had a problem.  Please contact beta@newdigs.com for support!");
				closeModal("anon_property_login_overlay");
			}
			break;
		case 101:
			getModal("anon_property_login", 500);
			break;
		default:
			if(return_message) debug("code: " + code + " \nmsg: " + return_message);
			break;
	}
}

