// Script for Google Maps on the "Find Your YMCA Centre" page
var map;
var marker = [];
var bounds = new GLatLngBounds();

var iconYMCA = new GIcon();
iconYMCA.image = '/images/google.png';
iconYMCA.shadow = '/images/google-shadow.png';
iconYMCA.iconSize = new GSize(25, 25);
iconYMCA.shadowSize = new GSize(30, 25);
iconYMCA.iconAnchor = new GPoint(0, 0);
iconYMCA.infoWindowAnchor = new GPoint(15, 0);

var iconYMCAhover = new GIcon();
iconYMCAhover.image = '/images/google-hover.png';
iconYMCAhover.shadow = '/images/google-shadow.png';
iconYMCAhover.iconSize = new GSize(25, 25);
iconYMCAhover.shadowSize = new GSize(30, 25);
iconYMCAhover.iconAnchor = new GPoint(0, 0);
iconYMCAhover.infoWindowAnchor = new GPoint(15, 0);

var markerImage = [
	"/images/google.png",
	"/images/google.png",
	"/images/google.png",
	"/images/google-hover.png", //Hover
	"/images/google.png" // Visited
];

// Locations List
var locText = " ";
function sideBar(line, type, j){
	// locText += "<li><a href='"+type+"' ";
	// locText += "onclick='GEvent.trigger(marker["+j+"],\"click\"); return false;' ";
	locText += "<li><a href='#map' ";
	locText += "onclick=\"zoomIn("+j+");\" ";
	locText += "onmouseover='GEvent.trigger(marker["+j+"],\"mouseover\")' ";
	locText += "onmouseout='GEvent.trigger(marker["+j+"],\"mouseout\")' ";
	locText += ">";
	locText += line;
	locText += "<\/a><\/li>";
	document.getElementById("locationsList").getElementsByTagName("ul")[0].innerHTML = locText;
}

function loadMap(){
	map = new GMap2(document.getElementById("map"));
	map.setCenter(new GLatLng(-34.614495, 150.950000), 7);
	map.addControl(new GMapTypeControl(1));
	map.addControl(new GLargeMapControl());
	map.addControl(new GScaleControl(256));
	new GKeyboardHandler(map);
	map.enableContinuousZoom();
	map.enableDoubleClickZoom();

	/*// Make the Copyright Notice smaller
	for(var i = 0; i < map.getContainer().childNodes.length; ++i){
		// if(map.getContainer().childNodes[i].innerHTML.indexOf(String.fromCharCode(1­69))!== -1){
			map.getContainer().childNodes[i].style.fontFamily = 'Verdana, sans-serif';
			map.getContainer().childNodes[i].style.fontSize = '8px';
			map.getContainer().childNodes[i].style.color = '#666666';
			map.getContainer().childNodes[i].style.marginBottom = '-1px';
		// break;
		// }
	} */

	GDownloadUrl("/google-maps-locations.php", function(data) {
		var xml = GXml.parse(data);
		var markers = xml.documentElement.getElementsByTagName("marker");
		for (var i = 0; i < markers.length; i++) {
			var name = markers[i].getAttribute("name");
			var address = markers[i].getAttribute("address");
			var type = markers[i].getAttribute("type");
			var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));
			sideBar(name,type,i);
			html = "<p class='google-marker'><strong>" + name + "<\/strong> <br \/>" + address + " <br \/><a href='" + type + "'>Visit " + name + "<\/a> or <a href='javascript:zoomIn(" + i + ")'>zoom closer</a><\/p>";
			ZMarker(point,name,html,1,0,i,null);
			bounds.extend(point);
		}
	});

	// Fit and zoom 2 lines out
	// map.setCenter(bounds.getCenter(),map.getBoundsZoomLevel(bounds));
	// map.zoomOut();map.zoomOut();
}

function zoomIn(i) {
	map.setCenter(marker[i].getPoint(), 15);
	GEvent.trigger(marker[i], "click");
}

function zoomOut(i) {
	map.setCenter(new GLatLng(-34.614495, 150.950000), 7);
	GEvent.trigger(marker[i], "click");
}

var n=1;
function count(){
	n++; return n;
}
function ZMarker(point,label,html,n,imInd,i,visited) {
	function sendBack(marker,b) {
		return GOverlay.getZIndex(marker.getPoint().lat())-n*10000;
	}
	marker[i] = new GMarker(point, {icon:iconYMCA, title:label, zIndexProcess:sendBack});
	map.addOverlay(marker[i]);
	// marker[i].setImage(markerImage[imInd]);
	marker[i].visited = visited;

	GEvent.addListener(marker[i], "click", function() {
		if (map.getZoom()>=15) {
			html = html.replace("zoomIn","zoomOut");
			html = html.replace("closer","out");
		} else {
			html = html.replace("zoomOut","zoomIn");
			html = html.replace("out","closer");
		}
		marker[i].openInfoWindowHtml(html);
		GEvent.trigger(marker[i],"mouseout");
	});
	GEvent.addListener(marker[i],'mouseover',function() {
		marker[i].setImage(markerImage[3]);
		document.getElementById("locationsList").getElementsByTagName("a")[i].className = 'hover';
	});
	GEvent.addListener(marker[i],'mouseout',function() {
		if(marker[i].visited){
			marker[i].setImage(markerImage[4]);
			document.getElementById("locationsList").getElementsByTagName("a")[i].className = 'visited';
		}else{
			marker[i].setImage(markerImage[0]);
			document.getElementById("locationsList").getElementsByTagName("a")[i].className = '';
		}
		marker[i].visited = true;
	});
	GEvent.addListener(marker[i], "infowindowclose", function() {
		map.removeOverlay(marker[i]);
		ZMarker(point,label,html,count(), 4,i,marker[i].visited);
	});
}

//EventManager.Add(window,'load', loadMap);
//EventManager.Add(window,'unload', GUnload);
$(document).ready(function(){
	loadMap();
});
$(window).unload(function() {
	GUnload();
});

