﻿//UK Searches ---------------------------------
var map;
var localSearch = new GlocalSearch();
//UK Searches ---------------------------------

//Ireland Searches ----------
var geocoder = null;
var currentContent = null;
//Ireland Searches ----------

//UK Searches --------------------------------------------------
var icon = new GIcon();
icon.image = "http://www.google.com/mapfiles/marker.png";
icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
icon.iconSize = new GSize(20, 34);
icon.shadowSize = new GSize(37, 34);
icon.iconAnchor = new GPoint(10, 34);

function usePointFromPostcode(postcode, callbackFunction) {

    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);
		        callbackFunction(point);
		    } else {
		        alert("Postcode not found!");
		    }
		});

    localSearch.execute(postcode + ", UK");
}


function placeMarkerAtPoint(point) {
    var marker = new GMarker(point, icon);
    map.addOverlay(marker);
    map.setCenter(point, 17);

}

function setCenterToPoint(point) {
    map.setCenter(point, 17);
}

function showPointLatLng(point) {
    alert("Latitude: " + point.lat() + "\nLongitude: " + point.lng());
}

function mapLoad() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));


        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(54.622978, -2.592773), 5, G_HYBRID_MAP);

        usePointFromPostcode(document.getElementById("postcode").value, placeMarkerAtPoint);

    }
}

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();
        }
    }
}
//UK Searches -----------------------------------------------------------------------------------------

//Ireland Searches ------------------------------------------------------------------------------------
function initialize() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(54.622978, -2.592773), 5, G_HYBRID_MAP);
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        geocoder = new GClientGeocoder();

        // add an event listener
        //GEvent.addListener(map, "click", function(marker, point) {
        //if (marker) {
        //    map.removeOverlay(marker);
        //} else {
        //    map.addOverlay(new GMarker(point));
        //}
        //document.getElementById("message").innerHTML = point.toString();
        // get and append the values inthe text box
        //currentContent = document.mymap.points.value;
        //document.mymap.points.value = currentContent + "new GLatLng" + point.toString() + "," + "\n";
        //});

        showAddress(document.getElementById("postcode").value);
    }
}

function showAddress(address) {
    if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
              if (!point) {
                  alert(address + " not found");
              } else {
                  map.setCenter(point, 17, G_HYBRID_MAP);
                  var marker = new GMarker(point);
                  map.addOverlay(marker);
                  // marker.openInfoWindowHtml(address);
                  marker.openInfoWindowHtml("<font face=Arial color=#f7921c size=4><b>We Are Here ...</font>");
              }
          }
        );
    }
}
//Ireland Searches ---------------------------------------------------------------------------------------------------

//check postcode area for Ireland and load the relevant search Geomap -------------
function CheckPostCode(){
    var postcodecheck = document.getElementById("postcode").value.toString();
    //alert("check = " + postcodecheck.indexOf("Ireland"));  // For Debugging purposes only!!!!
    if (postcodecheck.indexOf("Ireland") >= 0 ){
        initialize();
    }else{
        mapLoad();
    }
}
//check postcode area for Ireland and load the relevant search Geomap -------------

addLoadEvent(CheckPostCode);

//Unload the map for any search ------------------------------------
addUnLoadEvent(GUnload);
//Unload the map for any search ------------------------------------




