
//var guess = new GIcon(G_DEFAULT_ICON);
//guess.image = "http://www.google.com/mapfiles/circle-green.png";
//http://www.google.com/intl/en_us/mapfiles/circle-green-shadow45.png
//guess.shadow = "http://www.google.com/mapfiles/circle-green-shadow45.png";
//guess.iconSize = new GSize(32,32);
//guess.iconAnchor = new GPoint(12,35);
//guess.infoWindowAnchor = new GPoint(12,5);
//guess.shadowAnchor = new GPoint(12,0);

/*
ppIcon.image = "images/icon_plug_red.png";
ppIcon.iconSize = new GSize(35,35);
ppIcon.shadow = "images/icon_plug_shadow.png";
ppIcon.shadowSize = new GSize(35,35);
ppIcon.shadowAnchor = new GPoint(12,0);
ppIcon.iconAnchor = new GPoint(12,35);
ppIcon.infoWindowAnchor = new GPoint(12,5);
*/
var resultsMap;

function drawCircle(lat,lon,radius) { 
      var cColor = "#FF0000";
      var cWidth = 4;
      var r2d = 180/Math.PI; 
      var Clat = (radius/68.703);   
      var Clon = radius/(69.172 * Math.cos(lat * (Math.PI / 180)))
   //   alert("lat: "+ Clat +" lon: "+ Clon);
	  var Cpoints = []; 
	  for (var i=0; i < 105; i++) { 
		     var theta = Math.PI * (i/50); 
			 Cx = lat + (Clat * Math.cos(theta))* 1.25; 
			 Cy = lon + (Clon * Math.sin(theta))* 1.25; 
			 var P = new GLatLng(Cx,Cy);
			 Cpoints.push(P); 
	  }; 
	  resultsMap.addOverlay(new GPolyline(Cpoints,cColor,cWidth)); 
 } 
	
function setMapType(gmap, type) {
	gmap.setMapType(type);
	if (type == G_NORMAL_MAP)
		mapType = 0;
	if (type == G_HYBRID_MAP)
		mapType = 1;
	if (type == G_SATELLITE_MAP)
		mapType = 2;
	setCookie('mapType',''+ mapType +'');
//	alert(mapType);
}	
		
GMap2.prototype.boxMap = function(center, span) {
	var spec = this.spec;
	var zoom = spec.getLowestZoomLevel(center, span, this.viewSize);
	this.setCenter(new GLatLng(center.x, center.y), zoom);
}

GMap2.prototype.zoomToMarkers = function(minX, maxX, minY, maxY, slopPercentage, heightOffsetPct) {
		var center = new GLatLng((minX + maxX) / 2, (minY + maxY) / 2)
		span = new GSize(Math.abs(maxX - minX), Math.abs(maxY - minY));
		slopWid = 0;
		slopHgt = 0;
		
		slopPercentage = 10;
		
		if (typeof slopPercentage != "undefined")
		{
			slopWid = span.width * slopPercentage / 200;
			slopHgt = span.height * slopPercentage / 200;
			span.width  *= 1 + slopPercentage / 100;
			span.height *= 1 + slopPercentage / 100;
		}
		deltaHgt = 0;
		if (typeof heightOffsetPct != "undefined")
		{
			deltaHgt = span.height * heightOffsetPct / 100;
			center = new GLatLng(center.lat() + deltaHgt, center.lng());
		}
		// needs slop
		var bounds = new GLatLngBounds(new GLatLng(minX-slopHgt, minY-slopWid), new GLatLng(maxX+slopHgt, maxY+slopWid)); // sw, ne
		var zoom = this.getBoundsZoomLevel(bounds, span);
		//var center = bounds.getCenter();
		//alert(minX-slopHgt+","+ minX);
		this.setCenter(center, zoom);
		//alert("TEST");
}
	
function createMarkerWithInfo(point, html, icon) {
  var marker = new GMarker(point, icon);

  // Show this marker's index in the info window when it is clicked
  var html = html;
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(html);
  });

  return marker;
}

function showHideMap(ele) {
	if (ele.innerHTML == 'Hide Map') {
		ele.innerHTML = 'Show Map';
		new Effect.BlindUp(document.getElementById('resultsMap'), {duration: 1.0});
		setCookie('showMap','false');
	}
	else {
		ele.innerHTML = 'Hide Map';
		new Effect.BlindDown(document.getElementById('resultsMap'), {duration: 1.0, afterFinish: function() {centerResultsMap();}});
		//centerResultsMap();
		setCookie('showMap','true');
	}
}

function setCookie(cookieName,cookieValue) {
 var today = new Date();
 var expire = new Date();
 var nDays=90;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
}

function createResultsMap(ele) {
	if (GBrowserIsCompatible()) {
		resultsMap = new GMap2(document.getElementById(ele));
		resultsMap.addControl(new GLargeMapControl());
		resultsMap.enableDoubleClickZoom();
		
		resultsMap.setCenter(new GLatLng(38.754083,-95.273437), 3);
	}
}

function addPointToResultsMap(GUID, lat, lon, html,accuracy) {
	var point = new GLatLng(lat,lon);
		
	if (accuracy > 6)	
		marker = createMarkerWithInfo(point,''+ html +'');
	else
		marker = createMarkerWithInfo(point,''+ html +'');
	if (GUID != 0) 
		_markers[GUID] = marker;
	resultsMap.addOverlay(marker);
}

function createMapThumb(ele, lat, lon)  {
	if (GBrowserIsCompatible()) {
	
		map = new GMap2(document.getElementById(ele));
		//map.addControl(new GOverviewMapControl());
		map.setCenter(new GLatLng(37.4419,-122.1419), 13);
		
		var point = new GLatLng(lat,lon);
		var marker = new GMarker(point);
	
		map.addOverlay(marker);
		map.setCenter(point,9);
	}
}

function createDefaultMap(ele, bGUID) {
	GUID = bGUID;
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById(ele));
		map.setCenter(new GLatLng(38.754083,-95.273437), 2);
		map.enableDoubleClickZoom() ;
		//map.enableContinuousZoom();
	}
}

function createUpdateMap(ele, lat, lon, bGUID)  {
	GUID = bGUID;
	ele = document.getElementById(ele);
	
	while(ele.hasChildNodes())
		ele.removeChild(ele.firstChild);
	
	if (GBrowserIsCompatible()) {
		map = new GMap2(ele);
		map.addControl(new GLargeMapControl());
		map.setCenter(new GLatLng(37.4419,-122.1419), 13);
		map.enableDoubleClickZoom();
		//map.enableContinuousZoom();
		
		var point = new GLatLng(lat, lon);
		var marker = new GMarker(point);
		map.addOverlay(marker);
		map.setCenter(point,13);
		GEvent.addListener(map, 'click', function(overlay, point) {
			map.clearOverlays(); 
			map.addOverlay(new GMarker(point));
			updateLatLon(GUID,point.y,point.x);
			//alert(point.x);
			});
	}
}

function updateLatLon(GUID,lat,lon) {
	http.open('GET', domain +'getLatLon.aspx?action=update&GUID='+ GUID +'&lat='+ lat +'&lon='+ lon, false);
	http.send(null);
	if (http.readyState == 4 && http.status == 200)
	{
		var res = http.responseText;
	}
}

var map;

function createMap(element, lat, lon)  {

	ele = document.getElementById(element);
	
	while(ele.hasChildNodes())
		ele.removeChild(ele.firstChild);
	
	if (GBrowserIsCompatible()) {
		map = new GMap2(ele);
		map.addControl(new GLargeMapControl());
		map.enableDoubleClickZoom();
		//map.enableContinuousZoom();
		
		map.setCenter(new GLatLng(37.4419,-122.1419), 13);
		
		var point = new GLatLng(lat, lon);
		var marker = new GMarker(point);
		map.addOverlay(marker);
		map.setCenter(point,13);
	}
}

function getLatLonMoveMap(ele,GUID,address, zip) {
	http.open('GET', domain +'getLatLon.aspx?address='+ escape(address) +'&zip='+ escape(zip), false);
	http.send(null);
	moveMap(ele,GUID);
}
	
function moveMap(ele,GUID) {
	if (http.readyState == xmlUtil.COMPLETE && http.status == 200)
	{
		var res = http.responseText;
		resultArray = new Array();
		
		if (res.length > 0)
		{
			resultArray = res.split(',');
			if (resultArray.length == 2) {
				createUpdateMap(ele,resultArray[0],resultArray[1],GUID);
				updateLatLon(GUID,resultArray[0],resultArray[1]);
			}
		}
	}
}

function userRedrawMap(prefix,GUID) {
	var address = document.getElementById('address1').value;
	var zip = document.getElementById(prefix +'_zipCode').value;
	getLatLonMoveMap('map',GUID,address,zip);
}
