
/* ************************************************************************* */
$(document).ready(function()
{	
	// Resize the map div to 100% anytime the window is scrolled or resized and onload
	$("#map").css("width", $(document).width()).css("height", $(document).height());
	$(window).scroll(function()
	{
		$("#map").css("width", $(document).width()).css("height", $(document).height());
	});
	$(window).resize(function()
	{
		$("#map").css("width", $(document).width()).css("height", $(document).height());
	});
	
	// Draw a Google Map centered on a random location from the JSON data
	drawMap();
	
	// Wire up the random location link
	$("#caption a").click(drawMap);
	
	// Wrap a span around ampersands so we can use better ones
	$("li:contains('&')", document.body).contents().each(function()
	{
		if(this.nodeType == 3)
		{
			$(this).replaceWith(this.nodeValue.replace( /&/g, "<span class='amp'>&</span>" ));
		}
	});
	
	// Wrap a span around atsigns so we can use better ones
	$("li:contains('@')", document.body).contents().each(function()
	{
		if(this.nodeType == 3)
		{
			$(this).replaceWith(this.nodeValue.replace( /@/g, "<span class='atsign'>@</span>" ));
		}
	});
	
	// Add section highlights when mousing over header links
	$("#facebookLink").mouseover(function(){ $("h2").addClass("highlight"); });
	$("#facebookLink").mouseout(function(){ $("h2").removeClass("highlight"); });
	$("#twitterLink").mouseover(function(){ $("#twitter").addClass("highlight"); });
	$("#twitterLink").mouseout(function(){ $("#twitter").removeClass("highlight"); });
	$("#readerLink").mouseover(function(){ $("#reader").addClass("highlight"); });
	$("#readerLink").mouseout(function(){ $("#reader").removeClass("highlight"); });
	$("#blogLink").mouseover(function(){ $("#blog").addClass("highlight"); });
	$("#blogLink").mouseout(function(){ $("#blog").removeClass("highlight"); });
	
	// Rollover behavior for Facebook status
	$("h2 a").mouseover(function(){ $("h2").addClass("highlight"); });
	$("h2 a").mouseout(function(){ $("h2").removeClass("highlight"); });
	
	// Rollover behaviors for Twitter and Reader items
	$("#twitter li a").mouseover(function(event){ $(this).parent().addClass("active"); });
	$("#twitter li a").mouseout(function(event){ $(this).parent().removeClass("active"); });
	$("#reader li a").mouseover(function(event){ $(this).parent().addClass("active"); });
	$("#reader li a").mouseout(function(event){ $(this).parent().removeClass("active"); });
	$("#blog li a").mouseover(function(event){ $(this).parent().addClass("active"); });
	$("#blog li a").mouseout(function(event){ $(this).parent().removeClass("active"); });
});
/* ************************************************************************* */



/* ************************************************************************* */
function drawMap()
{
	$.getJSON("/js/locations.js", function(data)
	{
		// Get a random location
		var location = data.locations[Math.floor(Math.random() * data.locations.length)];
	
		// Draw the map
		if(GBrowserIsCompatible())
		{
			var map = new GMap2(document.getElementById("map"));
			map.setMapType(G_SATELLITE_MAP);
			new GClientGeocoder().getLatLng(
				location.address,
				function(point){ if(point){ map.setCenter(point, location.zoom); } }
			);
		}
		// Update the caption
		$("#caption p strong").html(location.name);
		$("#caption p span").html(location.description);
	});
	
	// Track the interaction in GA
	pageTracker._trackPageview("/event/map/load");
}
/* ************************************************************************* */

