$(document).ready (function () {
	var	my_jPlayer 		= $("#jquery_jplayer"),
		opt_play_first 	= false,
		opt_auto_play 	= true,
		first_track 	= true,
		num 			= 1;

	// Instance jPlayer
	my_jPlayer.jPlayer({
		ready: function () {
			$(".ecouteurs").click (function () {
				if (first_track) {
					$("#jp_container").fadeIn (500, function () { 
						$("#jp_container ul").width ($("#jp_container ul li").length * 286);
						lecture (1);
						$(".cp-play").removeClass ("ecouteurs");
						
					});
				}
			});
		},
		timeupdate : 	function(event) {},
		play : 			function(event) {},
		pause : 		function(event) {},
		ended : 		function(event) {},
		cssSelectorAncestor : 	"#Player",
		backgroundColor : 		"#9f8274",
		swfPath : 				"/extension/ezflow/design/ezflow/javascript/jquery.jplayer",
		supplied : 				"mp3",
		wmode : 				"window",
		cssSelector : {
			play : '.cp-play',
			pause : '.cp-pause'
		}
	});

	$("#jp_container .track").click(function(e) {
		my_jPlayer.jPlayer ("setMedia", {
			mp3: $(this).attr ("href")
		});
		if((opt_play_first && first_track) || (opt_auto_play && !first_track)) {
			my_jPlayer.jPlayer ("play");
		}
		first_track = false;
		$(this).blur ();
		return false;
	});

	$("#jp_container #Fermer").click (function () {
		$("#jp_container").fadeOut (500);
		$("#jquery_jplayer").jPlayer ("stop");
		$(".cp-play").addClass ("ecouteurs");
		first_track 	= true;
		$("#jp_container ul").css ("left", 0);
		$("#Prev").hide ();
		$("#Next").show ();
		num = 1;
		return false;
	});
	
	$("#Prev").click (function () {
		num--;
		var left = (-286 * (num-1));
		if (num >= 1) $("#jp_container ul").stop().animate ({ left : left }, "fast", function () {
			lecture (num);
			if (num == 1) $("#Prev").fadeOut ("fast");
			$("#Next").fadeIn ("fast");
		});
		return false;
	});
	
	$("#Next").click (function () {
		var left = -286 * num;
		if (num < $("#jp_container ul li").length) $("#jp_container ul").stop().animate ({ left : left }, "fast", function () {
			num++;
			lecture (num);
			if (num == $("#jp_container ul li").length) $("#Next").fadeOut ("fast");
			$("#Prev").fadeIn ("fast");
		});
		return false;
	});
	
	function lecture (n) {
		my_jPlayer.jPlayer ("setMedia", {
			mp3: $("#jp_container ul li:nth-child(" + n + ") .track").attr ("href")
		});
		my_jPlayer.jPlayer ("play");
		first_track = false;
	}
});
