var map = null;
var MAP_HOLDER = 'locationMap';
var locationsPlottedPoints = new Array();
var tempLocationsToPlotArray = new Array();
var HOME_LATITUDE = 0;  // DEFAULT LATITUDE
var HOME_LONGITUDE = 0; // DEFAULT LONGITUDE
var DEFAULT_ZOOM = 10;
var ajaxLoaderImg = getElement('ajaxloader');

//locationsArray IS AN ARRAY OF LATLNGS.
//EACH ITEM IS 'Id,Lat,Lng'
//var locationsArray = new Array('1,-34.926,138.598','2,-34.922,138.599','3,-34.929,138.6','4,-34.921,138.6','5,-34.936,138.606','6,-34.5757,138.9293');


function GM_InitializeMap(){														//INITIALIZE GOOGLE MAP
	//debugger;
	if (GBrowserIsCompatible() && locationsArray && locationsArray.length > 0) {
		try{			
			map = new GMap2(getElement(MAP_HOLDER));
			if(enableMapControls){
				map.addControl(new GMapTypeControl());								//CHOOSE Map Satellite Hybrid MAP TYPE
			}
			if(enableMapZoom){
				map.addControl(new GSmallZoomControl());							//SMALL PLUS/MINUS ZOOM CONTROL
			}
			map.enableScrollWheelZoom();
			map.disableDoubleClickZoom();
			GM_Begin();
		}catch(e){
			GM_HideMapHolder();
		}		
	}else{
		GM_HideMapHolder();															//BROWSER NOT COMPATIBLE SO HIDE MAP
	}
}

function GM_Begin(){
	if(locationsArray.length>0){
		map.setCenter(new GLatLng(parseFloat(locationsArray[0].split(',')[1]), parseFloat(locationsArray[0].split(',')[2])), DEFAULT_ZOOM);
		GM_PlotLocationsFromLatLng(locationsArray);									//PLOT THE LOCATIONS
		GM_SetMapView(locationsPlottedPoints);										//ZOOM BASED ON ARRAY OF POINTS
	}
}

function GM_PlotLocationsFromLatLng(locationsToPlotArray){	
	if(locationsToPlotArray.length>0){
		locationsPlottedPoints = new Array(locationsToPlotArray.length);			//ADD THE POINT RETURNED FROM GM_AddLocationIcon TO AN ARRAY TO USE WITH GM_SetMapView
		for(i=0;i<locationsToPlotArray.length;i++){			
			locationsPlottedPoints[i] = GM_AddLocationIcon(
														parseFloat(locationsToPlotArray[i].split(',')[0]),
														parseFloat(locationsToPlotArray[i].split(',')[1]),
														parseFloat(locationsToPlotArray[i].split(',')[2])
												    );
		}
	}
}

function GM_AddLocationIcon(locationId, latitude, longitude){
	var point = new GLatLng(latitude, longitude);	
	map.addOverlay(marker = new GMarker(point));	
	GEvent.addListener(marker, 'mouseover', function(){GM_GetLocationInfo(this, locationId)});
	GEvent.addListener(marker, 'mouseout', function(){hideWindow(locationId)});
	return point;
}

/* START AJAX FUNCTIONS */
function GM_GetLocationInfo(marker, locationId){
	locationClicked = marker;
	if(locationId > 0){
		ajaxObject = initAjaxObject();
		ajaxObject.onreadystatechange = processAjaxReadyState_GM_GetLocationInfo;
		ajaxObject.open('GET', '/utility/locationdescriptionxml.aspx?locationId=' + locationId + '&ms=' + new Date().getTime(), true);
		ajaxObject.send('');
	}
}
function processAjaxReadyState_GM_GetLocationInfo(){	
	if (ajaxObject.readyState == 4){
		if(ajaxObject.status == '200'){			
			var venueName = '';
			var rating = '';
			var telephoneNumber = '';
			var addressLine1 = '';
			if(ajaxObject.responseXML.getElementsByTagName('LocationInfo')){
				if(ajaxObject.responseXML.getElementsByTagName('LocationInfo')[0]){
					hotelInfo = ajaxObject.responseXML.getElementsByTagName('LocationInfo')[0];
					if(hotelInfo.getElementsByTagName('VenueName')[0].childNodes[0].nodeValue){
						venueName = hotelInfo.getElementsByTagName('VenueName')[0].childNodes[0].nodeValue;
						if(hotelInfo.getElementsByTagName('Rating')[0].childNodes[0]){
							rating = hotelInfo.getElementsByTagName('Rating')[0].childNodes[0].nodeValue;
						}
						if(hotelInfo.getElementsByTagName('TelephoneNumber')[0].childNodes[0]){
							telephoneNumber = hotelInfo.getElementsByTagName('TelephoneNumber')[0].childNodes[0].nodeValue;
						}
						if(hotelInfo.getElementsByTagName('AddressLine1')[0].childNodes[0]){
							addressLine1 = hotelInfo.getElementsByTagName('AddressLine1')[0].childNodes[0].nodeValue;
						}
						customInfoWindow(locationClicked, venueName, rating, telephoneNumber, addressLine1);
					}
				}
			}
			//DO NOT SET ajaxObject = null HERE AS IT CAUSES ERRORS IF 
			//WE HAVE MULIPLE CALLS TO THIS FUNCTION RUNNING AT THE SAME TIME
			//IT IS SET TO NULL ON window.onunload
		}
	}
}
/* END AJAX FUNCTIONS */

/* HELPER FUNCTIONS */
function GM_SetMapView(points){														//ZOOMS MAP TO CORRECT ZOOM LEVEL BASED ON AN ARRAY OF LOCATIONS PLOTTED
   var bounds = new GLatLngBounds();
   for (var i=0; i< points.length; i++){
      bounds.extend(points[i]);
   }
   map.setZoom(map.getBoundsZoomLevel(bounds));
   map.setCenter(bounds.getCenter());
}
function getElement(elementId){														//CROSS BROWSER document.getElementById
	if(document.getElementById){
		return document.getElementById(elementId);
	}else{
		return document.all[elementId];
	}
}
function GM_HideMapHolder(){
	getElement(MAP_HOLDER).style.display = 'none';
}
function CustomGUnload(){
	ajaxObject = null;
	GUnload();
}
function hideWindow(locationId){
	getElement('venueinfo').innerHTML = '';
}

function customInfoWindow(locationClicked, venueName, rating, telephoneNumber, addressLine1){
	getElement('venueinfo').innerHTML = '<strong>' + venueName + '</strong> (' + rating + '/10)<br />Tel: ' + telephoneNumber + '<br />Address: ' + addressLine1;
	getElement('venueinfo').style.visibility = 'visible';	
}

function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == '*' && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, '\\-');
	var oRegExp = new RegExp('(^|\\s)' + strClassName + '(\\s|$)');
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

function initAjaxObject(){
	if (window.XMLHttpRequest){
		// W3C METHOD
		return new XMLHttpRequest();
	}else if (window.ActiveXObject){
		// IE METHOD
		return new ActiveXObject('Microsoft.XMLHTTP');
	}else{
		return;
	}
}


/* WINDOW LOAD/UNLOAD FUNCTIONS */
if(window.addEventListener){														//EXECUTED ON WINDOW LOAD/UNLOAD EVENT. ATTEMPTS FF/MOZ ETC THEN IE THEN OVERIDES window.onload/unload EVENTS
	window.addEventListener('load', function(){GM_InitializeMap();},false);
	window.addEventListener('unload', function(){CustomGUnload();},false);
}else if(window.attachEvent){
	window.attachEvent('onload', GM_InitializeMap);
	window.attachEvent('onunload', CustomGUnload);
}else{
	window.onload = function() {GM_InitializeMap();};
	window.onunload = function() {CustomGUnload();};
}
