/*======================================================================*\
|| #################################################################### ||
|| # PMNet Version 3.0												  #	||
|| # ---------------------------------------------------------------- # ||
|| # Copyright ©2003-2008 John Slone. All Rights Reserved. 			  #	||
|| # This file may not be redistributed in whole or significant part. # ||
|| #  																  # ||
|| #################################################################### ||
\*======================================================================*/

/* mp3player.js */

var MP3 = {
	
	mp3player : null,
	cdid : null,
	infobox : null,
	isplaying : false,
	currenttrack : 0,
	lastclick : null,
	title : null,
	// Define the start/stop buttons so we can have multiple sizes
	playbutton : '/images/icons/play.png',
	stopbutton : '/images/icons/stop.png',
	
	ready : function(play,info,cdid)
	{
		this.mp3player = play[0];
		this.infobox = info[0];
		this.cdid = cdid;
	},
	
	loadTrack : function(trackid,el,cdid)
	{
		this.cdid = cdid;
		if ( trackid != this.currenttrack )
		{
			// THey're switching tracks - change all play buttons to stopped
			$(".playbutton").attr('src', this.playbutton);
			url = '/audio/' + this.cdid + '/' + trackid + '.mp3';
			this.mp3player.SetVariable("method:setUrl", url);
			this.currenttrack = trackid;
			this.play(el);
		}
		else if ( this.isplaying )
		{
			this.stop(el);
		}
		return false;
	},

	play : function(el)
	{
		this.lastclick = el;
		
		// this.title = $("div[trackid='"+this.currentrack+"']").text();
		this.title = $(el).parents('tr').find('.tracktitle').text();
		
		$(".nowplaying", this.infobox).html(this.title);
		
		$(this.lastclick).find('img').attr('src',this.stopbutton);
		this.mp3player.SetVariable('method:play',"");
		this.mp3player.SetVariable('enabled',"true");
		this.isplaying = true;
	},
	
	stop : function()
	{
		$(this.lastclick).find('img').attr('src', this.playbutton);
		this.mp3player.SetVariable('method:stop',"");
		this.mp3player.SetVariable('enabled',"true");
		this.isplaying = false;
		this.currenttrack = null;
	},
	
	listener : {
		
		onInit : function()
		{
			this.position = 0;
		},
		
		onUpdate : function()
		{
			$(".position", this.infobox).html(MP3.convertTime(this.position));
			$(".duration", this.infobox).html(MP3.convertTime(this.duration));
			$(".loadpercent", this.infobox).html(this.bytesPercent);
			if ( this.bytesPercent == 100 && ( parseInt(this.position) >= parseInt(this.duration) ) )
			{
				// End of song
				MP3.stop();
			}
			
			if ( this.isPlaying != "true" )
			{
				MP3.stop();
			}
		}

	},
	
	convertTime : function(s)
	{
		// Convert milliseconds to typical hh:mm:ss format
		secs = s / 1000;
		ret = '';
		if ( (secs / 3600) >= 1 )
		{
			ret = Math.floor(secs/3600) + ':';
			secs = secs - Math.floor(secs/3600)*3600;
		}
		mins = Math.floor(secs / 60);
		ret += this.padNumber(mins,2) + ':';
		secs = secs - Math.floor(secs/60)*60;
		ret += this.padNumber(Math.floor(secs),2);
		return ret;
	},
	
	padNumber : function(s,num)
	{
		if ( s < 10 )
		{
			return '0' + s
		}
		else
		{
			return s;
		}
		ret = '';
		s = ''+s+'';
		if ( s.length < num )
		{
			for ( i = 0; i <= (s.length-num); i++)
			{
				ret += '0';
			}
		}
		ret += s;
		return ret;
	}
}

/*======================================================================*\
|| ####################################################################
|| # File Revision:    $Revision: 665 $
|| # Last Change:      $LastChangedDate: 2010-01-14 01:34:50 -0800 (Thu, 14 Jan 2010) $
|| # Last Commit By:   $Author: JDS $
|| ####################################################################
\*======================================================================*/
