﻿    
var map;
var venues;
var bounds;

function fitMap(map) {

    map.setZoom(map.getBoundsZoomLevel(bounds));
    map.setCenter(bounds.getCenter());
}

    
function load()
{
  if (GBrowserIsCompatible()) 
  {
        bounds = new GLatLngBounds();
  
        var latitude = $get("Latitude").value;
        var longitude = $get("Longitude").value;
        
        map = new GMap2(document.getElementById("map"));    
        map.setCenter(new GLatLng(latitude,longitude), 14);
        
        map.addControl(new GSmallMapControl());
        map.addControl(new GScaleControl());
        map.addControl(new GMapTypeControl());
        
        map.setMapType(G_NORMAL_MAP);
              
       if(venues!=null) {      
           for(var i=0; i<venues.length; i++)
           {
               GetVenueCoordinates(venues[i], AddVenueMarker, i == venues.length-1); 
           }
       }
  }
}

function AddVenueMarker(lat, lon, venue, lastone) {

    var latLng = new GLatLng(lat, lon)
    bounds.extend(latLng);

    if (lastone) {
        //map.setCenter(new GLatLng(lat, lon), 14);
        fitMap(map);
    }
    

    var blueIcon = new GIcon(G_DEFAULT_ICON);
    blueIcon.image = "/Images/Icons/GoogleMap/club.png";
    /*blueIcon.iconSize = new GSize(32, 37);/*
    blueIcon.shadowSize = new GSize(50, 37);
    blueIcon.iconAnchor = new GPoint(18, 18);*/
    
    var markerOptions = { icon:blueIcon };



    var marker = new GMarker(new GLatLng(lat, lon), markerOptions);
  // var marker = new GMarker(new GLatLng(lat,lon), blueIcon);
   map.addOverlay(marker);
   var link = "<h4><a href='" + venue.Url + "'>" + venue.Name + "</a></h4>";
   var img = "<img src='" + venue.ImageUrl + "' alt='venue' width='75px' height='75px' style='float:left; margin-right:5px; margin-bottom:5px;' />";
   var html = "<div style='width:200px;'>" + link + "<div style='text-align:left'>";

   if (venue.Events == null) {
       html = html + img + venue.Summary + "</div></div>";
   }
   else { 
       for (var i = 0; i < venue.Events.length; i++) {
           html = html + "<hr></hr><div><a href='" + venue.Events[i].Url + "'>" + venue.Events[i].Date + " - " + venue.Events[i].Name + "</a></div>";
       }
       html = html + "</div></div>";
   }
   
   GEvent.addListener(marker, "click", function()
   {marker.openInfoWindowHtml(html);});
   
       var tooltip = new Tooltip(marker,venue.Name, 4);
    marker.tooltip = tooltip;
    map.addOverlay(marker);
    map.addOverlay(tooltip);
    GEvent.addListener(marker,'mouseover',function(){
	    this.tooltip.show();
    });
    GEvent.addListener(marker,'mouseout',function(){
	    this.tooltip.hide();
});
}
    

    


function GetVenueCoordinates(venue, callbackFunction, lastone) 
{
    if (venue.Latitude != 0 && venue.Longitude != 0 && venue.Latitude != null && venue.Longitude != null)
  {
      callbackFunction(venue.Latitude, venue.Longitude, venue, lastone);  
  }
  else
  {
  var localSearch = new GlocalSearch();
  localSearch.setSearchCompleteCallback(null,
    function() {

        if (localSearch.results[0]) {
            var resultLat = localSearch.results[0].lat;
            var resultLng = localSearch.results[0].lng;
            if (window.PageMethods) {
                if (PageMethods.CacheCoordinates)
                    PageMethods.CacheCoordinates(venue.Id, resultLat, resultLng);
            }
            callbackFunction(resultLat, resultLng, venue, lastone);
        } else {
            //alert("Postcode not found!");
        }
    });     
  localSearch.execute(venue.Postcode + ", UK");
  }
}


//window.onload=load;

$(document).ready(function() {
    window.onunload=GUnload;
});


