﻿function centreMap(postcode, mymap) {
    var localSearch = new GlocalSearch();
    localSearch.setSearchCompleteCallback(null,

	function() {
	    if (localSearch.results[0]) {
	        mymap.setCenter(new GLatLng(localSearch.results[0].lat, localSearch.results[0].lng), 15);
	    }
	});

    localSearch.execute(postcode + ", UK");
}

function usePointFromPostcode(postcode, html, mymap) {
    var localSearch = new GlocalSearch();
    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);
	        mymap.addOverlay(createMarker(point, html));
	    }
	});

    localSearch.execute(postcode + ", UK");
}

function createMarker(point, html) {
    var baseIcon = new GIcon();
    baseIcon.image = "/graphics/map_icon.png";
    baseIcon.iconSize = new GSize(15, 15);
    baseIcon.iconAnchor = new GPoint(4, 20);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);
    var mapIcon = new GIcon(baseIcon);
    // Set up our GMarker object
    var marker = new GMarker(point, mapIcon);
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html);
    });

    return marker;
}

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();
        }
    }
}

//Open a popup window for the editor
function openPopup(url) {
    width = 520;
    height = 550;
    x = parseInt(screen.width / 2.0) - (width / 2.0);
    y = parseInt(screen.height / 2.0) - (height / 2.0);

    var win = window.open(url, "editorPopup", "top=" + y + ",left=" + x + ",scrollbars=no,dialog=yes,minimizable=no,modal=yes,width=" + width + ",height=" + height + ",resizable=no");
}