$(document).ready(function(){
		
	//Add debugging alerts
	var debug = false;
	
	//Zebra stripe and hide tables
	var tables = $("div.league div.leaguetable");
	for(i=0; i<tables.length; i++) {
		//Hide the leaguetable divs on startup
		$(tables[i]).hide();
		
		//Zebra stripe
		$("tr", tables[i]).each(
			function(j){
				if(j%2 == 0) {$(this).addClass("alt")}
			}
		);
	}
	
	//Add show-hide to h3 tags
	$("div.league h3").each(
		function(i) {
			$(this).click(
				function() {
					var parent = $(this).parent();
					$("div.leaguetable", parent).animate
					({height: 'toggle'}, 450, "easeInOutSine");
				});

					

			/*
			$(this).mouseover(
				function() {
					var parent = $(this).parent();
					$("div.leaguetable", parent).slideDown("slow");

			});
			*/
		});
	
	// Attach Google analytics tracking code to all links out of the site
	// This assumes that you set the rel attribute on a link to contain the 'external'
	// Then you add to this the name that you want to see appear in GA tracking

	// Convert rel="external" links to open in new window
	$('a[rel*=external]').attr("title", function() {
		var oldtitle = $(this).attr("title");
		$(this).attr("title", oldtitle + " (opens in a new browser window)");
	})
	.click(function(event) {
		trackGAEvent($(this),event, "rel", "external ");
	})
	.keypress(function() {
		trackGAEvent($(this),event, "rel", "external ");
	});
	
	// Track using GA. We create a _trackPageview event, although _trackEvent might be more consistant
	// Pass in the name of the attribute we want to search, and any prefix we want to strip
	function trackGAEvent(obj,event, attribute, prefix) {
		// Does the targeted attribute contain anything after prefix has been stripped?
		if(obj.attr(attribute).substring(prefix).length > 0 ) {
			var GAroot = escape("/outgoing/" + obj.attr(attribute).substring(prefix.length));
			
			if(debug == true) {
				alert(GAroot);
				event.preventDefault();
				return false;
			}else{
				pageTracker._trackPageview(GAroot);
			}
			// Check if the link is a mailto: to avoid it opening a new window
			if(obj.attr("href").toLowerCase().indexOf('mailto') == -1) {
				// Stop the default action (href) on the event from firing
				event.preventDefault();
				window.open(obj.attr('href'));
			}
		}
	}
});
