﻿var map;
var localSearch = new GlocalSearch();

var icon = new GIcon();

var iconVersion = "TS";

if (iconVersion == "NTA") {
    //NTA icon
    icon.image = "images/nta-icon.png";
    icon.shadow = "images/nta-icon-SHADOW.png";
    icon.infoWindowAnchor = new GPoint(15, 18);

    icon.iconSize = new GSize(50, 32);
    icon.shadowSize = new GSize(50, 32);
    icon.iconAnchor = new GPoint(15, 31);
}
else {
    //TS icon
    icon.image = "images/ts-icon.png";
    icon.shadow = "images/ts-icon-shadow.png";
    icon.infoWindowAnchor = new GPoint(11, 11);

    icon.iconSize = new GSize(40, 32);
    icon.shadowSize = new GSize(40, 32);
    icon.iconAnchor = new GPoint(11, 31);
}

var markerY = 1;
var getY;


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_NORMAL_MAP);

        //map.addControl(new GOverviewMapControl());

        map.disableDragging();

        //map.enableInfoWindow()


    }
}

function SetMapToLatLng(lat, lng) {
    map.setCenter(new GLatLng(lat, lng), 15);
}

function townSet(town, country) {
    if (country == "" || country == null)
        country = "UK";

    UsePointFromPostcode(town, country, setCenterToPoint, markerY);
}

function AddToMap(pointAdd, titleAdd, textAdd) {
    markerAdd = CreateMarker(pointAdd, titleAdd, textAdd);
    map.addOverlay(markerAdd);
    var htmlout = ("" + textAdd + "");
    markerAdd.bindInfoWindowHtml(htmlout);
}

function CreateMarker(point1, title1, html1) {

    var opts1 = new Object();
    opts1.clickable = false;
    opts1.icon = icon;
    opts1.title = title1;

    var markera1 = new GMarker(point1, opts1);
    return markera1;
}

function UsePointFromPostcode(postcode, callbackFunction, Y) {

    getY = Y;

    localSearch.setSearchCompleteCallback(null,
		function(Y, usepubname, usepubtown, usepubid) {

		    if (localSearch.results[0]) {
		        var resultLat = localSearch.results[0].lat;
		        var resultLng = localSearch.results[0].lng;
		        var point = new GLatLng(resultLat, resultLng);
		        var goY = getY;
		        callbackFunction(point, goY, true);
		    } else {
		        callbackFunction(null, null, false);
		    }
		});

    localSearch.execute(postcode + ", UK");
}

function LookupPostcode(postcode) {
    if (postcode == "" || postcode == null) {
        alert("Please enter a value");
    }
    else {
        UsePointFromPostcode(postcode, AlertPostcode, 1)
    }
}

function AlertPostcode(point, goY, success) {
    if (success == false) {
        alert('Lookup failed, please be more specific.');
    }
    else {
        GoogleLookupOK(point.lat(), point.lng());
    }
}