var map;
var localSearch = new GlocalSearch();

/*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);*/


var baseIcon = new GIcon(G_DEFAULT_ICON);
baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
baseIcon.iconSize = new GSize(20, 34);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);

var numPerRow = 20;
var iconIndex = 0;
var iconInfos =  new Array(20);
var iconPoints = new Object();

function usePointFromPostcode(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);
                iconPoints[iconIndex] = point;
                if((iconIndex + 1)==numPerRow){
                    placeOverLays();
                }
                //callbackFunction(point);
                iconIndex++;
                //alert(iconInfos[iconIndex]);
            }else{
                //alert("Postcode not found!");
            }
        });    
        
    localSearch.execute(postcode + ", UK");
}

function placeMarkerAtPoint(point){
    alert(iconIndex);
    // Create a lettered icon for this point using our icon class
    var letter = String.fromCharCode("A".charCodeAt(0) + iconIndex);
    var letteredIcon = new GIcon(baseIcon);
    letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
    
    var marker = new GMarker(point,letteredIcon);
    // Set up our GMarkerOptions object
    map.addOverlay(marker,{ icon:letteredIcon });
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(iconInfos[iconIndex]);
    });
}
//Places all over lays on the map.
function placeOverLays(){
    i=0;
    $.each(iconPoints,function(k,point){
        // Create a lettered icon for this point using our icon class
        var letter = String.fromCharCode("A".charCodeAt(0) + i);
        var letteredIcon = new GIcon(baseIcon);
        letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
        var marker = new GMarker(point,letteredIcon);
        var info = iconInfos[i];
        // Set up our GMarkerOptions object
        map.addOverlay(marker,{ icon:letteredIcon });
        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(info);
        });
        i++;
    });
    setLocation(0);
}
function clearOverLays(){
    $.each(iconPoints,function(index,pointer){
        map.removeOverlay(pointer);
    });
    iconInfos =  new Array(20);
    iconPoints = new Object();
    iconIndex = 0;
}
//Sets the point to one of the overlays
function setLocation(i){
    map.setCenter(iconPoints[i],15);
}
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(54.622978,-2.592773), 5, G_HYBRID_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);
