﻿//<![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 = 4;
var draggable = true;
            
addOnloadEvent(InitMap); // = InitMap;

//
// Load map, wrapped in a function to try and get around IE7 and its bollocks "Operation aborted" issue
//
function InitMap(){
    if (GBrowserIsCompatible()) {


        map = new GMap2(document.getElementById("map"));

        
        if (getPageName() != "your-details.aspx") {
            map.addControl(new GLargeMapControl());
            map.addControl(new GMapTypeControl());
        }
        else
        {
            // only add a small control
            map.addControl(new GSmallMapControl()); ;
        }
 
            // set centre
            var hdLat = 0;
            var hdLng = 0;

            
            if(hdLatID != null)
                hdLat = document.getElementById(hdLatID).value;
            
            if(hdLngID != null)
                hdLng = document.getElementById(hdLngID).value;
            
            if (hdLat ==0){
            
                // set some defaults
                hdLat = 0;
                hdLng = 0;
                defZoom = 2;
            }

            if (getPageName() == "your-location.aspx") {
                defZoom = 7;
            }

                        
		    var center = new GLatLng(hdLat, hdLng);
    		
		    map.setCenter(center, defZoom);
		    
            var baseIcon = new GIcon(G_DEFAULT_ICON);
            baseIcon.iconSize = new GSize(15, 26);
            baseIcon.shadowSize = new GSize(30, 27);
            baseIcon.iconAnchor = new GPoint(9, 27);
            baseIcon.infoWindowAnchor = new GPoint(9, 2);
            
            var burnsIcon = new GIcon(baseIcon);
            burnsIcon.image = "/get-involved/gmap/bigmarker.png";
		    

		    marker = new GMarker(center, { icon: burnsIcon, draggable: draggable })
    		
            GEvent.addListener(marker, "dragstart", function() {
              map.closeInfoWindow();
            });
    		
		    GEvent.addListener(marker, "dragend", function() {
		      document.getElementById(hdLatID).value = marker.getPoint().lat();
		      document.getElementById(hdLngID).value = marker.getPoint().lng();
		    });
    		
		    map.addOverlay(marker);

    }
    else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }
}


// ---------------------------
// function to add multiple window.onload
function addOnloadEvent(fnc){
  if ( typeof window.addEventListener != "undefined" )
    window.addEventListener( "load", fnc, false );
  else if ( typeof window.attachEvent != "undefined" ) {
    window.attachEvent( "onload", fnc );
  }
  else {
    if ( window.onload != null ) {
      var oldOnload = window.onload;
      window.onload = function ( e ) {
        oldOnload( e );
        window[fnc]();
      };
    }
    else
      window.onload = fnc;
  }
}

// ---------------------------
// function to get page name
function getPageName() {
    var sPath = window.location.pathname;
    return sPath.substring(sPath.lastIndexOf('/') + 1);
}

