﻿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 = "images/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, "mouseover", 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");
}

//Q and A JS functionality
function flip(obj) {
	if((obj.nextSibling.nextSibling.style.display == '') || (obj.nextSibling.nextSibling.style.display == 'none'))
		obj.nextSibling.nextSibling.style.display = 'block';
	else
		obj.nextSibling.nextSibling.style.display = 'none';
}

function apply_QAndA_Drop() {

	var browser=navigator.appName;
	var childPos = 0;
	
	if(document.getElementById("qanda"))
	{
		if(browser.indexOf("Microsoft") == -1)
			childPos = 1;
		
		for(x=0;x<document.getElementById("qanda").childNodes.length;x++) {
		 	try {
		  		if(document.getElementById("qanda").childNodes[x].getAttribute("href").indexOf("#") > -1) {
					document.getElementById("qanda").childNodes[x].onclick = function() {flip(this);return false;};
				}
	  	    }
			catch(err) {}
		}
	}
}