﻿var geocoder = new GClientGeocoder();
	var foundLocation = false;
	var map;
	var directionsPanel;
	var currentLocationIndex = 0;
	var myText = '';

	//LEAVE ALONE START
	function load() {
		if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById("map"));
			//map.addControl(new GSmallMapControl());
	        map.setUIToDefault();
			map.setCenter(new GLatLng(-41.000000,172.000000), 5);
		}
	}

	function createMarker(point,html,icon) {
		var marker = new GMarker(point, icon);
			GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(html);
			});
		return marker;
	}
			
	/*function findMe(fromLocation, toLocation){
		//get the destination
		foundLocation = false;
		txtBox = document.getElementById(fromLocation);
		point = getAddress(txtBox.value, toLocation);
	}

	function getAddress(address, toLocation) {
		geocoder.getLatLng(
			address,
			function(point) {
				if (!point) {
					alert(address + " ");
				} else{
					map = new GMap2(document.getElementById("map"));
					directionsPanel = document.getElementById("route");
					directions = new GDirections(map, directionsPanel);
					directions.load(txtBox.value + ", Auckland" + " to " + toLocation);
				}
			}
		);
	}*/		

	function loadPointers(centerMap){
		//Clear pointers off map
		map.clearOverlays();
		//Add current back in 
		//addPoint(locations[currentLocationIndex], map);
		addPoint(map);
		//center map if reqd
		if(centerMap){
			changeLocation(locations[currentLocationIndex][0],locations[currentLocationIndex][1],locations[currentLocationIndex][2]);	
		}
	}

	function addPoint(map2){
		var i=0;
		for (i=0;i<locations.length;i++){
			myPoint = locations[i];
			var point = new GLatLng(parseFloat(myPoint[0]),parseFloat(myPoint[1]),parseInt(myPoint[2]));
			
			myText = myPoint[6]+myPoint[3];

			if(myPoint[4] == true){
				var icon = new GIcon();
				icon.image = "web/design/img/mapPointer.gif";
				icon.iconSize = new GSize(22, 40);
				icon.iconAnchor = new GPoint(10, 40);
				icon.infoWindowAnchor = new GPoint(5, 1);
				var marker = createMarker(point,'<div class="mapInfoContent">' + myText + '</div>', icon);
				map2.addOverlay(marker);
			}
		};
	}

	function changeLocation(lat, lng, zoom) {
		map.setZoom(zoom);
		map.panTo(new GLatLng(lat,lng));
	}
	
	function changeMapLocation(){
		var newLocation = document.getElementById("loactionList").value;
		map.setZoom(10);
		if (newLocation == "myCity") {
			map.panTo(new GLatLng(-41.000000,172.000000));
			map.setZoom(5);
		} else if (newLocation == "Auckland") {
			map.panTo(new GLatLng(-36.8473850,174.7657350));
		} else if (newLocation == "Christchurch"){
			map.panTo(new GLatLng(-43.5316370,172.6366450));
		} else if (newLocation == "Dunedin"){
			map.panTo(new GLatLng(-45.8745600,170.5033880));
		} else if (newLocation == "Gisborne"){
			map.panTo(new GLatLng(-38.6663810,178.0259490));
		} else if (newLocation == "Hamilton"){
			map.panTo(new GLatLng(-37.7882630,175.2838830));
		} else if (newLocation == "Kaitaia"){
			map.panTo(new GLatLng(-35.1116520,173.2618480));
		} else if (newLocation == "Kapiti"){
			map.panTo(new GLatLng(-40.9211870,175.0026040));
		} else if (newLocation == "Katikati"){
			map.panTo(new GLatLng(-37.5537790,175.9171390));
		} else if (newLocation == "Masterton"){
			map.panTo(new GLatLng(-40.9489490,175.6607640));
		} else if (newLocation == "Mount Maunganui"){
			map.panTo(new GLatLng(-37.6410950,176.1847300));
		} else if (newLocation == "Napier"){
			map.panTo(new GLatLng(-39.4917330,176.9162810));
		} else if (newLocation == "Nelson"){
			map.panTo(new GLatLng(-41.2707860,173.2840000));
		} else if (newLocation == "New Plymouth"){
			map.panTo(new GLatLng(-39.0580540,174.0764330));
		} else if (newLocation == "Palmerston North"){
			map.panTo(new GLatLng(-40.3557460,175.6121900));
		} else if (newLocation == "Petone"){
			map.panTo(new GLatLng(-41.2254680,174.8791500));
		} else if (newLocation == "Queenstown"){
			map.panTo(new GLatLng(-45.0311040,168.6627310));
		} else if (newLocation == "Rotorua"){
			map.panTo(new GLatLng(-38.1398260,176.2470730));
		} else if (newLocation == "Taupo"){
			map.panTo(new GLatLng(-38.6865940,176.0696940));
		} else if (newLocation == "Tauranga"){
			map.panTo(new GLatLng(-37.6870780,176.1668130));
		} else if (newLocation == "Upper Hutt"){
			map.panTo(new GLatLng(-41.1245800,175.0690320));
		} else if (newLocation == "Wellington"){
			map.panTo(new GLatLng(-41.2864800,174.7762170));
		} else if (newLocation == "Whakatane"){
			map.panTo(new GLatLng(-37.9584350,176.9858880));
		} else if (newLocation == "Whangarei"){
			map.panTo(new GLatLng(-35.7228700,174.3226850));
		}
	}
	//END LEAVE ALONE