// http://www.google.com/apis/maps/
/*global google window */

var map, map_nav, iconBase;

function createMarker(east, north, id, name, status) {
	status = status.substr(1);
	var statuses = {
		iconC: {status: 'Closed', colour: 'red'},
		iconT: {status: 'Threatened', colour: 'yellow'},
		iconO: {status: 'Open', colour: 'green'},
		iconN: {status: 'Non-Scouts Canada', colour: 'purple'},
		iconU: {status: 'Unknown', colour: 'blue'}
	};
	if (!statuses[status]) {
		status = 'iconU';
	}
	
	// Create marker with correct coloured icon and tool-tip
	var marker = new google.maps.Marker(
		new google.maps.Point(east, north),
		{icon: new google.maps.Icon(iconBase, 'http://scoutdocs.ca/graphics/icon_' + statuses[status].colour + '.png'), title: name}
		);

	// Show this marker's info window when clicked
	var html = '<b><a target="_blank" href="http://scoutdocs.ca/Camps/Camps.php?camp=' + id + '">' + name + '</a></b><br>Status: ' + statuses[status].status;
	google.maps.Event.addListener(marker, 'click', function () {
		marker.openInfoWindowHtml(html);
	});

	map.addOverlay(marker);
	return 0;
}

function recenter_bynum(east, north, zoom) {
	map.setCenter(new google.maps.LatLng(north, east), zoom);
}

function recenter_byname(name) {
	recenter_bynum(map_nav[name].east, map_nav[name].north, map_nav[name].zoom);
}

function recenter(name) {
	window.location.hash = name;
	recenter_byname(name);
}

function recenter_click() {
	// Second parameter to getAttribute causes IE to act like other browsers. 
	// See http://msdn2.microsoft.com/en-us/library/ms536429.aspx
	recenter(this.getAttribute('href', 2).substr(1));
}

function closebox() {
	document.getElementById('info').style.display = 'none';
	document.getElementById('h1').className = 'noinfo';
}

function load() {
	if (google.maps.BrowserIsCompatible) {

		// Enable navigation links
		var navLinks = document.getElementById('nav').getElementsByTagName('a');
		for (var i = 0; i < navLinks.length; i++) {
			navLinks[i].onclick = recenter_click;
		}
		// Enable closebox
		document.getElementById('closebox').onclick = closebox;

		// Hide error message and show navigation, etc.
		document.getElementById('error').style.display = 'none';
		document.getElementById('instructions').style.display = 'block';
		document.getElementById('legend').style.display = 'block';
		document.getElementById('closebox').style.display = 'block';
		document.getElementById('nav').style.display = 'block';

		map_nav = {
			'CA-BC': {east: -123, north: 51, zoom: 6},
			'CA-AB': {east: -114, north: 54, zoom: 5},
			'CA-SK': {east: -105, north: 54, zoom: 5},
			'CA-MB': {east: -97, north: 50, zoom: 7},
			'CA-ON-N': {east: -86, north: 48, zoom: 6},
			'CA-ON-S': {east: -79, north: 44, zoom: 7},
			'CA-QC': {east: -75, north: 46, zoom: 7},
			'CA-NB-PE-NS': {east: -64, north: 45.5, zoom: 7},
			'CA-NL': {east: -61, north: 50, zoom: 6},
			'CA': {east: -94, north: 53, zoom: 4}
		};

		var frag = window.location.hash;
		if (frag) {
			frag = frag.substr(1);
		}

		map = new google.maps.Map2(document.getElementById('map'));

		if (map_nav[frag]) {
			recenter_byname(frag);
		} else if (google.loader.ClientLocation && google.loader.ClientLocation.address.country_code === 'CA') {
			recenter_bynum(google.loader.ClientLocation.longitude, google.loader.ClientLocation.latitude, 8);
		} else {
			recenter_byname('CA');
		}

		map.addControl(new google.maps.LargeMapControl());
		map.addControl(new google.maps.MapTypeControl());
		map.addControl(new google.maps.ScaleControl());
		map.addControl(new google.maps.OverviewMapControl());
		map.enableScrollWheelZoom();

		iconBase = new google.maps.Icon();
		iconBase.shadow = 'http://scoutdocs.ca/graphics/icon_shadow.png';
		iconBase.iconSize = new google.maps.Size(12, 20);
		iconBase.shadowSize = new google.maps.Size(22, 20);
		iconBase.iconAnchor = new google.maps.Point(6, 20);
		iconBase.infoWindowAnchor = new google.maps.Point(5, 1);

		var request = google.maps.XmlHttp.create();
		request.open('GET', 'camps.kml', true);
		request.onreadystatechange = function () {
			if (request.readyState === 4) {
/*<Placemark id="amisk">
<name>Camp Amisk</name>
<Point><coordinates>-97.15107,49.740067</coordinates></Point>
<styleUrl>#iconO</styleUrl>
<description><![CDATA[<div><b><a target="_blank" href="http://scoutdocs.ca/Camps/Camps.php?camp=amisk">Camp Amisk</a></b></div><div>Status: Open</div>]]></description>
</Placemark>*/
				var markers = request.responseXML.getElementsByTagName('Placemark');
				for (var i = 0; i < markers.length; i++) {
					var marker = markers.item(i);
					var coord = marker.getElementsByTagName('Point')[0].firstChild.firstChild.nodeValue.split(',', 2);
					createMarker(
						parseFloat(coord[0]),
						parseFloat(coord[1]),
						marker.getAttribute('id'),
						marker.getElementsByTagName('name')[0].firstChild.nodeValue,
						marker.getElementsByTagName('styleUrl')[0].firstChild.nodeValue
					);
				}
			}
		};
		request.send(null);
	}
}

function unload() {
	google.maps.Unload();
}

google.load('maps', '2');
google.setOnLoadCallback(load);
window.onunload = unload;
