// JavaScript Document
/*	-------------------------------------------------
	BROWSER PLUGIN
	-------------------------------------------------	*/
	(function($) {
		var userAgent = navigator.userAgent.toLowerCase();
	
		$.browser = {
			version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1],
			safari: /webkit/.test( userAgent ),
			opera: /opera/.test( userAgent ),
			msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
			mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
		};
	})(jQuery);

/*	-------------------------------------------------
	CONSTANTS
	-------------------------------------------------	*/
	var STR_STANDARD_ERROR			= "Some errors were found while attempting to submit this form.\n\n";
	var STR_SLIDER_TOOLTIP_HTML		= "<div class=\"slider_tooltip_wrapper\" id=\"[SLIDERNAME]Tooltip[COUNTER]\"><div class=\"slider_tooltip_[ALIGN]\"><strong>$2,500,000</strong></div></div>";
	var INT_MULT_SQFT				= 250.00;
	var INT_MULT_PRICE				= 125000;
	var ARR_PRICE_STEPPING			= [0, 25000, 50000, 75000, 100000, 125000, 150000, 175000, 200000, 225000, 250000, 275000, 300000, 325000, 350000, 375000, 400000, 450000, 500000, 550000, 600000, 650000, 700000, 750000, 800000, 850000, 900000, 950000, 1000000, 1250000, 1500000, 1750000, 2000000, 2250000, 2500000, 2750000, 3000000];
	var INT_POPUP_TYPE_EMAILLISTING	= 0;
	var INT_POPUP_TYPE_SCHEDULE		= 1;
	var INT_POPUP_TYPE_CONTACT		= 2;
	var INT_POPUP_TYPE_ALERTS		= 3;
	var INT_COUNTER_PROPERTIES		= 0;
	var INT_COUNTER_SEARCHES		= 1;

/*	-------------------------------------------------
	VARIABLES
	-------------------------------------------------	*/
	var objMap;
	var objPano;
	var boolDisplayTooltips			= true;
	var arrPopupProfiles			= {livechat: {width: 640, height: 480, resizable: 1, center: 0, status: 0, scrollbars: 1, toolbar: 0}, details: {width: 1024, height: 768, resizable: 1, center: 1, status: 0, scrollbars: 1, toolbar: 0}};

/*	-------------------------------------------------
	FUNCTIONS
	-------------------------------------------------	*/
	function processHeaderLogin() {
		var boolErrorFound			= false;
		var strErrorText			= "";
		
		if (($("#strHeaderEmail").val().length == 0) || ($("#strHeaderEmail").val() == STR_HEADER_LOGIN_USERNAME)) {
			boolErrorFound			= true;
			strErrorText			+= "- Please enter your e-mail address.\n";
		} else if (($("#strHeaderEmail").val().indexOf("@") == -1) || ($("#strHeaderEmail").val().indexOf(".") == -1)) {
			boolErrorFound			= true;
			strErrorText			+= "- Please enter a valid e-mail address.\n";
		}
		if ($("#strHeaderPassword").val().length == 0) {
			boolErrorFound			= true;
			strErrorText			+= "- Please enter your password.\n";
		}
		
		if (boolErrorFound) {
			alert(STR_STANDARD_ERROR + strErrorText);
			return false;
		} else {
			return true;
		}
	}
	
	function processMinimalHeaderLogin() {
		var boolErrorFound			= false;
		var strErrorText			= "";
		
		if (($("#strHeaderEmail").val().length == 0) || ($("#strHeaderEmail").val() == STR_HEADER_LOGIN_USERNAME)) {
			boolErrorFound			= true;
			strErrorText			+= "- Please enter your e-mail address.\n";
		} else if (($("#strHeaderEmail").val().indexOf("@") == -1) || ($("#strHeaderEmail").val().indexOf(".") == -1)) {
			boolErrorFound			= true;
			strErrorText			+= "- Please enter a valid e-mail address.\n";
		}
		if ($("#strHeaderPassword").val().length == 0) {
			boolErrorFound			= true;
			strErrorText			+= "- Please enter your password.\n";
		}
		
		if (boolErrorFound) {
			alert(STR_STANDARD_ERROR + strErrorText);
			return false;
		} else {
			if (window.opener) {
				if (window.opener.document.forms.frmHeaderLogin) {
					window.opener.document.forms.frmHeaderLogin.strHeaderEmail.value	= $("#strHeaderEmail").val();
					window.opener.document.forms.frmHeaderLogin.strHeaderPassword.value	= $("#strHeaderPassword").val();
					window.opener.document.forms.frmHeaderLogin.submit();
					window.opener.focus();
					window.close();
					
					return false;
				} else {
					return true;
				}
			} else {
				return true;
			}
		}
	}
	
	function submitSmallSearchTool() {
		$("#strSQFT").val($("#sliderSquareFootage").slider("value"));
		$("#strBedrooms").val($("#sliderBedrooms").slider("value"));
		$("#strBathrooms").val($("#sliderBathrooms").slider("value"));
		$("#strPrice").val($("#sliderPrice").slider("values").join("|"));
		
		$("#frmToolSubmit").submit();
	}
	
	function setupSliderTooltips() {
		var arrSliderList		= new Array("sliderSquareFootage", "sliderBedrooms", "sliderBathrooms", "sliderPrice");
		var strTooltipHTML		= "";
		var intNumChildren		= 0;
		var intSliderCounter	= 0;
		
		for (var intCounter = 0; intCounter < arrSliderList.length; intCounter++) {
			intNumChildren		= $("#" + arrSliderList[intCounter]).children("a").length;
			
			if (intNumChildren > 0) {
				if (intNumChildren == 2) {
					for (var intSliderCounter = 0; intSliderCounter < 2; intSliderCounter++) {
						strTooltipHTML		= STR_SLIDER_TOOLTIP_HTML;
						strTooltipHTML		= strTooltipHTML.replace("[SLIDERNAME]", arrSliderList[intCounter]);
						strTooltipHTML		= strTooltipHTML.replace("[COUNTER]", intSliderCounter.toString());
						
						switch (intSliderCounter) {
							case 0 :
								strTooltipHTML		= strTooltipHTML.replace("[ALIGN]", "left");
								
								$("#" + arrSliderList[intCounter] + " a:first").append(strTooltipHTML);
								break;
								
							case 1 :
								strTooltipHTML		= strTooltipHTML.replace("[ALIGN]", "right");
	
								$("#" + arrSliderList[intCounter] + " a:last").append(strTooltipHTML);
								break;
						}
					}
				} else {
					/*
					var objFragment				= document.createDocumentFragment();
					var objElement1				= document.createElement("div");
						objElement1.className	= "slider_tooltip_wrapper";
						objElement1.id			= arrSliderList[intCounter] + "Tooltip" + intCounter;
						
					var objElement2				= document.createElement("div");
						objElement2.className	= "slider_tooltip_left";
						
					var objElement3				= document.createElement("strong");
					var objTextNode				= document.createTextNode("Text");
					
					objElement3.appendChild(objTextNode);
					objElement2.appendChild(objElement3);
					objElement1.appendChild(objElement2);
					objFragment.appendChild(objElement1);
					
					$("#" + arrSliderList[intCounter] + " a:first").append(objFragment);
					*/										 
					
					strTooltipHTML		= STR_SLIDER_TOOLTIP_HTML;				
					strTooltipHTML		= strTooltipHTML.replace("[SLIDERNAME]", arrSliderList[intCounter]);
					strTooltipHTML		= strTooltipHTML.replace("[COUNTER]", "0");
					strTooltipHTML		= strTooltipHTML.replace("[ALIGN]", "left");
					
					$("#" + arrSliderList[intCounter] + " a:first").append(strTooltipHTML);
					
				}
			}
		}
	}
	
	function generateTooltipValue(strType, intValue) {
		var strTargetText		= "";
		
		switch (strType) {
			case "sliderSquareFootage" :
				if (intValue == 0) {
					strTargetText	= "Any sq. ft.";
				} else {
					strTargetText	= Math.round(parseInt(intValue) * INT_MULT_SQFT) + " sq. ft.";
				}
				break;
				
			case "sliderBedrooms" :
		//				if ((objUI.value == 0) || (objUI.value == $("#sliderBedrooms").slider("option", "max"))) {
				if (intValue == $("#sliderBedrooms").slider("option", "max")) {
					strTargetText	= "Any #/beds";
				} else {
					strTargetText	= parseInt(intValue) + "+ beds";
				}
				break;
				
			case "sliderBathrooms" :
		//				if ((objUI.value == 0) || (objUI.value == $("#sliderBathrooms").slider("option", "max"))) {
				if (intValue == $("#sliderBathrooms").slider("option", "max")) {
					strTargetText	= "Any #/baths";
				} else {
					strTargetText	= parseInt(intValue) + "+ baths";
				}
				break;
				
			case "sliderPrice" :
	//			strTargetText		= "$" + $.currency(parseInt(intValue) * INT_MULT_PRICE, {s: ",", d: ".", c: 0});
				strTargetText		= "$" + $.currency(parseFloat(ARR_PRICE_STEPPING[intValue]), {s: ",", d: ".", c: 0});
				break;
		}
		
		return strTargetText;
	}
	
	function showTooltip(objEvent, objUI) {
		if (boolDisplayTooltips) {
			if (objUI.handle) {
				if ((objUI.handle.firstChild) && (objEvent.target)) {
					if ((objUI.handle.firstChild.id) && (objEvent.target.id)) {
						var strTargetID		= objEvent.target.id;
						var strTargetText	= "";
						var strTooltipID	= objUI.handle.firstChild.id;
						var strTextArea		= $(objUI.handle.firstChild).children("div").children("strong");
						
						if ((strTextArea) && (strTooltipID) && (strTargetID)) {
							strTargetText	= generateTooltipValue(strTargetID, objUI.value);
							
							$(strTextArea).html(strTargetText);
							$("#" + strTooltipID).show();
						}
					}
				}
			}
		}
	}
	
	function showTooltipDirect(strTooltipKey, strTargetID, intValue) {
		var objTooltipTarget	= $("#" + strTargetID);
		var objTextTarget		= $("#" + strTargetID + " strong");
		var strTargetText		= "";
		
		if (objTooltipTarget) {
			strTargetText		= generateTooltipValue(strTooltipKey, intValue);
			$(objTextTarget).html(strTargetText); 
			$(objTooltipTarget).show();
		}
	}
	
	function hideTooltip(objEvent, objUI) {
		var arrSliderList		= new Array("sliderSquareFootage", "sliderBedrooms", "sliderBathrooms", "sliderPrice");
		var strTooltipHTML		= "";
		var intNumChildren		= 0;
		var intSliderCounter	= 0;
		
		for (var intCounter = 0; intCounter < arrSliderList.length; intCounter++) {
			intNumChildren		= $("#" + arrSliderList[intCounter]).children("a").length;
			
			if (intNumChildren > 0) {
				if (intNumChildren == 2) {
					for (var intSliderCounter = 0; intSliderCounter < 2; intSliderCounter++) {
						$("#" + arrSliderList[intCounter] + "Tooltip" + intSliderCounter).hide();
					}
				} else {
					$("#" + arrSliderList[intCounter] + "Tooltip0").hide();
				}
			}
		}
	}
	
	function focusTop() {
		$.facebox.close();
		window.scroll(0, 0);
	}
	
	function saveProperty(intMLSID) {
		jQuery.facebox(function() { 
			jQuery.get(	"/search/_save.php",
						{
							id: intMLSID
						}, 
						function(txtData) {
							jQuery.facebox(txtData)
						})
		});
	}
	
	function requestInfo(intMLSID) {
		jQuery.facebox(function() { 
			jQuery.get(	"/search/_schedule.php", 
						{
							id: intMLSID
						}, 
						function(txtData) {
							jQuery.facebox(txtData)
						})
		});
	}
	
	function registerForAlerts() {
		jQuery.facebox(function() { 
			jQuery.get(	"/_common/_alerts.php", 
						{
						}, 
						function(txtData) {
							jQuery.facebox(txtData)
						})
		});
	}
	
	function popupFormSubmit(intPopupType) {
		var boolErrorFound		= false;
		var strErrorText		= "";
		
		switch (intPopupType) {
			case INT_POPUP_TYPE_EMAILLISTING :
				if ($("#strPopupName").val().length < 3) {
					boolErrorFound		= true;
					strErrorText		+= "- Please enter your name.\n";
				}
				if ($("#strPopupRecipName").val().length < 3) {
					boolErrorFound		= true;
					strErrorText		+= "- Please enter the recipient's name.\n";
				}
				if ($("#strPopupRecipEmail").val().length < 3) {
					boolErrorFound		= true;
					strErrorText		+= "- Please enter the recipient's e-mail.\n";
				}
				
				if (!boolErrorFound) {
					$("#lyrPopupSubmit").hide();
					$("#lyrPopupWorking").show();
					
					$.get(	"/detail/_email-send.php", 
							{
								id: $("#intPopupMLSID").val(), 
								name: $("#strPopupName").val(), 
								recipname: $("#strPopupRecipName").val(), 
								recipemail: $("#strPopupRecipEmail").val()
							}, 
							function(txtData) {
								$("#lyrPopupSend").hide();
								$("#lyrPopupSuccess").show();
								setTimeout(5000, function() { $.facebox.close(); });
							}, 
							"json");
				} else {
					alert(STR_STANDARD_ERROR + strErrorText);
				}
				break;
				
			case INT_POPUP_TYPE_SCHEDULE :
				if ($("#strPopupName").val().length < 3) {
					boolErrorFound		= true;
					strErrorText		+= "- Please enter your name.\n";
				}
				if ($("#strPopupEmail").val().length < 3) {
					boolErrorFound		= true;
					strErrorText		+= "- Please enter your e-mail.\n";
				}
				
				if (!boolErrorFound) {
					$("#lyrPopupSubmit").hide();
					$("#lyrPopupWorking").show();
					
					$.get(	"/search/_schedule-send.php", 
							{
								id: $("#intPopupMLSID").val(), 
								name: $("#strPopupName").val(), 
								email: $("#strPopupEmail").val(), 
								comments: $("#txtPopupComments").val()
							}, 
							function(txtData) {
								$("#lyrPopupSend").hide();
								$("#lyrPopupSuccess").show();
								setTimeout(5000, function() { $.facebox.close(); });
							}, 
							"json");
				} else {
					alert(STR_STANDARD_ERROR + strErrorText);
				}
				break;
				
			case INT_POPUP_TYPE_CONTACT :
				if ($("#strPopupName").val().length < 3) {
					boolErrorFound		= true;
					strErrorText		+= "- Please enter your name.\n";
				}
				if ($("#strPopupEmail").val().length < 3) {
					boolErrorFound		= true;
					strErrorText		+= "- Please enter your e-mail.\n";
				}
				
				if (!boolErrorFound) {
					$("#lyrPopupSubmit").hide();
					$("#lyrPopupWorking").show();
					
					$.get(	"/search/_contact-send.php", 
							{
								id: $("#intPopupMLSID").val(), 
								name: $("#strPopupName").val(), 
								email: $("#strPopupEmail").val(), 
								comments: $("#txtPopupComments").val()
							}, 
							function(txtData) {
								$("#lyrPopupSend").hide();
								$("#lyrPopupSuccess").show();
								setTimeout(5000, function() { $.facebox.close(); });
							}, 
							"json");
				} else {
					alert(STR_STANDARD_ERROR + strErrorText);
				}
				break;
				
			case INT_POPUP_TYPE_ALERTS :
				if ($("#strPopupName").val().length < 3) {
					boolErrorFound		= true;
					strErrorText		+= "- Please enter your name.\n";
				}
				if ($("#strPopupEmail").val().length < 3) {
					boolErrorFound		= true;
					strErrorText		+= "- Please enter your e-mail.\n";
				}
				if ($("#strPopupPhone").val().length < 3) {
					boolErrorFound		= true;
					strErrorText		+= "- Please enter your phone number.\n";
				}
				if ($("#txtPopupComments").val().length < 3) {
					boolErrorFound		= true;
					strErrorText		+= "- Please describe what sort of listings you are looking for.\n";
				}
				
				if (!boolErrorFound) {
					$("#lyrPopupSubmit").hide();
					$("#lyrPopupWorking").show();
					
					$.get(	"/_common/_alerts-send.php", 
							{
								name: $("#strPopupName").val(), 
								email: $("#strPopupEmail").val(), 
								phone: $("#strPopupPhone").val(), 
								comments: $("#txtPopupComments").val()
							}, 
							function(txtData) {
								$("#lyrPopupSend").hide();
								$("#lyrPopupSuccess").show();
								setTimeout(5000, function() { $.facebox.close(); });
							}, 
							"json");
				} else {
					alert(STR_STANDARD_ERROR + strErrorText);
				}
				break;
		}
		
		// always return false so the form never gets submitted, since we do it via a jQuery GET request anyway
		return false;
	}
	
	function updateHeaderCounters(intCounterType, boolIncrement) {
		var lyrPointer;
		var txtCurrentData		= "";
		var txtSuffix			= "";
		var arrDataParts		= new Array();
		var intCurAmount		= 0;
		
		switch (intCounterType) {
			case INT_COUNTER_PROPERTIES :
				txtCurrentData		= $("#login_header_sp").html();
				txtSuffix			= "saved properties";
				lyrPointer			= $("#login_header_sp");
				break;
				
			case INT_COUNTER_SEARCHES :
				txtCurrentData		= $("#login_header_ss").html();
				txtSuffix			= "saved searches";
				lyrPointer			= $("#login_header_ss");
				break;
		}
		
		if ((txtCurrentData.indexOf(" ", 0) > -1) && (txtSuffix.length > 0)) {
			arrDataParts		= txtCurrentData.split(" ");
			
			if (arrDataParts.length > 0) {
				intCurAmount		= parseInt(arrDataParts[0]);
				
				if (boolIncrement) {
					intCurAmount++;
				} else {
					intCurAmount--;
				}
				
				$(lyrPointer).html(intCurAmount.toString() + " " + txtSuffix);
			}
		}
	}