/*global $*/
/*global HG*/
HG = function () {
	var initHome = function () {		
		//cycle through home page banners
		$("#home #banner").cycle({ 
			fx: 'scrollLeft',
			timeout: 20000
		});			
	};
	
		
	var initDirectory = function () {
		var map = [];
		var officeLayer = {};
		var iconData = {};
		var icons = {};
		
		if (GBrowserIsCompatible()) {			
			$.getJSON("/_scripts/offices.js", function (json) {
				officeLayer = json.officeLayer;
				iconData = json.iconData;
				setupGMap("MI", 44.17461998, -84.45940403, 6);
				setupGMap("OH", 41.54579133, -82.2588333, 7);
				setupGMap("GA", 33.9073744, -84.3636743, 7);
			});
		}
	
		//initialize a google map
		function setupGMap(state, lat, lng, zoom) {
			map[state] = new GMap2(document.getElementById(state));
			map[state].setCenter(new GLatLng(lat, lng), zoom);
			
			var stateLayer = officeLayer[state];
			for (var i in stateLayer) {
				if (stateLayer.hasOwnProperty(i)) {
					var layer = stateLayer[i];
					for (var j in layer.places) {
						if (layer.places.hasOwnProperty(j)) {
							var place = layer.places[j];
							var icon = getIcon(place.icon);
							var posn = new GLatLng(place.posn[0], place.posn[1]);
							place.directions = place.address.replace(/<br \/>/gi, " ");
							var marker = createMarker(posn, place, icon);
							map[state].addOverlay(marker);
							if (place.name == "Headquarters - Southfield" || place.name == "Atlanta" || place.name == "Independence") {
								GEvent.trigger(marker, 'click');
							}
						}
					}
				}
			}
			
			map[state].addControl(new GMapTypeControl());
			map[state].addControl(new GLargeMapControl());
		}
	
		//grab an icon for a google map
		function getIcon(images) {
			var icon = null;
			if (images) {
				if (icons[images[0]]) {
					icon = icons[images[0]];
				} else {
					icon = new GIcon();
					icon.image = "http://www.hantzgroup.com/_images/gmaps/" + images[0] + ".png";
					var size = iconData[images[0]];
					icon.iconSize = new GSize(size.width, size.height);
					icon.iconAnchor = new GPoint(size.width >> 1, size.height >> 1);
					icon.infoWindowAnchor = new GPoint(size.width >> 1, size.height >> 1);
					icon.shadow = "http://www.hantzgroup.com/_images/gmaps/" + images[1] + ".png";
					size = iconData[images[1]];
					icon.shadowSize = new GSize(size.width, size.height);
					icons[images[0]] = icon;
				}
			}
			return icon;
		}
		
		//create a marker at a specific point on the map
		function createMarker(posn, place, icon) {
			var marker = new GMarker(posn, {title: place.name, icon: icon, draggable: false });
			var html = "<div class='gmapWindow'>" +
				"<h3>" + place.name + "</h3>" + 
				"<p> " + place.address + "<br />" +
					"<a href='http://maps.google.com/maps?q=" + place.directions + "' target='_blank' title='To " + place.name + "'>Directions &raquo;</a><br />" +
					"Phone: " + place.phone + "<br />" + 
					"Fax: " + place.fax + "</p>" + 
				"<br />" + 
			"</div>";
			GEvent.addListener(marker, 'click', function () {
				marker.openInfoWindowHtml(html);
			});
			return marker;
		}
	};
	
	return {
		init : function () {
			if ($("#home").length) {
				initHome();
			}
			if ($("body.directory").length) {
				initDirectory();
			}
		},
		
		// recursively run through an array to fade out or in objects
		// and finally execute a callback function
		// this function simplifies all of the nesting function
		// that were previously necessary to hide and show things on screen
		fadeCallback : function (fadeArray, callback) {
			var fade = fadeArray.shift();
			var target = fade[0]; // jquery object
			var direction = fade[1];
			var speed = fade[2];
			
			if (fadeArray.length >= 1) {
				if (direction === 'Out') {
					$(target).fadeOut(speed, HG.fadeCallback(fadeArray, callback));
				} else { // 'In'
					$(target).fadeIn(speed, HG.fadeCallback(fadeArray, callback));
				}
			} else {
				if (direction === 'Out') {
					$(target).fadeOut(speed, callback);
				} else { // 'In'
					$(target).fadeIn(speed, callback);
				}
			}
		}
	};
}();

// initialize jQuery event listeners, etc.
$(document).ready(function () {
	HG.init();
});

