// JavaScript Document
//Google maps 
	var directionsample = "Please Enter the address here. e.g Herwarthstrasse 4,12207 Berlin ,Germany";
	var embassyaddress = "Herwarthstrasse 4,12207 Berlin ,Germany";
	var isMapOn=false;
	var map=null;
	var geocoder = null;	
	var gdir;
	// Create a base icon for all of our markers that specifies the
	// shadow, icon dimensions, etc.
	var baseIcon = new GIcon();
	baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	baseIcon.iconSize = new GSize(20, 34);
	baseIcon.shadowSize = new GSize(37, 34);
	baseIcon.iconAnchor = new GPoint(9, 34);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);
	baseIcon.infoShadowAnchor = new GPoint(18, 25);
	
	function startDirectionsTextAreaEdit()
	{
		var dir = document.getElementById("directionsdiv");
		if(dir.value == directionsample)
		{
			dir.value = embassyaddress;
		}
	}
	function handleDirectionBtnClick()
	{		
		if(gdir != null){gdir.clear();}
		var dir = document.getElementById("directionsdiv");
		map = new GMap2(document.getElementById("center_main_map"));
		map.addControl(new GSmallMapControl());
		gdir = new GDirections(map, document.getElementById("directionsdescr"));
  	    GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
		G_END_ICON.image =  "images/small_flag2.png";
		map.addControl(new GMapTypeControl());
		setDirections(dir.value, embassyaddress, "en_US");
	}
	function handleGetDirections()
	{
		initialize();
		var mp = document.getElementById("center_main_map");
		mp.style.height = "400px";
		showDirectionSection();		
	}
	function setDirections(startAddress,endAddress,locale)
	{
		 gdir.load("from: " + startAddress + " to: " + endAddress,
                { "locale": locale });
	}
	function onGDirectionsLoad(){ 
      // Use this function to access information about the latest load()
      // results.

      // e.g.
      // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	 
	}
	function removeMap()
	{
		GUnload();
		document.getElementById("center_main").removeChild(document.getElementById("center_main_map"));
		createDataArea();
		isMapOn=false;
	}
	
	function createDrawArea(id,container)
	{
		createDataArea();
		showMapInstructions();
		var div = document.createElement('div');
		var data = document.getElementById("center_main_data");
		div.setAttribute('id',id);
		document.getElementById(container).insertBefore(div,data);
	}
	function createDataArea()
	{
		var div = document.createElement('div');
		div.setAttribute('id','center_main_data');
		var elem = document.getElementById("center_main");
		elem.removeChild(document.getElementById("center_main_data"));
		elem.appendChild(div);
	}
	function showMapInstructions()
	{
		var div = document.getElementById("center_main_data");		
		div.innerHTML += "<br /><ul><li>Please use the arrow buttons on the map to navigate through it.</li><li>Alternatively, click on the map and move it to navigate around</li><li>Use the +/- key to zoom In/Out respectively</li><li>click on the 'map','satellite' or 'hybrid' button to change the view</li></ul>";		
	}
	function showDirectionSection()
	{
		addDirectionHolder();
		var dir = document.createElement('div');
		dir.setAttribute('style',"margin-left:25px");
		dir.innerHTML = 'Get directions from : <br /><textarea id="directionsdiv" rows="3" cols="50" name="directions" onclick="startDirectionsTextAreaEdit()">'+directionsample+'</textarea><button style="margin-left:5px" onclick="handleDirectionBtnClick()">Directions</button>';
		document.getElementById("center_main_data").appendChild(dir);
	}
	function addDirectionHolder()
	{
		var el = 	document.getElementById("center_main_data");		
		var curr = el.innerHTML;
		el.innerHTML = '<div id="directionsdescr"></div>' + curr;
	}
	function setEmbassyMarkerOptions(latlng)
	{
		var letter = String.fromCharCode("E".charCodeAt(0)+0);
    	var letteredIcon = new GIcon(baseIcon);
	 	letteredIcon.image = "images/small_flag.png";//"http://www.google.com/mapfiles/marker" + letter + ".png";
	 	markerOptions = { icon:letteredIcon };
		var marker = new GMarker(latlng,markerOptions);
		GEvent.addListener(marker, "click", function() {
    	marker.openInfoWindowHtml(getEmbassyMarkerMessage());  });
		map.addOverlay(marker);
		marker.openInfoWindowHtml(getEmbassyMarkerMessage());
	}
	function showLocation(latlng,zm)
	{
    	GEvent.addListener(map, "error", handleErrors);
		map.setCenter(latlng, zm);		
		setEmbassyMarkerOptions(latlng);
	}
	function initialize()
	{
		if(GBrowserIsCompatible())
		{
			createDrawArea("center_main_map","center_main");
			map = new GMap2(document.getElementById("center_main_map"));
			geocoder = new GClientGeocoder();
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
//			map.disableDragging();
			makeGeoCodeRequest(embassyaddress);
			isMapOn = true;
		}
	}
	function makeGeoCodeRequest(address)
	{
		geocoder.getLatLng(address,handleGeoCodeResult);		
	}
	function handleGeoCodeResult(latlong)
	{
		if(!latlong)
		{
			alert("address not found");
		}
		else
		{
			showLocation(latlong,15);				
		}
	}
	function getEmbassyMarkerMessage()
	{
		var styles = "background-color:#EFF8FA;padding-left:5px;padding-top:0px;font-family:  Arial,Helvetica,sans-serif;font-size:11px;color:#3C3728;float:left;";
		var output='<img style="float:left; padding-left:2px;padding-top:2px;" src="images/embassy_building_small.jpg" border="none" /><div style="'+styles+'">Embassy of <br>The Republic of Sierra Leone<br>Herwarthstrasse 4,<br> 12207 Berlin, Germany</div>';
		return output;
	}
	function handleErrors()
	{
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("The specified address cannot be found. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.					getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("Your request could not be processed at this time. Please try again later.\n Error code: " + gdir.getStatus().code);
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("Your request could not be processed at this time. Please try again later.\n Error code: " + gdir.getStatus().code);
   
	   else if (gdir.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: " + gdir.getStatus().code);

	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	    
	   else alert("An unknown error occurred.");
	   
	}