﻿var map;
var point;
var localSearch = new GlocalSearch();
var directionsPanel;
var directions;

var icon = new GIcon();
icon.image = "../images/SL/marker.png";
icon.shadow = "../images/SL/shadow.png";
icon.iconSize = new GSize(70, 52);
icon.shadowSize = new GSize(86, 37);
icon.iconAnchor = new GPoint(64, 52);




function usePointFromPostcode(location, callbackFunction) {    
	localSearch.setSearchCompleteCallback(null, 
		function() {
			
			if (localSearch.results[0])
			{		
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				point = new GLatLng(resultLat,resultLng);
				placeMarkerAtPoint(point);
			}else{
				//alert("Location not found!");
			}
		});			
	localSearch.execute(location);
}

function placeMarkerAtPoint(point)
{
	var marker = new GMarker(point,icon);
	map.addOverlay(marker);
	setCenterToPoint(point)
}

function setCenterToPoint(point)
{
	map.setCenter(point, 14);
}

function showPointLatLng(point)
{
	alert("Latitude: " + point.lat() + "\nLongitude: " + point.lng());
}

function mapLoad() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map_canvas"));
		map.addControl(new GLargeMapControl());
		map.setCenter(new GLatLng(54.622978,-2.592773), 5);
	}
}

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();
	  }
	}
}

function getdirections(addline1, addtown, addpcode) {
    var fromaddress = MM_findObj(addline1).value + ", " + MM_findObj(addtown).value + ", " +MM_findObj(addpcode).value
    //alert(fromaddress);
    //map = new GMap2(document.getElementById("map_canvas"));
    directionsPanel = document.getElementById("PnlDirections");
    //map.setCenter(new GLatLng(49.496675,-102.65625), 3);
    directions = new GDirections(map, directionsPanel)
    directions.load("from: " + fromaddress + " to: " + point)
    GEvent.addListener(directions, "addoverlay", function() {
        MM_findObj('PnlStoreDetails').style.display = 'none';
        MM_findObj('PnlDirCont').style.display = '';
    });
}

function cleardirections(){
    directions.clear();
    MM_findObj('PnlStoreDetails').style.display = '';
    MM_findObj('PnlDirCont').style.display = 'none';
    setCenterToPoint(point);
}

//addLoadEvent(mapLoad);
addUnLoadEvent(GUnload);



