﻿/************************************** Splendid **************************************
 * Created By: Steve Doggett
 * Created On: 8th August 2008 
 *
 * Edited ----------------------------------------------------------------------------
 *		By:				On:
 * Description -----------------------------------------------------------------------
 *		Google map controls
 * 		
 * Functions -------------------------------------------------------------------------
 *		initializeMap()             // Create the map and adds a draggable icon into the middle of it
 *      getBoundingBox()            // Retrieve the bounding box coordinates of the current map
 *      initialiseDragMarker()      // Create a draggable marker to pin onto the map
 *      createMarker()              // Creates a blue pin marker to add to the map. Not draggable
 *      createDraggableMarker()     // Create a draggable new location marker for the map
 ************************************************************************************/
 
/******************************** Global Variables **********************************/
var map = null;                         // Global variable for the map object. Set in initialiser!
var geocoder = new GClientGeocoder();   // Creates a new global geocoding object

/****
* Create a new location icon template
****/
var newLocIcon = new GIcon();
newLocIcon.shadow = "";
newLocIcon.iconSize = new GSize(35, 89);
newLocIcon.iconAnchor = new GPoint(0, 89);
newLocIcon.infoWindowAnchor = new GPoint(17, 45);
newLocIcon.image = 'img/mapPin.png';

/******************************** Map Initialiser ***********************************/

/****
 * Create the map and adds a draggable icon into the middle of it
 ****/
function initializeMap(mapID, lat, lng, zoom) {
    // Check that the browser is compatible with Google maps.
    if (GBrowserIsCompatible()) {
        var centrePoint = new GLatLng(lat, lng);    // Just sets an initial centre point for the map. Could come from db or something if needed
        map = new GMap2(document.getElementById(mapID));              // This actually creates the map
        map.setCenter(centrePoint, zoom);           // Sets the centre point for the map and the initial zoom level
//        map.addControl(new GLargeMapControl());               // Adds in the zoom control
//        map.addControl(new GScaleControl());                    // Adds in the scale indicator
        //        map.addControl(new GMapTypeControl());                // Adds the map/satellite/hybrid buttons to the map.
        map.setUIToDefault()
    } else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
      return
    }
}

/*********************************** Markers ****************************************/

/****
 * Creates a blue pin marker to add to the map. Not draggable
 ****/
function createMarker(point, html) {
    var options = {};
    var customIcon = new GIcon(newLocIcon); // newLocIcon is defined at the top of this script
    
    options = {icon: customIcon};
    var marker = new GMarker(point, options);

    // Add a click event to the marker to display the info window when marker clicked.
    if (html !== null && html !== "") {
        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(html);
        });
    }
    
    return marker;
}

/****
 * Centres tje map on the place selected from the drop wond box
 ****/
function showPlaceOnMap(lat, lng, place, zoom)
{
    // If no lat/long then don't do anything as it's going to cause problems.
    if( lat == '' || lng == '')
     return;

    map.clearOverlays();
    
    // create the point and pan the map to the point. Could use setCentre if you don't want the map to animate.
    var point = new GLatLng(lat, lng);
    map.panTo(point, zoom);
    // Adds a draggable marker to the centre of the map
    var myMarker = createMarker(point, place);
    map.addOverlay(myMarker);
}
