/* Error messages for possible errors */
	var error_address_empty 	= 'Please enter a valid address first.';
	var error_invalid_address 	= 'This address is invalid. Make sure to enter your street number and city as well.'; 
	var error_google_error 		= 'There was a problem processing your request, please try again.';
	var error_no_map_info		= 'Sorry! Map information is not available for this address.';
	var map_compatible  = false; /* Whether or not user's browser is compatible to show the map */
	var gdir				  	= null; /* Instance of Google Directions object */
	
	/* Check if the browser is compatible */
	if( GBrowserIsCompatible() ) {
		map_compatible = true;
	}
		
	/* 	Initialize the map.
		@param obj	| JSON	Must contain the the following elements. displayId, map, geocoder, address
	*/
	function initialize_map(obj) {
		if( map_compatible ) {
			obj.map 	  	= new GMap2(document.getElementById(obj.displayId));        
			obj.geocoder = new GClientGeocoder();
			show_address(obj);
			
			/* This displays the zoom controls for the map. If you don't want them just delete the line */
			obj.map.addControl(new GSmallMapControl());
			
			/* This displays the map type. If you don't want that feature then just delete this */
			obj.map.addControl(new GMapTypeControl());
			
		}
	}
		
	/*	This function will move the map and shows the address passed to it.
		@param	address	| String
		@param 	obj		| JSON	Must contain the the following elements. displayId, map, geocoder, address, currentAddress
	*/
	function show_address(obj) {
		if( map_compatible && obj.geocoder ) {
			/* Save this address in current_address value to use later if user wants directions */
			obj.currentAddress = obj.address;	
			obj.geocoder.getLatLng(
			obj.address,
			function( point ) {
				if( !point ) {
					alert(error_no_map_info);
				} else {
					obj.map.setCenter(point, 13);
					obj.map.setCenter(point, 13);
					var marker = new GMarker(point);
					obj.map.addOverlay(marker);
					marker.openInfoWindowHtml(obj.markerData); //	The data that appears in the marker bubble.
				}
			}
			);
		}
		return false;
	}
	
	/*	Get the directions 
		@param 	obj		| JSON	Must contain the the following elements. displayId, map, geocoder, address, currentAddress
	*/
	function get_directions(obj) {
		if( map_compatible ) {
			if( document.direction_form.from_address.value == '' ) {
				alert(error_address_empty);
				return false;
			}
			/**
			 * Delete the contents of 'directions' DIV first 
			 * because user might ask for directions more than once.
			**/
			document.getElementById('directions').innerHTML = '';
		
			gdir = new GDirections(obj.map, document.getElementById('directions'));
			
			/* Setup to event handlers, one: when the directions are loaded, two: if there was any error */
			GEvent.addListener(gdir, 'load',  onGDirectionsLoad);
			
			/* Show the directions */
			set_directions(document.direction_form.from_address.value, obj.currentAddress);			
		}
		return false;
	}
		
	/* This will actually set the directions on the map and loads the direction table */
	function set_directions(fromAddress, toAddress) {
    	gdir.load("from: " + fromAddress + " to: " + toAddress,
                	{ "locale": "en" });
    }
	
	/* This function will be called when the directions are loaded */
	function onGDirectionsLoad(){
		/* We will simple scroll down to the directions, but with a little delay so it's loaded */
		setTimeout('eval(\'window.location = "#directions_table"\;\')', 500);
	}

$(function(){
	//	Map Information Objects
	var rockLoc = {"markerData" : "Pie Slingers Rock Spring<br/>56A Fieldstone Village Drive<br/>Rock Spring, GA 30739", "displayId" : "rockLocDiv", "map" : null, "geocoder" : null, "address" : "56 Fieldstone Drive Rock Spring, GA 30739", "currentAddress" : null};
	var signalLoc = {"markerData" : "Pie Slingers Signal<br/>1309 Taft Highway<br/>Signal Mountain, TN 37377", "displayId" : "signalLocDiv", "map" : null, "geocoder" : null, "address" : "1309 Taft Highway Signal Mountain, TN 37377", "currentAddress" : null} 
	initialize_map(rockLoc);
	initialize_map(signalLoc);
	
	$('a.rockDir').livequery("click", function(){
		$.facebox({ajax: '/resources/library/pages/mapRock.html'});
		return false;
	});
	
	$('a.signalDir').livequery("click", function(){
		$.facebox({ajax: '/resources/library/pages/mapSignal.html'});
		return false;
	});
	
	$('#dirRockSubmit').livequery("click", function(){
		get_directions(rockLoc);
		return false;
	});
	
	$('#dirSignalSubmit').livequery("click", function(){
		get_directions(signalLoc);
		return false;
	});
	
});

