/**
 * Handles the playing and stopping of songs
 */
var browserReady = false;//is the browser ready to start handling events?
var currentSongID = 0;
var curPlayBtn = 0;//the button relating to the currently playing song
var $action = '';

$(function(){
	$(document.body).append("<div id='pInfo'></div>");//holds the media playback info message
	
	$btn = $("a.md-btn");//the new songs play/pause button
	
	$btn.click(function(evt){
		$sb = $(this).parent().siblings("td.m_duration");
		$id = $sb.attr('id').substring(4);
		$dur = $sb.text();
			
		handleMediaBtn($(this), $id, $dur);
		playbackInfo(evt);
		
		return false;
	});
	
	$btn1 = $("a.p1-btn");//the top music downloads and fusiongreen recommends play/pause button
	$btn1.click(function(evt){
		$sb = $(this).parent().parent().siblings("div");
		$id = $sb.find("span[class=trName]").attr('id').substring(4);
		
		handleMediaBtn($(this), $id);
		playbackInfo(evt);
		
		return false;
	});
	
	$btn2 = $("a.p2-btn");//the top tens play/pause button
	$btn2.click(function(evt){
		$sb = $(this).parent().parent().siblings("div.ttNo");
		$dur = $sb.text();
		$id = $sb.attr('id').substring(4);
		
		handleMediaBtn($(this), $id, $dur);
		playbackInfo(evt);
		
		return false;
	});
	
	/**
	 * Sets the state of the button to either playing or pause
	 * @param {Object} $el The button
	 * @param boolean $active should the button be set to play?
	 */
	var handleMediaBtn = function($el, $id) {
		$cl = $el.attr('class');
		
		if($el){
			if($cl.indexOf('play') != -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('pause').addClass('play');
				}else{
					stop();
				}
				
				$el.removeClass('play').addClass('pause');
				
				curPlayBtn = $el;
				play($id);
			}else if($cl.indexOf('pause') != -1){//if the button is already in a clicked state
				$el.removeClass('pause').addClass('play');
				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){
	var player = getMovieRef("fgplayer");
	if(player){
		try{
			if(currentSongID)//stop any previously playing song
				stop();
			
			player.playSong(id);
			currentSongID = id;
		}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("Cannot play the song specified");
	}
}

var stop = function(){
	try{
		var player = getMovieRef("fgplayer");
		if(player){
			player.stopSong(currentSongID);
		}else
			displayError("Cannot stop the song specified");
	}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>");
	});
}

/**
 * Displays the playback message to the user
 * @param {Object} evt
 */
var playbackInfo = function(evt){
	//$('div#pInfo').css({"left": (evt.pageX-120)+"px", "top": evt.pageY+"px"}).fadeIn('slow');//position the msg box
	$('div#pInfo').css({"left": (evt.pageX-120)+"px", "top": evt.pageY+"px"}).fadeIn('fast');//position the msg box
}

function playbackMsg(msg){
	//console.log('playbackMsg called: '+msg);
	$(document).ready(function(){
		//$('div#pInfo').css({"left": (x-120)+"px", "top": y+"px"}).html(msg).fadeTo('fast', 0.7);
		$('div#pInfo').empty().html(msg).fadeTo('fast', 0.8);
	});
}

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

/**
 * 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);
	});
}


