// JavaScript Document
$(document).ready(function() {
	
	// ----------------------------------------------------
	// SUBMENU SLIDE EFFECT 
	// ----------------------------------------------------
	
	$("#sidebar nav a:not(.menuactive)").mouseenter(function() {
		$(this).stop().animate({backgroundPosition: "(-225px 0px)"}, {duration: 200});
	}).mouseleave(function() {
		$(this).stop().animate({ backgroundPosition: "(0px 0px)" }, {duration: 100});
	});
	
	// ----------------------------------------------------
	// JPLAYER MUSIC PLAYER
	// ----------------------------------------------------
	
	// Display the playlist
	$("#playList ul.hidden").show();
	
	function musicPlayer(index, numPlayers, mp3File, oggFile) {
		var preload = "none";
		if(index == 1) { 
			index = "";
			preload = "auto";
		}
		$("#musicPlayer"+index).jPlayer({
			ready: function() {
				this.element.jPlayer("setFile", mp3File, oggFile);
			},
			swfPath: "uploads/thecats/js/jPlayer",
			oggSupport: true,
			graphicsFix: false,
			customCssIds: true,
			preload: preload
		}).jPlayer("cssId", "pause", "jplayer_pause"+index).jPlayer("cssId", "play", "jplayer_play"+index).jPlayer("onSoundComplete", function() {
			if(index <= numPlayers) {
				//alert(index);
				$("#musicPlayer"+index++).jPlayer("playHead", 0); // Crossover the players here
			}
		});
	}
	
	var numMusicPlayers = $("#playList li").length;
	
	for(i = 1; i <= numMusicPlayers; i++) {
		var index = i;
		if(index == 1) {
			index = "";
		}
		var mp3File = $("#musicPlayer"+index+" > .fileLocation.mp3").text();
		var oggFile = $("#musicPlayer"+index+" > .fileLocation.ogg").text();
		musicPlayer(i, numMusicPlayers, mp3File, oggFile);
	}

	// Extra .click() event handlers to avoid both jPlayers playing together.
	function pausePlayer1(e) {
		$("#musicPlayer").jPlayer("pause");
	}
	function pausePlayer2(e) {
		$("#musicPlayer2").jPlayer("pause");
	}
	function pausePlayer3(e) {
		$("#musicPlayer3").jPlayer("pause");
	}
	function pausePlayer4(e) {
		$("#musicPlayer4").jPlayer("pause");
	}
	
	$("#jplayer_play").click(function() {
		pausePlayer2();
		pausePlayer3();
		pausePlayer4();
	});
	$("#jplayer_play2").click(function() {
		pausePlayer1();
		pausePlayer3();
		pausePlayer4();
	});
	$("#jplayer_play3").click(function() {
		pausePlayer1();
		pausePlayer2();
		pausePlayer4();
	});
	$("#jplayer_play4").click(function() {
		pausePlayer1();
		pausePlayer2();
		pausePlayer3();
	});
});
