// JavaScript Document
var points = [];
var num_points = 0;
var box;
var map;
var ajax_url='search1.php?'; //default search
var infopanel;
var markerlist;
var baseIcon = new GIcon(G_DEFAULT_ICON);

function initialize()
{
	if (GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById("map_canvas"));
		//map.disableDragging();

		// Reset some defaults
		$("#saved_lat1").val("35.505400093441295");
		$("#saved_lat2").val("36.954281585675965");
		$("#saved_long1").val("-89.3682861328125");
		$("#saved_long2").val("-84.2706298828125");
		$("#bedrooms").val("*");
		$("#bathrooms").val("*");
		$("#pricemin").val("Min");
		$("#pricemax").val("Max");
		$("#sqftmin").val("Min");
		$("#sqftmax").val("Max");
		$("#startnum").val(0);
		$("#domap").val("true");

		infopanel = document.getElementById("infopanel");
		map.addControl(new GSmallMapControl());

		var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(0,0));
		map.addControl(new GZoomControl(
		{
			nOpacity:.2,
			sBorder:"2px solid #5E2E13"
		},
		{
			sButtonHTML:"<img src='/images/icons/icon-mag.png' />",
			sButtonZoomingHTML:"<img src='/images/icons/icon-mag-active.png' />",
			oButtonZoomingStyle:"",
//			sButtonHTML:"<img src='/images/icons/zoom-button.gif' />",
			nOverlayRemoveMS: 1000,
			oButtonStartingStyle:{width:'28px',height:'30px',background:'transparent'}
		},
		/* third set of options specifies callbacks */
		{

			dragEnd:function(nw,ne,se,sw,nwpx,nepx,sepx,swpx) {
				hideme();
				$("#saved_lat1").val(sw.lat());
				$("#saved_lat2").val(ne.lat());
				$("#saved_long1").val(sw.lng());
				$("#saved_long2").val(ne.lng());
				displaymarkers();
			}//dragEnd
		}
		), topRight);
		map.setCenter(new GLatLng(36.1639, -86.7847), 11); //36.1339, -86.7847
		$("#gzoom-control").css( "background-color", "transparent" );
	}//GBrowserIsCompatible
}//initialize()

function displaymarkers()
{
	map.clearOverlays();
	clearlistings();

	ajax_url = "search1.php?" +  "l1="+ $("#saved_lat1").val() + "&n1="+ $("#saved_long1").val() + "&l2="+ $("#saved_lat2").val() + "&n2="+ $("#saved_long2").val() + "&bedrooms=" + $("#bedrooms").val() + "&bathrooms=" + $("#bathrooms").val() + "&sqftmax=" + $("#sqftmax").val() + "&sqftmin=" + $("#sqftmin").val() + "&pricemin=" + $("#pricemin").val() + "&pricemax=" + $("#pricemax").val() + "&proptype=" + $("#typeofhome").val();

	var request = GXmlHttp.create();
	request.open('GET',ajax_url, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4)
		{
			$("#listingul").show();
			var xmlsource = request.responseXML;
			index = 0;
			points = [];
			var errors = xmlsource.documentElement.getElementsByTagName("error");

			if (errors.length > 0)
			{
				infopanel.innerHTML = errors[0].getAttribute("message");
				return;
			}
			var markerlist = xmlsource.documentElement.getElementsByTagName("house");
			var totalnum = 0;

			var start = parseInt( $("#startnum").val() );
			var maxnum = ( 12 + start > markerlist.length ) ? markerlist.length : 12 + start;
			var rescounter = 1;
			for (var i=start;i < maxnum;i++)
			{
				$("#result" + (rescounter++)).html( buildlisting( markerlist[i] ) );
				totalnum = markerlist[i].getAttribute("totalcount");
			}//for
			if ( $("#domap").val() == "true" )
			{
				var maxmarkers = ( markerlist.length > 1000 ) ? 1000 : markerlist.length;
				for ( i = 0; i < maxmarkers; i++ )
				{
					info_txt = markerlist[i].getAttribute("address");
					info_city = markerlist[i].getAttribute("city");
					info_state = markerlist[i].getAttribute("state");
					info_zip = markerlist[i].getAttribute("zip");
					info_mls = markerlist[i].getAttribute("mlsnum");
					info_pic = markerlist[i].getAttribute("primarypictureurl");
					addmarker(parseFloat(markerlist[i].getAttribute("lat")),
								parseFloat(markerlist[i].getAttribute("lon")), info_txt, info_city, info_state, info_zip, info_mls, info_pic);
				}
			}
			start++;
			$("#listnums").html(start + "-" + maxnum + " of");
			$("#totalnum").html("<strong>" + totalnum + "</strong>");
			$("#gzoom-control").css( "background-color", "transparent" );
		}//request.readyState == 4
	}//function onreadystatechange
	$("#domap").val("true");
	request.send(null);
}

function addmarker(lat,long,info_txt, info_city, info_state, info_zip, info_mls, info_pic)
{
	var point = new GLatLng(parseFloat(lat),parseFloat(long),true);
	points.push(point);
	var marker_ndx=index+1;
	baseIcon.image = "/images/icons/markers/marker.png";
	var markerOptions = { icon:baseIcon };
	var marker = new GMarker(point,markerOptions);
	map.addOverlay(marker);
	GEvent.addListener(marker, "click",
		function() {
			marker.openInfoWindowHtml("<div class='iwstyle'><div style='float: left; padding-right: 5px;'><img src='" + info_pic + "' width='80' height='60'></div>" + info_txt + "<br>" + info_city + ", " + info_state + " " + info_zip + "<br><a href='/listings/detail.php?mls=" + info_mls + "'>View Listing Details</a></div>");
		}
	);
	index++;
}

function buildlisting( obj )
{
	var html = "";
	if ( obj.getAttribute("primarypictureurl").length > 0 )
		html += "<img src=\"" + obj.getAttribute("primarypictureurl") + "\" width=\"91\" height=\"70\" />";

	html += "<p><span>" + obj.getAttribute("address") + "</span><br />";
	html += "$" + addCommas( obj.getAttribute("listprice") ) + "<br />";
	if ( +obj.getAttribute("bedrooms") > 0 )
		html += obj.getAttribute("bedrooms") + " BR/";
	if ( +obj.getAttribute("fullbaths") > 0 )
		html += obj.getAttribute("fullbaths") + " BA/";
	if ( +obj.getAttribute("halfbaths") > 0 )
		html += obj.getAttribute("halfbaths") + " &frac12; BA/";
	html += addCommas( obj.getAttribute("squarefeet") ) + " sq ft<br>";
	html += "<a href='/listings/detail.php?mls=" + obj.getAttribute("mlsnum") + "'>VIEW PROPERTY</a>";
	html += "</p>";
	return html;
}

function clearlistings()
{
	$("#map-helper").hide('fast');
	for ( i=1; i<=12; i++ )
	{
		$("#result" + (i)).html( "<p>&nbsp;</p>" );
	}
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function nextlistings()
{
	var listnums = $("#listnums").html();
	$("#domap").val("false");
	if ( listnums != "&nbsp;" )
	{
		listnums = listnums.substr(0, listnums.length - 3);
		bits = listnums.split("-");
		sttwelve = parseInt( bits[0] ) + 11;

		var totalnum = $("#totalnum").html();
		totalnum = parseInt( totalnum.substr( 8, totalnum.length - 17 ) );

		if ( sttwelve > totalnum )
			return true;
		else
		{
			$("#startnum").val( sttwelve );
			displaymarkers();
		}
	}
}

function prevlistings()
{
	var listnums = $("#listnums").html();
	$("#domap").val("false");
	if ( listnums != "&nbsp;" )
	{
		listnums = listnums.substr(0, listnums.length - 3);
		var bits = listnums.split("-");
		var sttwelve = parseInt( bits[0] ) - 13;

		if ( sttwelve < 0 )
			return true;
		else
		{
			$("#startnum").val( sttwelve );
			displaymarkers();
		}
	}
}

function do_displaymarkers()
{
	$("#domap").val("false");
	$("#startnum").val(0);
	displaymarkers();
}

function clearminmax( which )
{
	if ( ( $("#"+which).val() == "Max" ) || ( $("#"+which).val() == "Min" ) )
		$("#"+which).val("");
}

