var map;
var dirn;

var directionsPanel;
var directions;

function customPanel(map,mapname,dirn,div) {
 var html = "";

 function waypoint(point, type, address) {
  var target = '"' + mapname+".showMapBlowup(new GLatLng("+point.toUrlValue(6)+"))"  +'"';
  html += '<table border="0" style="margin: 10px 0px;  border-collapse: collapse; color: #757575;">';
  html += '  <tr>';
  html += '    <td valign="middle" style="width: 20px;">';
  html += '      <img src="http://www.google.com/intl/en_ALL/mapfiles/icon-dd-' +type+ '-trans.png" class="png">'
  html += '    </td>';
  html += '    <td valign="top" style="width:100%;"><p style="font-size:110%;"><b>';
  html +=        address;
  html += '    </b>';
  html += '';
  html += '';
 }

 function waypoint2(point, type, address) {
  var target = '"' + mapname+".showMapBlowup(new GLatLng("+point.toUrlValue(6)+"))"  +'"';
  html += '<table style="margin: 10px 0px;  border-collapse: collapse; color: #757575;">';
  html += '  <tr>';
  html += '    <td style="padding: 4px 15px 0px 5px; vertical-align: middle; width: 20px;">';
  html += '      <img src="http://www.google.com/intl/en_ALL/mapfiles/icon-dd-' +type+ '-trans.png" class="png">'
  html += '    </td>';
  html += '    <td style="vertical-align: middle; width: 100%;"><p style="font-size:110%;"><b>';
  html += init_accom;
  html += '    </b></p>';
  html += '';
  html += '';
 }

 function routeDistance(dist) {
  html += '<br />' + dist + '</p></td></tr></table>';
 }      

 function detail(point, num, description, dist) {
  var target = '"' + mapname+".showMapBlowup(new GLatLng("+point.toUrlValue(6)+"))"  +'"';
  html += '<table style="margin: 0px; padding: 0px; border-collapse: collapse;">';
  html += '  <tr>';
  html += '    <td style="border-top: 1px solid #757575; margin: 0px; padding: 0.3em 3px; vertical-align: top; text-align: right;"><p style="font-size:110%;">';
  html += '      <a href="javascript:void(0)"> '+num+'. </a>';
  html += '    </p></td>';
  html += '    <td style="border-top: 1px solid #757575; margin: 0px; padding: 0.3em 3px; vertical-align: top; width: 100%;"><p style="font-size:110%;">';
  html +=        description;
  html += '    </p></td>';
  html += '    <td style="border-top: 1px solid #757575; margin: 0px; padding: 0.3em 3px 0.3em 0.5em; vertical-align: top; text-align: right;"><p style="font-size:110%;">';
  html +=        dist;
  html += '    </p></td>';
  html += '  </tr>';
  html += '</table>';
 }
           
 function copyright(text) {
  html += '<div style="font-size: 110%;">' + text + "</div>";
 }
            
 for (var i=0; i<dirn.getNumRoutes(); i++) {
  if (i==0) {
   var type="play";
  } else {
   var type="pause";
  }
  var route = dirn.getRoute(i);
  var geocode = route.getStartGeocode();
  var point = route.getStep(0).getLatLng();
  waypoint(point, type, geocode.address);
  routeDistance(route.getDistance().html+" (about "+route.getDuration().html+")");

  for (var j=0; j<route.getNumSteps(); j++) {
   var step = route.getStep(j);
   detail(step.getLatLng(), j+1, step.getDescriptionHtml(), step.getDistance().html);
  }
 }
            
 var geocode = route.getEndGeocode();
 var point = route.getEndLatLng();
 html += '<div class="underlineGrey"></div>'
 waypoint2(point, "stop", init_accom);

 copyright(dirn.getCopyrightsHtml());
 div.innerHTML = html;

}

function loadDirections(dst_lat,dst_lng) {
 pcode = document.getElementById("pcode").value;
 var from = 'from '+pcode+', uk to '+dst_lat+','+dst_lng;
 if (pcode != '' && pcode != 'TQ9 7AE') {

  if (GBrowserIsCompatible()) {
   map = new GMap2(document.getElementById("locationMap"));
   map.addControl(new GSmallMapControl());
   map.enableScrollWheelZoom();
   map.enableDoubleClickZoom();
   map.addControl(new GMapTypeControl());
   map.setCenter(new GLatLng(dst_lat,dst_lng), 15);

   dirn = new GDirections(map);

   GEvent.addListener(dirn,"error", function() {
    alert("Directions Failed: "+dirn.getStatus().code);}
   );	

   dirn.load(from, {getSteps:true});
		
   GEvent.addListener(dirn,"load", function() {
    setTimeout('customPanel(map,"map",dirn,document.getElementById("directions"))', 9);
   });

  }
 }
}























function mapDirections() {
 pcode = document.getElementById("mapPostcode").value;
 var from = 'from '+pcode+', uk to 50.336175,-3.684304';

 if (GBrowserIsCompatible()) {
  map = new GMap2(document.getElementById("cMap"));
  map.addControl(new GSmallMapControl());
  map.enableScrollWheelZoom();
  map.enableDoubleClickZoom();
  map.addControl(new GMapTypeControl());
  map.setCenter(new GLatLng(50.336175,-3.684304), 15);

  directionsPanel = document.getElementById("cDirections");
  directions = new GDirections(map, directionsPanel);
  directions.load(from);
//directions.load("500 Memorial Drive, Cambridge, MA to 4 Yawkey Way, Boston, MA 02215 (Fenway Park)");

 }
}