var map;
var directions;
var directionsPanel;

      function createMarker(point,html) {
        var marker = new GMarker(point);
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });

        // The new marker "mouseover" listener        
        GEvent.addListener(marker,"mouseover", function() {
          marker.openInfoWindowHtml(html);
        });        
        
        return marker;
      }
	function dpbasic()
   {
   directionsPanel = document.getElementById("route");
   directionsPanel.innerHTML ="";
   
   basicdirections = document.createElement("p");
	basicdirections.setAttribute("id","basicdirections");
	basicdirections.appendChild(document.createTextNode("Directions from Kirkwall Harbour"));
	
	directionslist = document.createElement("ol");
	directionslist.setAttribute("id","directionslist");
	basicdirections.appendChild(directionslist);
	
	directions1 = document.createElement("li");
	directions1.appendChild(document.createTextNode("From Kirkwall Harbour keep the sea on your left."));
	directionslist.appendChild(directions1);
	
	directions2 = document.createElement("li");
	directions2.appendChild(document.createTextNode("Across the roundabout onto Cromwell Road."));
	directionslist.appendChild(directions2);
	
	directions3 = document.createElement("li");
	directions3.appendChild(document.createTextNode("Continue on this road as it leads into Carness Road."));
	directionslist.appendChild(directions3);
	
	directions4 = document.createElement("li");
	directions4.appendChild(document.createTextNode("Follow the coastal road take the left at the junction and continue with the sea on your left."));
	directionslist.appendChild(directions4);	
	
	directions5 = document.createElement("li");
	directions5.appendChild(document.createTextNode("Avalon House is the 4th turning on your right and the second house up to the right."));
	directionslist.appendChild(directions5);	
	
	return basicdirections;
   }   
      
      
      
function initialize() {

	directionsPanel = document.getElementById("route");
    directionsPanel.appendChild(dpbasic());    
    
    
    if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(58.994604,-2.956696), 13);
	//map.addControl(new GSmallMapControl());
// Get the default GMapUIOptions.
  var uiOptions = map.getDefaultUI();
  
  // Disable scroll wheel zoom.
  uiOptions.controls.maptypecontrol  = false;
  uiOptions.maptypes.satellite  = false;
  uiOptions.maptypes.hybrid  = false;
  uiOptions.maptypes.physical  = false;
  uiOptions.controls.menumaptypecontrol = false;

  // Now set the map's UI with the tweaked options.
  map.setUI(uiOptions);


	//var point = new GLatLng(58.998494,-2.943478);
	//var marker = createMarker(point,'Avalon House Second house on the right.');
	//map.addOverlay(marker);

		
    directionsPanel = document.getElementById("route");
    directionsPanel.appendChild(dpbasic());
	//directionsPanel.innerHTML="<p>From Kirkwall Harbour keep the sea on your left into Cromwell Road leading to Carness Road continue along the coast road until you arrive at a junction and continue with the sea on your left and we are the 4th turning on your right and Avalon House is the second house up on the right.</p>";
    directions = new GDirections(map, directionsPanel);    
	}
else{alert("Sorry, the Google Maps API is not compatible with this browser");}
}





function getdirections() {
	var arrivalpointselect=document.getElementById("selectstart")
	var directionsPanel = document.getElementById("route");
	var arrivalpoint
	for (var i=0; i<arrivalpointselect.options.length; i++){
		if (arrivalpointselect.options[i].selected==true){
			arrivalpoint = arrivalpointselect.options[i].value;
			break
		}
	}
	if(arrivalpoint!="noselection")
	{

	var stringsfordirections = getdirectionsstring(arrivalpoint);
	var directionsstring = stringsfordirections.directionsstring;
	var printstring = stringsfordirections.printstring;
	
	//var printbutton = document.getElementById("printbutton");
	//printbutton.setAttribute("type","button");
	//printbutton.onclick = "openprintpage(\""+printstring+"\")";
	if (document.getElementById("printlink"))
	{
	printlink = document.getElementById("printlink");
	printlink.setAttribute("href",printstring);
	}
	else{
	printlink = document.createElement("a");
	printlink.setAttribute("id","printlink");
	printlink.setAttribute("target","_blank");
	printlink.setAttribute("href",printstring);
	printlink.appendChild(document.createTextNode("Print Directions"));
	document.getElementById("arrivalpoint").appendChild(printlink);
	}
	directionsPanel.innerHTML ="";	
	directions.load(directionsstring);
	}
	else
	{
	//directionsPanel.innerHTML="<p>From Kirkwall Harbour keep the sea on your left into Cromwell Road leading to Carness Road continue along the coast road until you arrive at a junction and continue with the sea on your left and we are the 4th turning on your right and Avalon House is the second house up on the right.</p>"
initialize();
	}
		
	

}	

function getdirectionsstring(startpoint) {
	switch(startpoint)
	{
	case "Kirkwall" :
	var directionsstring = "from: Northlink Ferry Terminal, Kirkwall @58.999909,-2.974119  to: Avalon House @58.998494,-2.943478";
	var printstring = "http://maps.google.com/maps?f=d&saddr=Northlink Ferry Terminal, Kirkwall @58.999909,-2.974119&daddr=Avalon House @58.998494,-2.943478&hl=en&pw=2";
	return {directionsstring : directionsstring, printstring : printstring};	
	break;
	case "Pentland":
	var directionsstring = "from: Pentland Ferries, St Margaret\'s Hope@58.832231,-2.961738  to: Avalon House @58.998494,-2.943478";
	var printstring = "http://maps.google.com/maps?f=d&saddr=Pentland Ferries, St Margaret\'s Hope@58.832231,-2.961738&daddr=Avalon House @58.998494,-2.943478&hl=en&pw=2";
	return {directionsstring : directionsstring, printstring : printstring};					
	break;
	case "Airport":
	var directionsstring = "from: Kirkwall Airport@58.952952,-2.901442 to: Avalon House @58.998494,-2.943478";
	var printstring = "http://maps.google.com/maps?f=d&saddr=Kirkwall Airport@58.952952,-2.901442&daddr=Avalon House @58.998494,-2.943478&hl=en&pw=2";
	return {directionsstring : directionsstring, printstring : printstring};	
	break;
	case "Stromness":
	var directionsstring = "from: Northlink Ferry Terminal, Stromness @58.964650,-3.295330 to: Avalon House @58.998494,-2.943478";
	var printstring = "http://maps.google.com/maps?f=d&saddr=Northlink Ferry Terminal, Stromness @58.964650,-3.295330&daddr=Avalon House @58.998494,-2.943478&hl=en&pw=2";
	return {directionsstring : directionsstring, printstring : printstring};						
	break;
	}

}