// jQuery onLoad
$(function() {
	function setEqualHeight() {
		var maxHeight = 0;
		var $max;
		$("#content > .column").each(function() {
			var $this = $(this);
			var height = $this.height();
			if (height > maxHeight) {
				maxHeight = height;
				$max = $this;
			}
		}).not($max).css("min-height", maxHeight);
	}
	setEqualHeight();

	$("a[rel='external']").click(function(event) {
		window.open($(this).attr("href"));
		event.preventDefault();
	});

	$(".hover-fade").hover(
		function(event) { $(this).stop(true, true).fadeTo("slow", 0.6); },
		function(event) { $(this).stop(true, true).fadeTo("slow", 1.0); }
	);

	if (0 < $(".validate").length) {
		$("label.required").append(" <em>*</em>");

		$.getScript("/s/js/jquery.validate.min.js", function() { 
			$("form").validate();
		});
	}
});

// Google Maps v3 APIs
var loadMaps = function() {
	var map = document.getElementById("map");
	if (null == map) {
		return;
	}

	if ("function" === typeof loadMaps.beforeLoadCallback) {
		loadMaps.beforeLoadCallback(map);
	}

	function mapsLoaded() {
		var houston = new google.maps.LatLng(29.76317, -95.362587);
		var latlng = new google.maps.LatLng(29.663667, -95.173016);

		var myOptions = { 
			center: houston, 
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			mapTypeControl: false,
			navigationControl: true,
			navigationControlOptions: google.maps.NavigationControlStyle.SMALL,
			zoom: "undefined" != typeof loadMaps.zoom ? loadMaps.zoom : 8
		};
		var gmap = new google.maps.Map(map, myOptions);

		if ("function" === typeof loadMaps.afterLoadCallback) {
			loadMaps.afterLoadCallback(gmap);
		}

		if ("undefined" == typeof loadMaps.marker) {
			loadMaps.marker = {
				title: "Get Driving Directions",
				click: function(gmap, marker) {
					// infowindow.open(gmap, marker);
				}
			};
		}

		var mapLoad = google.maps.event.addListener(gmap, "tilesloaded", function() {
			$(map).addClass("gmap");
			google.maps.event.removeListener(mapLoad);
		});

		var icon = new google.maps.MarkerImage(
			"/s/img/fox.png",
			new google.maps.Size(24, 25),
      			new google.maps.Point(0, 0),    // origin  
			new google.maps.Point(12, 12)   // anchor
		);

		var marker = new google.maps.Marker({
			position: latlng, 
			icon: icon,
			map: gmap, 
			title: loadMaps.marker.title
		});

		google.maps.event.addListener(marker, "click", function() { 
			loadMaps.marker.click(gmap, marker);
		});
	}

	google.load("maps", "3", { "callback": mapsLoaded, "other_params": "sensor=false" });
	$(window).unload( function () { GUnload(); } );
}

// Load Google Maps when ready
if ("undefined" !== typeof google) {
	google.setOnLoadCallback(function() {
		loadMaps();
	});
}