$(document).ready(function() {
	// switch team member on click
	$('a').click(function()
	{				
		//clear all on classes
		$("#" + $(this).attr("class") + "Group a").removeClass("on");
		
		var name = $(this).html().replace(" ", "_");
		name = name.replace(/[^a-zA-Z_]+/g, "");
		
		setName(name, $(this).attr("class"));
		
		if(!$(this).hasClass("on"))
			$(this).addClass('on');
	});
	
	$('a').hover(function()
	{
		$(this).css('cursor','pointer');
	});
		
	// Load new biography and set up ajax picture
	function setName(name, team)
	{	
		var height = $("#" + team + "Holder").height();
		$("#" + team + "Holder").css("height", height);
		
		$("#" + team + "Holder").html($("#" + name).html());
		$("#" + team + "Holder").animate( { height: $("#" + name).height() }, { queue:false, duration:200 } );
		
		// If the image is found, show it - otherwise just show default image
		var imageLocation = "/images/people/" + name.toLowerCase() + ".jpg";
		$.ajax({url: imageLocation, type:'HEAD', 
			success: function(msg){ $("#" + team + "Image").attr('src', imageLocation); }, 
			error: function(msg){ $("#" + team + "Image").attr('src', "/images/people/default.jpg"); } 
		});
	}
	
	// Find all <ul> and set the first team member to be "on"
	function init()
	{
		for(var i = 0; i < $("ul").length; i++)
		{
			var group = $("ul")[i];
			
			if(group.children.length)
			{
				var member = $("ul")[i].children[0].childNodes[0];
				
				var name = member.innerHTML.replace(" ", "_");
				name = name.replace(/[^a-zA-Z_]+/g, "");

				setName(name,  member.className);
				
				member.className = member.className + " on";
			}
		}
	}
	
	init();
});