Ext.namespace("web.base.audio.player");

////////////////////////////////////////////////
// Called by the EP_onEvent(evt) function
// (located in 'ep_player.js') when the status of 
// the player changes.
function EP_eventHandler(evt) 
{
	// This function may be ignored.  Listen for ephonicEvent instead. 
}


/**
 * For more information on using the e-phonic flash player, see
 * http://www.e-phonic.com/mp3player/documentation/#javascript
 * This object requires the swfobject.js and ep_player.js to be included in
 * the resulting html sent to the browser.  Download these files from the
 * e-phonic website for free.
 * NOTE:  Pass in the flashPlayerLocation (path to ep_player.swf) if it is not in the
 * same directory as the html file executing this Object.
 */
web.base.audio.player.ephonic = Ext.extend(Ext.Panel, {
	initComponent: function(){
		
		// defaults go here:
        var defConfig = {
            border: false,
			flashPlayer: {
				backgroundColor: "#FFFFFF",
				location: 'ep_player.swf', // IMPORTANT!  Change if not in same location as the html executing this.
				height: 200,
				id: Ext.id(),
				isLoaded: false,
				version: "9",
				width: 200
			},
			/**
		 	* See http://kb2.adobe.com/cps/127/tn_12701.html
		 	* for a list of parameters for the flash object.
		 	*/
			params:  {
				allowscriptaccess: "always"
			},
            plain: true,
			variables: {
				autoplay: "false",
				buffertime: "1",
		 		notifyonevent: "true", // this parameter activates the use of event callbacks like MP3_PLAYING / MP3_PAUSED / MP3_STOPPED
		 		repeat: "false",
		 		shuffle: "false"
		 		// additional variables:
		 		// skin - path to skin xml file located in skins directory of downloaded player
		 		// playlist - path to a playlist xml file
			}
        };
        if(this.variables) Ext.applyIf(this.variables, defConfig.variables);
        if(this.params) Ext.applyIf(this.params, defConfig.params);
        if(this.flashPlayer) Ext.applyIf(this.flashPlayer, defConfig.flashPlayer);
        Ext.applyIf(this,defConfig);
        
        if(!this.id) this.id = Ext.getId();
        
        web.base.audio.player.ephonic.superclass.initComponent.call(this); 
		this.addEvents("ephonicEvent", "mp3Loading", "mp3Buffering", "mp3Playing", "mp3Previous", "mp3Next", "mp3Select", "mp3Paused", "mp3Complete", "mp3Stopped");
        EP_eventHandler = EP_eventHandler.createInterceptor(this.eventHandler, this);       
	},
	afterRender: function(){
        var wh = this.ownerCt.getSize();
        Ext.applyIf(this, wh);
		web.base.audio.player.ephonic.superclass.afterRender.call(this);	

		var so = new SWFObject(this.flashPlayer.location, this.flashPlayer.id, this.flashPlayer.width, this.flashPlayer.height, this.flashPlayer.version, this.flashPlayer.backgroundColor);
		
		// loop over the variables object:
		for(var variable in this.variables){
			so.addVariable(variable, this.variables[variable]);
		}
		
		for(var param in this.params){
			so.addParam(param, this.params[param]);
		}
		/*
		if(this.skin) so.addVariable("skin", this.skin);
		if(this.playlist) so.addVariable("playlist", this.playlist);
		if(this.autoplay) so.addVariable("autoplay", this.autoplay);
		if(this.shuffle) so.addVariable("shuffle", this.shuffle);
		if(this.repeat) so.addVariable("repeat", this.repeat);
		if(this.buffertime) so.addVariable("buffertime", this.buffertime);
		
		// this parameter activates the use of event callbacks
		// like MP3_PLAYING / MP3_PAUSED / MP3_STOPPED
		if(this.notifyonevent) so.addVariable("notifyonevent", this.notifyonevent);
		
		if(this.allowscriptaccess) so.addParam("allowscriptaccess", this.allowscriptaccess);
		*/
		so.write(this.id);
	},
	/**
	 * track - A track object having properties url, songTitle and author.
	 * @return The XML to be passed into the function EP_loadMP3. 
	 */
	buildPlaylistTrackXML: function(track){
		var loadCommand = "<location>" + track.url + "</location><creator>" + track.author + "</creator><title>" + track.songTitle + "</title>";
		return loadCommand;
	},
	doMP3Loading: function(evt){
		this.fireEvent("mp3Loading", evt);
	},
	doMP3Buffering: function(evt){
		this.fireEvent("mp3Buffering", evt);
	},
	doMP3Playing: function(evt){
		var t = this.getTrackInfo();
		this.fireEvent("mp3Playing", evt, t);
	},
	doMP3Prev: function(evt){
		this.fireEvent("mp3Previous", evt);
	},
	doMP3Next: function(evt){
		this.fireEvent("mp3Next", evt);
	},
	doMP3Select: function(evt){
		this.fireEvent("mp3Select", evt);
	},
	doMP3Paused: function(evt){
		this.fireEvent("mp3Paused", evt);
	},
	doMP3Complete: function(evt){
		this.fireEvent("mp3Complete", evt);
	},
	doMP3Stopped: function(evt){
		this.fireEvent("mp3Stopped", evt);
	},
	eventHandler: function(evt){
		switch(evt) {
			
			case 'MP3_LOADING':
				this.doMP3Loading(evt);
				break;
			case 'MP3_BUFFERING':
				this.doMP3Buffering(evt);
				break;
			case 'MP3_PLAYING':
				this.doMP3Playing(evt);
				// get track info
				/*
				var t = EP_getTrackInfo('ep_player');
	
				// write the event to the text-area in the HTML page
				EP_logEvent('playlistIndex: '+t.playlistIndex);
				EP_logEvent('location: '+t.location);
				EP_logEvent('title: '+t.title);
				EP_logEvent('annotation: '+t.annotation);
				EP_logEvent('creator: '+t.creator);
				EP_logEvent('info: '+t.info);
				EP_logEvent('image: '+t.image);
				EP_logEvent('album: '+t.album);
				EP_logEvent('trackNum: '+t.trackNum);
				EP_logEvent('link: '+t.link);
				EP_logEvent('----------------------------');
				*/
				break;			
			case 'MP3_PREV':
				this.doMP3Prev(evt);
				break;				
			case 'MP3_NEXT':
				this.doMP3Next(evt);
				break;			
			case 'MP3_SELECT':
				this.doMP3Select(evt);
				// write the event to the text-area in the HTML page
				// EP_logEvent("Select MP3 (#"+EP_getTrackInfo('ep_player').playlistIndex+") from playlist ...");
				break;
			case 'MP3_PAUSED':
				this.doMP3Paused(evt);
				break;
			case 'MP3_COMPLETE':
				this.doMP3Complete(evt);
				break;			
			case 'MP3_STOPPED':
				this.doMP3Stopped(evt);
				break;						
			default:
				break;
		}	
		
		this.fireEvent("ephonicEvent", evt);

		// permit the EP_eventHandler method to execute also
		// (if someone is already using their own EP_eventHandler and dropping
		// this object in, they may appreciate it).
		return true;
	},
	/**
	 * Returns an object containing the following info (from the playlist) from the selected track:
	 * playlistIndex
	 * location
	 * identifier
	 * title
	 * annotation
	 * creator
	 * info
	 * image
	 * album
	 * trackNum
	 * duration
	 * link
	 */
	getTrackInfo: function(){
		return EP_getTrackInfo(this.flashPlayer.id);
	},
	loadMP3: function(url, author, songTitle){
		var loadCommand = this.buildPlaylistTrackXML({url: url, author: author, songTitle: songTitle}); 
		EP_loadMP3(this.flashPlayer.id, loadCommand);
	},
	loadPlayList: function(playlistFile){
		EP_loadPlayList(this.flashPlayer.id, playlistFile);
	},
	next: function(){
		EP_next(this.flashPlayer.id);
	},
	pause: function(){
		EP_pause(this.flashPlayer.id);
	},
	play: function(){
		EP_play(this.flashPlayer.id);
	},
	playIndex: function(index){
		EP_playIndex(this.flashPlayer.id, index);
	},
	previous: function(){
		EP_prev(this.flashPlayer.id);
	},
	/**
	 * tracks is an array of tracks.  Each track object has a url, author, and sontTitle
	 * properties.
	 */
	setPlayList: function(tracks){
		var playListXML = "";
		for(var index = 0; index < tracks.length; index = index + 1){
			playListXML += "<track>" + this.buildPlaylistTrackXML(tracks[index]) + "</track>";
		}
		EP_setPlayListXML(this.flashPlayer.id, playListXML);
	},
	setPlayListXML: function(playListXML){
		EP_setPlayListXML(this.flashPlayer.id, playListXML);
	},
	stop: function(){
		EP_stop(this.flashPlayer.id);
	}
});
Ext.reg("audio.ephonic", web.base.audio.player.ephonic);
