var map;
var localSearch = new GlocalSearch();
var mapzoom;
var storelat;
var storelong;


function addEvent(oTarget, sType, fpDest) {
  sType = 'on'+sType;
  var oOldEvent = oTarget[sType];
  if (typeof oOldEvent != "function") {
    oTarget[sType] = fpDest;
  } else {
    oTarget[sType] = function(e) {
      oOldEvent(e);
      fpDest(e);
    }
  }
}


function getDirectionsToStore(  )
{
	postcode = document.getElementById('postcodeHolder').value;
	if(postcode)
	{
		var start = document.getElementById('start').value  + '+uk';
		window.open("http://maps.google.co.uk/maps?saddr=" + escape(start) + "&daddr=" + postcode);
	}
}

function getDirectionsFromStore( )
{
	postcode = document.getElementById('postcodeHolder').value;
	if(postcode)
	{
		var end = document.getElementById('end').value + '+uk';
		window.open("http://maps.google.co.uk/maps?daddr=" + escape(end) + "&saddr=" + postcode);
	}
}


function placeMarker()
{
	var Icon = new GIcon();
	Icon.image = "/library/images/stow_map.png";
	Icon.iconSize = new GSize(44, 44);
	
	Icon.iconAnchor = new GPoint(20, 40);
	title = '';
	point = new GLatLng(storelat,storelong);
	var marker = new GMarker(point, {icon: Icon, title: title}); 
	marker.name = title; // add some info that we can access later for linking				 
	marker.id =  '';
	// add marker to the map
	map.addOverlay(marker);
}


function createMap()
{
  	// basic  function just to create the google map
	document.getElementById('mapHolder').style.display = 'inline';
	map = new GMap2(document.getElementById('largeMap'));
  	map.addControl(new GLargeMapControl());
  	map.addControl(new GMapTypeControl());
  	map.addMapType(G_PHYSICAL_MAP);
  	map.addMapType(G_NORMAL_MAP); 

}

function closeWindow()
{
  document.getElementById('mapHolder').style.display = 'none';	
}


function addMapListeners()
{
  	// lets add some listeners for click and zoom events
  	GEvent.addListener(map, "moveend", function() { checkZoom(); });
  	GEvent.addListener(map, "zoom", function() { checkZoom(); });
}

function checkZoom()
{
	placeMarker(); 
}

function showLargeMap( loc, postcode )
{
   document.getElementById('postcodeHolder').value = postcode;
    mapLoad(loc);
}

function mapLoad(city) 
{
  if(!document.getElementById(city + 'Lat')) { city = 'glasgow'; }
  // unless we are setting the zoom to be something else default it to 4 veiw of europe
  if (GBrowserIsCompatible()) 
  {
	  storelat = document.getElementById(city + 'Lat').value;
	  storelong = document.getElementById(city + 'Lng').value;
      createMap();  
 	  map.setCenter(new GLatLng(storelat,storelong), 15, G_NORMAL_MAP);
	  placeMarker()
      //addMapListeners(); 	  
            
  }
}


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

function addUnLoadEvent(func) {
// we need this to avoid memory leaks
var oldonunload = window.onunload;
if (typeof window.onunload != 'function') {
  window.onunload = func;
  } else {
    window.onunload = function() {
        oldonunload();
            func();
              }
              }
}



addLoadEvent(mapLoad);


addUnLoadEvent(GUnload);


