var map;  
var oldOverlay;
var markerColors = new Array("5d5dff","cca166","62d962","acddf1","ee53ee","ff4050","f2f261");
var markerCount = 0;

	function trimString (str) {
	
	  str = this != window? this : str;
	  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
	  
	}


  		function addMarkers(mapArray)
		{
			
			
			for (i = 0; i<mapArray.length-1; i++)
			{
				
				//alert(mapArray[i][0] + ',' + mapArray[i][1]+ ',' +mapArray[i][2]+ ',' +mapArray[i][3]);
				//var point = new GPoint(mapArray[i][3],mapArray[i][2]);
				
				//thisText = "<table width='40'><tr><td nowrap colspan=2><br /><b>" + mapArray[i][1] + "</b><br /><a href=\"javascript:locationDetails('" + mapArray[i][0] + "')\">more information</a></td></tr>";
				
				thisText = "<table width='40'><tr><td nowrap colspan=2><br /><b>" + mapArray[i][1] + "</b><br /><a href=\"javascript:openwindow('location_details.asp?id=" + mapArray[i][0] + "','550','450','location=no,status=no,toolbar=no,top=10,left=10,scrollbars=yes')\">more information</a></td></tr>";
				
				thisLink = "<tr><td width=100% nowrap colspan=2><br /><a href=\"javascript: zoomIn(" + mapArray[i][2] + "," + mapArray[i][3] + ",0);\" onClick=\"zoomIn(" + mapArray[i][2] + "," + mapArray[i][3] + ",0);\">zoom in</a> | <a href=\"javascript: zoomOut(" + mapArray[i][2] + "," + mapArray[i][3] + ",6);\" onClick=\"zoomOut(" + mapArray[i][2] + "," + mapArray[i][3] + ",6);\">zoom out</a></td></tr></table>";
				
				thisText = thisText + thisLink;
				
				//eval("my_marker_"+trimString(mapArray[i][3])+" = marker_"+mapArray[i][3]+".createMarker("+mapArray[i][1]+","+mapArray[i][2]+",thisText,"+mapArray[i][0]+")");
				var point = eval("new GPoint(" + mapArray[i][2] + "," + mapArray[i][3] + ")");
				//var marker = new GMarker(point);
				var marker = createMarker(point, thisText,true)
			    map.addOverlay(marker);

				
			}

			
			
			//eval("marker_" + theName +".toggleMarkersVisible()");
			
		

			
		}
		

		function clickChangeMap(id,user)
		{
			
			
			/*mapType = map.getCurrentMapType();
			
			for (var i in mapType)
                    alert( "maptype." + i + " = " + mapType[i] );
			*/
			
			if (id) {
				theName = id;
				
			}else if (user){
				
				theName = 'user';
				
			}
			
			if (markerCount > markerColors.length-1) markerCount = 0 ;
			
			//alert(id);
			
			if (eval("typeof switch_"+ theName + " == 'undefined'"))
			{
				eval("switch_"+theName+" = 1");
				
			}else{

				if (eval("switch_"+theName + " == 1"))
				{
					
					eval("switch_"+theName+" = 0");
				}else{
					
					
					eval("switch_"+theName+" = 1");
				}
			}
			
			if (eval("switch_"+theName + " == 1"))
			{
				eval("icon_" + theName + " = new GIcon(createIcon('" + markerColors[markerCount] + "'))");
				eval("marker_" + theName+" = new MarkerGroup(map, icon_" + theName + ")");
				
				if (id) {
					thisurl = 'scripts/include.main.asp?action=getLocations&tag=' + id;
				}else if (user){
					thisurl = 'scripts/include.main.asp?action=myLocations&id='+user;
				}
					
				makeRequest(thisurl,'GET','','','addToMap')
			
				if (id)
				{		
					var myStyle= document.getElementById("span_"+theName);
					//getElementById(div)
					myStyle.style.fontSize = '120%';
					myStyle.style.color = '#' + markerColors[markerCount];
					markerCount += 1; 
				}
				if (typeof globalOverlay != 'undefined')
				{
					//map.removeOverlay(globalOverlay);
				}
			}else{
				
				if (id)
				{
					var myStyle= document.getElementById("span_"+theName);
					//getElementById(div)
					myStyle.style.fontSize = '100%';
					myStyle.style.color = 'black';
				}
				
				thisurl = 'scripts/include.main.asp?action=removeFromSession&tag=' + theName;
				makeRequest(thisurl,'GET')
				eval("marker_" + theName +".toggleMarkersVisible()");
			}
			

		}
		
		//function changeMap(value)
		//change what is viewed on the map
		function changeMap(val)
		{
			
			//alert(val.name + " = " + val.value + "and is checked is " + val.checked);
			theName = val.name;
			
			
			if (val.checked) 
			{			
				
				 
				eval("icon_" + val.name + " = new GIcon(createIcon('red'))");
				eval("marker_" + val.name+" = new MarkerGroup(map, icon_" + val.name + ")");
				thisurl = 'scripts/include.main.asp?action=getLocations&tag=' + val.name;
				makeRequest(thisurl,'GET','','','addToMap')
				

				if (typeof globalOverlay != 'undefined')
				{
					map.removeOverlay(globalOverlay);
				}
				
				
			}else{
				//alert("marker_" + val.name+".toggleMarkersVisible()");
				thisurl = 'scripts/include.main.asp?action=removeFromSession&tag=' + val.name;
				//alert(thisurl);
				makeRequest(thisurl,'GET')
				eval("marker_" + val.name+".toggleMarkersVisible()");
				
			}
			
			//loadMapPage(thisurl);
			//createMap(mapArray);
			
		}
		
		function draw_border(districtMap)
		{
		
			
			map.addOverlay(new GPolyline(districtMap));
			//alert(districtMap.length);
		
		
		}
		
		
		
		function jumpto(location)
		{
			
			coords = location.split(",");
			//thisText = "<table width='40'><tr><td nowrap colspan=2><br /><a href=\"javascript:locationDetails('" + coords[2] + "')\">" + coords[3] + "</a></td></tr>";
			thisText = "<table width='40'><tr><td nowrap colspan=2><br /><b>" + coords[3] + "</b><br /><a href=\"javascript:openwindow('location_details.asp?id=" + coords[2] + "','550','450','location=no,status=no,toolbar=no,top=10,left=10,scrollbars=yes')\">more information</a></td></tr>";
			
			
			thisLink = "<tr><td width=100% nowrap colspan=2><br /><a href=\"javascript: zoomIn(" + coords[0] + "," + coords[1] + ",0);\" onClick=\"zoomIn(" + coords[0] + "," + coords[1] + ",0);\">zoom in</a> | <a href=\"javascript: zoomOut(" + coords[0] + "," + coords[1] + ",6);\" onClick=\"zoomOut(" + coords[0] + "," + coords[1] + ",6);\">zoom out</a></td></tr></table>";
			thisText = thisText + thisLink;
			map.centerAndZoom(new GPoint(coords[0],coords[1]), 4);	
			var html = "<b>" + thisText + "</b>";
			map.openInfoWindowHtml(map.getCenterLatLng(),html);
			
		}


		function createMap(mapArray)
		{
					
			if (GBrowserIsCompatible()) {
				
				
				map = new GMap(document.getElementById("map"));
				map.addControl(new GLargeMapControl());
				map.addControl(new GMapTypeControl());
					
					
				GEvent.addListener(map, "moveend", function() {
				  var center = map.getCenterLatLng();
				  var latLngStr = '(' + center.y + ', ' + center.x + ')';
				  //alert(latLngStr);
				  var bounds = map.getBoundsLatLng();
					//alert("x = " + bounds.maxX + " to " + bounds.minX);
					//alert("y = " + bounds.maxY + " to " + bounds.minY);
					var width = bounds.maxX - bounds.minX;
					var height = bounds.maxY - bounds.minY;
				  
				});
				
				
					
			map.centerAndZoom(new GPoint(-81.376762,28.726119), 7);
			
			}	
			
		}


		// Create our "tiny" marker icon
		function createIcon(color)
		{
			
			// Create a base icon for all of our markers that specifies the
			var icon = new GIcon();
			
			switch (color)
			{
				
				
				case 'red':
					//icon.image = "images/mm_20_red.png";
					//icon.image = "images/marker.png";
			
					
					break;
					
				case 'blue':
					//icon.image = "images/mm_20_blue.png";
					break;
			}
		
			//icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
			//icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
			icon.image = "images/marker.png";
			
			icon.iconSize = new GSize(26, 37);
			//icon.shadowSize = new GSize(22, 20);
			icon.iconAnchor = new GPoint(6, 20);
			icon.infoWindowAnchor = new GPoint(5, 1);
			return icon;
		}

		
		
		
		function createMarker(point, text) {
				var marker = new GMarker(point,createIcon('red'));
				
				// Show this marker's index in the info window when it is clicked
				var html = "<b>" + text + "</b>";
				GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(html);
				});
				
				return marker;
				
				}
				
				


		function openApp()
		{

			//alert('here we go');
			createMap('');
			makeRequest('scripts/include.main.asp?action=display_markers','GET','content','','addToMap'); 
		}


        function toggleMarker(id)
        {

            //alert("googleMap.removeOverlay('this_marker_"+id+"')");
           // googleMap.removeOverlay("this_marker_"+id);
          //eval("marker_" + trimString(thisTag) +".toggleMarkersVisible()")
          


        }


		function zoomIn(lng,lat,level) {
			map.centerAndZoom(new GPoint(lng,lat), level);
		}
		
		function zoomOut(lng,lat,level) {
			map.centerAndZoom(new GPoint(lng,lat), level);
		}		
		
