/**
 * Handles the playing of songs in an album
 *
 * Similar to mfns.js but with changes to what's being updated when
 * a track is playing
 */
 var browserReady = false;//is the browser ready to start handling events?
 var curPlayBtn = 0;//the button relating to the currently playing song
 var currentSongID = 0;
 var playbackMsgEl;//holds a reference to the element that currently displays the playback status
 
 
 $(function(){
	$btn = $("div.asong");//the play/pause button being clicked
	
	$btn.click(function(evt){
		$this = $(this);
		$txtID = $this.parent().attr("id");
		if($txtID){
			$sepInd = $txtID.indexOf("-");
			if($sepInd != -1){
				$id = $txtID.substring($sepInd+1);
				$dur = $this.siblings("div.al-info").text();
				
				//also get the album id since skits have an id of 0
				$aid = $txtID.substring(2, $sepInd);
				
				handleMediaBtn($this, $id, $aid, $dur);
				
				return false;
			}
		}
	});
	
	/**
	 * Sets the state of the button to either playing or pause
	 * @param {Object} $el The button
	 * @param Number $id The ID of the song to play/pause
	 * @param Number $aid The album ID
	 * @param boolean $active should the button be set to play?
	 */
	var handleMediaBtn = function($el, $id, $aid, $dur) {
		$cl = $el.attr('class');
		
		if($el){
			if($cl.indexOf('alplay') != -1){//if the button is already in a paused state
				if(curPlayBtn){//if there was a song previously playing, stop it and change its state
					curPlayBtn.removeClass('alpause').addClass('alplay');
				}else{
					stop();
				}
				
				$el.removeClass('alplay').addClass('alpause');
				
				curPlayBtn = $el;
				play($id, $aid);
			}else if($cl.indexOf('alpause') != -1){//if the button is already in a clicked state
				$el.removeClass('alpause').addClass('alplay');
				stop();
			}
		}
	}
 });
 
 
 
 
 
 
 
 
 
 
 
 /**
 * Other functions
 */
var browser_init = function(){
	browserReady = true;
}

var getMovieRef = function(movieName){
	if (navigator.appName.indexOf("Microsoft") != -1) {
		 return window[movieName];
	 } else {
		 return document[movieName];
	 }
}

var isReady = function(){
	return browserReady;
}

var play = function(id, aid){
	var player = getMovieRef("alplayer");
	if(player){
		try{
			if(currentSongID)//stop any previously playing song
				stop();
			
			player.playSong(id, aid);
			currentSongID = id;
			playbackMsgEl = $('div#as'+$aid+'-'+$id).find("div.al-info");
		}catch(e){
			displayError(e.toString());//"A problem occured in playing songs within your browser. It is either the version of flash player installed in your browser is not capable of playing songs or it was not even installed at all.\n Upgrade your flash player by installing the latest version at <a href='http://www.macromedia.com/go/getflashplayer'>http://www.macromedia.com/go/getflashplayer</a>"
		}
	}else{
		displayError("A problem occured in playing the specified song. Please refresh the page and try again");
	}
}

var stop = function(){
	try{
		var player = getMovieRef("alplayer");
		if(player){
			player.stopSong(currentSongID);
		}else
			displayError("A problem occured in stopping the specified song. Please refresh the page and try again");
	}catch(e){
		displayError("A problem occured in playing songs within your browser. It is either the version of flash player installed in your browser is not capable of playing songs or it was not even installed at all.\n Upgrade your flash player by installing the latest version at <a href='http://www.macromedia.com/go/getflashplayer'>http://www.macromedia.com/go/getflashplayer</a>");
	}
}

var displayMsg = function(msg){
	$(function(){
		$.facebox(msg);
	});
}

var displayError = function(err){
	$(function(){
		$.facebox("<div class='msg-err'>"+err+"</div>");
	});
}

function playbackMsg(msg){
	//console.log('playbackMsg called: '+msg);
	$(document).ready(function(){
		if(playbackMsgEl)
			playbackMsgEl.text(msg);
	});
}

function showPlayBtn(){
	if(curPlayBtn){//if there was a song previously playing, stop it and change its state
		curPlayBtn.removeClass('alpause').addClass('alplay');
	}
}

/**
 * Utility method for prinitng msgs
 * @param String msg The message to print
 */
var debug = function(msg){
	$(function(){
		//$.facebox("<div class='err1'>msg</div>");
		console.log(msg);
	});
}
