	// Global Variables
	var currentSlidePage = 1;
	var currentFeatured = 1;
	var ajaxDataType, type, saleType, deepLink;
	var __global = this;

	// Get Deeplink values
	function getURLVars(){
		
		var hash = location.hash.substr(1);
		var result = {};

		if (!hash) return false;

		var pairs = hash.split("&");

		for (var i = 0; i < pairs.length; i++){			
			var splitPairs = pairs[i].split("=");
			result[splitPairs[0]] = splitPairs[1];
		}

		return result;

	}

	
	$(document).ready(function (){

		ajaxDataType = ($.browser.msie) ? 'text' : 'XML';
		//ajaxDataType = 'XML';
		
		deepLink = getURLVars();
		if (deepLink) {
			ajax_propDetail();
		}

		ajax_updateTotals();
		

		ajax_slideData(); // Load all slides
		ajax_featuredData(currentFeatured); // Load featured data

		// button actions for featured next button
		$("#featuredNext_btn").click(function (){
			ajax_featuredData(parseInt(currentFeatured) + 1);
			return false;
		});
		
		// button actions for prev slide button
		$("#contentNav_prev_btn").click(function (){
			ajax_slideData(parseInt(currentSlidePage) - 1);
			return false;
		});
		
		// button actions for next slide button
		$("#contentNav_next_btn").click(function (){
			ajax_slideData(parseInt(currentSlidePage) + 1);
			return false;
		});

		// button actions for accordian header
		$(".accordHead").click(function (){			
			// Stupid accordian actions that the client wanted.
			var activePane = $(this).parent().find(".accordBody");
			if ($(".state", this).html() == "+"){
				$(".state", this).html("&ndash;");
			}else{
				$(".state", this).html("+");
			}
			$(activePane).slideToggle("fast");	
		


			// ** This is what makes the accordian work right
			/*$(".accordBody").slideUp("fast");
			$(".accordHead .state").html("+");

			var activePane = $(this).parent().find(".accordBody");
			if (activePane.css("display") == "none") {
				activePane.slideDown("fast");
				$(".state", this).html("&ndash;");
			}*/
		});

		// button actions for register form
		$("#signup_btn").click(register);

		// focus event for register field
		$("#register_input").focus(function (){
			if ($(this).val() == "Email Address") $(this).val("");
		});

		$("#register_input").blur(function (){
			if ($(this).val() == "") $(this).val("Email Address");
		});
		

		// Search select functionality
		$("#search_select").change(function (){

			if ($(this).val() == "0") return false;

			type = undefined;
			saleType = undefined;

			var vals = $(this).val().split(":");
			if (vals.length == 2){
				type = vals[0];
				saleType = vals[1]
			}else{
				type = vals[0];
			}
			
			$("#propDetails").slideUp('fast');
			ajax_slideData();
			return false;
		});


		// Show / Hide Loader
		$("#loading").bind("ajaxSend", function(){
			$(this).show();
		}).bind("ajaxComplete", function(){
			$(this).hide();
		});

		
	});

	// Register 
	function register (){
		
		var errors = [];		
		var emailRegEx = new RegExp(/^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/);
		if ($("#register_input").val() == 'Email Address' || !emailRegEx.test($("#register_input").val())) errors.push({msg:"Invalid Email Address.", field:$("#register_input")});	

		if (errors.length){
			var newHTML = "";
			for (var i=0; i < errors.length; i++){
				newHTML += errors[i].msg;
			}
			$("#register label").html(newHTML);
		}else{
			$.ajax({
				type:"GET", 
				url:"ajax/register.cfm", 
				data:{email: $("#register_input").val(), site: $("#site_input").val()}, 
				success:function (xml){
					$("#register label").html($("message", xml).text());
				}});
		}
	}

	
	// Results Filter
	function filterSlides (f, fString, btn){
		$(".nav").removeClass("selected");
		$(btn).addClass("selected");

		type = undefined;
		saleType = undefined;
		__global[f] = fString;
		ajax_slideData();		
		$("#propDetails").slideUp('fast');
		return false;
	}
	

	// Ajax Calls : Get Slide Data
	function ajax_slideData (page){
		if (page == undefined) var page = 1;

		var dataObj = {};
		dataObj.currentPage = page;
		if (type != undefined) dataObj.type = type;
		if (saleType != undefined) dataObj.saleType = saleType;

		$.ajax({dataType: ajaxDataType, url: "ajax/getProperties.cfm", data: dataObj, success: createSlides});
	}

	// Ajax Calls : Get Featured Data
	function ajax_featuredData(page){
		$.ajax({dataType: ajaxDataType, url: "ajax/getProperties.cfm", data: {type:"featured", currentPage:page, maxRecords:1}, success: showFeatured});
	}

	// Ajax Calls : Get Detail Data
	function ajax_propDetail (){

		var dataObj = {};
		if (deepLink){
			dataObj.id = deepLink.id;
			deepLink = false;
		}else{
			dataObj.id = this.id;
		}	

		$.ajax({dataType: ajaxDataType, url: "ajax/getProperties.cfm", data: dataObj, success: showDetails});
	}

	// Ajax Calls : Get Totals
	function ajax_updateTotals(){
		$.ajax({dataType: ajaxDataType, url: "ajax/getTotals.cfm", data: {}, success: function (xml){

			if ($.browser.msie) xml = checkData(xml);

			$("#saleTotal").html($("saletotal", xml).text());
			$("#leaseTotal").html($("leasetotal", xml).text());
			$("#sqftTotal").html($("sqfoottotal", xml).text());

		}});
	}


	function checkData(data){
		var d = new ActiveXObject( 'Microsoft.XMLDOM');
		d.async = false;
		d.loadXML(data);
		return d;
	}

	//Create Slides
	function createSlides (xml){

		if ($.browser.msie) xml = checkData(xml);

		//alert();
		
		$("#slides").html("");
		
		$("item", xml).each(function (){
			var id 		= $(this).attr('id');
			var name 	= $(this).attr('name');
			var img 	= ($(this).attr('image') != '') ? $(this).attr('image') : "http://www.cityofdaytonproperties.com/global/ui_elements/slide_placeHolder.jpg";
			
			var truncName = (name.length >= 27) ? name.slice(0, name.lastIndexOf(" ")) + "..." : name;
			
			var slideCode = '<div class="slide"><a id="'+id+'" href="#id='+id+'"><img src="'+img+'" alt="'+name+'" /></a><div class="slide_title">'+truncName+'</div></div>';
			$("#slides").append(slideCode);		
		});

		if ($("#slides").html() == "") $("#slides").html("<p>There are currently no properties that match your criteria.</p>");

		$(".slide a").click(ajax_propDetail);
		$("#slides").append('<div class="clear"></div>');
		
		currentSlidePage = parseInt($("page", xml).text());

		if (parseInt($("pageTotal", xml).text()) > 1){
			$("#contentHead .pages").show();
			$("#contentHead .pageNum").text(currentSlidePage);
			$("#contentHead .pageTotal").text(parseInt($("pagetotal", xml).text()));

		}else{
			$("#contentHead .pages").hide();
		}
	}
	
	//Update Featured Properties Panel
	function showFeatured (xml){
		if ($.browser.msie) xml = checkData(xml);
		
		var propInfo 				= {};
		propInfo.id 				= $("item", xml).attr("id");
		propInfo.name			= $("item", xml).attr("name");
		propInfo.sqFootage		= $("item", xml).attr("sqFootage");
		propInfo.price				= $("item", xml).attr("price");
		propInfo.image			= ($("item", xml).attr("image") == "") ? "http://www.cityofdaytonproperties/global/ui_elements/slide_placeHolder.jpg" : $("item", xml).attr("image");

		var dataHTML 			= "<strong>" + propInfo.name+"</strong><br />";
		if (propInfo.sqFootage != "") 	dataHTML	+= propInfo.sqFootage+"<br />";
		if (propInfo.price != "") 			dataHTML	+= propInfo.price+"<br />";
		dataHTML					+= "<a href=\"#id="+propInfo.id+"\">View</a>";

		$("#featured .featured_img IMG").attr("src", propInfo.image);
		$("#featured .data").html(dataHTML);

	
		$("#featured .data a, #featured .featured_img IMG").attr("id", propInfo.id);
		$("#featured .data a, #featured .featured_img IMG").click(ajax_propDetail);

		currentFeatured = parseInt($("page", xml).text());

	}

	//Display Property Details
	function showDetails (xml) {
		
		if ($.browser.msie) xml = checkData(xml);

		$("#propDetails .detail_item").show();
		$(".accordItem").show();
		$(".accordBody").show();
		$(".accordHead .state").html("&ndash;");
		
		var propInfo 				= {};
		propInfo.id 				= $("item id", xml).text();
		propInfo.type				= $("item type", xml).text();
		propInfo.saleType		= $("item saletype", xml).text();
		propInfo.name			= $("item name", xml).text();
		propInfo.sqFootage		= $("item sqFootage", xml).text();
		propInfo.price			= $("item price", xml).text();
		propInfo.address		= $("item address", xml).text();
		propInfo.city				= $("item city", xml).text();
		propInfo.state			= $("item state", xml).text();
		propInfo.zip				= $("item zip", xml).text();
		propInfo.acreage		= $("item acreage", xml).text();
		propInfo.lotsize			= $("item lotsize", xml).text();
		propInfo.desc				= $("item desc", xml).text();
		propInfo.addinfo			= $("item addinfo", xml).text();
		propInfo.extLink			= $("item extLink", xml).text();
		propInfo.image1			= $("item image1", xml).text();
		propInfo.image2			= $("item image2", xml).text();
		propInfo.image3			= $("item image3", xml).text();
		propInfo.image4			= $("item image4", xml).text();
		propInfo.image5			= $("item image5", xml).text();
		propInfo.showMap		= $("item showMap", xml).text();
		propInfo.lat				= $("item lat", xml).text();
		propInfo.lng				= $("item lng", xml).text();
		propInfo.contactInfo	= $("item contactInfo", xml).text();
		
		location.hash = "id="+propInfo.id;

		propInfo.fullAddress = propInfo.address + "<br />" + propInfo.city + ", " + propInfo.state + " " + propInfo.zip;
				
		if (propInfo.extLink.length > 40){
			linkText = propInfo.extLink.substr(0, 40) + "...";
		}else{
			linkText = propInfo.extLink;
		}
		if (propInfo.extLink != "") propInfo.extLink = "<a href=\""+propInfo.extLink+"\" target=\"_blank\">"+linkText+"</a>";

		$("#propDetails #title").html(propInfo.name);
		if (propInfo.sqFootage != '') 		{ $("#propDetails #sq_ft SPAN").html(propInfo.sqFootage); } 										else { $("#propDetails #sq_ft").hide(); }
		if (propInfo.price != '') 				{ $("#propDetails #price SPAN").html(propInfo.price); } 												else { $("#propDetails #price").hide(); }
		if (propInfo.acreage != '') 			{ $("#propDetails #acreage SPAN").html(propInfo.acreage); } 										else { $("#propDetails #acreage").hide(); }
		if (propInfo.lotsize != '') 				{ $("#propDetails #lotsize SPAN").html(propInfo.lotsize); } 											else { $("#propDetails #lotsize").hide(); }
		if (propInfo.saleType != '') 			{ $("#propDetails #propType SPAN").html(propInfo.type+" - "+propInfo.saleType); } 	else { $("#propDetails #propType").hide(); }
		if (propInfo.fullAddress != '') 		{ $("#propDetails #address SPAN").html(propInfo.fullAddress); }									else { $("#propDetails #address").hide(); }
		if (propInfo.extLink != '') 			{ $("#propDetails #exLink SPAN").html(propInfo.extLink); } 											else { $("#propDetails #exLink").hide(); }
		if (propInfo.desc != '') 				{ $("#propDetails #desc SPAN").html(propInfo.desc); } 												else { $("#propDetails #desc_accord").hide(); }
		if (propInfo.addinfo != '') 			{ $("#propDetails #addinfo SPAN").html(propInfo.addinfo); } 										else { $("#propDetails #addinfo_accord").hide(); }
		if (propInfo.addinfo != '') 			{ $("#propDetails #addinfo SPAN").html(propInfo.addinfo); } 										else { $("#propDetails #addinfo_accord").hide(); }
		if (propInfo.contactInfo != '') 		{ $("#propDetails #contactinfo SPAN").html(propInfo.contactInfo); } 								else { $("#propDetails #contactinfo_accord").hide(); }

		if (propInfo.image1 != ''){
			$("#propDetails .mainImg").html("<img src=\""+propInfo.image1+"\" alt=\""+propInfo.name+"\" />");
		}else{
			$("#propDetails .mainImg").html("<img src=\"http://www.cityofdaytonproperties.com/global/ui_elements/slide_placeHolder.jpg\" alt=\""+propInfo.name+"\" />");
		}

		$("#propDetails .thumbs").html("");

		if (propInfo.image1 != '') $("#propDetails .thumbs").append("<div class=\"thumb\"><a href=\"#\"><img src=\""+propInfo.image1+"\" alt=\""+propInfo.name+" - Thumbnail 1\" /></a></div>");
		if (propInfo.image2 != '') $("#propDetails .thumbs").append("<div class=\"thumb\"><a href=\"#\"><img src=\""+propInfo.image2+"\" alt=\""+propInfo.name+" - Thumbnail 2\" /></a></div>");
		if (propInfo.image3 != '') $("#propDetails .thumbs").append("<div class=\"thumb\"><a href=\"#\"><img src=\""+propInfo.image3+"\" alt=\""+propInfo.name+" - Thumbnail 3\" /></a></div>");
		if (propInfo.image4 != '') $("#propDetails .thumbs").append("<div class=\"thumb\"><a href=\"#\"><img src=\""+propInfo.image4+"\" alt=\""+propInfo.name+" - Thumbnail 4\" /></a></div>");
		if (propInfo.image5 != '') $("#propDetails .thumbs").append("<div class=\"thumb\"><a href=\"#\"><img src=\""+propInfo.image5+"\" alt=\""+propInfo.name+" - Thumbnail 5\" /></a></div>");

		$("#propDetails .thumbs A").click(function (){
			$("#propDetails .mainImg IMG").attr("src", $("IMG", this).attr("src"));
			return false;
		});

		// set up property details zebra striping
		$(".accordBody").each(function (){
			$(".detail_item", this).removeClass("alt");
			$(".detail_item", this).filter(":visible").filter(":odd").addClass("alt");
		});

		$("#propDetails").slideDown('fast', function (){
			if (propInfo.showMap == "1"){
				$("#map_accord").show();
				loadMap(propInfo.lat, propInfo.lng);
			}else{
				$("#map_accord").hide();
			}
		});
		
	}
	
	function loadMap(lat, lng){
		lat = parseFloat(lat);
		lng = parseFloat(lng);
		if (GBrowserIsCompatible()) {
	        var map = new GMap2(document.getElementById("googMap"));
			var mapCenter = new GLatLng(lat,lng);
			map.setCenter(mapCenter, 13);
			map.getInfoWindow(); 
	        map.addControl(new GSmallMapControl());
			
			var marker = new GMarker(mapCenter);
			map.addOverlay(marker);
			GEvent.addListener(marker, "click", function (){
				if (document.getElementById("pano") == null){
					$("BODY").append("<div id='pano' style='width: 300px; height: 200px;'><div>");	
					marker.openInfoWindow(document.getElementById("pano"), {maxWidth:300, maxHeight:200});
					var panoramaOptions = { latlng:mapCenter };	
					var myPano = new GStreetviewPanorama(document.getElementById("pano"), panoramaOptions);
					myPano.checkResize()
				}				
		  	});
			
      	}		
	}
	
	
	
