	var map;
	var markersArray = [];
	
	var fLatitude = -26.431228064506424;
	var fLongitude = 134.560546875;
	var nZoomLevel = 4;	
	
	function initMap() {
	
		if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById("map"));
		}
	}
	
	function loadMaps(t,sAddress,nPostcode,nStateID,nGroupID) {

		if (GBrowserIsCompatible()) {
			map.setCenter(new GLatLng(fLatitude, fLongitude), nZoomLevel);		
			map.addControl(new ExtLargeMapControl());
			map.addControl(new GOverviewMapControl());
		
			var mgr = new GMarkerManager(map);
		
			GDownloadUrl('GeoMapXML.asp?Action=Search&t=' + t + '&Address=' + sAddress + '&Postcode=' + nPostcode + '&StateID=' + nStateID + '&Group=' + nGroupID, function(data, responseCode)	{		
				var xml = GXml.parse(data);
				var markers = xml.documentElement.getElementsByTagName("marker");				
				for (var i = 0; i < markers.length; i++) {
					var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));
					sDisplay = markers[i].getAttribute("disp");			
					nDistributorLocationID = markers[i].getAttribute("locid");	
					nDistributorID = markers[i].getAttribute("distid");										
					var marker = createMarker(point, markers[i].getAttribute("title"), sDisplay, nDistributorLocationID, nDistributorID, markers[i].getAttribute("locType"), sAddress, nPostcode, nStateID, nGroupID);					
					markersArray.push(marker);
					//map.addOverlay(marker);
					//mgr.addMarker(marker, 0); // now added after as array
				}
				mgr.addMarkers(markersArray, 0); // Add all markers at once
				mgr.refresh();
      		})
    	}
	}
	
	function createMarker(point, str, sDisplay, nDistributorLocationID, nDistributorID, locType, sAddress, nPostcode, nStateID, nGroupID) {
	
		var icon = new GIcon();
		if(locType == 1){
			icon.image = "images/GeoMap/SFS_DistributorIcon.png";	
			icon.shadow = "images/GeoMap/SFS_DistributorIconShad.png";
			icon.iconSize = new GSize(30, 30);
			icon.shadowSize = new GSize(30, 30);
			icon.iconAnchor = new GPoint(15, 15);
			icon.infoWindowAnchor = new GPoint(18, 1);
		} else if(locType == 2) {
			icon.image = "images/GeoMap/SFS_DistributorTruck.png";	
			icon.shadow = "images/GeoMap/SFS_DistributorIconShad.png";
			icon.iconSize = new GSize(30, 30);
			icon.shadowSize = new GSize(30, 30);
			icon.iconAnchor = new GPoint(15, 15);
			icon.infoWindowAnchor = new GPoint(18, 1);		
		} else {
			icon.image = "images/GeoMap/SFS_DistributorIcon.png";	
			icon.shadow = "images/GeoMap/SFS_DistributorIconShad.png";
			icon.iconSize = new GSize(30, 30);
			icon.shadowSize = new GSize(30, 30);
			icon.iconAnchor = new GPoint(15, 15);
			icon.infoWindowAnchor = new GPoint(18, 1);		
		}		
	
		var marker = new GMarker(point, icon);
		
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(createDistributorHtml(point, str, sDisplay,nDistributorLocationID, nDistributorID, sAddress, nPostcode, nStateID, nGroupID));
		});					
		
		return marker;
	}
	
	function createDistributorHtml(p, markerTitle, sDisplay, nDistributorLocationID, nDistributorID, sAddress, nPostcode, nStateID, nGroupID) {
		
		return "<div style=\"color:#C80024; font-size:11px; font-family: Arial, Verdana, Helvetica, sans-serif;\"><br/><strong>" + markerTitle + "</strong></div><div style=\"color:#666666; font-size:11px; font-family: Arial, Verdana, Helvetica, sans-serif;\"><br/>" + unescape(sDisplay) + "</div><div style='text-align: right;'><a href='DistributorLocations.asp?Action=Location&ID=" + nDistributorLocationID + "&DID=" + nDistributorID + "&Address=" + sAddress + "&Postcode=" + nPostcode + "&StateID=" + nStateID + "&Group=" + nGroupID + "' style='font-size: 11px; text-decoration: underline;'>View this Distributor</a></div>";
	}
	
	function getCount() {
		var bounds = map.getBounds();
		var maxX = bounds.getNorthEast().lng();
		var minX = bounds.getSouthWest().lng();
		var maxY = bounds.getNorthEast().lat();
		var minY = bounds.getSouthWest().lat();
		var count = 0;
		
		for (var x = 0; x < markersArray.length; x++) {
		
			if (markersArray[x].getPoint().lng() <= maxX && markersArray[x].getPoint().lng() >= minX) {
				if (markersArray[x].getPoint().lat() <= maxY && markersArray[x].getPoint().lat() >= minY) {
					count++;
				}
			}
		}
		
		return count;
	}

	function showAddress(address,postcode,state,group) {
	
		switch(parseInt(state)) {
		
			case 1:
				stateStr = 'Victoria';
				break;
			case 2:
				stateStr = 'New South Wales';			
				break;
			case 3:
				stateStr = 'Western Australia';			
				break;
			case 4:
				stateStr = 'South Australia';		
				break;
			case 5:
				stateStr = 'Queensland';			
				break;
			case 6:
				stateStr = 'Tasmania';			
				break;	
			case 7:
				stateStr = 'Northern Territory';		
				break;
			case 7:
				stateStr = 'Australian Capital Territory';						
				break;
			default:
				stateStr = '';
		}
	
		var geocoder = new GClientGeocoder();
		address += ',' + postcode + ' ' + stateStr + ', Australia';

		geocoder.getLatLng(
			address,
			function(point) {
			
				if (!point) {
					//document.getElementById('SearchResult').innerHTML = 'Address not found';
				} else {
					//document.getElementById('SearchResult').innerHTML = '';
					map.setCenter(point, 15);			
					var zoomLevel = map.getZoom();
					
					for(zoomLevel; zoomLevel >= 0; zoomLevel--)
	                {
						map.setZoom(zoomLevel);				
	                    if(getCount() >= (markersArray.length <= 4 ? markersArray.length : 4))
	                    {
							map.setCenter(point, zoomLevel);						
	                        break;
	                    }
	                }
					if (postcode == '') {
						map.setZoom(6);
					}

				}
			}
		);
	}

	
	// Inidividual location functionality
	
	function initLocationsMap(nID,t,sAddress,sPostcode,nStateID,nGroup) {

		var fLatitude = -26.431228064506424;
		var fLongitude = 134.560546875;
		var nZoomLevel = 4;
		var marker;
		var markerTitle;
		var locType;
		
		if (GBrowserIsCompatible()) {
			
			map.setCenter(new GLatLng(fLatitude, fLongitude), nZoomLevel);			
			map.addControl(new GOverviewMapControl());		
			map.addControl(new GMapTypeControl());
			map.addControl(new ExtLargeMapControl());
			
			GDownloadUrl("GeoMapXML.asp?Action=Location&ID=" + nID + "&Address=" + sAddress + "&Postcode=" + sPostcode + "&StateID=" + nStateID + "&Group=" + nGroup + "&t=" + t, function(data, responseCode)	{				
			var xml = GXml.parse(data);
			markers = xml.documentElement.getElementsByTagName("marker");			
			
			for (var i = 0; i < markers.length; i++) {
	
				var display = markers[i].getAttribute("disp");	
				var mappoint = new GLatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));
				map.setCenter(mappoint, parseFloat(markers[i].getAttribute("zoom")));
				var locType = markers[i].getAttribute("loctype");	
				
				addMarker(mappoint, markers[i].getAttribute("title"), map, markers[i].getAttribute("locid") ,locType,display);	
				markerTitle = markers[i].getAttribute("title");	
			}		
	
		})			
	
		}
		
		var mapTypes = map.getMapTypes();
		map.setMapType(mapTypes[3]);		
			
	}	
	
	
	function addMarker(mappoint,markerTitle,map,nLocationID,locType,sDisplay){
	
		var icon = new GIcon();
		
		if(locType == 1){
			icon.image = "images/GeoMap/SFS_DistributorIcon.png";	
			icon.shadow = "images/GeoMap/SFS_DistributorIconShad.png";
			icon.iconSize = new GSize(30, 30);
			icon.shadowSize = new GSize(30, 30);
			icon.iconAnchor = new GPoint(15, 15);
			icon.infoWindowAnchor = new GPoint(18, 1);
		} else if(locType == 2) {
			icon.image = "images/GeoMap/SFS_DistributorTruck.png";	
			icon.shadow = "images/GeoMap/SFS_DistributorIconShad.png";
			icon.iconSize = new GSize(30, 30);
			icon.shadowSize = new GSize(30, 30);
			icon.iconAnchor = new GPoint(15, 15);
			icon.infoWindowAnchor = new GPoint(18, 1);		
		} else {
			icon.image = "images/GeoMap/SFS_DistributorIcon.png";	
			icon.shadow = "images/GeoMap/SFS_DistributorIconShad.png";
			icon.iconSize = new GSize(30, 30);
			icon.shadowSize = new GSize(30, 30);
			icon.iconAnchor = new GPoint(15, 15);
			icon.infoWindowAnchor = new GPoint(18, 1);		
		}						

		// create new marker
		var marker = new GMarker(mappoint,icon);
		
		var latitude = mappoint.lat();
		var longitude = mappoint.lng();	
		
		// add marker to arrray
		map.addOverlay(marker); 
		var markerClass = new Marker(marker,markerTitle);
		markersArray.push(markerClass);				
		
		// add event listeners
		GEvent.addListener(marker, 'mouseover', function(overlay, mappoint) {

			marker.openInfoWindowHtml("<div style=\"color:#C80024; font-size:11px; font-family: Arial, Verdana, Helvetica, sans-serif;\"><br/><strong>" + markerTitle + "</strong></div><div style=\"color:#666666; font-size:11px; font-family: Arial, Verdana, Helvetica, sans-serif;\"><br/>" + unescape(sDisplay) + "</div><div style=\"text-align: right; color: #4499cc; font-size:10px; font-family: Arial, Verdana, Helvetica, sans-serif;\">Latitude:&nbsp;" + latitude + "<br/>Longitude:&nbsp;" + longitude + "</div>");			

		}
		);	
		
		GEvent.addListener(marker, 'mouseout', function(overlay, mappoint) {

			marker.closeInfoWindow();			

		}
		);			
	}
	
	function Marker(marker,markerTitle) {
		this.marker = marker;
		this.description = markerTitle;
	}
	
	function goToMarker(idx){
		if( idx >= 0 && idx < markersArray.length ){
			var location = markersArray[idx].marker.getLatLng();
			map.panTo(location, map.getZoom());
		}
	}
	
	function removeMarker(idx){
		if( idx >= 0 && idx < markersArray.length ){

			map.removeOverlay(markersArray[idx].marker);
			markersArray.splice(idx,1);	
		}
	}
	
	function removeAllMarkers(){
		for (var i = 0; i < markersArray.length; i++) {
			map.removeOverlay(markersArray[i].marker);
		}
	
		markersArray = new Array();
	}
	
	
	// function to trim string
	function trim(word){
		while (word.substring(0,1) == ' ')
			word = word.substring(1, word.length);
	
		while (word.substring(word.length-1, word.length) == ' ')
			word = word.substring(0,word.length-1);
	
		return word;
	}
	
	function onGDirectionsLoad(){ 
		// Use this function to access information about the latest load()
		// results.
		// e.g.
		// document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
		// and yada yada yada...
	};	
	
