/********************************************************************************************************************************\
 * From: http://webmaster-forums.code-head.com/showthread.php?t=979
\********************************************************************************************************************************/
/* Error messages for possible errors */
var error_address_empty 	= 'Please enter a valid address first.';
var error_invalid_address 	= 'This address is invalid.';
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.';

/* The default address of your store (your main office or store maybe), we will display this address on the map on startup */
/* Edit this */
// var default_address = '1600 Amphitheatre Parkway, Mountain View, CA  94043';

var current_address = null; /* Current address we are displaying, we save it here for directions */
var map				  = null; /* Instance of Google Maps object */
var geocoder		  = null; /* Instance of Google Deocoder object */
var gdir				  = null; /* Instance of Google Directions object */
var map_compatible  = false; /* Whether or not user's browser is compatible to show the map */

/* Check if the browser is compatible */
if( GBrowserIsCompatible() ) {
	map_compatible = true;
}

var element_id = null;

/* Initialize the map this will be called when the document is loaded from: <body onLoad="initialize_map();"> */
function initialize_map(default_address, _element_id) {
	element_id = _element_id;
	if( map_compatible ) {
		$('#google_maps_' + element_id).slideDown();
		map 	  	= new GMap2(document.getElementById('map_canvas_' + element_id));        
		geocoder = new GClientGeocoder();
		show_address(default_address);
	}
}

/* This function will move the map and shows the address passed to it */
function show_address(address) {
	if( map_compatible && geocoder ) {
		/* Save this address in current_address value to use later if user wants directions */
		current_address = address;		
		geocoder.getLatLng(
		address,
		function( point ) {
			if( !point ) {
				alert(error_no_map_info);
			} else {
				map.setCenter(point, 13);
				var marker = new GMarker(point);
				map.addOverlay(marker);
				marker.openInfoWindowHtml(address);
			}
		}
		);
	}
	return false;
}

function show_address_if_exists(address, element) {
	geocoder = new GClientGeocoder();
	if( map_compatible && geocoder ) {
		/* Save this address in current_address value to use later if user wants directions */
		geocoder.getLatLng(
		address,
		function( point ) {
			if( !point ) {
				/* Dont how anything */
			} else {
				$('#' + element).css('height', '250px');
				map = new GMap2(document.getElementById(element));
				map.setCenter(point, 12);
				var marker = new GMarker(point);
				map.addOverlay(marker);
				map.addControl(new GSmallMapControl());
			}
		}
		);
	}
	return false;
}

/* Get the directions */
function get_directions(_element_id) {	
	if( map_compatible ) {
		element_id = _element_id;
		var direction_value = eval('document.direction_form_' + element_id + '.from_address.value');
		if( direction_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_' + element_id).innerHTML = '';
		
		gdir = new GDirections(map, document.getElementById('directions_' + element_id));
		
		/* Setup to event handlers, one: when the directions are loaded, two: if there was any error */
		GEvent.addListener(gdir, 'load',  onGDirectionsLoad);
		GEvent.addListener(gdir, 'error', handleErrors);
		
		/* Show the directions */
		set_directions(direction_value, current_address);			
	}
	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 will handle the errors might happen while retrieving the directions */
function handleErrors(){
	if( gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS )
		alert(error_invalid_address);
	else if( gdir.getStatus().code == G_GEO_SERVER_ERROR )
		alert(error_google_error);
	else if( gdir.getStatus().code == G_GEO_MISSING_QUERY )
		alert(error_address_empty);
	else 
		alert(error_invalid_address);
}

/* This function will be called when the directions are loaded */
function onGDirectionsLoad(){
	/* We will simply scroll down to the directions, but with a little delay so it's loaded */
	setTimeout('eval(\'window.location = "#directions_table_' + element_id +'"\;\')', 500); /* Uncomment this line if you need to */
}