var directionsPanel;
var directions;
var map, map_dir, geocoder = null;
var initialized = false;
var toaddress; 
var uselatlon = false;


function initialize(title, address, showaddress, latlon) {
	if (arguments.length == 2) { //If there is no alternative address. 
		showaddress = address; 
	}
	if (arguments.length == 4){
		if(showaddress == "")
		{
			showaddress = address;
		}
	}
	
  if (GBrowserIsCompatible()) {
	initialized = true;
	if(latlon != null) {
		toaddress = latlon;
		address = latlon;
		uselatlon = true;
	}
	else{
		toaddress = address; //Set for later use by directions.	
	} 
	var map_canvas = document.getElementById("map_canvas"); //Make the Canvas Visible. 
	map_canvas.style.visibility = "visible";
	map_canvas.style.display = "block";
	var directions_link = document.getElementById("directionslink");
	if(document.getElementById("directionsform").style.display != "block") {
	directions_link.style.visibility = "visible";
	directions_link.style.display = "block";
	}
    map = new GMap2(document.getElementById("map_canvas"));
	geocoder = new GClientGeocoder();
	geocoder.getLatLng(address,  
			function(point) {
        			if (!point) {
          					alert(address + " not found");
        						} 
					else {
          				map.setCenter(point, 13);
						map.addControl(new GLargeMapControl3D());
						
						//Set up a marker and HTML to accompany point. 
						var marker = new GMarker(point);
						var html = "<p><strong>" + title + ", United Producers</strong> <br />" + showaddress  + "</p>";
						//Add an event handler for when the tag is clicked. 
						GEvent.addListener(marker, "click", function() {
					    map.openInfoWindowHtml(point, html);
					  });
						
						//Add Marker
          				map.addOverlay(marker);
          				marker.openInfoWindowHtml(html,{maxWidth: '350'});
							}
      					}
    				);
  				}
  			}


function showDirections (from_form) {
	map_dir = new GMap2(document.getElementById("map_canvas"));
	map_dir.addControl(new GLargeMapControl3D());
	directionsPanel = document.getElementById("directions");
	if(directions != null)
	{
		directions.clear();
	}
  	directions = new GDirections(map_dir, directionsPanel);
	
	//Set Listeners
    GEvent.addListener(directions, "error", handleErrors);

	//Set Directions and Make Directions Area Visible
	function setDirections(fromAddress, toAddress) {
		var directions_area = document.getElementById("directions-holder");
		var textlink = document.getElementById("textlink");
		directions_area.style.visibility = "visible";
		directions_area.style.display = "block";
		textlink.style.visibility = "visible";
		if(uselatlon) {
			directions.load("from: " + fromAddress + " to: United Producers@" + toAddress);
		}
		else {
		directions.load("from: " + fromAddress + " to: " + toAddress);
	}
	}

	//Handle Any Errors and Output
	function handleErrors(){
   if (directions.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + directions.getStatus().code);
   else if (directions.getStatus().code == G_GEO_SERVER_ERROR)
     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + directions.getStatus().code);
   else if (directions.getStatus().code == G_GEO_MISSING_QUERY)
     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + directions.getStatus().code);
   else if (directions.getStatus().code == G_GEO_BAD_KEY)
     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + directions.getStatus().code);
   else if (directions.getStatus().code == G_GEO_BAD_REQUEST)
     alert("A directions request could not be successfully parsed.\n Error code: " + directions.getStatus().code);
   else alert("An unknown error occurred.");
    }

	
	var from = from_form.address.value + " " + from_form.city.value + ", " + from_form.state.value + " " + from_form.zip.value;
    setDirections(from, toaddress);
}

function showForm() {
	var directionsform = document.getElementById("directionsform");
	var directionslink = document.getElementById("directionslink");
	directionsform.style.display = "block";
	directionsform.style.visibility = "visible";
	directionslink.style.display = "none";
}

function toggleDir() {
	var state = document.getElementById("directions-holder").style.display;
	if (state == "block") {
		document.getElementById("directions-holder").style.display = "none";
		document.getElementById("textlink").style.visibility = "hidden";
	}
	else {
		document.getElementById("directions-holder").style.display = "block";
		document.getElementById("textlink").style.visibility = "visible";
	}
}

window.onunload = function() { //Prevents Memory Leaks
	if(initialized) {
		GUnload();
	}
}
