/*	-------------------------------------------------
	CONSTANTS/LANGUAGE STRINGS
	-------------------------------------------------	*/
	var STR_HEADER_LOGIN_USERNAME		= "E-mail";
	var STR_DETAIL_QUESTION_DEFAULT		= "Your Question";
	var STR_DETAIL_EMAIL_DEFAULT		= "Your E-mail";
	var STR_SEARCH_REMARKS_DEFAULT		= "Remarks";
	var STR_SEARCH_SAVE_DEFAULT			= "Name this search";

/*	-------------------------------------------------
	READY, STEADY... GO!
	-------------------------------------------------	*/
	$(document).ready(function() {
		/*	-------------------------------------------------
			CLIENT-SIDE XSLT HTML ENTITY DECODING
			-------------------------------------------------	*/
			/*
			switch (strPageID.toLowerCase()) {
				case "search" :
				case "detail" :
				case "blog" :
					// these pages are decoded on the server side.
					break;
				
				default :
					go_decoding();
					break;
			}
			*/
		
		/*	-------------------------------------------------
			WEBSITEALIVE CODE
			-------------------------------------------------	*/
			var js = document.createElement('script');
			js.setAttribute('language', 'javascript');
			js.setAttribute('type', 'text/javascript');
			js.setAttribute('src','http://a2.websitealive.com/669/Visitor/vTracker_v2.asp?websiteid=0&groupid=669');
			document.getElementsByTagName('head').item(0).appendChild(js);

		/*	-------------------------------------------------
			HEADER FORM TRICKERY!  Yeah, see?
			-------------------------------------------------	*/
			$("#strHeaderEmail").focus(function() {
				if ($("#strHeaderEmail").val() == STR_HEADER_LOGIN_USERNAME) {
					$("#strHeaderEmail").val("");
				}
			});
			$("#strHeaderEmail").blur(function() {
				if ($("#strHeaderEmail").val() == "") {
					$("#strHeaderEmail").val(STR_HEADER_LOGIN_USERNAME);
				}
			});
		
		/*	-------------------------------------------------
			SLIDERS (like that awesome TV show)
			-------------------------------------------------	*/
			if (strPageID.toLowerCase() != "search") {
				$("#sliderSquareFootage").slider({
					animate: true, 
					range: false, 
					min: 0, 
					max: 24, 
					step: 1, 
					start: function (event, ui) { showTooltip(event, ui); }, 
					slide: function (event, ui) { showTooltip(event, ui); }, 
					change: function (event, ui) { showTooltip(event, ui); }, 
					stop: function (event, ui) { setTimeout(hideTooltip, 750, event, ui); }
				});
				$("#sliderBedrooms").slider({
					animate: true, 
					range: false, 
					min: 0, 
					max: 7, 
					step: 1, 
					start: function (event, ui) { showTooltip(event, ui); }, 
					slide: function (event, ui) { showTooltip(event, ui); }, 
					change: function (event, ui) { showTooltip(event, ui); }, 
					stop: function (event, ui) { setTimeout(hideTooltip, 750, event, ui); }
				});
				$("#sliderBathrooms").slider({
					animate: true, 
					range: false, 
					min: 0, 
					max: 7, 
					step: 1, 
					start: function (event, ui) { showTooltip(event, ui); }, 
					slide: function (event, ui) { showTooltip(event, ui); }, 
					change: function (event, ui) { showTooltip(event, ui); }, 
					stop: function (event, ui) { setTimeout(hideTooltip, 750, event, ui); }
				});
				$("#sliderPrice").slider({
					animate: true, 
					range: true,
					min: 0, 
					max: (ARR_PRICE_STEPPING.length - 1), 
					values: [0, (ARR_PRICE_STEPPING.length - 1)], 
					step: 1, 
					start: function (event, ui) { showTooltip(event, ui); }, 
					slide: function (event, ui) { showTooltip(event, ui); }, 
					change: function (event, ui) { showTooltip(event, ui); }, 
					stop: function (event, ui) { setTimeout(hideTooltip, 750, event, ui); }
				});
			} else {
				$("#sliderSquareFootage").slider({
					animate: true, 
					range: false, 
					min: 0, 
					max: 24, 
					step: 1, 
					start: function (event, ui) { showTooltip(event, ui); }, 
					slide: function (event, ui) { showTooltip(event, ui); }, 
					change: function (event, ui) { showTooltip(event, ui); }, 
					stop: function (event, ui) { loadMarkers(); setTimeout(hideTooltip, 750, event, ui); }
				});
				$("#sliderBedrooms").slider({
					animate: true, 
					range: false, 
					min: 0, 
					max: 7, 
					step: 1, 
					start: function (event, ui) { showTooltip(event, ui); }, 
					slide: function (event, ui) { showTooltip(event, ui); }, 
					change: function (event, ui) { showTooltip(event, ui); }, 
					stop: function (event, ui) { loadMarkers(); setTimeout(hideTooltip, 750, event, ui); }
				});
				$("#sliderBathrooms").slider({
					animate: true, 
					range: false, 
					min: 0, 
					max: 7, 
					step: 1, 
					start: function (event, ui) { showTooltip(event, ui); }, 
					slide: function (event, ui) { showTooltip(event, ui); }, 
					change: function (event, ui) { showTooltip(event, ui); }, 
					stop: function (event, ui) { loadMarkers(); setTimeout(hideTooltip, 750, event, ui); }
				});
				$("#sliderPrice").slider({
					animate: true, 
					range: true,
					min: 0, 
					max: (ARR_PRICE_STEPPING.length - 1), 
					values: [0, (ARR_PRICE_STEPPING.length - 1)], 
					step: 1, 
					start: function (event, ui) { showTooltip(event, ui); }, 
					slide: function (event, ui) { showTooltip(event, ui); }, 
					change: function (event, ui) { showTooltip(event, ui); }, 
					stop: function (event, ui) { loadMarkers(); setTimeout(hideTooltip, 750, event, ui); }
				});
			}
			
			// setup slider tooltips
			setupSliderTooltips();
			
   		/*	-------------------------------------------------
			GLOBAL SEARCH FORM
			-------------------------------------------------	*/
			$("#btnToolSearch").click(function() {
				submitSmallSearchTool();
			});
			
			$("#strSearchQuery").keydown(function(objEvent) {
				if (objEvent.keyCode == 13) {
					if (strPageID.toLowerCase() == "search") {
						loadMarkers();
					} else {
						submitSmallSearchTool();
					}
				}
			});
			
		/*	-------------------------------------------------
			ALERTS POPUP
			-------------------------------------------------	*/
			$("#header_alerts_link").click(function() {
				registerForAlerts();
			});

		/*	-------------------------------------------------
			SITE POPUPS
			-------------------------------------------------	*/
			$(".popup").popupwindow(arrPopupProfiles); 

		/*	-------------------------------------------------
			HURR WE GO!
			-------------------------------------------------	*/
			switch (strPageID.toLowerCase()) {
				case "home" :
					var objPlotOptions	= {
						grid:	{borderWidth: 0.5, backgroundColor: null, color: "#9a9a9a", tickColor: "#ffffff"}
					};
					
					$.plot($("#graphHomeSales"), 
						[{
							data: arrDataSalesListed,
							lines: {show: true}, 
							color: "#85c68f", 
							shadowSize: 0
						}, {
							data: arrDataSalesSold,
							lines: {show: true}, 
							color: "#9a9a9a", 
							shadowSize: 0
						}], 
						$.extend(true, {}, objPlotOptions, 
								 {
									xaxis: {tickFormatter: function (objValue, objAxis) { return arrDataSalesDates[objValue.toFixed()]; }}, 
									yaxis: {tickFormatter: function (objValue, objAxis) { return "$" + objValue.toFixed(); }}
								 })
					);
					$.plot($("#graphMortgageRates"), 
						[{
							data: arrDataMortRates,
							lines: {show: true}, 
							color: "#9a9a9a", 
							shadowSize: 0
						}], 
						$.extend(true, {}, objPlotOptions, 
								 {
									xaxis: {tickFormatter: function (objValue, objAxis) { return arrDataMortDates[objValue.toFixed()]; }}, 
									yaxis: {tickFormatter: function (objValue, objAxis) { return objValue.toPrecision(3) + "%"; }}
								 })
					);
					break;
			
				case "search" :
					$("#btnSearch").click(function() {
						loadMarkers();
					});
					$("#btnClear").click(function() {
						resetAllSearchOptions();
					});
					$("#map_hideshow_button").click(function() {
						toggleResultsPane();
					});
					
					// set up sorting
					$(".sort_header").click(function() {
						if (intCurSortType > -1) {
							sortSidebar(intCurSortType, true);	
						}
					});
													
					$("#sort_price").click(function() {
						sortSidebar(INT_SORT_PRICE, true);
					});					
					$("#sort_beds").click(function() { 
						sortSidebar(INT_SORT_BEDS, true);
					});
					$("#sort_baths").click(function() { 
						sortSidebar(INT_SORT_BATHS, true);
					});
					$("#sort_new").click(function() { 
						sortSidebar(INT_SORT_DATE, true);
					});
					
					// misc forms
					$("#strRemarks").focus(function() {
						if ($("#strRemarks").val() == STR_SEARCH_REMARKS_DEFAULT) {
							$("#strRemarks").val("");
						}
					});
					$("#strRemarks").blur(function() {
						if ($("#strRemarks").val() == "") {
							$("#strRemarks").val(STR_SEARCH_REMARKS_DEFAULT);
						}
					});
					$("#strRemarks").keydown(function(objEvent) {
						if (objEvent.keyCode == 13) {
							loadMarkers();
						}
					});
					$("#strNearQuery").keydown(function(objEvent) {
						if (objEvent.keyCode == 13) {
							loadMarkers();
						}
					});
					$("#strSearchSaveName").focus(function() {
						if ($("#strSearchSaveName").val() == STR_SEARCH_SAVE_DEFAULT) {
							$("#strSearchSaveName").val("");
						}
					});
					$("#strSearchSaveName").blur(function() {
						if ($("#strSearchSaveName").val() == "") {
							$("#strSearchSaveName").val(STR_SEARCH_SAVE_DEFAULT);
						}
					});
					$("#btnSearchSave").click(function() {
						saveSearch();
					});
					
					// search tool nav
					$("#navigation_home").click(function() {
						resetSearchToolNav();
						
						$("#search_tool_home").show();
						$("#navigation_home").addClass("navigation_home_selected");
					});
					$("#navigation_town").click(function() {
						resetSearchToolNav();
						
						$("#search_tool_town").show();
						$("#navigation_town").addClass("navigation_town_selected");
					});
					$("#navigation_near").click(function() {
						resetSearchToolNav();
						
						$("#search_tool_near").show();
						$("#navigation_near").addClass("navigation_near_selected");
					});
					$("#navigation_house").click(function() {
						resetSearchToolNav();
						
						$("#search_tool_house").show();
						$("#navigation_house").addClass("navigation_house_selected");
					});
					$("#navigation_lot").click(function() {
						resetSearchToolNav();
						
						$("#search_tool_lot").show();
						$("#navigation_lot").addClass("navigation_lot_selected");
					});
					
					$(".radTown").change(function() {
						var strThisID		= $(this).val();
						var dblTownLat		= parseFloat($("#strTownLat_" + strThisID).val());
						var dblTownLng		= parseFloat($("#strTownLng_" + strThisID).val());
						
						jumpToTown(dblTownLat, dblTownLng);
					});

					// start up map
					initializeMap();
					break;
				
				case "detail" :
					$(".minimal_link").click(function() {
						if (window.opener) {
							window.opener.location		= $(this).attr("href");
							window.opener.focus();
							window.close();
						} else {
							window.location				= $(this).attr("href");
						}
					});
					$(".tour_link").click(function() {
						openVirtualTour($(this).attr("rel"));
					});
					$("#txtQuestion").focus(function() {
						if ($("#txtQuestion").val() == STR_DETAIL_QUESTION_DEFAULT) {
							$("#txtQuestion").val("");
						}
					});
					$("#txtQuestion").blur(function() {
						if ($("#txtQuestion").val() == "") {
							$("#txtQuestion").val(STR_DETAIL_QUESTION_DEFAULT);
						}
					});
					$("#strQuestionEmail").focus(function() {
						if ($("#strQuestionEmail").val() == STR_DETAIL_EMAIL_DEFAULT) {
							$("#strQuestionEmail").val("");
						}
					});
					$("#strQuestionEmail").blur(function() {
						if ($("#strQuestionEmail").val() == "") {
							$("#strQuestionEmail").val(STR_DETAIL_EMAIL_DEFAULT);
						}
					});
					
					$("#zillow_1yr").click(function() {
						resetZillowLayers();

						$("#zillow_image_1yr").show();
						$("#zillow_1yr").addClass("zillow_1yr_selected");
					});
					$("#zillow_5yr").click(function() {
						resetZillowLayers();

						$("#zillow_image_5yr").show();
						$("#zillow_5yr").addClass("zillow_5yr_selected");
					});
					$("#zillow_10yr").click(function() {
						resetZillowLayers();

						$("#zillow_image_10yr").show();
						$("#zillow_10yr").addClass("zillow_10yr_selected");
					});
					
					$("#quickbutton_see_home").click(function() {
						$.facebox.close();
						requestInfo(parseInt($("#intMLSID").html()));
					});
					$("#quickbutton_add_favorites").click(function() {
						$.facebox.close();
						saveProperty(parseInt($("#intMLSID").html()));
					});
					$("#quickbutton_email_property").click(function() {
						$.facebox.close();
						emailProperty(parseInt($("#intMLSID").html()));
					});
					
					// school nav
					$("#school_elementary").click(function() {
						resetSchoolNav();
						
						$("#school_list_elementary").show();
						$("#school_elementary").addClass("elementary_selected");
					});
					$("#school_middle").click(function() {
						resetSchoolNav();
						
						$("#school_list_middle").show();
						$("#school_middle").addClass("middle_selected");
					});
					$("#school_high").click(function() {
						resetSchoolNav();
						
						$("#school_list_high").show();
						$("#school_high").addClass("high_selected");
					});

					$(".property_thumbnail").click(function() {
						$("#photo_loader").show();
						$("#imgHolder").attr("src", "/_content/_large.php?f=" + $(this).attr("rel"));
						
						$("#imgHolder").load(function() {
							$("#photo_loader").hide();
						});
					});
					
					$("#btnQuestionSubmit").click(function() {
						if (($("#txtQuestion").val() != "") && ($("#txtQuestion").val() != STR_DETAIL_QUESTION_DEFAULT) && ($("#strQuestionEmail").val() != "") && ($("#strQuestionEmail").val() != STR_DETAIL_EMAIL_DEFAULT)) {
							askQuestion($("#intMLSID").html());
						} else {
							alert("Please fill in a question, and if you are not logged in, your e-mail address where you can be reached.");
						}
					});
					$("#btnAlertSubmit").click(function() {
						$.facebox.close();
						alertSignup(parseInt($("#intMLSID").html()));
					});
										
					if (GBrowserIsCompatible()) {
						var objLocation	= new GLatLng(parseFloat(strPropertyLat), parseFloat(strPropertyLng));
						
						objMap			= new GMap2(document.getElementById("detail_left_map_object"));
						objMap.setCenter(objLocation, 13);
						objMap.addControl(new GMenuMapTypeControl());
						objMap.addControl(new GSmallZoomControl());
						objMap.disableDragging();

						objMarker		= new GMarker(new GLatLng(parseFloat(strPropertyLat), parseFloat(strPropertyLng)), objIcons_Residential);
						objMap.addOverlay(objMarker);
						
						// street view
						objPano			= new GStreetviewPanorama(document.getElementById("detail_left_photos_streetview_object"), {latlng: objLocation});
						GEvent.addListener(objPano, "error", function(intErrorCode) { $("#detail_left_photos_streetview").hide(); $("#header_streetview_reset_link").hide(); $("#detail_left_photos_streetview_unavailable").show(); });
						
						$("#header_streetview_reset_link").click(function() {
							objPano.setLocationAndPOV(new GLatLng(parseFloat(strPropertyLat), parseFloat(strPropertyLng)), {yaw: 0, pitch: 5, zoom: 0});
						});
					}
					break;

				case "account-properties" :
					$(".delete").click(function() {
						var intMLSID		= getMLSID(this.id);
												
						if (intMLSID > 0) {
							confirmDeleteProperty(intMLSID);
						}
					});
					$(".contact").click(function() {
						var intMLSID		= getMLSID(this.id);
												
						if (intMLSID > 0) {
							contactAgent(intMLSID);
						}
					});
					$(".email").click(function() {
						var intMLSID		= getMLSID(this.id);
												
						if (intMLSID > 0) {
							emailListing(intMLSID);
						}
					});
					$(".similar").click(function() {
						var intMLSID		= getMLSID(this.id);
						var txtRelated		= $("#options-" + intMLSID).val();
						
						if (txtRelated.length > 0) {
							txtRelated		= parseRelatedString(txtRelated);
							
							if (txtRelated.length > 0) {
								$("#rel").val(txtRelated);
								$("#frmRelatedSearch").submit();
							}
						}
					});
					break;

				case "account-searches" :
					$(".search_recall_submit").click(function() {
						var strSearchID			= $(this).attr("rel");
						
						$("#sid").val(strSearchID);
						$("#frmSearchSubmit").submit();
					});
					$(".search_toggle_checkbox").change(function() {
						var strSearchID			= this.value;
						var intChecked			= this.checked;
						
						$.get(	"/account/searches/_toggle.php", 
							  	{
									sid: strSearchID, 
									notify: (intChecked) ? 1 : 0
								}, 
								function() {
									alert("Successfully updated the notify status on that search!");
								});
					});
					break;

				case "charleston" :
					// start up map
					if (GBrowserIsCompatible()) {
						objMap			= new GMap2(document.getElementById("map_object"));
						objMap.setCenter(new GLatLng(32.793053, -79.941294), 13);
						objMap.disableDragging();
					}
					break;
					
				case "community" :
					if (GBrowserIsCompatible()) {
						objMap			= new GMap2(document.getElementById("map_object"));
						objMap.setCenter(new GLatLng(dblLatitude, dblLongitude), 14);
						objMap.disableDragging();
						objMap.addControl(new GMapTypeControl());
						objMap.addControl(new GSmallZoomControl());
						
						objMarker			= new GMarker(new GLatLng(dblLatitude, dblLongitude), objIcons_Residential);
						objMap.addOverlay(objMarker);
					}
					break;
					
				case "buyer" :
					swfobject.embedSWF(	"/_flash/oxylus.flvplayer.swf", 
										"video_object", 
										"480", 
										"385", 
										"9.0.0", 
										"_js/swfobject/expressInstall.swf", 
										{
											xml:	"/buyer/settings.xml?f=" + strPageAttachment
										}, 
										{menu: "false", wmode: "transparent", base: "/_content/_pages/"});
					break;

				case "seller" : 
					swfobject.embedSWF(	"/_flash/oxylus.flvplayer.swf", 
										"video_object", 
										"480", 
										"385", 
										"9.0.0", 
										"_js/swfobject/expressInstall.swf", 
										{
											xml:	"/seller/settings.xml?f=" + strPageAttachment
										}, 
										{menu: "false", wmode: "transparent", base: "/_content/_pages/"});
					break;
			}
	});

/*	-------------------------------------------------
	SO HARD TO SAY GOODBYE :(
	-------------------------------------------------	*/
	window.onunload = function() {
		switch (strPageID.toLowerCase()) {
			case "charleston" :
			case "community" :
			case "search" :
				GUnload();
				break;
		}
	};
