// JavaScript Document// 

function loadMap(initZoomLevel) {

  if (GBrowserIsCompatible()) {
  
  	// create map container
	var map = new GMap2($("map_canvas"));
	
  	// format map
  	formatMap(map);
	
	// gets locations from the Db and draws them on the map
	getLocations(map,initZoomLevel);
	
  }
  
}

function formatMap(map) {

	// add map formatting options
	map.enableContinuousZoom();
	map.enableScrollWheelZoom();
	//map.enableGoogleBar();
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	
}

// calls script to get info from db and pass to plotLocations
function getLocations(map, zoomLevel) {
	
	// handle the response
	var hand = function(locations) {
		
		var msgDiv = $("msg");
		
		if (this.req.readyState == 4) {
			
			msgDiv.innerHTML = "";
			msgDiv.className = "";
			
			// add the locations to the map canvas
			plotLocations(map, locations, zoomLevel);
			
		}
		else {
			msgDiv.innerHTML = "Loading...";
			msgDiv.className = "loading";
		}
			
	}
	
	// instantiate AJAX class and request URL
	var myXHR = new Snoopy();
	myXHR.doGet("Scripts/getLocations.asp", hand);
	
}

// shows an address based on input
function showAddress(address) {
	
   	var hand = function(html) {
		
		var msgDiv = $("msg");
		
		if (this.req.readyState == 4) {
			
			msgDiv.innerHTML = "";
			msgDiv.className = "";
			
			var theMap = new GMap2($("map_canvas"));
			formatMap(theMap);
			
			var geocoder = new GClientGeocoder();
			
			geocoder.getLatLng(
			address,
			function(point) {
			  if (!point) {
				alert(address + " not found");
			  } else {
				
				// set the map center to the address requested
				theMap.setCenter(point, 13);
				
				// create a marker on the map and display a window with the address information
				var marker = new GMarker(point);
				theMap.addOverlay(marker);
				marker.openInfoWindowHtml(html);
				
			  }
			  
			}
			
			);
			
		}
		else {
			msgDiv.innerHTML = "Loading...";
			msgDiv.className = "loading";
		}
			
	}
	
	// instantiate AJAX class and request URL
	var myXHR = new Snoopy();
	myXHR.doGet("Scripts/getGeneralLocationInfo.asp?address=" + address, hand);
  
}

function showLocation(locationID, zoomLevel) {
	
	// handle the response
	var hand = function(locations) {
		
		var msgDiv = $("msg");
		
		if (this.req.readyState == 4) {
			
			msgDiv.innerHTML = "";
			msgDiv.className = "";
			
			// get each location object; passed in from getLocations
			var addresses =  new Array;
			addresses = JSON.parse(locations);
		
			// if there are objects to loop through...
			if (addresses.length > 0) {
		
				for (var i=0;i<addresses.length;i++) {
					
					var theMap = new GMap2($("map_canvas"));
					formatMap(theMap);
				
					// declare JSON object and assign it
					var myAddress = addresses[i];
					//document.write(myAddress.locationAddress);
					
					var geocoder = new GClientGeocoder();
					
					geocoder.getLatLng(
						myAddress.locationAddress,
						function(point) {
						  if (!point) {
							alert(myAddress.locationAddress + " not found");
						  }
						  else {
							createInfoWindow(theMap, point, locationID);
						  }
						  
						}
						 
					);
					
					//create the point and send it information to display in the bubble
					createGeoPoint(theMap, 
								   myAddress.locationAddress, 
								   myAddress.locationID,
								   myAddress.locationIcon, 
								   zoomLevel);
					
								
				}
			
			}

		}
		else {
			msgDiv.innerHTML = "Loading...";
			msgDiv.className = "loading";
		}
			
	}
	
	// instantiate AJAX class and request URL
	var myXHR = new Snoopy();
	myXHR.doGet("Scripts/getLocations.asp?id=" + locationID, hand);
	
}

// parse the address list and create the geo points on the map
function plotLocations(theMap, locations, zoomLevel) {

	// get each location object; passed in from getLocations
	var addresses =  new Array;
	addresses = JSON.parse(locations);

	// if there are objects to loop through...
	if (addresses.length > 0) {

		for (var i=0;i<addresses.length;i++) {
		
			// declare JSON object and assign it
			var myAddress = addresses[i];
			
			//create the point and send it information to display in the bubble
			createGeoPoint(theMap, 
						   myAddress.locationAddress, 
						   myAddress.locationID,
						   myAddress.locationIcon,
						   zoomLevel);
						
		}
	
	}

}

function createGeoPoint(theMap, address, locationID, locationIcon, zoomLevel) {
	
	var geocoder = new GClientGeocoder();
			
	geocoder.getLatLng(
		address,
		function(point) {
		  if (!point) {
			alert(address + " not found");
		  }
		  else {
			
			theMap.setCenter(point, zoomLevel);
			
			// get marker with address geocoded and other information passed in
			var marker = createMarker(theMap, point, locationID, locationIcon);
			
			// add the marker to the page
			theMap.addOverlay(marker);	
			
		  }
		  
		}
		 
	);
}


function createMarker(theMap, point, locationID, iconFile) {

	// Create our custom marker icon
	var customIcon = new GIcon();
	customIcon.image = iconFile;
	customIcon.iconAnchor = new GPoint(12, 20);
	customIcon.infoWindowAnchor = new GPoint(5, 1);
	
	// Set up our GMarkerOptions object
	markerOptions = { icon:customIcon };

	// create marker with options
	var marker = new GMarker(point, markerOptions); //
	marker.value = locationID;
	
	// add onclick event for the map markers to display useful information
	GEvent.addListener(marker, "click", function() {
	
		// handle the response
		var hand = function(strHTML) {
			
			if (this.req.readyState == 4) {
				// open the window
				theMap.openInfoWindowHtml(point, strHTML);
			}
			else {
				theMap.openInfoWindowHtml(point, "Loading...");	
			}
				
		}
		
		// instantiate AJAX class and request URL
		var myXHR = new Snoopy();
		myXHR.doGet("Scripts/getLocationInfo.asp?id=" + locationID, hand);
	
	});
	
	return marker;
  
}

function createInfoWindow(theMap, point, locationID) {
	
	// handle the response
	var hand = function(str) {
		
		if (this.req.readyState == 4) {
			// open the window
			theMap.openInfoWindowHtml(point, str);
		}
			
	}
	
	// instantiate AJAX class and request URL
	var myXHR = new Snoopy();
	myXHR.doGet("Scripts/getLocationInfo.asp?id=" + locationID, hand);
	
}

function getDirections() {
	
	// get address values
	var from = $("txtFrom").value;
	var to = $("txtTo").value;
	
	if (from && to) {
		
		var theMap = new GMap2($("map_canvas"));
		formatMap(theMap);
		
		var directionsPanel = $("direction_results_list");
		directionsPanel.innerHTML = "";
		
		// create new directions object
		directions = new GDirections(theMap, directionsPanel);
		
		// try to validate directions
		GEvent.addListener(directions, "load", function() {
		
			var statusCode = directions.getStatus().code;
			handleDirectionsStatus(statusCode, directionsPanel);
			
		});
		
		// load directions
		directions.load(from + " to " + to);
		
		// UI clean up
		directionsPanel.style.display = "block";
		$("cmdClearDirections").style.visibility = "visible";
		
	}
	
}

function clearDirections(clearButton) {

	var theMap = new GMap2($("map_canvas"));
	formatMap(theMap);
	
	var directionsPanel = $("direction_results_list");
	
	// new directions object
	directions = new GDirections(theMap, directionsPanel);
	
	// clear directions
	directions.clear();
	
	// UI clean up
	clearButton.style.visibility = "hidden";
	directionsPanel.innerHTML = "";
	directionsPanel.style.display = "none";
	
	// clear address values
	var txtFrom = $("txtFrom");
	var txtTo = $("txtTo");
	
	txtFrom.value = "";
	txtTo.value = "";
	
	// reset map
	loadMap(13);
	showInterestPoints();
	
}

function handleDirectionsStatus(statusCode, directionsPanel) {

	switch(statusCode)
	{
		
		case G_GEO_SUCCESS:
			//directionsPanel.innerHTML = "Success: " + statusCode;
			break;
		case G_GEO_BAD_REQUEST:
			directionsPanel.innerHTML = "Bad Request: " + statusCode;
			break;
		case G_GEO_SERVER_ERROR:
			directionsPanel.innerHTML = "Server Error: " + statusCode;
			break;
		case G_GEO_MISSING_QUERY:
			directionsPanel.innerHTML = "Invalid Address: " + statusCode;
			break;
		case  G_GEO_MISSING_ADDRESS:
			directionsPanel.innerHTML = "Invalid Address: " + statusCode;
			break;
		case G_GEO_UNKNOWN_ADDRESS:
			directionsPanel.innerHTML = "Invalid Address: " + statusCode;
			break;
		case G_GEO_UNAVAILABLE_ADDRESS:
			directionsPanel.innerHTML = "Invalid Address: " + statusCode;
			break;
		case G_GEO_UNKNOWN_DIRECTIONS:
			directionsPanel.innerHTML = "Invalid Address: " + statusCode;
			break;
		case G_GEO_BAD_KEY:
			directionsPanel.innerHTML = "Bad Key: " + statusCode;
			break;
		case G_GEO_TOO_MANY_QUERIES:
			directionsPanel.innerHTML = "Too Many Queries: " + statusCode;
			break;
		default:
			directionsPanel.innerHTML = "Could not process your request";
			formatMap(theMap);
			break;
	}
	
}
