var map;
var directions;
var directionsPanel;
var startAddress;
var endAddress;

jQuery(function(){
	
	var locations = new Array (
		"6200 W. Old Shakopee Rd, Bloomington, MN 55438",
		"6851 Flying Cloud Drive, Eden Prairie, MN",
		"5300 Shoreline Drive, Mound, MN"
	)
	
	if (GBrowserIsCompatible()) {     
		map = new GMap2(document.getElementById("map"));
		directionsPanel = document.getElementById("directions");
		directions = new GDirections(map, directionsPanel);
		
		map.setCenter(new GLatLng(44.876049,-93.4552), 9);
		map.setUIToDefault();
		
		createLocationMarkers(locations, map);
	}
	else {
		alert("Your browser does not support Google Maps");
	}
	
	jQuery("#getDirections").submit(function(){
		var selectedLocation = jQuery("#getDirections input:checked").attr("id");
		var userLocation = jQuery("#fromAddress").val();
		
		if (!userLocation) {
			alert("Please fill in the address field.");
			return false;
		}
		
		if (!selectedLocation) {
			alert("Please select a facility location.");
			return false;
		}
		
		if ( selectedLocation == 'bloomington' ) {
			selectedLocation = "44.80828,-93.362879"
		} 
		
		if ( selectedLocation == 'edenPrairie' ) {
			selectedLocation = locations[1];
		} 
		
		if ( selectedLocation == 'mound' ) {
			selectedLocation = locations[2];
		}
		
		showAddress(selectedLocation, userLocation);
		
		return false;
	});
	
	//Print Page
	jQuery(".print").live('click', function(){
		window.print();
		return false;
	});
});

function createLocationMarkers(locations, utilizedMap) {
	for (i=0; i<=locations.length-1; i++) {
		var geocoder = new GClientGeocoder();
		var currentLocation = locations[i];

		geocoder.getLatLng(currentLocation, function(locationLatLng) {
			var marker = new GMarker(locationLatLng);
			utilizedMap.addOverlay(marker);
		});
	}
}

function directionsRequest (selectedLocation, userLocation) {
	if (selectedLocation == '44.80828,-93.362879') {
		endAddress = "<p class='endAddress'><strong>End Address:</strong><br />6200 W. Old Shakopee Rd, Bloomington, MN 55438</p>";
	} else {
		endAddress = "<p class='endAddress'><strong>End Address:</strong><br />" + selectedLocation + "</p>";
	}
	
	directions.load("from: " + userLocation + " to: " + selectedLocation);
	
	jQuery(".print").remove();
	jQuery(".endAddress").remove();
	endAddress += "<a href='#' class='print'>Print this Map</a>";
	jQuery("#getDirections").before(endAddress);
}

function showAddress(selectedLocation, userLocation) {
	startAddress = '<p class="startAddress"><strong>Start Address:</strong><br />' + userLocation + '</p>';
	jQuery(".startAddress").remove();
	jQuery("#directions").before(startAddress);
	
	var geocoder = new GClientGeocoder();
	geocoder.getLatLng(
		userLocation, 
		function(userLocation){
			if(!userLocation) {
				alert("Address not found");
			} else {
				directionsRequest(selectedLocation, userLocation);
			}
		}
	);
}
