﻿//<![CDATA[
//
// Added this all as a seperate js file with seperate AJAX calls rather
// than using the ajax.lib.js in the /common folder
// Mainly just to make it a bit more portable

// main marker
var marker;
var map;
var defZoom = 1;

//
// Load map, wrapped in a function to try and get around IE7 and its bollocks "Operation aborted" issue
//
function InitMap(country, type){
    if (GBrowserIsCompatible()) {
    	
            map = new GMap2(document.getElementById("map"));
            map.addControl(new GLargeMapControl());
            map.addControl(new GMapTypeControl());
            map.enableScrollWheelZoom()
                        
            // set centre
            var hdLat = 0;
            var hdLng = 0;
            
                        
            if(country != null)
            {
                var centre = new Request.JSON({ 
                async: false, 
                url : "/common/centre.ashx?country=" + country, 
                onComplete: function(centre) 
                {
                    hdLat = centre.Lat;
                    hdLng = centre.Lng;
                    defZoom = 4;                        
	                                    
                } } ).send();
            }
                        
            if (hdLat ==0){
            
                // set some defaults
                hdLat = 30.29701788337205;
                hdLng = -30.29701788337205;
                defZoom = 1;
            }
            //                                                         \$/
            // lets do some coco magic and get all of the lat n lngs @(-_-)@
            //                                                          ^            
            // Perhaps the next time you do some COCO magic you should 
            // consider the ramifications of more than 10 people signing up.
            
            var center = new GLatLng(hdLat, hdLng);
            map.setCenter(center, defZoom);
            
            // Created a bas icon from which I've created a custom icon that's around 50% smaller.
            var baseIcon = new GIcon(G_DEFAULT_ICON);
            baseIcon.iconSize = new GSize(12, 21);
            baseIcon.shadowSize = new GSize(25, 21);
            baseIcon.iconAnchor = new GPoint(9, 21);
            baseIcon.infoWindowAnchor = new GPoint(9, 2);
            
            var burnsIcon = new GIcon(baseIcon);
            burnsIcon.image = "/get-involved/gmap/littlemarker.png";
            
            //added country, too much data was being loaded
            var strHandler = "/common/locations.ashx?country=" + country + "&type=" + type;
            $("loader").style.display = "block";
            try {

                var myRequest = new Request.JSON({ url: strHandler, onComplete:
                    function(locations) {
                        var loc = null;

                        if (locations != null) {

                            locations.each(function(location) {
                                loc = new GLatLng(location.Lat, location.Lng);

                                var marker = new GMarker(loc, { icon: burnsIcon, draggable: false });

                                GEvent.addListener(marker, "click", function() {

                                    var myHTMLRequest = new Request.JSON({ url: '/common/get-detail.ashx?id=' + location.IDGuid,
                                        onSuccess: function(html) {                                            
                                            marker.openInfoWindow(html.Value);
                                        }
                                    }).get();

                                });

                                map.addOverlay(marker);
                            });
                            
                            
                            
                        } else {

                            alert('There are no map points for your selection, try toggling between Public & Private')

                        }
                        $("loader").style.display = "none";
                    }
                }).send();
  
                    
                    //try catch block
                    } catch(err) {
                       
                       //JSON String is probably too big. 
                       
                    }
 
		    

    }
    else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }
}