var map; 
var marker = [];
var bounds;
var markers = [];
var gicons = [];
var count = 0;  

var smallMapControl;
var mapTypeControl;
var directionsControl;
		
function initialize() 
{
	if (GBrowserIsCompatible()) 
	{
	map = new GMap2(document.getElementById("city-map"));
	
	smallMapControl = new GSmallMapControl();
	mapTypeControl = new GMapTypeControl();
	directionsControl = new DirectionsControl();
	
	map.setCenter(new GLatLng(40.2558, -111.6586), 13);
	map.addControl(smallMapControl);
	map.addControl(mapTypeControl);
	if($("#city-map").is(".directions"))map.addControl(directionsControl);
	
	bounds = new GLatLngBounds();
	// Create new geocoding object
	geocoder = new GClientGeocoder();
	
	gicons['school'] = new GIcon(); 
    gicons['school'].image = '/images/school-icon-sm.gif';
	gicons['school'].iconSize = new GSize(22, 27);
    gicons['school'].shadowSize = new GSize(22, 20);
    gicons['school'].iconAnchor = new GPoint(16, 16);
    gicons['school'].infoWindowAnchor = new GPoint(5, 1);
	
	gicons['demographics'] = new GIcon(gicons['school'],'/images/demo-icon-sm.gif');
	
	
	gicons['aquariums'] = new GIcon(gicons['school'],'/images/aquarium-icon-sm.gif');
	gicons['aquariums'].iconSize = new GSize(35,23);
	
	gicons['methlabs'] = new GIcon(gicons['school'],'/images/methlabs-icon-sm.gif');
	gicons['methlabs'].iconSize = new GSize(25,34);
	
	gicons['hospitals'] = new GIcon(gicons['school'],'/images/hospitals-icon-sm.gif');
	gicons['hospitals'].iconSize = new GSize(25,34);
	
	gicons['nursinghomes'] = new GIcon(gicons['school'],'/images/nursinghome-icon-sm.gif');
	gicons['nursinghomes'].iconSize = new GSize(32,27);
	
	gicons['zoo'] = new GIcon(gicons['school'],'/images/zoo-icon-sm.gif');
	gicons['zoo'].iconSize = new GSize(35,23);
	
	gicons['college'] = new GIcon(gicons['school'],'/images/college-icon-sm.gif');
	gicons['college'].iconSize = new GSize(25,32);
	
	gicons['libraries'] = new GIcon(gicons['school'],'/images/libraries-icon-sm.gif');
	gicons['libraries'].iconSize = new GSize(35,35);
	
	gicons['sexoffender'] = new GIcon(gicons['school'], '/images/sexoffender-icon-sm.gif'); 
	gicons['sexoffender'].iconSize = new GSize(25,25);
	
	gicons['airports'] = new GIcon(gicons['school'], '/images/airport-icon-sm.gif'); 
	gicons['airports'].iconSize = new GSize(25,25);

	gicons['crimeinfo'] = new GIcon(gicons['school'], '/images/criminal-icon-sm.gif'); 
	gicons['crimeinfo'].iconSize = new GSize(20,30);
	
	gicons['waterquality'] = new GIcon(gicons['school'], '/images/water-icon-sm.gif'); 
	gicons['waterquality'].iconSize = new GSize(19,26);
	
	gicons['envhazard'] = new GIcon(gicons['school'], '/images/polluters-icon-sm.gif'); 
	gicons['envhazard'].iconSize = new GSize(18,28);
	
	gicons['airquality'] = new GIcon(gicons['school'], '/images/air-icon-sm.gif'); 
	gicons['airquality'].iconSize = new GSize(29,27);
	
	gicons['polluters'] = new GIcon(gicons['school'], '/images/hazards-icon-sm.gif'); 
	gicons['polluters'].iconSize = new GSize(18,28);
	
	gicons['hospitals'] = new GIcon(gicons['school'], '/images/hospital-icon-sm.gif'); 
	
	gicons['police'] = new GIcon(gicons['school'], '/images/police-icon-sm.gif'); 
	gicons['police'].iconSize = new GSize(19,30);
	
	gicons['fire'] = new GIcon(gicons['school'], '/images/fire-icon-sm.gif'); 
	gicons['fire'].iconSize = new GSize(18,28);
	
	gicons['emergancyserv'] = new GIcon(gicons['school'], '/images/utility-icon-sm.gif'); 
	gicons['emergancyserv'].iconSize = new GSize(24,24);

	gicons['cemeteries'] = new GIcon(gicons['school'], '/images/cemeteries-icon-sm.gif'); 
	gicons['cemeteries'].iconSize = new GSize(24,24);

	gicons['fcctowers'] = new GIcon(gicons['school'], '/images/fcc-icon-sm.gif'); 
	gicons['fcctowers'].iconSize = new GSize(24,24);
	
	gicons['house'] = new GIcon(gicons['school'], '/images/home-icon-sm.png'); 
	gicons['house'].iconSize = new GSize(39,38);
	
	addMarkers();
	
	// This code centers and fixes zoom so that all markers are visible
	// bounds is updated when markers are created -- bounds.extend(point)
	var myzoom = map.getBoundsZoomLevel(bounds);
	map.setZoom(myzoom-1);
	
	if(map.getZoom() > 13) {
		map.setZoom(13);
	}
	
	var center = bestCenter(bounds);
	map.setCenter(center);
	map.savePosition();
	}
};
		
   	function addToMap(mymarker, markernum)
   	{
   		if(mymarker.tries != 2) {
       		geocoder.getLocations(mymarker.address, function(response)
       		{
			if (response.Status.code == 200)
			{
				// Retrieve the object
				place = response.Placemark[0];
			
				// Retrieve the latitude and longitude
				point = new GLatLng(place.Point.coordinates[1],
						place.Point.coordinates[0]);
			
				bounds.extend(point);
				var myzoom = map.getBoundsZoomLevel(bounds);
				map.setZoom(myzoom-1);
				var center = bestCenter(bounds);
				map.setCenter(center);
				map.savePosition();
				
				// Add the marker to map
				map.addOverlay(createMarker(point, mymarker));
				markers.push(mymarker);

	        	} else if (response.Status.code == 620) {
	        		//console.log('timeout: '+ mymarker.tries);
	        		if(mymarker.tries > 0) {
		        		mymarker.tries = mymarker.tries + 1;
	        		} else {
	        			mymarker.tries = 1;
	        		}
	        		mymarker.responseCode = response.Status.code;
	        		setTimeout(function(){
	        			//console.log('try again');
	        			addToMap(mymarker, markernum);
	        		},3000);
	        	} else {
	        		//console.log(response);
	        	}
       		});
       	} else {
       		console.log('giving up on '+ mymarker.address);
       	}
   	};
   	
   	//Function used by php geocoder
   	function addToMap2(mymarker, markernum)
   	{
   		point = new GLatLng(mymarker.lat, mymarker.lng);
		bounds.extend(point);
				
		// Add the marker to map
		map.addOverlay(createMarker(point, mymarker));
   	};


	function createMarker(point, mymarker)
	{
		// Create a marker
		var markerg = new GMarker(point, gicons[mymarker.type]);
		mymarker.mark = markerg;
		markers.push(mymarker);
		var text = mymarker.text;
		GEvent.addListener(markerg, "click", function() { 
			markerg.openInfoWindowHtml(mymarker.text);
		}); 
		
		return markerg; 
	};
	
	function toggleTypes(mtype, el) {
		if(jQuery(el)) jQuery(el).toggleClass('icon-off');
		for (var i=0; i < markers.length; i++) {
			if(markers[i].type == mtype) {
				if(markers[i].mark.isHidden()) {
					markers[i].mark.show();
				} else {
					markers[i].mark.hide();
				}
			}
		}
	};
	function bestCenter(bounds) { 
		var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) / 2; 
		var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) / 2; 
		if ( (dist(clat, clng, bounds.getNorthEast().lat(), bounds.getNorthEast().lng())) > (dist(clat, (clng - 180), bounds.getNorthEast().lat(), bounds.getNorthEast().lng())) ) 
			return new GLatLng(clat, (clng - 180)); 
		else 
			return new GLatLng(clat, clng); 
	};
	function dist(lat1, lon1, lat2, lon2) { 
		var rad = 6371; 
		var p1X = lat1 / 180 * Math.PI; 
		var p1Y = lon1 / 180 * Math.PI; 
		var p2X = lat2 / 180 * Math.PI; 
		var p2Y = lon2 / 180 * Math.PI; 
		return Math.acos(Math.sin(p1Y) * Math.sin(p2Y) + Math.cos(p1Y) * Math.cos(p2Y) * Math.cos(p2X-p1X)) * rad; 
	};










	function DirectionsControl() {};

	// To "subclass" the GControl, we set the prototype object to
	// an instance of the GControl object
	DirectionsControl.prototype = new GControl();

	// Creates a one DIV for each of the buttons and places them in a container
	// DIV which is returned as our control element. We add the control to
	// to the map container and return the element for the map class to
	// position properly.
	DirectionsControl.prototype.initialize = function(map) {
	  var container = document.createElement("div");

	  var directionsDiv = document.createElement("div");
	
	  this.setButtonStyle_(directionsDiv);
	  container.appendChild(directionsDiv);
	  directionsDiv.appendChild(document.createTextNode("Driving Directions"));

	  GEvent.addDomListener(directionsDiv, "click", function() {
	    gotoDirections();
	  });


	  map.getContainer().appendChild(container);
	  return container;
	};

	// By default, the control will appear in the top left corner of the
	// map with 7 pixels of padding.
	DirectionsControl.prototype.getDefaultPosition = function() {
	  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(250, 7));
	};

	// Sets the proper CSS for the given button element.
	DirectionsControl.prototype.setButtonStyle_ = function(button) {
	  button.style.textDecoration = "none";
	  button.style.color = "#000000";
	  button.style.backgroundColor = "#FFCC33";
	  button.style.font = "small Arial";
	  button.style.border = "1px solid black";
	  button.style.padding = "1px";
	  button.style.marginBottom = "3px";
	  button.style.textAlign = "center";
	  button.style.width = "6em";
	  button.style.cursor = "pointer";
	  button.style.width = '120px'; 
	};


	
jQuery(function(){
	$('#fill-map-btn').click(function(){ gotoMap(); return false; });
});


var gotoDirections = function()
{
	// Remove controls
	map.removeControl(mapTypeControl);
	map.removeControl(directionsControl);
	
	// Some animation
	$('#city-map').animate({
							left:'280px',
							top:'110px',
							width:'270px'
							});
							
	$('#directions-wrapper').animate({top: '0px'});
	$('#map-wrapper').animate({height: ($('#directions').height()+150)+'px'});
	$('#directions').animate({left: '6px'});
};

var gotoMap = function()
{
	map.addControl(mapTypeControl);
	map.addControl(directionsControl);
	
	$('#city-map').animate({
							left: '0px',
							top: '0px',
							width: '557px'
							});
							
	$('#directions-wrapper').animate({top: '-110px'});
	$('#map-wrapper').animate({height: '387px'});
	$('#directions').animate({left: '-400px'});
	
	return false;
};







// From: http://gomoxiemedia.com/dev/dir_map.html
	var gdir;
    var dirmap;
    function loadDir() {
      if (GBrowserIsCompatible()) { 
      	dirmap = map;
        gdir = new GDirections(dirmap, document.getElementById("directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
		dirmap.addControl( new GSmallMapControl());
      }
    }
    
    function getDir(fromAddress) {
    	loadDir();
    	toAddress = document.dirForm.toaddress.value;
      	gdir.load("from: " + fromAddress + " to: " + toAddress);
      return false;
    }

    function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	    
	   else alert("An unknown error occurred.");
	   
	}

	function onGDirectionsLoad(){ 
		var html = $('#directions').html();
		var timer = null;
		timer = setInterval(function(){
			if($('#directions').html() != html)
			{
				$('#map-wrapper').animate({height: ($('#directions').height()+150)+'px'});
				clearInterval(timer);
				return false;
			}
		}, 1000);
	}



