$(document).ready(function(){
	
	// Navigation
	var hash = window.location.hash;
	var href = $('#menu li a').each(function(){
		var link = $(this).attr('href');
		if(hash==link){
			$(hash).slideDown();
			$(this).addClass('selected');
			
		} else if(!hash) {
			$('#home').slideDown();
			$('#menu li:first a').addClass('selected');
		}											
	});

	$('#menu li a').click(function(){
		var link = $(this).attr('href');
		if(link==window.location.hash){} 
		else{
			window.location.hash = link;
			$('#menu .selected').removeClass('selected');
			$(this).addClass('selected');
			$('.tab').slideUp();
			$(link).slideDown();			
			document.title = "Thierry Castel - " + link.substring(1,2).toUpperCase() + link.substring(2,20);					  
		}
		return false;		
	});

	// Little things

	$('ul#tweets li:first').addClass('first');
	$('ul#tweets li:last').addClass('last');
	$('ul#tweets li:odd').addClass('tweet_even');
  	$('ul#tweets li:even').addClass('tweet_odd');
  	$('ul#tweets li:not(.first)').hover(
		function(){
			$(this).addClass('hover').stop().animate({height: '50px'}, 500);
		},
		function(){
			$(this).removeClass('hover').stop().animate({height: '3px'}, 500);			
		})
	
	$('#music li:not(.playlist_current)').live('mouseover', function(){
		$(this).stop().animate({'paddingLeft' : '70px'}, 400);		
	})
	$('#music li:not(.playlist_current)').live('mouseout', function(){
		$(this).stop().animate({'paddingLeft' : '55px'}, 400, function(){
			$(this).removeAttr('style');
		});
	})
	
	var email = 'tscastel'+'@hotmail.com';
	$('.mail').text(email).attr('href', 'mailto:' + email);
	
	$('#download a')
		.mouseover(function(){
			$(this).stop().animate(
				{backgroundPosition:"(50 3)"}, 700, 'easeOutElastic')
			})
		.mouseout(function(){
			$(this).stop().animate(
				{backgroundPosition:"(50 9)"}, 
				{duration:200})
			})

	// Music player


	var playItem = 0;
	var myPlayList = [
		{name:"Dear Boss",mp3:"music/dear_boss.mp3", ogg:"music/dear_boss.ogg", img:"images/music_dear_boss.png", subtext: "played by the Castel/van Damme quartet"},
		{name:"Auguste Gusteau",mp3:"music/auguste_gusteau.mp3", ogg:"music/auguste_gusteau.ogg", img:"images/music_auguste.png", subtext: "played by the Castel/van Damme quartet"},
		{name:"Knights and Minstrels",mp3:"music/knights_and_minstrels.mp3", ogg:"music/knights_and_minstrels.ogg", img:"images/music_knights.png", subtext: "played by the Castel/van Damme quartet"},
		{name:"Lous Harbour",mp3:"music/lous_harbour.mp3", ogg:"music/lous_harbour.ogg", img:"images/music_lou.png", subtext: "played by the Castel/van Damme quartet"}
	];


	
	$("#play").jPlayer( {
	    ready: function () {
	      	displayPlayList();
			playListInit(false); // Defines the mp3
	    },
		oggSupport: false
	  })
		.jPlayerId("play", "player_play")
		.jPlayerId("pause", "player_pause")
		.jPlayerId("stopPlaying", "player_stop")
		.jPlayerId("playBar", "player_progress_play_bar")
		.jPlayerId("bufferShow", "bodys")
		.jPlayerId("volumeMax", "player_volume_max")
		.jPlayerId("volumeBar", "player_volume_bar")
		.jPlayerId("volumeBarValue", "player_volume_bar_value")
		.onProgressChange( function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
			var myPlayedTime = new Date(playedTime);
			var ptMin = (myPlayedTime.getUTCMinutes() < 10) ? "0" + myPlayedTime.getUTCMinutes() : myPlayedTime.getUTCMinutes();
			var ptSec = (myPlayedTime.getUTCSeconds() < 10) ? "0" + myPlayedTime.getUTCSeconds() : myPlayedTime.getUTCSeconds();
			$("#play_time").text(ptMin+":"+ptSec);

			var myTotalTime = new Date(totalTime);
			var ttMin = (myTotalTime.getUTCMinutes() < 10) ? "0" + myTotalTime.getUTCMinutes() : myTotalTime.getUTCMinutes();
			var ttSec = (myTotalTime.getUTCSeconds() < 10) ? "0" + myTotalTime.getUTCSeconds() : myTotalTime.getUTCSeconds();
			$("#total_time").text(ttMin+":"+ttSec);
		})
		.onSoundComplete( function() {
			playListNext();
		});

		$("#prev").click( function() {
			playListPrev();
			return false;
		});

		$("#next").click( function() {
			playListNext();
			return false;
		});
		// keyboard controls
		var captured = false;
		var isIE = navigator.appName.toLowerCase().indexOf("explorer") > -1;
		document.onkeydown = function(e){
			var ev = isIE?event:e;
			if(ev.charCode && ev.charCode == 32)
				playPause();
			else{
				switch(ev.keyCode){
					case 32:
						playPause();
						break;
					case 39:
						playListNext();
						break;
					case 37:
						playListPrev();
						break;
					case 38:
						playListPrev();
						break;
					case 40:
						playListNext();
						break;
				}
			}
		}
		function displayPlayList() {
			for (i=0; i < myPlayList.length; i++) {
				$("#music ul").append("<li id='playlist_item_"+i+"'><img src="+ myPlayList[i].img +"><p>"+ myPlayList[i].name +"</p><p class='played_by'>"+ myPlayList[i].subtext +"</p></li>");
				$("#playlist_item_"+i).data( "index", i ).hover(
					function() {
						if (playItem != $(this).data("index")) {
							$(this).addClass("playlist_hover").stop().animate({'paddingLeft' : '70px'}, 400);
						}
					},
					function() {
						$(this).removeClass("playlist_hover");
						if(playItem != $(this).data("index")){
							$(this).stop().animate({'paddingLeft' : '55px'}, 400, function(){
								$(this).removeAttr('style')
							});							
						}
							
					}
				).click( function() {
					var index = $(this).data("index");
					if (playItem != index) {
						playListChange( index );
					} else {
						playStop();
					}
				});
			}
			$("#music ul li:last").addClass('last');
			
		}

		function playListInit(autoplay) {
			if(autoplay) {
				playListChange( playItem );
			} else {
				playListConfig( playItem );
			}
		}

		function playListConfig( index ) {
			$("#playlist_item_"+playItem).removeAttr('style').removeClass("playlist_current");
			$("#playlist_item_"+index).addClass("playlist_current");
			playItem = index;
			$("#play").setFile(myPlayList[playItem].mp3, myPlayList[playItem].ogg);
			$("#song span").text(myPlayList[playItem].name);
		}

		function playListChange( index ) {
			playListConfig( index );
			$("#play").play();
		}

		function playListNext() {
			var index = (playItem+1 < myPlayList.length) ? playItem+1 : 0;
			playListChange( index );
		}

		function playListPrev() {
			var index = (playItem-1 >= 0) ? playItem-1 : myPlayList.length-1;
			playListChange( index );
		}
		
		function playPause() {
			if($('#player_play:visible').length == 0){
				$('#play').pause()
				
			} else {
				$('#play').play();
			}
		}
		function playStop() {
			if($('#player_play:visible').length == 0){
				$('#play').stopPlaying()
				
			} else {
				$('#play').play();
			}
		}	
});
						   




