

// Set map position defaults
var centerLatitude = 38.75408327579141;
var centerLongitude = -96.15234375;
var startZoom = 3;



/* Developer Note 10/02/2008:
1.  Added map_functions.js code here
*/



var map, manager;

// Creates the HTML for marker bubbles and for the Google Maps point list
function getMarkerHTML(markerType, isBalloon, pointData) {
	var html = '';
	if (isBalloon) {	// Create HTML for the popup marker balloon
	    html += '<div class="Balloon">' + pointData.name + '<br />' + pointData.address + '<br />';
		if (markerType == "manager") {	// display additional information for the regional sales managers
		    html += pointData.phone + '<br />' + pointData.cell + ' (cell)<br /><a href="mailto:' + pointData.email + '">' + pointData.email + '</a>';
		}
		html += '</div>';
	} else				// Create HTML for the Google Maps API's point list
		html = '<div class="label">' + pointData.abbr + '</div><a href="' + pointData.wp + '">' + pointData.name + '</a>';
	return html;
}

// Returns the click function for the Google Map's marker points 
function createMarkerClickHandler(marker, markerType, pointData) {
	return function() {
		marker.openInfoWindowHtml(getMarkerHTML(markerType, true, pointData));
		return false;
	};
}

// Add a marker to the Google Map
function createMarker(pointData) {
	var latlng = new GLatLng(pointData.latitude, pointData.longitude);
	var icon = new GIcon(G_DEFAULT_ICON);
	opts = {
		"icon": icon,
		"clickable": true,
		"labelText": pointData.abbr,
		"labelOffset": new GSize(-24,-15)
	};

	var marker = new LabeledMarker(latlng, opts);
	var handler = createMarkerClickHandler(marker, markerType, pointData);
	GEvent.addListener(marker, "click", handler);

	var listItem = document.createElement('li');
	listItem.innerHTML = getMarkerHTML(markerType, false, pointData);
	listItem.getElementsByTagName('a')[0].onclick = handler;

	return marker;
}

// Get the window height, accommodating the different browsers' methods
function windowHeight() {
	// Standard browsers (Mozilla, Safari, etc.)
	if (self.innerHeight)
		return self.innerHeight;
	// IE 6
	if (document.documentElement && document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
	// IE 5
	if (document.body)
		return document.body.clientHeight;
	// Just in case. 
	return 0;
}

// Handle window resizing
function handleResize() {
	var height = windowHeight() - 30;
}

// Set up the map, initialize points
function initMap() {

	handleResize();

	map = new GMap2(document.getElementById("map"));
	map.addControl(new GSmallMapControl());
	map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);

	manager = new GMarkerManager(map);

	batch = [];
	for(id in markers) {
		batch.push(createMarker(markers[id]));
	}

	manager.addMarkers(batch, 0,15);
	manager.refresh();

}

// Set up handlers
window.onresize = handleResize;
window.onload = initMap;
window.onunload = GUnload;



/* Developer Note 10/02/2008:
2.  End of map_functions.js code
3.  Beginning of LabeledMarker.js code
*/



/*
* LabeledMarker Class
*
* Copyright 2007 Mike Purvis (http://uwmike.com)
* 
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* 
*       http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This class extends the Maps API's standard GMarker class with the ability
* to support markers with textual labels. Please see articles here:
*
*       http://googlemapsbook.com/2007/01/22/extending-gmarker/
*       http://googlemapsbook.com/2007/03/06/clickable-labeledmarker/
*/

/* Constructor */
function LabeledMarker(latlng, options){
    this.latlng = latlng;
    this.labelText = options.labelText || "";
    this.labelClass = options.labelClass || "markerLabel";
    this.labelOffset = options.labelOffset || new GSize(0, 0);
    
    this.clickable = options.clickable || true;
    
    if (options.draggable) {
    	// This version of LabeledMarker doesn't support dragging.
    	options.draggable = false;
    }
    
    GMarker.apply(this, arguments);
}


/* It's a limitation of JavaScript inheritance that we can't conveniently
   extend GMarker without having to run its constructor. In order for the
   constructor to run, it requires some dummy GLatLng. */
LabeledMarker.prototype = new GMarker(new GLatLng(0, 0));


// Creates the text div that goes over the marker.
LabeledMarker.prototype.initialize = function(map) {
	// Do the GMarker constructor first.
	GMarker.prototype.initialize.apply(this, arguments);
	
	var div = document.createElement("div");
	div.className = this.labelClass;
	div.innerHTML = this.labelText;
	div.style.position = "absolute";
	map.getPane(G_MAP_MARKER_PANE).appendChild(div);

	if (this.clickable) {
		// Pass through events fired on the text div to the marker.
		var eventPassthrus = ['click', 'dblclick', 'mousedown', 'mouseup', 'mouseover', 'mouseout'];
		for(var i = 0; i < eventPassthrus.length; i++) {
			var name = eventPassthrus[i];
			GEvent.addDomListener(div, name, newEventPassthru(this, name));
		}

		// Mouseover behaviour for the cursor.
		div.style.cursor = "pointer";
	}
	
	this.map = map;
	this.div = div;
}

function newEventPassthru(obj, event) {
	return function() { 
		GEvent.trigger(obj, event);
	};
}

// Redraw the rectangle based on the current projection and zoom level
LabeledMarker.prototype.redraw = function(force) {
	GMarker.prototype.redraw.apply(this, arguments);
	
	// We only need to do anything if the coordinate system has changed
	if (!force) return;
	
	// Calculate the DIV coordinates of two opposite corners of our bounds to
	// get the size and position of our rectangle
	var p = this.map.fromLatLngToDivPixel(this.latlng);
	var z = GOverlay.getZIndex(this.latlng.lat());

    offset = this.labelOffset.width;

	this.div.style.left = (p.x + offset) + "px";
	this.div.style.top = (p.y + this.labelOffset.height) + "px";
	this.div.style.zIndex = z + 1; // in front of the marker
}

// Remove the main DIV from the map pane, destroy event handlers
LabeledMarker.prototype.remove = function() {
	GEvent.clearInstanceListeners(this.div);
	this.div.parentNode.removeChild(this.div);
	this.div = null;
	GMarker.prototype.remove.apply(this, arguments);
}



/* Developer Note 10/02/2008:
4.  End of LabeledMarker.js code
*/


