var localSearch = new GlocalSearch();
var map;
var code;

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) {

	code = postcode;
	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,true);
	map.addOverlay(marker);
	map.setCenter(point, 15);
	getMarkers(point);
}

		function getMarkers(point){
                       
			var imageDir = "../../../images/locator/";
			var Icon = new GIcon();
			Icon.image = imageDir + "pp.gif";
			Icon.iconSize = new GSize(25, 25);
			Icon.shadow = imageDir + "myshadow.png";
			Icon.shadowSize = new GSize(36, 34);
			Icon.iconAnchor = new GPoint(5, 34);
			Icon.infoWindowAnchor = new GPoint(5, 2);
			Icon.transparent = imageDir + "mytran.png";
			Icon.printImage = imageDir + "mymarkerie.gif";
			Icon.mozPrintImage = imageDir + "mymarkerff.gif";
			Icon.printShadow = imageDir + "myshadow.gif";



			var urlstr="read.php?lat=" + point.lat() + "&lng=" + point.lng();
			var request = GXmlHttp.create();
			request.open('GET', urlstr , true);	// request XML from PHP with AJAX call
			request.onreadystatechange = function () {
				if (request.readyState == 4) {
					var xmlDoc = request.responseXML;
					locations = xmlDoc.documentElement.getElementsByTagName("location");
					markers = [];
                                        
					if (locations.length){
						for (var i = 0; i < locations.length; i++) { // cycle thru locations
							markers[i] = new GMarker(new GLatLng(locations[i].getAttribute("lat"),locations[i].getAttribute("lng")), Icon);
							gmarkers[i] = markers[i];
                                                        // Add attributes to the marker so we can poll them later.
							// When clicked, an overlay will have these properties.
							var ServedBy = locations[i].getAttribute("Servedby");
                                                        if(locations[i].getAttribute("Servedby") == null ) { ServedBy = ''; }					
                     
                                                        var name = locations[i].getAttribute("name");
                                                        if(locations[i].getAttribute("name")  == null ) { name = ''; }
                     
                                                        var Address1 = locations[i].getAttribute("Address1");
                                                        if(locations[i].getAttribute("Address1") == null ) { Address1 = ''; }
                     		
                                                        var Address2 = locations[i].getAttribute("Address2");
                                                        if(locations[i].getAttribute("Address2") == null ) { Address2 = ''; }
                     							
                                                        var Address3 = locations[i].getAttribute("Address3");
                                                        if(locations[i].getAttribute("Address3") == null ) { Address3 = ''; }
                     							
                                                        var Address4 = locations[i].getAttribute("Address4");
                                                        if(locations[i].getAttribute("Address4") == null ) { Address4 = ''; }
                     							
                                                        var postcode = locations[i].getAttribute("postcode");
                                                        if(locations[i].getAttribute("postcode") == null ) { postcode = ''; }
                                                        
                                                        var distance = locations[i].getAttribute("distance");
                                                        if(locations[i].getAttribute("distance") == null ) { distance = ''; }
                                                        
                                                        var fullAddress = name;
														if(Address1!='') fullAddress = fullAddress + "<br />\n\r" + Address1;
														if(Address2!='') fullAddress = fullAddress + "<br />\n\r" + Address2;
														if(Address3!='') fullAddress = fullAddress + "<br />\n\r" + Address3;
														if(Address4!='') fullAddress = fullAddress + "<br />\n\r" + Address4;
														if(postcode!='') fullAddress = fullAddress + "<br />\n\r" + postcode;
														
														markers[i].infowindow = "<div id=\"infowindow\" style=\"white-space: nowrap;\">" + fullAddress +"<br />\n\r" + ServedBy +"<br />\n\rdistance: " + distance + "</div>\n";
        // add a line to the side_bar html
        side_bar_html += '<li style="list-style-type:none; font-size:75%; padding:0 0 8px 0; margin:0;"><a class="gmap" href="javascript:myclick(' + i + ')">' + name + ' ' +Address1 + ' (' + distance + ')</a></li>';
        htmls[i] = "<div id=\"infowindow\" style=\"white-space: nowrap;\">" + fullAddress +"<br />\n\r" + ServedBy +"<br />\n\rdistance: " + distance +"</div>\n";
        
                                                        // Useful things to store on a marker (Not needed for this example, could be removed)
							// Tells you what index in the markers[] array an overlay is
							markers[i].markerindex = i;
							// Store the location_id of the location the marker represents.
							// Very useful to know the true id of a marker, you could then make
							// AJAX calls to the database to update the information if you had it's location_id
							markers[i].db_id = locations[i].getAttribute("location_id");
							map.addOverlay(markers[i]);
                                               }
					}
                                        // put the assembled side_bar_html contents into the side_bar div
                                        document.getElementById("side_bar").innerHTML = side_bar_html;
				}
			}
			request.send(null);
		}
		
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(55.941998,-3.174798), 4, G_NORMAL_MAP);
	}
}

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();
		}
	}
}

//addLoadEvent(mapLoad);
addUnLoadEvent(GUnload);
