var default_zoom_level = 15;
var map;
var geocoder;
var directionsPanel;
var directions; 
var location_lat;
var location_long;	
var marker;
var point;
var get_directions_form_html = "Get Driving Directions: <br/>\
	<div style=\"float:left; clear:both; padding:3px 0;\"><span style=\"float:left; width:70px;\">Address:</span> <input id=\"directions_addr\" type=\"text\" style=\"width:164px; *width:184px;\" /></div><br/>\
	<div style=\"float:left; clear:both; padding:0 0 8px;\"><span style=\"float:left; width:70px; *padding: 5px 0 0;\">City/State:</span> <input id=\"directions_city\" type=\"text\" size=\"10\"/>\
	Zip: <input id=\"directions_zip\" type=\"text\" size=\"5\"/></div><br/>\
	<div style=\"float:left; clear:both;\"><input type=\"button\" value=\"get directions\" \
	onclick=\"driving_directions();\"/></div>";
var loading_image_html = '<img id=\"ajax_directions_loading\" src=\"/images/ajax-loader.gif\" />';
var directions_prompt = '<br/><br/>Click on the map marker to get driving directions.<br/><br/>';
var click_info_listener;
var mouseover_info_listener;
var marker_manager;
var locale = 'en';

function addAddressToMap(response)
{				
	if (!response || response.Status.code != 200) 
	{
		google_map_url = 'http://maps.google.com/maps?f=q&hl=' + locale + '&geocode=&q=' + end_address;
		status = window.open(google_map_url,'full_map_page');		
  	} 
	else 
	{
		place = response.Placemark[0];
	 	location_lat = place.Point.coordinates[1];
	 	location_long = place.Point.coordinates[0];
	 	
	   	point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
	   	marker = new GMarker(point);
	   	
		map.setCenter(new GLatLng(location_lat,location_long),default_zoom_level);        
		map.setMapType(G_NORMAL_MAP);
		map.addOverlay(marker);
		map.addControl(new GLargeMapControl());
		
		click_info_listener = GEvent.bind(marker, "click", map, function() {				
			map.openInfoWindowHtml(point,get_directions_form_html);
		});								
		
		mouseover_info_listener = GEvent.bind(map, "mouseover", map, function() {				
			map.openInfoWindowHtml(point,get_directions_form_html);
			
			//only show the info window on mouse over once.
			GEvent.removeListener(mouseover_info_listener);
		});				
	}
}

function driving_directions()
{
   	//remove the text and drop in the ajax loading image
   	directionsPanel.innerHTML = loading_image_html;

	start_address = document.getElementById('directions_addr').value + ' '
  						+ document.getElementById('directions_city').value + ' ' 
  						+ document.getElementById('directions_zip').value;
  	end_address = location_lat + "," + location_long;

  	directions_string =  'from: ' + start_address + ' to: '	+ end_address;
	directions.load(directions_string, { "locale": locale});		
			  	
	// if directions do not go well.  open up a new window for the directions.
	GEvent.addListener(directions, "error", function() {
		google_map_url = 'http://maps.google.com/maps?f=q&hl=' + locale + '&geocode=&saddr=' + start_address + '&daddr=' + end_address;
		status = window.open(google_map_url,'full_map_page');
		
		//clear the directions panel to remove the loading image since the ajax directions failed
		directionsPanel.innerHTML = '<br/><br/>Please check the popup window for your directions.';
		
		//check if the popup was blocked.  if it was, change the url.
		if (status == null)
		{
			//document.location = google_map_url;
		}
	});

	//when the directions are successful, hide the original marker 
	//so the direction markers are the only ones displayed.
	GEvent.addListener(directions, "addoverlay", function() {
		map.closeInfoWindow();
		marker.hide();
	});			
	
	
	GEvent.addListener(directions, "load", function() {
		//clear the directions prompt text before any overlay or div content is inserted
		directionsPanel.innerHTML = '';
	});
}
