﻿/*extern GBrowserIsCompatible, GIcon, GSize, GPoint, GMap2,
	GSmallMapControl, GMapTypeControl, GOverviewMapControl,
	GLatLng, GMarker, GEvent, YAHOO, GUnload */
/*members Dom, Event, addControl, addListener, addOverlay, address,
	baseIcon, baseURL, bind, closeInfoWindow, getElementById, getElementsBy,
	getElementsByTagName, getLocByURL, getTarget, host, href, iconAnchor,
	iconSize, image, infoShadowAnchor, infoWindowAnchor, length, load,
	locClick, location, locations, map, mapCenter, marker,
	openInfoWindowHtml, parentNode, protocol, replace, resetMap, setCenter,
	shadow, shadowSize, thumbClick, title, toLowerCase, unload, url, util,
	x, y, buildInfo, setZoom */
var Map = {
	mapCenter  : null,
	map        : null,
	baseIcon   : null,
	baseURL    : window.location.href.replace(/#.*$/,''),
	locations  : [ ],
	buildInfo  : function(oLoc) {
		return '<div style="width: 300px;"><div class="title infolink"><a href="' + oLoc.url + '">' + oLoc.title + '<\/a><\/div><hr />' + oLoc.address + '</div>';
	},
	thumbClick : function() {
		this.marker.openInfoWindowHtml(Map.buildInfo(this));
	},
	locClick : function(e) {
		var oTarget = YAHOO.util.Event.getTarget(e);
		var oLI     = oTarget.parentNode;
		var baseURL = this.baseURL;
		var oLink   = YAHOO.util.Dom.getElementsBy(function(o) { return (o.href !== baseURL + '#null'); }, 'a', oLI)[0]; if (!oLink) { return; }
		var sURL    = oLink.href;
		var oLoc    = this.getLocByURL(sURL); if (!oLoc) { return; }
		var sInfo   = this.buildInfo(oLoc);
		oLoc.marker.openInfoWindowHtml(sInfo);
	},
	getLocByURL : function(sURL) {
		var s  = (window.location.protocol + '//' + window.location.host).toLowerCase();
		var s1 = (sURL) ? sURL.toLowerCase() : 'x';
		var s2 = '';
		var x = this.locations.length; while (x--) {
			s2 = (this.locations[x].url) ? this.locations[x].url.toLowerCase() : 'y';
			if (s1 === s + s2) { return this.locations[x]; }
		}
	},
	unload: function() {
		try {
			GUnload();
		} catch (e) {
			// Workaround to bug in GUnload.
			// This is actually the officially recommended solution.
		}
	},

	resetMap: function() {
		this.map.setCenter(this.mapCenter, 10);
		this.map.closeInfoWindow();
	},

	load : function() {

		if (GBrowserIsCompatible()) {

			// Setup icon
			this.baseIcon = new GIcon();
			this.baseIcon.shadow = '/Content/Upload/AssetMgmt/siteImages/shadow.png';
			this.baseIcon.image  = '/Content/Upload/AssetMgmt/siteImages/blue-dot2.png';
			this.baseIcon.iconSize = new GSize(20, 34);
			this.baseIcon.shadowSize = new GSize(37, 34);
			this.baseIcon.iconAnchor = new GPoint(9, 34);
			this.baseIcon.infoWindowAnchor = new GPoint(9, 2);
			this.baseIcon.infoShadowAnchor = new GPoint(18, 25);

			// Setup map
			this.mapCenter = new GLatLng(42.275059, -88.398901);
			this.map = new GMap2(document.getElementById("map") );
			this.map.addControl(new GSmallMapControl());
			this.map.addControl(new GMapTypeControl());
			this.map.addControl(new GOverviewMapControl());
			this.map.setCenter(this.mapCenter, 10);

			// Add locations
			var oLoc, i = this.locations.length; while (i--) {
				oLoc = this.locations[i];
				oLoc.marker = new GMarker(new GLatLng(oLoc.x, oLoc.y), this.baseIcon);
				this.map.addOverlay(oLoc.marker);
				GEvent.bind(oLoc.marker, 'click', oLoc, this.thumbClick );
			}

			// If there is only one location, target it.
			if (this.locations.length === 1) {
				oLoc = this.locations[0];
				this.map.setZoom(14);
				oLoc.marker.openInfoWindowHtml(this.buildInfo(oLoc));
			}

			// Setup "Show On Map" links
			var sBaseURL = this.baseURL;
			var oEls, elLocs = document.getElementById('Locations'); if (elLocs) {
				oEls = YAHOO.util.Dom.getElementsBy(function(o) { return (o.href === sBaseURL + '#null'); }, 'a', elLocs);
				YAHOO.util.Event.addListener(oEls, 'click', this.locClick, this, true);
			}

			// Setup "Reset Map" button
			var oButton;
			var elCaption = document.getElementById('mapCaption'); if (elCaption) {
				oButton = elCaption.getElementsByTagName('button')[0];
				if (oButton) { YAHOO.util.Event.addListener(oButton, 'click', this.resetMap, this, true); }
			}

			YAHOO.util.Event.addListener(window, 'unload', this.unload, this, true);

		}

	}
};
