﻿/*
*** author:         jake williamson - jsa digital
*** mod date:		10/may/07

--- description:    google map javascript functions.

--- functions:      loadMapBasedOnKnownGeocodes()
                    findLocationBasedOnAddress()
                    loadMapBasedOnAddress()
                    googleMapCheckBrowser()
                    createMarkerIcon()
                    
--- dependants:     dotNetFix.js
*/

//var subPath = "http://development.jsa.com.au/franklins/"
var subPath = "/"
var mapIconSml = subPath+"stores/images/mm_30_red.png"
var mapIconSmlW = 22;
var mapIconSmlH = 30;
var mapIconSmlShadow = subPath+"stores/images/mm_30_shadow.png"
var mapIconSmlShadowW = 32;
var mapIconSmlShadowH = 30;

var mapIconLrg = subPath+"stores/images/mm_45_red.png"
var mapIconLrgW = 32;
var mapIconLrgH = 45;
var mapIconLrgShadow = subPath+"stores/images/mm_45_shadow.png"
var mapIconLrgShadowW = 42;
var mapIconLrgShadowH = 45;

var map;
var geocoder;
var icon;

//determin if this is a dot.net page
if (strDotNetFix != "") {

        //append the prefix that the dot.net master pages has added to all the form fields
        strDotNetFix = strDotNetFix;
    
    } else {
    
        //return an empty string
        strDotNetFix = "";

}

//loadsa a map and sets it's marker icon to the centre based on the passed geocodes
loadMapBasedOnKnownGeocodes = function(mapID,latitude,longitude,blnDraggable) {

    //if the current browser can display the map
    if (GBrowserIsCompatible()) {

        //get the div to load the map into
        map = new GMap2(document.getElementById(strDotNetFix+mapID));

        //add the nav controls
        map.addControl(new GLargeMapControl());

        //add the buttons to switch between map types
        map.addControl(new GMapTypeControl());

        //get the centre of the map based on the geocode
        var centrePoint = new GLatLng(latitude, longitude);

        //set the centre and zoom level based on the passed geocodes
        map.setCenter(centrePoint, 16);

        //create a marker icon
        icon = createMarkerIcon("lrg");
        
        //
        if (blnDraggable == false) {

            //add the marker icon to the map
            map.addOverlay(new GMarker(centrePoint, icon));
            
        } else {
        
            //these two hidden fields will hold the long/lat for the database add/amend
            var hfLatitude = document.getElementById(strDotNetFix+"hfLatitude");
            var hfLongitude = document.getElementById(strDotNetFix+"hfLongitude");

            //place the marker icon on the map, make it draggable and
            var marker = new GMarker(centrePoint, {icon:icon, draggable: true});
		    map.addOverlay(marker);
    		
		    //set the default message
		    marker.openInfoWindowHtml("You can drag this marker<br>to your exact location.");
    	
		    //if the user 'drags' the marker, close the info window
		    GEvent.addListener(marker, "dragstart", function() {
			    map.closeInfoWindow();
		    });
    		
		    //when the icon is 'dropped'
		    GEvent.addListener(marker, "dragend", function() {
    		
		        //set the long/lat hidden field values with the new values
		        hfLongitude.value = marker.getPoint().lng();
		        hfLatitude.value = marker.getPoint().lat();
    		    
			    //let the user know the long/lat has bee updated
			    marker.openInfoWindowHtml("The location has been updated.");

		    });
		    
        }		

    }

}


//findLocationBasedOnAddress() is called when you click on the Search button in the form.
//it geocodes the address entered into the form and adds a marker to the map at that location.
findLocationBasedOnAddress = function() {

	//grab the address fields from the form
	var addressOne = document.getElementById(strDotNetFix+"txtAddressOne").value
	var suburb = document.getElementById(strDotNetFix+"txtSuburb").value
	var state = document.getElementById(strDotNetFix+"ddlState").value
	var postcode = document.getElementById(strDotNetFix+"txtPostcode").value

  	//format the full address
    var strAddress = addressOne + ',' + suburb + ',' + state + ',' + postcode + ',Australia';
    
    geocoder = new GClientGeocoder();
  
    geocoder.getLocations(strAddress, loadMapBasedOnAddress);
    
}


//loadMapBasedOnAddress() is called when the geocoder returns an answer. it adds a marker to the map with an open info window
loadMapBasedOnAddress = function(response) {

    map = new GMap2(document.getElementById(strDotNetFix+"map"));

    //if the address hasnt been found
    if (!response || response.Status.code != 200) {

        //let the user know
        alert("Sorry, we were unable to find the address.\n\nPlease try the following:\n\n  - Change to a general postcode");

    } else {

        //these two hidden fields will hold the long/lat for the database add/amend
        var hfLatitude = document.getElementById(strDotNetFix+"hfLatitude");
        var hfLongitude = document.getElementById(strDotNetFix+"hfLongitude");

        //remove any exisiting markers
        map.clearOverlays();

        //add the controls
        map.addControl(new GSmallMapControl());

        //returns the placemark from the array
        var place = response.Placemark[0];
        
        //get the long/lat from the array
        var latitude = place.Point.coordinates[1];
        var longitude = place.Point.coordinates[0];

        //set the values of the hidden fields
        hfLatitude.value = latitude;
        hfLongitude.value = longitude;

        //get the centre of the map based on the geocode
		var centrePoint = new GLatLng(latitude, longitude);

        //set the centre and zoom level based on the passed geocodes
        map.setCenter(centrePoint, 16);        

        //create a marker icon
        icon = createMarkerIcon("lrg");
        
        //place the marker icon on the map, make it draggable and .
        var marker = new GMarker(centrePoint, {icon:icon, draggable: true});
		map.addOverlay(marker);
		
		//set the default message
		marker.openInfoWindowHtml("You can drag this marker<br>to your exact location.");
	
		//if the user 'drags' the marker, close the info window
		GEvent.addListener(marker, "dragstart", function() {
			map.closeInfoWindow();
		});
		
		//when the icon is 'dropped'
		GEvent.addListener(marker, "dragend", function() {
		
		    //set the long/lat hidden field values with the new values
		    hfLongitude.value = marker.getPoint().lng();
		    hfLatitude.value = marker.getPoint().lat();
		    
			//let the user know the long/lat has bee updated
			marker.openInfoWindowHtml("The location has been updated.");

		});

    }

}


//simple check to make sure the current browser will support google maps
googleMapCheckBrowser = function() {

    if (GBrowserIsCompatible() == false) {
    
        alert("Your browser is not compatible with Google Maps.\n\nPlease use the latest versions of Internet Explorer or Firefox.");
        
    }

}


//creates a marker icon based on the images set in the config
createMarkerIcon = function(iconSize) {

    var markerIcon = new GIcon();
    var iconImg;
    var iconImgW;
    var iconImgH;

    var iconShadow;
    var iconShadowW;
    var iconShadowH;
    
    var iconAnchorW;
    var iconAnchorH;

    if (iconSize == "sml") {

        iconImg = mapIconSml;
        iconImgW = mapIconSmlW;
        iconImgH = mapIconSmlH;
        
        iconShadow = mapIconSmlShadow;
        iconShadowW = mapIconSmlShadowW;
        iconShadowH = mapIconSmlShadowH;

    } else {

        iconImg = mapIconLrg;
        iconImgW = mapIconLrgW;
        iconImgH = mapIconLrgH;
                
        iconShadow = mapIconLrgShadow;
        iconShadowW = mapIconLrgShadowW;
        iconShadowH = mapIconLrgShadowH;
    
    }
      
    //The foreground image URL of the icon.
    markerIcon.image = iconImg;
    
    //The shadow image URL of the icon.
    markerIcon.shadow = iconShadow;
    
    //The pixel size of the foreground image of the icon.
    markerIcon.iconSize = new GSize(iconImgW, iconImgH);
    
    //The pixel size of the shadow image.
    markerIcon.shadowSize = new GSize(iconShadowW, iconShadowH);
    
    //The pixel coordinate relative to the top left corner of the icon image at which this icon is anchored to the map.
    iconAnchorW = iconImgW / 2;
    iconAnchorH = iconImgH
    markerIcon.iconAnchor = new GPoint(iconAnchorW, iconAnchorH);
    
    //The pixel coordinate relative to the top left corner of the icon image at which the info window is anchored to this icon.
    markerIcon.infoWindowAnchor = new GPoint(12, 7);
    
    return markerIcon;
    
}


multipleClickableMarkers = function(mapID,strIDs,strLats,strLongs,strUrl,strIdVarName) {

    if (GBrowserIsCompatible()) {
    
        var bounds = new GLatLngBounds();
    
        var map = new GMap2(document.getElementById(strDotNetFix+mapID));
        //map.addControl(new GLargeMapControl());
        //map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(-33.867139, 151.207114), 16);
        
        var arrIDs = new Array();
        arrIDs = strIDs.split(",");
        
        var arrLats = new Array();
        arrLats = strLats.split(",");
        
        var arrLongs = new Array();
        arrLongs = strLongs.split(",");
        
        for (var i = 0; i < arrIDs.length; i++) {

            var point = new GLatLng(arrLats[i],arrLongs[i]);

            bounds.extend(point);
            
            var marker = createClickMarker(point,arrIDs[i],strUrl,strIdVarName);
            
            map.addOverlay(marker);
        
        }
        
        map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
        
    }

}

function createClickMarker(point,id,strUrl,strIdVarName) {

    //create a marker icon
    icon = createMarkerIcon("sml");

    var marker = new GMarker(point, icon);
    GEvent.addListener(marker, 'click', function() {

        document.location.href = strUrl + "?" + strIdVarName + "=" + id
        
    });

    return marker;

}