/**
 * This software is commercial and protected under copyright law
 * This software was written to run on www.brazilbronze.com ONLY
 *
 * @package www.brazilbronze.com
 * @subpackage maps.js
 * @copyright Hamidof.com 2007
 * @version 1.0
 * @author Hamid Alipour <hamid@hamidof.com>
**/

var current_address = null;
var map				  = null;
var geocoder		  = null;
var gdir				  = null;
var map_compatible  = false;		
if( GBrowserIsCompatible() ) {
	map_compatible = true;
}

function initialize_map() {
	if( map_compatible ) {
		map 	  	= new GMap2(get_object('map_canvas'));        
		geocoder = new GClientGeocoder();
		show_address(default_address);
		show('direction-form');
	}
}

function show_address(address) {
	if( geocoder && map_compatible ) {
		current_address = address;		
		geocoder.getLatLng(
		address,
		function( point ) {
			if( !point ) {
				// alert('Sorry!\nMap information is not available for this address.\n' + address);
			} else {
				map.setCenter(point, 13);
				var marker = new GMarker(point);
				map.addOverlay(marker);
				if( window.scroll ) {
					window.scroll(0, 0);
				}
			}
		}
		);
	}
	return false;
}

function get_directions() {
	if( document.direction_form.from_address.value == '' ) {
		alert('Oops! You forgot to enter an address.');
		return false;
	}
	get_object('directions').innerHTML = '';
	gdir = new GDirections(map, get_object('directions'));
	GEvent.addListener(gdir, 'load', onGDirectionsLoad);
	GEvent.addListener(gdir, 'error', handleErrors);
	set_directions(document.direction_form.from_address.value, current_address);
	return false;
}

function set_directions(fromAddress, toAddress) {
	gdir.load("from: " + fromAddress + " to: " + toAddress,
				{ "locale": "en" });
}

function handleErrors(){
	if( gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS )
		alert('Oops! Please double check your address.');
	else if( gdir.getStatus().code == G_GEO_SERVER_ERROR )
		alert('Sorry, please try again.');
	else if( gdir.getStatus().code == G_GEO_MISSING_QUERY )
		alert('Oops! You forgot to enter an address.');
	else 
		alert('Oops! Please double check your address.');
}

function onGDirectionsLoad(){
	setTimeout('eval(\'window.location = "#directions_table"\;\')', 500);
}