// import the Google Maps API
var googleKey;
var domain = window.location.toString().split("/")[2]; // this gets the domain
if( domain == "localhost" )
	googleKey = "ABQIAAAAj4u0lOk5OQDQ8_AGkXUqGhT2yXp_ZAY8_ufC3CFXhHIE1NvwkxRjkjVjKfLzLNrIneAA2shYOu-B4g";
else if( domain == "development" )
	googleKey = "ABQIAAAAj4u0lOk5OQDQ8_AGkXUqGhSFixu2GIM8PTE-IM8y5bEODgcMoRQj34Kgind8tclDWW0ig6oPn_vM9g";
else if( domain == "intermountainmls.vps-server.com" )
	googleKey = "ABQIAAAAj4u0lOk5OQDQ8_AGkXUqGhTo6PMrPkVH9qWnh08kn1kz1kfmJRRfvcRlg8F-6mpdiXF8bxuamTBCvA";
else if( domain == "www.spokaneopen.com" )
	googleKey = "ABQIAAAAzl69x63PTtG_OQtPt7dBchTV6h0naQA8Pi0HcmUjL6Ua2jZzaxSS7vYTSmivNE99UV-9e7T4hs30dA";
else if( domain == "spokaneopen.com" )
	googleKey = "ABQIAAAAzl69x63PTtG_OQtPt7dBchQ1k6M-CX-oKjis3CHjizTY7f4XjhS_GOx0Sw9ez19TgKBHk1GY1uV8Ag";
else if( domain == "209.34.171.130" )
	googleKey = "ABQIAAAAzl69x63PTtG_OQtPt7dBchS2R6X_ef_vCGMMsthIU-syCyGJ7BRERkp40ZIpoi0LR1yJM9ST4kJ_1g";
else // it must be development.insllc.net:5151
	googleKey = "ABQIAAAAj4u0lOk5OQDQ8_AGkXUqGhTBT1lgsJkkn1vmHojYqk3n09RO5hSuVr37jt57ApgF2Tua74WQHp-rHg";

document.write('<script src="http://maps.google.com/maps?file=api&amp;v=2&key='+googleKey+'" type="text/javascript"></script>');
		


// Global vars
var map;
var geocoder;
var latInp;
var lngInp;
var addressInp;
var cityInp;
var stateInp;
var cicon;
var mapControl;
var showWarn;
var warnMsg;
				
// Initialize the map
function load() 
{
  if (GBrowserIsCompatible()) 
  {
		var mapDiv = document.getElementById("map");
		// if we can't find the map div, return. it means we aren't editing any
		// open houses
  	if( !mapDiv )
			return;

		map = new GMap2(mapDiv);
		map.addControl(new CustomZoomControl());
		map.disableDragging();
		//mapControl = new GSmallMapControl();
		//map.addControl(mapControl);
		//GEvent.addListener(map, "mousemove", mapMouseMove);
		//GEvent.addListener(map, "mouseout", mapMouseOut);
		//GEvent.addListener(map, "mouseover", mapMouseOver);
		geocoder = new GClientGeocoder();

		cicon = new GIcon();
		cicon.image = "images/house_icon.gif";
		cicon.iconSize = new GSize(28, 31);
		cicon.iconAnchor = new GPoint(14, 31);
		//cicom.dragCrossImage = "../images/house_icon.gif";
		//cicom.dragCrossSize = new GPoint(5, 5);
		//cicon.dragCrossAnchor = new GPoint(0, 15);
		cicon.maxHeight = 15;
		//geoCodeAddress();
		//geoCodeAddress("
		//loadData(txtAddr, ddlCity, txtZip);
		//alert("loading");
	}
}

function loadCenterPoint(lat, lng)
{
	latInp = lat;
	lngInp = lng;
	zoomLevel = 11;
	markPoint(new GLatLng(lat,lng));
	hideError();
}

function markPoint(point)
{
	map.clearOverlays();
	map.setCenter(point, zoomLevel);
	
	marker = new GMarker(point, {icon: cicon, draggable: false, bouncy: false, clickable: false});
	//GEvent.addListener(marker, "dragend", markerDragEnd);
	map.addOverlay(marker);
	saveMarkedPoint();
}

/*
function loadData(txtAddr, txtCity, txtZip)
{
		var point = null;

		latInp = document.getElementById("lat");
		lngInp = document.getElementById("lng");
		addressInp = txtAddr;
		cityInp = txtCity;
		stateInp = txtZip;
		if( latInp.value == "0" || lngInp.value == "0" )
		{
			geoCodeAddress();
		}
		else
		{	
			if (latInp.value != "" && lngInp.value != "")
			{
				point = new GLatLng(latInp.value, lngInp.value);
				// this will cause our variables to overwrite each other,
				// but this is OK because it will be the same values.
				markGeoCode(point);
			}
		}
}
*/
function saveMarkedPoint()
{
	latInp = marker.getPoint().lat();
	lngInp = marker.getPoint().lng();
	document.getElementById("lat").value = latInp;
	document.getElementById("lng").value = lngInp;
}

function clearMarkedPoint()
{
	latInp = "";
	lngInp = "";	
}


// map and mouse events

var origPoint;
var dragging;
function mapMouseMove(point)
{
	if( dragging && marker )
	{
		marker.setPoint(point);
	}
}

function mapMouseOver()
{
	var textArea = document.getElementById("testText");
	textArea.select();
}

function mapMouseOut()
{
	if( dragging && marker )
	{
		marker.setPoint(origPoint);
	}
}

function dragIconStart()
{
	//map.disableDragging();
	dragging = true;
	if( marker )
		origPoint = marker.getPoint();
}

function dragIconEnd()
{
	//map.enableDragging();
	dragging = false;
}

function dragIconEndMap()
{
	var map = document.getElementById("testText");
	map.select();
	dragging = false;
	//map.enableDragging();
}

function markerDragEnd(v)
{
	saveMarkedPoint();
	map.panTo(marker.getPoint());
}

// marker events
var marker;

function markGeoCode(point)
{
/*
	map.clearOverlays();
	map.setCenter(point, 11);
	//alert("helo");
	marker = new GMarker(point, {icon: cicon, draggable: false, bouncy: false});
	//GEvent.addListener(marker, "dragend", markerDragEnd);
	map.addOverlay(marker);
	
	
	saveMarkedPoint();
	*/
	//loadCenterPoint(point.lat(), lngInp = point.lng());
	markPoint(point);
}
/*
function regeocode()
{
	geoCodeAddress();
}
*/

function displayError()
{
	var errorD = document.getElementById("errorDiv");
	errorD.style.display="block";
}

function hideError()
{
	var errorD = document.getElementById("errorDiv");
	errorD.style.display="none";
}

var zoomLevel = 10;
var geoCodeFromClick = false;

function geoCodeAddress(txtAddress, txtCity, txtZip)
{
	showWarn = false;
	warnMsg = "";
	hideError();
	//alert("from click: "+geoCodeFromClick);
	zoomLevel = 10;
	clearMarkedPoint();
	//var address = addressInp.value;
	//var city = cityInp.value;
	//var zip = stateInp.value;
	var attemptFullAddress = false;
	

	if( txtAddress != "Street name & number" && 
		(txtCity != "All Cities" || txtZip != "Zip code" ) && 
		geoCodeFromClick )
	{
		attemptFullAddress = true;
	}
	else if( txtAddress == "Street name &amp; number" && txtZip == "Zip code" )
	{
		zoomLevel = 7;
	}
	/*
	if( txtAddress == "Street name &amp; number" && txtZip == "Zip code" )
	{
		zoomLevel = 7;
	}
	else if( geoCodeFromClick )
	{
		attemptFullAddress = true;
	}
	*/
	geoCodeFromClick = false;
	
	//alert("full address: "+attemptFullAddress);
	

	
	if (txtAddress == "Street name & number")
	{
		//zoomLevel = 7;
		txtAddress = "";
	}
	if (txtZip == "Zip code")
	{
		//zoomLevel = 7;
		txtZip = "";
	}
	if (txtCity == "All Cities")
	{
		txtCity == ""
	}
	var codeStr = "";
	if (txtAddress != "")
	{
		codeStr += txtAddress+", ";
	}
	if (txtCity != "")
	{
		codeStr += txtCity+" ";
	}
	if (txtZip != "")
	{
		codeStr += txtZip;
	}
	//geocoder.getLatLng( txtAddress+", "+txtCity+" "+txtZip, 
	geocoder.getLatLng(codeStr, 
		function(p)
		{
			if( p )
			{
				markGeoCode(p);
			}
			else
			{
				if( attemptFullAddress )
				{
					displayError();
				}
				geocoder.getLatLng(txtCity+" "+txtZip,
					function(p2)
					{
						if( p2 )
						{
							markGeoCode(p2);
						}
						else
						{
							geocoder.getLatLng(txtZip,
							function(p3)
							{
								if( p3 )
								{
									markGeoCode(p3);
								}
								else
								{
									geocoder.getLatLng("Spokane, WA",
									function(p4)
									{
										if( p4 )
										{
											markGeoCode(p4);
										}
										else
										{
											// we can't do anything at this point
											// (except maybe hardcode a number)
										}
									});
								}
							});
						}
					});
				}
			});
}

function geoCodeAddressWPostback(txtAddress, txtCity, txtZip)
{
	showWarn = false;
	warnMsg = "";
	var dr = document.getElementById("fromDrag").value;
	if (dr == "1")
	{
		__doPostBack('lnkSearch', '');
		return;
	}
	hideError();
	//alert("from click: "+geoCodeFromClick);
	//zoomLevel = 10;
	clearMarkedPoint();
	//var address = addressInp.value;
	//var city = cityInp.value;
	//var zip = stateInp.value;
	var attemptFullAddress = false;
	
	if( txtAddress == "Street name &amp; number" && txtZip == "Zip code" )
	{
		zoomLevel = 7;
	}
	else if( geoCodeFromClick )
	{
		attemptFullAddress = true;
	}
	geoCodeFromClick = false;
	
	//alert("full address: "+attemptFullAddress);
	
	if (txtAddress == "Street name & number")
	{
		//zoomLevel = 7;
		txtAddress = "";
	}
	if (txtZip == "Zip code")
	{
		//zoomLevel = 7;
		txtZip = "";
	}
	if (txtCity == "All Cities")
	{
		txtCity == ""
	}
	var codeStr = "";
	if (txtAddress != "")
	{
		codeStr += txtAddress+", ";
	}
	if (txtCity != "")
	{
		codeStr += txtCity+" ";
	}
	if (txtZip != "")
	{
		codeStr += txtZip;
	}
	//geocoder.getLatLng( txtAddress+", "+txtCity+" "+txtZip,
	
	geocoder.getLatLng(codeStr, 
		function(p)
		{
			if( p )
			{
				markGeoCode(p);
				postBack();
			}
			else
			{
				
					geocoder.getLatLng(txtCity+" "+txtZip,
					function(p2)
					{
						if( p2 )
						{
							markGeoCode(p2);
							if (txtAddress != "")
							{
								showWarn = true;
								warnMsg = txtCity+" "+txtZip;
							}
							postBack();
						}
						else
						{
							geocoder.getLatLng(txtZip,
							function(p3)
							{
								if( p3 )
								{
									markGeoCode(p3);
									if (txtAddress != "")
									{
										showWarn = true;
										warnMsg = txtZip;
									}
									postBack();
								}
								else
								{
									geocoder.getLatLng("Spokane, WA",
									function(p4)
									{
										if( p4 )
										{
											markGeoCode(p4);
											if (txtAddress != "")
											{	
												showWarn = true;
												warnMsg = "Spokane, WA";
											}
											postBack();
										}
										else
										{
											// we can't do anything at this point
											// (except maybe hardcode a number)
											if (txtAddress != "")
											{
												showWarn = true;
												warnMsg = "No viable addresses found.";
											}
											postBack();
										}
									});
								}
							});
						}
					});
				}
			
		});
}

function postBack()
{
	//if we didn't match the exact address, we need to display the warning before posting back
	if (showWarn)
	{
		var ans = confirm("Exact address was not found.  Continue with map centered at " + warnMsg + " ?");
		if (ans)
		{
			__doPostBack('lnkSearch', '');
		}
		else
		{
			return false;
		}
	}
	else
	{
		__doPostBack('lnkSearch', '');
	}
}

/*
function responseEnd(sender, arguments)
{
	loadData();
}
*/


