var myInterval;
function closeMap(){
	//stop the position management function
	clearInterval(myInterval);
	//hide the map
	document.getElementById("floatingContainer").style.visibility = "hidden";
}
function showGMap(i){
	//start the position management function
	positionit();
	myInterval = setInterval("positionit()",100);
	//this function relies on an array (arrLocations) being written by view containing all location data
	//0=latitude
	//1=longitude
	//2=locationName
	//3=locationAddress
	if (GBrowserIsCompatible() && arrLocations) {
		objMapDiv = document.getElementById("mapDiv");
		document.getElementById("floatingContainer").style.visibility = "visible";
		var map = new GMap2(objMapDiv);
		var point = new GLatLng(arrLocations[i][0],arrLocations[i][1]);
		if (!point) {
			objMapDiv.style.height = "auto";
			objMapDiv.innerHTML = "<p style=\"text-align:center; font-weight:bold; color: #FF0000;padding-top: 10px;\">This address could not be found in Google maps.</p>";
		} else {
			objMapDiv.style.height = "300px"; 
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			//map.enableScrollWheelZoom();
			map.setCenter(point, 15);
			//if you just want to show normal map, remove the options for satellite and hybrid
			map.removeMapType(G_SATELLITE_MAP)
			map.removeMapType(G_HYBRID_MAP);
			var marker = customMarker(point,arrLocations[i][2],arrLocations[i][3],'normal');
			map.addOverlay(marker);
			//set initial load to display marker window
			marker.openInfoWindowHtml("<h4>" + arrLocations[i][2] + "</h4>" + arrLocations[i][3]);
			location.hash = 'location' + i;
			document.getElementById("directionLink").href = "http://maps.google.com/maps?f=d&daddr=" +  arrLocations[i][4] + "&sll=" + arrLocations[i][0] + "," + arrLocations[i][1];
		}
	}
}
function showLocation(i) {
	//function to center/zoom/show info window for items in search results
	arrMarker[i].openInfoWindowHtml("<h4>" + arrLocations[i][2]+ "</h4>" + arrLocations[i][3]);
}
var arrMarker = new Array();
function showGMapAll(centerLat,centerLong){
	if (GBrowserIsCompatible() && arrLocations) {
		objMapDiv = document.getElementById("mapDiv2");
		var map = new GMap2(objMapDiv);
		var mapOK = false;
		//loop through array of locations and add to map if a valid point
		for(i=0;i<arrLocations.length;i++){
			var point = new GLatLng(arrLocations[i][0],arrLocations[i][1]);
			if (point) {
				arrMarker.push(customMarker(point,arrLocations[i][2],arrLocations[i][3],'normal'));
				mapOK = true;				
			}
		}
		//if OK flag (e.g. - at least one valid point), add them to map
		if(mapOK){
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			map.setCenter(new GLatLng(centerLat,centerLong), 9);
			map.removeMapType(G_SATELLITE_MAP)
			map.removeMapType(G_HYBRID_MAP);
			for(m=0;m<arrMarker.length;m++){
				map.addOverlay(arrMarker[m]);
			}
			objMapDiv.style.height = "300px"; 
			objMapDiv.style.display = "block";
			location.hash = 'map';
		}else{
			objMapDiv.style.height = "auto";
			objMapDiv.innerHTML = "<p style=\"text-align:center; font-weight:bold; color: #FF0000;padding-top: 10px;\">This map cannot be generated. Please try again later.</p>";
		}
	}
}
//function to create a custom marker with listener attached
function customMarker(point,name,html,icontype) {
	var gicons = [];
	gicons["normal"] = new GIcon(G_DEFAULT_ICON);
	gicons["custom"] = new GIcon(G_DEFAULT_ICON, "images/googleCustomIcon.png");
	gicons["custom"].shadow = "images/googleCustomIconShadow.png";
	gicons["custom"].iconSize = new GSize(13, 21);
	gicons["custom"].shadowSize = new GSize(21, 24);
	
	var marker = new GMarker(point, gicons[icontype]);	
	//could also use "mouseover" as listener event
	GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml("<h4>" + name + "</h4>" + html);	
		});
	return marker;
}
function positionit() {
	//define universal reference to "staticcontent"
	var crossobj = (document.all) ? document.all.floatingContainer : document.getElementById("floatingContainer");					
	//define reference to the body object in IE
	var iebody = (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
	//define universal dsoc left point
	var dsocleft = (document.all) ? iebody.scrollLeft : pageXOffset;
	//define universal dsoc top point
	var dsoctop = (document.all) ? iebody.scrollTop : pageYOffset;
	//pageWidth = document.documentElement.clientWidth; - Old pageWidth code
	pageWidth = 762;
	pageHeight = document.documentElement.clientHeight;
	mapWidth = 428;
	//mapHeight = 448;
	mapHeight = 360;	
	var newLeft =  (pageWidth - mapWidth) / 2;
	var newTop = (pageHeight - mapHeight) / 2;
	//if the user is using IE 4+ or Firefox/ NS6+
	if (document.all || document.getElementById) {
		crossobj.style.left = parseInt(dsocleft) + newLeft + "px";
		crossobj.style.top = parseInt(dsoctop) + newTop + "px";
	}
}