/*
 * Frye / Wiles Google Maps Direction Tool
 * 
 * @version 1.0.0a 20080810
 * 
 * @author Andrew Murphy <andrew@fryewiles.com>
 * @copyright 2007 Frye / Wiles
 */



var fwDirections = {
	'id'       : {
		'map'        : 'map',          //id of map element
		'directions' : 'directions'    //id of directions element
	},
	'zoom'   : 14,      //default zoom level
	'locale' : 'en_US', //default locale


	'tool': {
		'map'        : null, //gmap map object
		'directions' : null, //gmap directions object
		'geocoder'   : null  //gmap geocoder object
	},
	'active': false, //is gmap installed and fwDirections initialized


	//initialize
	'init': function(){
		if(typeof GBrowserIsCompatible != 'undefined' && GBrowserIsCompatible()){
			fwDirections.active = true;

			var map = new GMap2(
				$(fwDirections.id.map)
			);
			var dir = new GDirections(
				map, $(fwDirections.id.directions)
			);
			var geocoder = new GClientGeocoder();

			GEvent.addListener(dir, "load", fwDirections.load_directions);
			GEvent.addListener(dir, "error", fwDirections.handle_errors);


			fwDirections.tool.map        = map; 
			fwDirections.tool.directions = dir;
			fwDirections.tool.geocoder   = geocoder;
		}

		delete fwDirections.init; //free memory
	},

	'center_map': function(address){
		if(fwDirections.active){
			fwDirections.tool.geocoder.getLatLng(
				address,
				function(coord){
					fwDirections.tool.map.setCenter(coord, fwDirections.zoom);
				}
			);
		}
	},

	'get_directions': function(fromAddress, toAddress){
		if(fwDirections.active){
			fwDirections.tool.directions.load(
				"from: " + fromAddress +
				" to:  " + toAddress,
	            { "locale": fwDirections.locale }
			);
		}
	},

	'load_directions': function(){},
	'handle_errors': function(){
		var errCode = fwDirections.tool.directions.getStatus().code;
		var msg = 'An error has occurred and we may not process your request. Please try again later';
		if(errCode == G_GEO_UNKNOWN_ADDRESS){
			msg = 'Address not found. This may be because the address is very new, mistyped, or incomplete. Please include your street address, city, and state.';
		}

		alert(msg);
	},
	'reset_directions': function(){
		$(fwDirections.id.directions).innerHTML = '';
	}
};


//Event.observe(window, 'load', fwDirections.init, false);
