var currentPosition = 0;

function registerPlayers() {
	for(i=1;i<=myPlayList.length;i++) {
		currentPosition = i;
		if(document.getElementById("jquery_jplayer"+currentPosition)) {
			$("#jquery_jplayer"+currentPosition).jPlayer({
				ready: function() {
					this.element.currentPosition = parseInt(this.element.jPlayer("getData", "id").substr(14));
					this.element.jPlayer("setFile", myPlayList[this.element.currentPosition-1].mp3);
					myPlayList[this.element.currentPosition-1].jqueryPlayTime = $("#jplayer_play_time"+this.element.currentPosition);
					myPlayList[this.element.currentPosition-1].jqueryTotalTime = $("#jplayer_total_time"+this.element.currentPosition);
				},
				customCssIds: true,
				nativeSupport: false,
				swfPath: "http://karabouvision.com/scripts/"
			})
			.jPlayer("cssId", "play", "jplayer_play"+currentPosition)
			.jPlayer("cssId", "pause", "jplayer_pause"+currentPosition)
			.jPlayer("cssId", "stop", "jplayer_stop"+currentPosition)
			.jPlayer("cssId", "loadBar", "jplayer_load_bar"+currentPosition)
			.jPlayer("cssId", "playBar", "jplayer_play_bar"+currentPosition)
			.jPlayer("cssId", "volumeMin", "jplayer_volume_min"+currentPosition)
			.jPlayer("cssId", "volumeMax", "jplayer_volume_max"+currentPosition)
			.jPlayer("cssId", "volumeBar", "jplayer_volume_bar"+currentPosition)
			.jPlayer("cssId", "volumeBarValue", "jplayer_volume_bar_value"+currentPosition)
			.jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
				myPlayList[this.element.currentPosition-1].jqueryPlayTime.text($.jPlayer.convertTime(playedTime));
				myPlayList[this.element.currentPosition-1].jqueryTotalTime.text($.jPlayer.convertTime(totalTime));
			});
			
			$("#jplayer_play"+currentPosition).click(function() {
				pauseAllAndPlay(this.id.substr(12));
			});
			$("#jplayer_load_bar"+currentPosition).click(function() {
				pauseAllAndPlay(this.id.substr(16));
			});
			$("#jplayer_play_bar"+currentPosition).click(function() {
				pauseAllAndPlay(this.id.substr(16));
			});
		}
	}
}

function pauseAllAndPlay(id) {
	for(i=1;i<=myPlayList.length;i++)
		$("#jquery_jplayer"+i).jPlayer("pause");
	$("#jquery_jplayer"+id).jPlayer("play");
}

function registerPlayersUI() {
	for(i=1;i<=myPlayList.length;i++)
		if(document.getElementById("jplayer_inst"+i))
			$("#jplayer_inst"+i).html('<div id="jquery_jplayer'+i+'"></div><div class="jp-playlist-player"><div class="jp-interface"><ul class="jp-controls"><li><a href="#" id="jplayer_play'+i+'" class="jp-play" tabindex="1">play</a></li><li><a href="#" id="jplayer_pause'+i+'" class="jp-pause" tabindex="1">pause</a></li><li><a href="#" id="jplayer_stop'+i+'" class="jp-stop" tabindex="1">stop</a></li><li><a href="#" id="jplayer_volume_min'+i+'" class="jp-volume-min" tabindex="1">min volume</a></li><li><a href="#" id="jplayer_volume_max'+i+'" class="jp-volume-max" tabindex="1">max volume</a></li></ul><div class="jp-progress"><div id="jplayer_load_bar'+i+'" class="jp-load-bar"><div id="jplayer_play_bar'+i+'" class="jp-play-bar"></div></div></div><div id="jplayer_volume_bar'+i+'" class="jp-volume-bar"><div id="jplayer_volume_bar_value'+i+'" class="jp-volume-bar-value"></div></div><div id="jplayer_play_time'+i+'" class="jp-play-time"></div><div id="jplayer_total_time'+i+'" class="jp-total-time"></div></div></div>');
}
