var CURRENT_CONSOLE_ID = null; // global variable to store current id console
var PREVIOUS_CONSOLE_ID = null; // global variable to store console ids
var RESET_CONSOLE = false;

$(document).ready(function(){  
	$("#jquery_jplayer").jPlayer(); // Instantiate the plugin 
	
	$("#jquery_jplayer").jPlayer("onSoundComplete", function() {
  		resetPlayingConsole(CURRENT_CONSOLE_ID); // reset controls for console that is currently playing
	});
	
	$("#jquery_jplayer").jPlayer("onProgressChange", function(lp,ppr,ppa,pt,tt) {
		var playedTime = $.jPlayer.convertTime(pt);
		var totalTime = $.jPlayer.convertTime(tt);
		
		if(ppa > 0){ // Show time played
			$('#' + CURRENT_CONSOLE_ID + ' .status').text(playedTime + ' of ' + totalTime); 
		} else { // Show percent loaded
			$('#' + CURRENT_CONSOLE_ID + ' .status').text('Loaded: ' + Math.floor(lp) + '%'); 
		}
	});
	
	$('.play_pause').click(function(event){
		PREVIOUS_CONSOLE_ID = CURRENT_CONSOLE_ID;
		CURRENT_CONSOLE_ID = getCurrentConsoleID($(this));
		
		// user clicked play button on new player console without stopping a previous console first
		if(PREVIOUS_CONSOLE_ID != null && CURRENT_CONSOLE_ID != PREVIOUS_CONSOLE_ID){
			resetPlayingConsole(PREVIOUS_CONSOLE_ID); // reset controls for console that is currently playing
			$('#' + PREVIOUS_CONSOLE_ID + ' .status').text(' ');
			$("#jquery_jplayer").jPlayer("stop");
		}
		
		var url = getLinkURL(CURRENT_CONSOLE_ID);
		
		if($("#jquery_jplayer").jPlayer("getData", "diag.isPlaying") == true){
			showPlayIcon($(this));
			$("#jquery_jplayer").jPlayer("pause");
		} else {
			showPausedIcon($(this));
			playSound(url);
		}
		event.preventDefault();		
	});
	
	// any stop button will stop the player
	$('.stop').click(function(event){
		resetPlayingConsole(CURRENT_CONSOLE_ID); // reset controls for console that is currently playing
		$("#jquery_jplayer").jPlayer("stop");
		event.preventDefault();
	});
});


function showPlayIcon(ele){
	ele.find('img').attr('src','graphics/btn-play.png');
}

function showPausedIcon(ele){
	//ele.text('[pause]');
	ele.find('img').attr('src','graphics/btn-pause.png');
}

function resetPlayingConsole(id){
	RESET_CONSOLE = true;
	var jqele = $('#' + id + ' .play_pause'); // find the currently active play_pause button
	showPlayIcon(jqele); // reset the currently active play_pause button
}

// get the id tag of the div containing the play button the user selected
function getCurrentConsoleID(ele){
	return  ele.parent().attr('id');
}

// get the mp3 file from the href of the corresopnding download anchor tag
function getLinkURL(ele){
	return $('#' + ele + ' .play_pause').attr('href');
}

function getLoadedURL(){
	return $("#jquery_jplayer").jPlayer("getData", "diag.src");
}

function playSound(url){
	if (url != getLoadedURL()){ // load new sound
		loadAndStartSound(url);
	} else {
		$("#jquery_jplayer").jPlayer("play"); // resume existing sound
	}
}

// clear previous file, set new file and play once loaded
function loadAndStartSound(url){
	$("#jquery_jplayer").jPlayer("clearFile").jPlayer("setFile",url).jPlayer("play");
}