
/**************************************************
	JS/FLASH MP3 PLAYLIST
	JORDAN WILSON 2009
**************************************************/



var mp3Playlists = Array();				// TRACKS ALL MP3PLAYLIST

var mp3pl_Params = Array();				// DEFAULT PARAMETERS
 mp3pl_Params['finish'] = 'play all';			// DEFAULT ACTION WHEN A SONG HAS FINSIHED
 mp3pl_Params['isTest'] = false;				// IF FLASH TESTING MODE IS ON OR OFF
 mp3pl_Params['asFunc'] = 'AS_Mp3Playlist';		// THE FUNCTION CALLED BY ACTION SCRIPT
 mp3pl_Params['swfPath'] = '/includes/mp3playlist/mp3playlist.swf';	// PATH TO THE SWF OBJECT (RELATIVE TO HTML)
 mp3pl_Params['swfWd'] = 250;					// WIDTH OF THE SWF OBJECT
 mp3pl_Params['swfHg'] = 14;					// HEIGHT OF THE SWF OBJECT
 mp3pl_Params['bgColor'] = '#FFFFFF';			// BACKGROUND COLOR OF THE SWF OBJECT
 mp3pl_Params['volume'] = 0.5;					// DEFAULT PLAYER VOLUME (0.0 - 1.0)
 mp3pl_Params['limit'] = 3;						// LIMIT NUMBER OF SIMULTANEOUS DOWNLOADS
 mp3pl_Params['stream'] = '/play';				// THE GET VALUE PASSED TO 'stream.php' WHEN REQUESTING A MP3



// CALLED BY ACTION SCRIPT
AS_Mp3Playlist = function(call) {

	var action = call[0];
	var caller = call[1];
	var params = (call[2] != undefined ? call[2] : Array('no params'));
	//if ($('readout')) { $('readout').innerHTML += '<br /><br />' + action + ', ' + caller + ', ' + params.join(' - '); }
		
	// SEARCH FOR THIS MP3 PLAYLIST
	for (var i = 0; i < mp3Playlists.length; i++) {
		if (mp3Playlists[i].id == caller) {
			switch (action) {
				case 'onLimit': mp3Playlists[i][action](params[0]); break;
				case 'onOpen': mp3Playlists[i][action](params[0], params[1]); break;
				case 'onSecurityError': mp3Playlists[i][action](params[0], params[1]); break;
				case 'onHTTPStatus': mp3Playlists[i][action](params[0], params[1]); break;
				case 'onIOError': mp3Playlists[i][action](params[0], params[1]); break;
				case 'onFinish': mp3Playlists[i][action](params[0]); break;
			}
		}
	}
};



// THE MP3 PLAYLIST CLASS
mp3Playlist = function(cont, p) {
	// ADD THIS PLAYLIST TO ARRAY
	mp3Playlists.push(this);
	
	// OBJECTS
	var This = this;
	this.Controls = null;
	this.Play = null;
	this.Next = null;
	this.Prev = null;
	this.SwfObj = null;
	this.Option = null;
	this.Songlist = null;
	this.Tracks = Array();
	
	// VARIABLES
	this.id = cont;
	this.playFirst = true;

	// SETTINGS
	if (p == null) { p = Array(); }
	this.Params = Array();
	this.Params['stream'] = (p['stream'] != null) ? p['stream'] : mp3pl_Params['stream'];
	this.Params['finish'] = (p['finish'] != null) ? p['finish'] : mp3pl_Params['finish'];
	this.Params['isTest'] = (p['isTest'] != null) ? p['isTest'] : mp3pl_Params['isTest'];
	this.Params['asFunc'] = (p['asFunc'] != null) ? p['asFunc'] : mp3pl_Params['asFunc'];
	this.Params['swfPath'] = (p['swfPath'] != null) ? p['swfPath'] : mp3pl_Params['swfPath'];
	this.Params['swfWd'] = (p['swfWd'] != null) ? p['swfWd'] : mp3pl_Params['swfWd'];
	this.Params['swfHg'] = (p['swfHg'] != null) ? p['swfHg'] : mp3pl_Params['swfHg'];
	this.Params['bgColor'] = (p['bgColor'] != null) ? p['bgColor'] : mp3pl_Params['bgColor'];
	this.Params['volume'] = (p['volume'] != null) ? p['volume'] : mp3pl_Params['volume'];
	this.Params['limit'] = (p['limit'] != null) ? p['limit'] : mp3pl_Params['limit'];
	this.Params['swfSettings'] = (p['swfSettings'] != null) ? p['swfSettings'] : Array();
		
		
	
	
	// SENT FROM SWF OBJECT
		// IF MANY TRACKS ARE LOADING
		this.onLimit = function(file_id) {
			// KEEP FOCUS ON CURRENTLY PLAYING TRACK
			$$('#' + This.Songlist + ' .act').each( function(elem) { elem.removeClassName('act'); });
			$(file_id).addClassName('act');
		};
		
		// WHEN THE MP3 IS OPENED
		this.onOpen = function(file_id) { };
		
		// ON SECURITY ERROR
		this.onSecurityError = function(file_id, message) { };
		
		// ON HTTP STATUS UPDATE
		this.onHTTPStatus = function(file_id, message) { };
		
		// ON INPUT/OUTPUT ERROR
		this.onIOError = function(file_id, message) {
			// NOT GETTING AN IO ERROR BECAUSE IT IS GETTING A RESULT
			
			// DISPLACE THE TRACK FROM THE PLAYLIST
			//title = $(file_id).down('.title');
			//Event.stopObserving(title, 'click', This.onSelect);
			//title.innerHTML += ' <small>(file may be missing)</small>';
			//$(file_id).removeClassName('track').addClassName('error');
			//alert('Song not found. ' + message);
		};
		
		// WHEN SONG FINSIHED PLAYING
		this.onFinish = function(file_id) {	
			switch (This.Params['finish']) {
				// PLAY TRACK AGAIN
				case 'repeat':
					this.onPlay();
					break;
					
				// PLAY NEXT TRACK
				case 'play all':
					This.onNext('onPlayAll');
					break;
					
				// SWITCH ICON TO PLAY
				default: This.switchPlay('play');
			}
		};
		
		
		
		
		
	// MANAGE TRACKS
		// GET THE URL OF THE TRACK
		this.getSongURL = function(file_id) {
			start = (cont + '_mp3_').length;
			url = '/stream/song/' + file_id.substr(start) + This.Params['stream'];
			return url;
		};
		
		// ADD TRACK
		this.addTrack = function(id) {
			track = cont + '_mp3_' + id;
			This.Tracks.unshift(track);
			Event.observe($(track).down('.title'), 'click', This.onSelect);
		};
		
		// REMOVE TRACK
		this.removeTrack = function(id) {
			track = cont + '_mp3_' + id;
			index = null;
			for(var i = 0; i < This.Tracks.length; i++) {
				if (This.Tracks[i] == track) { index = i; }
			}
			if (index != null) { This.Tracks.splice(index, 1); }
		};
		
		// WHEN A TRACK IS SELECTED
		this.onSelect = function(e) {
			This.playFirst = false;
			
			// SWITCH ICON TO PAUSE
			This.switchPlay('paus');
			
			// SWITCH SELECTED TRACKS
			$$('#' + This.Songlist + ' .act').each( function(elem) { elem.removeClassName('act'); });
			track = e.target.up('.track');
			track.addClassName('act');
			
			// LOAD/PLAY TRACK
			swf = document.getElementById(This.SwfObj);
			swf.SendToAS('onSelect', track.id, This.getSongURL(track.id));
		};
		
		// ENABLE ALL TRACKS
		this.enableTracks = function() {
			// SEARCH FOR CLASS NAMES 'track' AND CHILD SPAN WITH CLASS NAME 'title'
			$$('#' + This.Songlist + ' .track .title').each( function(elem) {
				Event.observe(elem, 'click', This.onSelect);
				This.Tracks.push(elem.up().id);
			});
		};


	// PLAYER BUTTONS
		// SWITCH PLAY/PAUSE
		this.switchPlay = function(action) {
			// SWITCH ICON TO PAUSE
			if (action == 'paus') {
				This.Play.removeClassName('play-btn').addClassName('paus-btn');
				Event.stopObserving(This.Play, 'click', This.onPlay);
				Event.observe(This.Play, 'click', This.onPaus);
				
			// SWITCH ICON TO PLAY
			} else if (action == 'play') {		
				This.Play.removeClassName('paus-btn').addClassName('play-btn');
				Event.stopObserving(This.Play, 'click', This.onPaus);
				Event.observe(This.Play, 'click', This.onPlay);
			}
		};
		
		// ON PLAY
		this.onPlay = function(e) {
			// SWITCH ICON TO PAUSE
			This.switchPlay('paus');
			
			// IF NOTHING SELECTED USE 'onNext' TO PLAY FIRST
			active = $(This.Songlist).down('ul li.act');
			if (active == undefined) {
				This.onNext();
			
			// PLAY ACTIVE TRACK
			} else {
				swf = document.getElementById(This.SwfObj);
				swf.SendToAS('onPlay');
			}
		};
		
		// ON PAUSE
		this.onPaus = function(e) {
			// SWITCH ICON TO PLAY
			This.switchPlay('play');
			
			// PAUSE TRACK
			swf = document.getElementById(This.SwfObj);
			swf.SendToAS('onPaus');
		};
		
		// ON NEXT
		this.onNext = function(e) {
			//alert(e);
			// FIND ACTIVE TRACK, DEFAULT TO FIRST
			active = $(This.Songlist).down('ul li.act');
			track = $(This.Tracks[0]);
			
			// IF THERE IS AN ACTIVE TRACK, GET NEXT
			if (active != undefined) {
				$(This.Songlist).down('ul li.act').removeClassName('act');
				index = This.Tracks.indexOf(active.id) + 1;
				if (index < This.Tracks.length) {
					track = $(This.Tracks[index]);
				}
			}
			
			// IF 'PLAYING ALL' AND REACHED THE END
			if (e == 'onPlayAll' && This.Tracks.indexOf(track.id) == 0) {
				// SWITCH TO PLAY
				This.switchPlay('play');
				
			// ELSE, PLAY NEXT TRACK
			} else {
				track.addClassName('act');
				This.switchPlay('paus');
			
				// LOAD/PLAY TRACK
				swf = document.getElementById(This.SwfObj);
				swf.SendToAS('onSelect', track.id, This.getSongURL(track.id));
			}
		};
		
		// ON PREV
		this.onPrev = function(e) {
			// FIND ACTIVE TRACK, DEFAULT TO LAST
			active = $(This.Songlist).down('ul li.act');
			track = $(This.Tracks[This.Tracks.length - 1]);
			
			// IF THERE IS AN ACTIVE TRACK, GET PREV
			if (active != undefined) {
				$(This.Songlist).down('ul li.act').removeClassName('act');
				index = This.Tracks.indexOf(active.id);
				if (index > 0) {
					track = $(This.Tracks[index - 1]);
				}
			}
			track.addClassName('act');
			This.switchPlay('paus');
			
			// LOAD/PLAY TRACK
			swf = document.getElementById(This.SwfObj);
			swf.SendToAS('onSelect', track.id, This.getSongURL(track.id));
		};
		
	
	
	
	
	// SWF OBJECT / PLAYHEAD
		// EMBED THE FLASH OBJECT
		this.embedFlashObject = function() {
			$(This.SwfObj).update('<p>The Playlist Controls did not initiate. Requires Flash Player v10.<p>');
			
			/*  CUSTOM PARAMETERS
				// PLAYHEAD BUTTON
				btn_wd = 8
				btn_hg = 16
				btn_x = 0
				btn_y = 0
				btn_cFill = 0xFEFEFE
				btn_cLine = 0x006699
				btn_cFillOver = 0x006699
				btn_cLineOver = 0x003366
				
				// LOADING/TOTAL BARS
				bar_wd = 150
				bar_hg = 16
				bar_x = 0
				bar_y = 0
				bar_tFill = 0xCCCCCC
				bar_tLine = 0x333333
				bar_lFill = 0xC2E0DE
				bar_lLine = 0x006699
				
				// TIME CLOCK
				time_wd = 70
				time_hg = 16
				time_x = 160
				time_y = 0
				time_fontSize = 10
				time_cFont = 0x999999
				time_cFill = 0xFFFFFF
			*/
			
			flashvar = {};
			 flashvar.id = cont;
			 flashvar.test = This.Params['isTest'];
			 flashvar.func = This.Params['asFunc'];
			 flashvar.vol = This.Params['volume'];
			 flashvar.limit = This.Params['limit'];
			 for (var v in This.Params['swfSettings']) {
				flashvar[v] = This.Params['swfSettings'][v];
			 }
			params = {};
			 params.quality = "high";
			 params.bgcolor = This.Params['bgColor'];
			attri = {};
			 attri.id = This.SwfObj;
			 attri.name = This.SwfObj;
			swfobject.embedSWF(This.Params['swfPath'], This.SwfObj, This.Params['swfWd'], This.Params['swfHg'], "10.0.0", false, flashvar, params, attri);
		};
		
		
		
		
		
	// VOLUME CONTROLS
		// UPDATE VOLUME
		this.updateVolume = function(v) {
			Icon = $(cont).down('.volume-icon');
			
			// UPDATE ICON
			if (Icon) {
				pos = (v < 0.05 ? 0: (v < 0.25 ? -20: v < 0.50 ? -40: v < 0.75 ? -60: -80 ) );
				$(Icon).setStyle({backgroundPosition: (pos + 'px -40px')})
			}
			
			// UPDATE SWF OBJECT
			try {
				swf = document.getElementById(This.SwfObj);
				swf.SendToAS('onVolume', v);
			} catch(er) {}
		};
			
		this.initVolumeSlider = function() {
			Hand = $(cont).down('.volume-handle');
			Trac = $(cont).down('.volume-track');
			
			// INITIATE THE SLIDER
			if (Hand && Trac) {
				This.volSlider = new Control.Slider(Hand, Trac, {
					onSlide: function(v) { This.updateVolume(v); },
					onChange: function(v) { This.updateVolume(v); }
				});
				This.volSlider.setValue(This.Params['volume']);
			}
		};
		
		
		
		
		
	// PLAYER OPTIONS
		this.PlayOption = function(opt) {
			This.Params['finish'] = opt;
			Event.stopObserving(this.Option, 'click');
			
			switch(opt) {
				case 'play once':
					This.Option.update('<span class="opt-play-once"></span>');
					Event.observe(This.Option, 'click', function() { This.PlayOption('repeat'); } );
					break;
					
				case 'repeat':
					This.Option.update('<span class="opt-repeat"></span>');
					Event.observe(This.Option, 'click', function() { This.PlayOption('play all'); } );
					break;
					
				case 'play all':
					This.Option.update('<span class="opt-play-all"></span>');
					Event.observe(This.Option, 'click', function() { This.PlayOption('play once'); } );
					break;
			}
		};
		
	

	
	
	// ADD AND TARGET CONTROL ELEMENTS
	this.Controls = $(cont + '_controls').id;
	if (this.Controls) {
		$(this.Controls).down('.playhead').innerHTML = '<div id="' + cont + '_swfobj"></div>';
		this.SwfObj = $(cont + '_swfobj').id;
		this.Play = $(this.Controls).down('.play-btn');
		this.Next = $(this.Controls).down('.next-btn');
		this.Prev = $(this.Controls).down('.prev-btn');

		This.embedFlashObject();
		This.initVolumeSlider();
		if (this.Play) Event.observe(this.Play, 'click', this.onPlay);
		if (this.Next) Event.observe(this.Next, 'click', this.onNext);
		if (this.Prev) Event.observe(this.Prev, 'click', this.onPrev);
		
		this.Option = $(this.Controls).down('.play-option');
		if (this.Option) { this.PlayOption(this.Params['finish']); }
	}
	
	// ADD AND TARGET SONG LIST AND TRACKS
	this.Songlist = $(cont + '_songlist').id;
	if (this.Songlist) {
		this.enableTracks();
	}
};



/* END OF FILE
**************************************************/

