var yearAttended = "";
var defaultPlayerActionPhoto;

$(document).ready(function(){			
//on page load,

		//preload the defualt player action photo
		defaultPlayerActionPhoto = new Image();
		defaultPlayerActionPhoto.src = "images/player-action-photo-default.jpg";		

		//add click events to the player column links		
		$(".playerListColumn ul li a").click(function() {							  
			selectPlayer(this);						
		});
		
		//get the year from the page name and set the global year attended variable
		var url = document.URL;
		var pageName = url.substring(url.lastIndexOf("/")+1,url.lastIndexOf("."));
		yearAttended = pageName.replace("players", "");
		
		//set the sub nav item for this year
		$(".subNav a[class*=year"+yearAttended+"]").addClass("selected");
		
		//select either the default player or a bookmarked player
		var objPlayerLink = $(".playerListColumn a[href*="+document.location.hash+"]");
		if(objPlayerLink.length == 1){
			//one player link was matched, select the bookmarked player
			selectPlayer(objPlayerLink);	
		}else{
			//no player links matched the bookmark or there is no bookmark, select the first player as default
			objPlayerLink = $(".playerListColumn a[href*=#]");
			selectPlayer(objPlayerLink[0]);	
		}
		
});		

function selectPlayer(objPlayerLink){
		//clear all selected links
		$(".playerListColumn ul li a").removeClass("selected");	
		
		//clear the player description and images
		$("#playerDescription").html("");
		$("#playerHeadshot").attr("src","images/spacer.gif");
		$("#teamLogo").attr("src","images/spacer.gif");
		$("#playerAttendedFlag").attr("src","images/spacer.gif");
		$("#playerActionPhoto").attr("src","images/spacer.gif");
		
		//set the clicked link to selected
		$(objPlayerLink).addClass("selected")
		
		//get an array of the player description p tags adjacent to the clicked player link
		var playerDescriptions = $(objPlayerLink).parent().children("p");
		
		//get the player name
		var playerName = $(objPlayerLink).html().replace("*","");
		
		//display the player photos using the player's name
		var playerImageName = playerName.toLowerCase().replace(/ /g, "-");
		$("#playerHeadshot").attr("src","images/"+yearAttended+"/"+playerImageName+"1.jpg");
		$("#playerActionPhoto").attr("src","images/"+yearAttended+"/"+playerImageName+"2.jpg");
		
		var img = new Image();
		img.onerror = function (evt) {
			//the player's action photo could not be found, load the default
			$("#playerActionPhoto").attr("src", defaultPlayerActionPhoto.src);
		}
		img.onload = function (evt) {
			//the player's action photo was loaded succesfully, display it
			$("#playerActionPhoto").attr("src", this.src);
		}
		img.src = "images/"+yearAttended+"/"+playerImageName+"2.jpg";
		
				
		//display the player description, team and attended status of the clicked player
		if(playerDescriptions.length > 0){
			
			//display the description
			var descriptionHTML = "";
			descriptionHTML += "<strong>"+playerName+"</strong><br />";
			for(i=0; i<playerDescriptions.length; i++){						
				descriptionHTML += $(playerDescriptions[i]).html() + "<br />";
			}				
			$("#playerDescription").html(descriptionHTML);
			
			//get the team name and use it to display the team logo
			var playerDescription1 = $(playerDescriptions[0]).html();
			var playerDescription1Array = playerDescription1.split(" - ");
			var teamName = playerDescription1Array[1].toLowerCase().replace(/ /g, "-");
			$("#teamLogo").attr("src","images/logo-"+teamName+".gif");	
			
			
			//show the attended status graphic if the player attended
			if($(objPlayerLink).html().indexOf("*") > -1){
				$("#playerAttendedFlag").attr("src","images/player-attended-esp.gif");
			}else{
				$("#playerAttendedFlag").attr("src","images/spacer.gif");
			}

		}else{
			$("#playerDescription").html("");
			$("#playerAttendedFlag").attr("src","images/spacer.gif");
			$("#teamLogo").attr("src","images/spacer.gif");
		}
		
}