﻿var map;
var localSearch = new GlocalSearch();
var iconr = new GIcon();
iconr.image = "http://labs.google.com/ridefinder/images/mm_20_purple.png";
iconr.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
iconr.iconSize = new GSize(12, 20);
iconr.shadowSize = new GSize(22, 20);
iconr.iconAnchor = new GPoint(6, 20);
iconr.infoWindowAnchor = new GPoint(5, 1);
var iconb = new GIcon();
iconb.image = "http://labs.google.com/ridefinder/images/mm_20_green.png";
iconb.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
iconb.iconSize = new GSize(12, 20);
iconb.shadowSize = new GSize(22, 20);
iconb.iconAnchor = new GPoint(6, 20);
iconb.infoWindowAnchor = new GPoint(5, 1);

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("Sorry "+postcode+" Postcode not found.");
			}
		});	
		
	localSearch.execute(postcode + ", UK");
}

function placeMarkerAtPoint(point){
	var marker = new GMarker(point, iconb);
	map.addOverlay(marker);
	map.setCenter(point,10);
}

function mapload(){

    if (GBrowserIsCompatible()) {

	map = new GMap2(document.getElementById("map"));
	map.addControl(new GSmallMapControl());
	map.setCenter(new GLatLng(54.622978,-2.592773), 5);
    
function createMarker(point,name,html) {
        var marker = new GMarker(point,iconr);
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });
        GEvent.addListener(marker,"mouseover", function() {
          marker.openInfoWindowHtml(html);
        });
        return marker;
}

process_it = function(doc) {
        var jsonData = eval('(' + doc + ')');
        for (var i=0; i<jsonData.markers.length; i++) {
          var point = new GLatLng(jsonData.markers[i].lat,jsonData.markers[i].lng);
          var marker = createMarker(point,jsonData.markers[i].label, jsonData.markers[i].html);
          map.addOverlay(marker);
        }
        
}
      GDownloadUrl("branches.json", process_it);

    }

    else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }
}
