/*----------------------------------
-- Google Maps Code for 
-- Stockton Heath Festival
-- Author: Ben Shearer
-- Amaze Plc
----------------------------------*/



$(document).ready(function(){
	init();
	///When the document is ready trigger the method to initialise google maps
	initialiseMaps();
	//Add the overlays to the map
	

	//initialise the event handlers for the menu click
	initialiseMenuEventHandlers();
});

//Google Maps Scripts

//Global Variables

//Below is the map object - we don't have Google maps script loaded at this time 
//so we'll just set it to an instance of an arbitrary object
var map = new Object();
var mapType;

//Places : A 2 dimensional array containingg the coordinates  
//of the pins and the id of the relevant div tag that contains the information
var places = [['GrappHeys', 53.363881,-2.561684],
			 ['AppletonHall', 53.352535,-2.57119],
			 ['Library', 53.37168,-2.576686],
			 ['ServicemansClub', 53.375841,-2.541538],
			 ['AlexandraPark', 53.373,-2.576],
			 ['ScoutHall', 53.372,-2.575],
             ['MillCommon', 53.372,-2.574],
			 ['Teepee', 53.372,-2.574],
             ['StThomasChurch', 53.373,-2.583],
             ['MethodistHall', 53.369,-2.583],
             ['ThornMarine', 53.367, -2.580], 
             ['RedLaneAllotments', 	53.365,-2.587],
             ['Studio', 53.377,-2.585],
			 ['TheCape', 53.371,-2.580],
             ['AckersPit', 53.372,-2.569],
             ['GrappenhallChurch', 53.377764,-2.554429],
             ['BellHouse', 53.37204,-2.53859]];



function initialiseMaps()
{
    if (GBrowserIsCompatible()) {
        //Set map object to be a google map (GMap2) and tell it where the map is meant to go (map_canvas div)
        map = new GMap2(document.getElementById("map_area"));
        
        //Coordinates of the center of the map you wish to display
        var initialCenter = new GLatLng(53.370284,-2.568773)
        
        // Set the zoom level
        var zoomLevel = 14;
        
        //Set the center point of  the map and it's zoom level
        map.setCenter(initialCenter, zoomLevel);
        
        //set the controls available (zoom, terrain type...) to the default
        map.setUIToDefault();
        
        //Get available map types and set the map type to the first map type - standard map.
        mapType = map.getMapTypes();
        map.setMapType(mapType[0]);

        initialiseOverlays(null);
        
        
    }
}

//Method:       initialiseOverlays
//Params:       itemID - the ID of the item selected from the menu (accepts null values for page load)
//Decription:   Places the markers on the map with the relevant info windows, if an 
//              id is passed it will open that info window once rendered
function initialiseOverlays(itemId)
{
    //Test to see if an ID is passed - if not render all pins normally, else open the relevant pin
    if(itemId == null)
    {
       for(i = 0; i < places.length; i++)
        {
            var geoCode = new GLatLng(places[i][1],places[i][2]);
            var infoDivID = places[i][0];

            createMarker(infoDivID, geoCode, false);            
        }
    }
    else
    {
        //Clear the pins from the map, we'll add them all again in a mo
        map.clearOverlays();
        
        //run through the array adding the pins
        for(i = 0; i < places.length; i++)
        {
            var geoCode = new GLatLng(places[i][1],places[i][2]);
            var geoCode = new GLatLng(places[i][1], places[i][2]);
            var infoDivID = places[i][0];

            //Test to see if itemID mmatches the ID stored in the array item we're up to
            if(itemId == places[i][0])
            {
                createMarker(infoDivID, geoCode, true);
            }
            else
            {           
                //Add the pin and bind the click event
                createMarker(infoDivID, geoCode, false);           
            }
    
       }
        
    
    }        
    
    
}


//Method:           createMarker
//Params:           infoId - [String] ID of the div containing the content for the Info Window
//                  geoCode - [GLatLng] Coordinates of the marker
//                  open - [boolean] Info Window is opened when created
//Description:      Creates a marker and binds the relevant information to its info window
function createMarker(infoId, geoCode, open) {

    //Create a new marker object
    var marker = new GMarker(geoCode);

    // Use JQuery to get the DIV tag with the relevant
    // information according to the ID stored in the array
    var infoData = $('div#' + infoId.toString()).html().toString();

    //Add the marker to the map
    map.addOverlay(marker);


    if (open == false) {
        // Bid the infoirmation to the marker so when 
        // it is clicked it opens with the relevant information
        //marker.bindInfoWindowHtml("<h1>BLAH</h1>", null);
            GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml(infoData, null); })
                
       
    }
    else {
    
        marker.openInfoWindowHtml(infoData, { onOpenFn: zoomTo(geoCode) });
    
    }
    

}



//Method:       initialiseMenuEventHandlers
//Description:  Adds event handlers to the menu items 
//              so that when clicked, they trigger the 
//              initialiseOverlays method with their ID passed to it
function initialiseMenuEventHandlers()
{
    var menuItem = $('ul#map-nav a').click(function(event){
    
        //Gets the event target (what was clicked)
        if(this == event.target)
        {
            //Get the class to pass to the initialiseOverlays method
            var item = $(this).attr('class');
            
            //Call initialiseOverlays Method
            initialiseOverlays(item);
            
        }
    });
}

//Method:       zoomTo 
//Params:       GLatLng coords - coordinates to pan, center or zoom to
//              
//Description:  Abstracted function to make it easier to channge the way the moves
//              uncomment the method you wish to try and comment out the active one
function zoomTo(coords)
{
    //Map will zoom in to the specified coordinates
    //map.zoomIn(coords,true,true);
    
    // Pan & Zoom
    // Map pans to the specified coordinates and zooms in
    
    map.panTo(coords);
    //map.setZoom(map.getZoom() + 1);
}