var to = '40.433302, -74.547043';
var gmapsReady = false;

jQuery(document).ready(function(){
	
	map = new GMap2(document.getElementById('map_canvas'), {
		size: new GSize(400, 300)
	});
	
	geocoder = new GClientGeocoder();
	geocoder.getLatLng(to, function(point){
		map.setCenter(point, 13);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.addOverlay(new GMarker(point));
		gmapsReady = true;
	});
	
	jQuery(document).bind('unload', function(){
		GUnload();
	});

       initMap();
       initDirections();

});

function initMap() {	
	jQuery('#go').click(function() {
		var office = jQuery('#offices').val();
		if(office == "South Brunswick") { to = '40.433302, -74.547043'; }
		if(office == "Milltown") { to = '180 Tices Lane East Brunswick, NJ 08816'; }
		map = new GMap2(document.getElementById('map_canvas'), {
			size: new GSize(400, 300)
		});

		geocoder = new GClientGeocoder();
		geocoder.getLatLng(to, function(point){
			map.setCenter(point, 13);
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
			map.addOverlay(new GMarker(point));
			gmapsReady = true;
		});

		jQuery(document).bind('unload', function(){
			GUnload();
		});
	});
	
}

function initDirections(){
	jQuery('#btnGet').click(function(){
		var from = jQuery('#dirStreet').val() + ', ' + jQuery('#dirCity').val() + ', ' + jQuery('#dirState').val() + '.  ' + jQuery('#dirZip').val();
		if (gmapsReady==true){
		        jQuery('#dirLoading').fadeIn('medium', function(){
		            jQuery('#directionsPanel').html('');
		            if (jQuery('#directionsPanel').is(':visible')){
		                jQuery('#directionsPanel').fadeOut('medium');
		            }
		            var directionsPanel = document.getElementById('directionsPanel');
		            var directions = new GDirections(map, directionsPanel);
		            directions.load('from: ' + from + ' to: ' + to, {travelMode:G_TRAVEL_MODE_DRIVING});
		            jQuery('#directionsPanel').fadeIn('medium', function(){
		                jQuery('#dirLoading').fadeOut('medium');
		            });
		        });
		    }
		
	});
	
}

