﻿var map;
var localSearch = new GlocalSearch();


// A function to create the marker and set up the event window
function createMarker(point,name,html)
{
    var marker = new GMarker(point);

    // The info window version with the "to here" form open
    to_htmls[i] = '<div style="font-size:12px; font-family: tahoma">' + html + '<br>Directions: <b>To here</b> - <a href="javascript:fromhere(' + i + ')">From here</a>' +
       '<br>Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
       '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
       '<INPUT value="Get Directions" TYPE="SUBMIT">' +
       '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + 
              // "(" + name + ")" + 
       '"/><div>';
    // The info window version with the "to here" form open
    from_htmls[i] = '<div style="font-size:12px; font-family: tahoma">' + html + '<br>Directions: <a href="javascript:tohere(' + i + ')">To here</a> - <b>From here</b>' +
       '<br>End address:<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
       '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
       '<INPUT value="Get Directions" TYPE="SUBMIT">' +
       '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() +
              // "(" + name + ")" + 
       '"/><div>';
       
    // The inactive version of the direction info
    html = '<div style="font-size:12px; font-family: tahoma">' + html + '<br>Directions: <a href="javascript:tohere('+i+')">To here</a> - <a href="javascript:fromhere('+i+')">From here</a><div>';

    //marker.openInfoWindowHtml(html);

    GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml(html);
      //marker.openInfoWindowTabsHtml([new GInfoWindowTab("Address",html)]);
    });
    
    // append map to document
	map.addOverlay(marker);
	marker.openInfoWindowHtml(html);
    
    gmarkers[i] = marker;
    htmls[i] = html;
    i++;
    return marker;
}

// functions that open the directions forms
function tohere(i)
{
    gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}

function fromhere(i) 
{
    gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}

function calcPointFromPostcode(postcode)
{
	localSearch.setSearchCompleteCallback(null, 
	function() 
	{
		if (localSearch.results[0])
		{		
			var resultLat = localSearch.results[0].lat;
			var resultLng = localSearch.results[0].lng;
			var point = new GLatLng(resultLat,resultLng);
			map.setCenter(point, 14);
	        map.setMapType(G_HYBRID_MAP);
            var sAddress = new String(document.getElementById("address").value).HTMLDecode();
            var marker = createMarker(point,"office",sAddress);

        }
		else
		{
			alert("Postcode not found!");
		}
	});	
	
	localSearch.execute(postcode + ", UK");
}

function mapLoad()
{
	if (GBrowserIsCompatible()) 
	{
        var sPostcode = document.getElementById("postcode").value;
	
	    if (sPostcode!="")
	    {
		    map = new GMap2(document.getElementById("map"));
		    map.addControl(new GLargeMapControl());
		    map.addControl(new GMapTypeControl());
            //map.addControl(new GOverviewMapControl(new GSize(100,100)));
            //map.addControl(new GScaleControl());
		    calcPointFromPostcode(sPostcode);
		}
    }
    else 
    {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }
}

function addLoadEvent(func) 
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function') 
  {
    window.onload = func;
  }
  else
  {
    window.onload = function()
    {
      oldonload();
      func();
    }
  }
}

function addUnLoadEvent(func) 
{
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function') 
	{
	  window.onunload = func;
	} 
	else 
	{
	  window.onunload = function() 
	  {
	    oldonunload();
	    func();
	  }
	}
}

addLoadEvent(mapLoad);
addUnLoadEvent(GUnload);

var gmarkers = [];
var htmls = [];
var to_htmls = [];
var from_htmls = [];
var i=0;
