// Variable to store the last hovering point over the map
var lastKnownHoveringPoint = null;
// Variable for the map
var map;

// Variable for mapicons
var mapBlackIcons = new Array();
var mapGreyIcons = new Array();







// Creates a marker at the given point with the given number label
function createMarker(point, html, icon) {
	
	
	var marker;
	
	if (icon) {
		marker = new GMarker(point, {icon: icon});   
	}
	else {
		marker = new GMarker(point);   	
	}
	
    GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);        	
    });
    return marker;
}


// Gets a marker loading on demand
function getMarkerIcon(iconNumber, blackIcons) {
	var iconSet;
	iconSet = mapBlackIcons;
	
	var fileloc = '/files/dhmedia/googlemap/';
	
	// if no iconNumber we want star icon
	if (!iconNumber) {
		var newIcon = new GIcon();
		newIcon.image = fileloc + "pin.png";
		newIcon.iconSize = new GSize(50, 44);
		newIcon.shadow = fileloc + "shadow.png";
		newIcon.shadowSize = new GSize(50, 44);
		newIcon.iconAnchor = new GPoint(16, 39);
		newIcon.infoWindowAnchor = new GPoint(16, 8);
		return newIcon;
	}
	
	var icon = iconSet[iconNumber];
	// Create and load the icon
	if (!icon) {
		var newIcon = new GIcon();
		newIcon.image = fileloc + "pin.png";
		newIcon.iconSize = new GSize(50, 44);
		newIcon.shadow = fileloc + "shadow.png";
		newIcon.shadowSize = new GSize(50, 44);
		newIcon.iconAnchor = new GPoint(16, 39);
		newIcon.infoWindowAnchor = new GPoint(16, 8);
		iconSet[iconNumber] = newIcon;
	} 
	
	return iconSet[iconNumber];	
}

// Closes any markers which are open
function closeMarker() {
	map.closeInfoWindow();
}

// Zooms (maximum zoom) to the marker and sets the centre of the map to it
function zoomMarker(marker) {
	// Code below does not actually detect currently available max zoom
	// - instead hard-code to one above the max to be safe
	//var zoom = map.getCurrentMapType().getMaximumResolution(point);
	map.closeInfoWindow();
    map.setCenter(marker.getPoint(), 16);
    GEvent.trigger(marker,  "click");
}


// Initialises the map
function Gload() {

	// Don't try and load the map if browser is not compatible
	//if (!GBrowserIsCompatible()) {
	//	return;
	//}
    
    // Load the map into the GenMapDiv id (this id should be used for all map apps across all pages)
    var mapObj = document.getElementById("map");
    map = new GMap2(mapObj);
    
    // Restrict max zoom to australia
    var mt = map.getMapTypes(); 
	   for (var i=0; i<mt.length; i++) {
          mt[i].getMinimumResolution = function() {return 3;}
          
        } 
      
    map.enableContinuousZoom();
    geoCoder = new GClientGeocoder();
    
    // Setup the scroll wheel zooming    
    /*
    GMap2.prototype.wheelZoom = function(event) {  
    	if(event.cancelable) event.preventDefault(); {
	    	zoomDiff = (event.detail || -event.wheelDelta) < 0 ? +1 : -1;
	        if (lastKnownHoveringPoint) {	        		
		    	// This is the undocumented way but is smooth as a baby
		        if (zoomDiff > 0)
		           map.zoomIn(lastKnownHoveringPoint, false, true);
		        else
		            map.zoomOut(lastKnownHoveringPoint, true);
		    }
		}
		return false;
	}		
	
	
	GEvent.addListener(map, "mousemove", function(point){lastKnownHoveringPoint =  point;});
	GEvent.addDomListener(mapObj, "DOMMouseScroll", map.wheelZoom);
	GEvent.addDomListener(mapObj, "mousewheel", map.wheelZoom);
	*/
	
	// Add a move listener to restrict the bounds range
      GEvent.addListener(map, "move", function() {      	
        checkBounds();
      });

      // The allowed region which the whole map must be within
      var allowedBounds = new GLatLngBounds(new GLatLng(-44.715514,110.302734), new GLatLng(-8.754795,156.093750));
      
      // If the map position is out of range, move it back
      function checkBounds() {
        // Perform the check and return if OK
        if (allowedBounds.contains(map.getCenter())) {
          return;
        }
        // It`s not OK, so find the nearest allowed point and move there
        var C = map.getCenter();
        var X = C.lng();
        var Y = C.lat();

        var AmaxX = allowedBounds.getNorthEast().lng();
        var AmaxY = allowedBounds.getNorthEast().lat();
        var AminX = allowedBounds.getSouthWest().lng();
        var AminY = allowedBounds.getSouthWest().lat();

        if (X < AminX) {X = AminX;}
        if (X > AmaxX) {X = AmaxX;}
        if (Y < AminY) {Y = AminY;}
        if (Y > AmaxY) {Y = AmaxY;}
        //alert ("Restricting "+Y+" "+X);
        map.setCenter(new GLatLng(Y,X));
      }
    
	
	// Setup the controsl (views) for the map
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	map.addControl(new GOverviewMapControl());
	map.setCenter(new GLatLng(-31.99143,115.86635), 5);		
	//map.addControl(new GOverviewMapControl(new GSize(100,100)));
}


function dirtog(lat, long) {
    
	var o = document.getElementById('directions'), c = document.getElementById('top_func'), t = document.getElementById('daddr1'), t = document.getElementById('daddr2'), g = document.getElementById('google-map');
    
	if (o.style.display == 'none') {
	o.style.display = 'block';
	c.style.display = 'block';
	t.value = lat + ',' + long;
	g.style.margin =  '0 0 0 0';
	

    
	} 
}
