// 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 == "www.stinker.com" )
	googleKey = "ABQIAAAAzl69x63PTtG_OQtPt7dBchQ4H5qq_fwvCcoCrfKJsWW6SFJvxBTZ2GKtOOS_pUsYSMchCf5-jh2M_w";
	else if ( domain == "stinker.com" )
	googleKey = "ABQIAAAAzl69x63PTtG_OQtPt7dBchTtYRTzGUWgsKlfRgnxbafMwotSphTa466rc9A62atDUsuqvIKUU8aiwQ";
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();
		geocoder = new GClientGeocoder();

		cicon = new GIcon();
		cicon.image = "images/house_icon.gif";
		cicon.iconSize = new GSize(28, 31);
		cicon.iconAnchor = new GPoint(14, 31);
		cicon.maxHeight = 15;
	}
}

function loadCenterPoint(lat, lng)
{
	latInp = lat;
	lngInp = lng;
	zoomLevel = 11;
	markPoint(new GLatLng(lat,lng));
	hideError();
}

function markPoint(point, lat, lng)
{
	//map.clearOverlays();
	//map.setCenter(point, zoomLevel);
	
	marker = new GMarker(point, {icon: cicon, draggable: false, bouncy: false, clickable: false});
	//map.addOverlay(marker);
	saveMarkedPoint(lat, lng);
}

function saveMarkedPoint(lat, lng)
{
	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()
{
	dragging = true;
	if( marker )
		origPoint = marker.getPoint();
}

function dragIconEnd()
{
	dragging = false;
}

function dragIconEndMap()
{
	var map = document.getElementById("testText");
	map.select();
	dragging = false;
}

function markerDragEnd(v)
{
	saveMarkedPoint();
	map.panTo(marker.getPoint());
}

// marker events
var marker;

function markGeoCode(point, lat, lng)
{
	markPoint(point, lat, lng);
}

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 geoCodeAddressWPostback(gcaddr, gccity, gczip, lnkSearch, lat, lng, acc)
{
	geocoder = new GClientGeocoder();
	var attemptFullAddress = false;
	var txtAddress = document.getElementById(gcaddr).value;
	var txtCity = document.getElementById(gccity).value;
	var txtZip = document.getElementById(gczip).value;
	
	if( txtAddress == "Street name &amp; number" && txtZip == "Zip code" )
	{
		zoomLevel = 7;
	}
	else if( geoCodeFromClick )
	{
		attemptFullAddress = true;
	}
	geoCodeFromClick = false;
	
	if (txtAddress == "Street name & number")
	{
		txtAddress = "";
	}
	if (txtZip == "Zip code")
	{
		txtZip = "";
	}
	if (txtCity == "All Cities")
	{
		txtCity == ""
	}
	var codeStr = "";
	if (txtAddress != "")
	{
		codeStr += txtAddress+", ";
	}
	if (txtCity != "")
	{
		codeStr += txtCity+" ";
	}
	if (txtZip != "")
	{
		codeStr += txtZip;
	}
	geocoder.getLatLng(codeStr, 
		function(p)
		{
			if( p )
			{
				markGeoCode(p, lat, lng);
				document.getElementById(acc).value = "0";
				postBack(lnkSearch);
			}
			else
			{
				
					geocoder.getLatLng(txtCity+" "+txtZip,
					function(p2)
					{
						if( p2 )
						{
							markGeoCode(p2, lat, lng);
							document.getElementById(acc).value = "1";
							if (txtAddress != "")
							{
								showWarn = true;
								warnMsg = txtCity+" "+txtZip;
							}
							postBack(lnkSearch);
						}
						else
						{
							geocoder.getLatLng(txtZip,
							function(p3)
							{
								if( p3 )
								{
									markGeoCode(p3, lat, lng);
									document.getElementById(acc).value = "1";
									if (txtAddress != "")
									{
										showWarn = true;
										warnMsg = txtZip;
									}
									postBack(lnkSearch);
								}
								else
								{
									geocoder.getLatLng("Boise, ID",
									function(p4)
									{
										if( p4 )
										{
											markGeoCode(p4, lat, lng);
											document.getElementById(acc).value = "1";
											if (txtAddress != "")
											{	
												showWarn = true;
												warnMsg = "Boise, ID";
											}
											postBack(lnkSearch);
										}
										else
										{
											// we can't do anything at this point
											// (except maybe hardcode a number)
											document.getElementById(acc).value = "1";
											if (txtAddress != "")
											{
												showWarn = true;
												warnMsg = "No viable addresses found.";
											}
											postBack(lnkSearch);
										}
									});
								}
							});
						}
					});
				}
			
		});
}

function postBack(lnkSearch)
{
	__doPostBack(lnkSearch, 'OnClick');
}


