<!--

	//Initialize variables
	var geocoder = new GClientGeocoder();
	var points;
	var placeNameDetails,placesLatLng;
	var mapWidget;
	var map;
	var zoomed = false;
	var icon = new Array();
	var markerPointsGlobal;
	var gbIsMarkerDragged = 0;
	var gbAddedTabberFixListener = false;
	
	// a 2d arrays of markers (pins). first index is marker type (home, work..),  
	// second index is list of markers of that kind
	var aaGMarkers = new Array();
	
	// an array of markers in the legend:
	var aLegendMarkers = new Array();
	
	// number of different kinds of markers:
	var iMarkerKindCount = 5;
	var asMarkerLabels = new Array("Home","Work","Shopping","Routine Activities","Entertainment");
	
			

	// called by Onload: inits a google map with zoom controls
	function initMap() {
	    if (GBrowserIsCompatible()) {
	    
	    	map = new GMap2(document.getElementById("map"));      
		    map.setCenter(new GLatLng(38,-104), 1);
		    map.enableDoubleClickZoom();
		 // map.enableScrollWheelZoom(); 
		    map.addControl(new GLargeMapControl());
		    map.addControl(new GScaleControl());
	 			map.addControl(new GOverviewMapControl());
	 			map.setZoom(3);
	 		
	 		
			GEvent.addListener(map,'load', _mapMovedSoMoveLegendCallback);
			GEvent.addListener(map,'move',_mapMovedSoMoveLegendCallback);		 
			GEvent.addListener(map,'zoomend', _mapMovedSoMoveLegendCallback);		
			GEvent.addListener(map,'dblclick', _mapMovedSoMoveLegendCallback);
			
			GMarker.prototype.isDragged = false;
			
			// creates the markers legend, and inits the global marker 2d array,
			// as well as the one time init of the marker icons for all five marker kinds:
			// --vzm
			for(i=1;i<=iMarkerKindCount;i++) {
				// init an array of markers for this marker type:
				aaGMarkers[i] = new Array();
				_initLegendMarker(i);
			}	
	
			/*if (!gbAddedTabberFixListener) {
				alert('installing dom listener.');
				obj = document.getElementById('gmaptab');
				listener = GEvent.addDomListener(obj, 'focus', function(e){ mapResizeCheck(e);});
				gbAddedTabberFixListener = true;
			 }*/
	 	}
	 	
	 	return map;
	}
	
	// looks like this was a way to display current markers, but a recursive grep indicates 
	// this isn't called anywhere. --vzm
	
	function mapResizeCheck(event)
	{
	
		// for map to work in tabber
		map.checkResize(); 
		// alert("did resize check.");
	
	}
	
	/* 
			should rename to "showDriversMarkers(markers)"
	
			this is the entry point.
	*/
 	function showCurrentUserMarkers(markerPoints){		
		
		//GEvent.trigger(map, event);

		// the first time this is called, add a DOM listener to the google map tab that 
		// forces google map to do a resize check. this way, when the user clicks on the google
		// map tab, the map will resize properly.
		

		// these are the driver's markers:
		if (!markerPoints)
			return;
			
		markerPointsGlobal = markerPoints;		
	
		//  get the bounds so we can set the location and zoom level of the map
		var bounds = new GLatLngBounds;
		// for each marker, create it, and expand the bounds to show it:
		 for(i=0; i<markerPointsGlobal.length-1; i++)
		    {	    	
		    	markerKind = markerPointsGlobal[i][0]; // markerkind: home, work
		    	x= markerPointsGlobal[i][1];
		    	y = markerPointsGlobal[i][2];
		    	cord = new GLatLng(x,y);	
		    	bounds.extend(cord); // extend the boundary
			   	_placeNewMarker(markerKind, cord);	
		    }
	  // use the bounds to set map to enclose driver's markers:
		map.setZoom(map.getBoundsZoomLevel(bounds));
		map.setCenter(bounds.getCenter());
//		map.refresh();
	}

	/*
		places a marker at the coordinates given
		
	*/
	function _placeNewMarker(iMarkerKind, cord)
	{	
	
		marker = new GMarker(cord, {icon:icon[iMarkerKind], draggable: true,  title:asMarkerLabels[iMarkerKind-1]});
		
		markerArray = aaGMarkers[iMarkerKind];
		iMarkerIndex = markerArray.length;
		
		infohtml = "A '" + asMarkerLabels[iMarkerKind-1] + "' Marker<br /><br/>";
		infohtml += "<a href = '#map' onclick='javascript:deleteMarker(" + iMarkerKind + "," + iMarkerIndex + ")'>[Remove Marker from Map]</a>";
		marker.bindInfoWindowHtml(infohtml,{maxWidth:200});	
		
		markerArray.push(marker);
		map.addOverlay(marker);
	 
	}

	/* removes a marker from the map and the array */
	function deleteMarker(iMarkerKind, iMarkerIndex) {
		// get the marker:
		markerArray = aaGMarkers[iMarkerKind];
		marker = markerArray[iMarkerIndex];
		
		// remove it from the map:
		map.removeOverlay(marker);
		
		// remove it from our array:
		markerArray.splice(iMarkerIndex,1);
	}

	// called by editCurrentUserMarkers, sets up the listeners for the map, gets the zipcode
	// value, and centers map on that area
	// old comment: "This is the geocode to get the latitude and longitude values" 
	function zoomMapToZip(zipElement) {
	
		//Events for Map	
		// get the lat/long value for the zipcode, setting map center and zoom level: --vzm
		
		address = document.getElementById(zipElement).value;
		geocoder.getLatLng(address,function(point) {
			if (point) {
				map.setCenter(point, 14);		        
			} 
			else {
			//	alert('Zip code ' + address + " not found.");
			}
		});
		
	}
		
	//  callback listener function for move map events. 
	// moves the pins in the "marker legend" back to the upper corner.
	// --vzm
	function _mapMovedSoMoveLegendCallback()
	{		
		// if the legend key array exists:
		// for each of the 5 marker kinds:
		if (isArray(aLegendMarkers)) {
			for(i=1;i<=iMarkerKindCount;i++) {
				if(aLegendMarkers[i]) {
					//  get the last pin that was placed of that type:
					marker = aLegendMarkers[i];
					// if that pin wasn't dragged:
					//if(marker && !marker.isDragged)
					//{	
						// get that pin's coordinates:
						cord = _getLegendMarkerGLatLng(i);
						// and give it new coordinates
						marker.setLatLng(cord);
					//}
				}
			}
		}
	}
	

	/* 
	
		1. Creates a div with label for the kind of marker (home, work, etc) 
		Used in displaying the "Marker Kind" legend in the upper right corner. 
		Clicking on the label also creates a marker of that kind in the center of the map. -vzm
	
		2. creates the marker icon for the given kind
		
		3. Creates a marker of the given "kind" (home, work..) in the marker legend
		(in upper right) --vzm 
	
	*/
	function _initLegendMarker(iMarkerKind)
	{
	    // 1. make the button:
		
		// get the map dimensions:
		mapElement = document.getElementById("map")
		mapTop = 0; //mapElement.offsetTop;
		mapLeft = 0; // mapElement.offsetLeft;
		mapWidth = mapElement.style.width;
		mapWidth = mapWidth.substring(0,mapWidth.length-2);
		
		// offset each button in the legend vertically, depending on its kind (home, work...)
		var top = 8 + (2*(iMarkerKind-1));
		var left = parseInt(mapLeft) + parseInt(mapWidth) - 97;

		// create an HTML div element at the proper location in the map,
		// and set its various params:
		var button = document.createElement("div");
		button.id = iMarkerKind+"Button";
		button.setAttribute("class", "markerButton");
		button.setAttribute("className", "markerButton");		
		button.style.top = top + "px";
		button.style.left = left + "px";
		
		// add the HTML div (marker label "button") to the page:
		mapElement.appendChild(button);
		
		// create an HTML img element for this kind of marker:
		var buttonImage = document.createElement("img");
		buttonImage.src = "/Brand_Driver_Site/markers/"+iMarkerKind+"button.png";
		
		// add a javascript event such that clicking on the html 
		// button creates a marker of that type.
		// (each image is of text spelling out the marker name 
		// in white on red text).
		buttonImage.onclick = function() {		
			point = map.getCenter();
			_placeNewMarker(iMarkerKind, point);
		}
		
		// add the button image as a child to the button div:
		button.appendChild(buttonImage);		
		//document.getElementById(iMarkerKind+"Button").style.visibility = "visible";
	
		// 2. create the marker icon:
		icon[iMarkerKind] = new GIcon();
		icon[iMarkerKind].iconSize = new GSize(38, 24);
		icon[iMarkerKind].iconAnchor = new GPoint(2,18); //set the position for left,top	
		// we should put .pngs in separate directory:
		icon[iMarkerKind].image = '/Brand_Driver_Site/markers/'+iMarkerKind+'.png';
		icon[iMarkerKind].infoWindowAnchor = new GPoint(2,18);

		// 3. create the marker itself:
		cord = _getLegendMarkerGLatLng(iMarkerKind);
	 	marker =  new GMarker(cord, {icon:icon[iMarkerKind], draggable: true, bouncy: false, title:asMarkerLabels[iMarkerKind-1]});
	 	aLegendMarkers[iMarkerKind] = marker;
	 		 	
	 	// when a legend marker is dragged, create a new marker:
	 	 GEvent.addListener(marker,'dragend', function() 
     	 	{	
     	 		// a legend marker has been dragged. move it back to where it was,
     	 		// and create a new marker.
     	 		

     	 		// note, getlatlng calls the google geocoding service, and can fail. use getlocation and handle
     	 		// error instead: --vzm

    	 		cord = this.getLatLng();
  				bGeoCoded = true;
  				
  				/*
				geocoder.getLocations(this, function(response) { 
					if (!response)  { 
						alert("We're sorry, Google's Map is currently unavailable. It's not your fault, nor ours. Please try placing a marker again later.");	
						// pause half a second.
						//alert('got error placing marker: ' + response.Status.code);
						bGeoCoded = false; // pause(500);
					} 
					else {
						switch (response.Status.code) {
							case G_GEO_SUCCESS:
								place = response.Placemark[0];
								cord = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
								bGeoCoded = true;
								break;						
							case G_GEO_SERVER_ERROR:
								alert("We're sorry, Google's Map is currently unavailable. It's not your fault, nor ours. Please try placing a marker again later.");
								bGeoCoded = false;
								break;
							case G_GEO_UNKNOWN_ADDRESS:
								alert("Please zoom into a zipcode before placing a marker.");
								bGeoCoded = false;
								break;
							case G_GEO_UNAVAILABLE_ADDRESS:
								alert('That address cannot be geocoded. Please place your pin someplace else.');
								bGeoCoded = false;
								break;
							case G_GEO_BAD_KEY:
								// this should never happen.
						 	case G_GEO_TOO_MANY_QUERIES:
						 		// handle this, try again after a pause
						 		pause(500);
						 		// try again;
						 		break;
							default:
								// only error left is G_GEO_MISSING_QUERY, shouldn't happen for nonhttp requests.
								alert("We're sorry, an error occurred ("+ response.Status.code + ") while placing the marker, please try again.");
								bGeoCoded = false;
								break;
						} 
					}
				});*/
				
			
				// place the legend marker back where it was:
     	 		legendCord = _getLegendMarkerGLatLng(iMarkerKind);
     	 		this.setLatLng(legendCord);

     	 		if (bGeoCoded) {
					// we can't call _placeNewMarker b/c then we can't access aaGMarkers
					// this is just copy & paste code from that method:
	//     	 		_placeNewMarker(newCord, iMarkerKind); 
					marker = new GMarker(cord, {icon:icon[iMarkerKind], draggable: true,  title:asMarkerLabels[iMarkerKind-1]});
					
					markerArray = aaGMarkers[iMarkerKind];
					iMarkerIndex = markerArray.length;
					
					infohtml = "A '" + asMarkerLabels[iMarkerKind-1] + "' Marker<br /><br/>";
					infohtml += "<a href = '#map' onclick='javascript:deleteMarker(" + iMarkerKind + "," + iMarkerIndex + ")'>[Remove Marker from Map]</a>";
					marker.bindInfoWindowHtml(infohtml,{maxWidth:200});	
					
					markerArray.push(marker);
					map.addOverlay(marker);		
				}
     		});     	

	 	// add the marker to the google map:
     	map.addOverlay(marker);
	}

	

	// gets the lat & long for a marker of the given kind that's located in the legend
	function _getLegendMarkerGLatLng(iMarkerKind)
	{
		gPt = new GPoint(map.getSize().width - 135, 23 + (iMarkerKind-1)*20);
		return map.fromContainerPixelToLatLng(gPt);
	}

	// only helper function for _mapMoveCallback? isn't there a builtin php function for this? --vzm
	function isArray(obj) {
		   if (obj.constructor.toString().indexOf("Array") == -1)
		      return false;
		   else
		      return true;
	}
	
	function showLayer( whichLayer )
	{
	  var elem, vis;
	  if( document.getElementById ) // this is the way the standards work
		elem = document.getElementById( whichLayer );
	  else if( document.all ) // this is the way old msie versions work
		  elem = document.all[whichLayer];
	  else if( document.layers ) // this is the way nn4 works
		elem = document.layers[whichLayer];
	  vis = elem.style;
	  // if the style.display value is blank we try to figure it out here
	 // if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
	//	vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
	  vis.visibility = 'visible'; // (vis.display==''||vis.display=='block')?'none':'block';
	  document.getElementById(1+"Button").style.visibility = "visible";
	  document.getElementById(2+"Button").style.visibility = "visible";
	  document.getElementById(3+"Button").style.visibility = "visible";
	  document.getElementById(4+"Button").style.visibility = "visible";
	  document.getElementById(5+"Button").style.visibility = "visible";
	}
	
	function hideLayer( whichLayer )
	{
	  var elem, vis;
	  if( document.getElementById ) // this is the way the standards work
		elem = document.getElementById( whichLayer );
	  else if( document.all ) // this is the way old msie versions work
		  elem = document.all[whichLayer];
	  else if( document.layers ) // this is the way nn4 works
		elem = document.layers[whichLayer];
	  vis = elem.style;
	  // if the style.display value is blank we try to figure it out here
	 // if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
	//	vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
	  vis.visibility = 'hidden'; // (vis.display==''||vis.display=='block')?'none':'block';
	}

	function pause(milliseconds) {
		var dt = new Date();
		while ((new Date()) - dt <= milliseconds) { /* Do nothing */ }
	}
// -->
