/**********************************************************************************
 * 
 * Emerald Forest Software -- Media Player
 * 
 * Currently supports Flash Audio  via Aptana AFLAX
 * 
 * This is NOT doing streaming dang....   it downloads entire file first....
 * 
 * 
 * 
 * Tested with:  Firefox 3.x (Linux, Win2k),  ie6 (Win2k)
 * 
 * 
 * 2009-11-03	Created:  All code is original, some code is duplicated in other modules to avoid
 *           	dependencies.  All of it is written by Emerald Forest Computer Services unless 
 *           	otherwise specified.  Copyright 2009 by Emerald Forest Software.
 * 
 * 
 * Note: minor bug: that should be fixed someday....  but fix is difficult
 * 		  if you stop a download in progress and then play it with a non-zero index
 * 		  we don't properly reset the Stopped Status.  works okay if you start from zero.
 *        this is a cosmetic issue, the functionality works fine.
 * 
 */

var efsMediaPlayerCLASS = function(){};



/*jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj*/
//must be in global scope...  'this' is undefined when invoked

var DocMouseX = 0, DocMouseY = 0, DocMoveWithMouse = false; 

function DocTrackMouse(e)
{
	
	if (window.event) 
	{
		DocMouseX = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
		DocMouseY = window.event.clientY + document.documentElement.scrollTop  + document.body.scrollTop;
	}
	else 
	{
		DocMouseX = e.clientX + window.scrollX;
		DocMouseY = e.clientY + window.scrollY;
	}
	
	if (DocMoveWithMouse)
	{
		efsMediaPlayer.efsShowBusy('Move');  //@@@Someday: an array of vectors
	}

}


//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&


	efsMediaPlayerCLASS.prototype.bEnableSongMonitor = false;
	
	//callbacks
	efsMediaPlayerCLASS.prototype.CallBkReady = null;    //control is usable, flash is ready
	efsMediaPlayerCLASS.prototype.CallBkLoading = null;  //begin loading a song
	efsMediaPlayerCLASS.prototype.CallBkStarted = null;  //started playing a song
	efsMediaPlayerCLASS.prototype.CallBkProgress = null; //playing a song -- only happens if bEnableSongMonitor
	efsMediaPlayerCLASS.prototype.CallBkStopped = null;  //either stop requested or end of song
	

/*jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj*/
//this has been tested with ie6 and Firefox 2, 3.  should work with any modern browser
	
efsMediaPlayerCLASS.prototype.$id = function (id)
{
	return document.getElementById(id);
}


/*jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj*/

efsMediaPlayerCLASS.prototype.IsUndefined = function (aVar)
{
	return (typeof(aVar) == 'undefined' || aVar === null) ? true : false;
}


/*jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj*/

efsMediaPlayerCLASS.prototype.AddEvent = function (Action, Func, TgtObj, BubbleParam)
{
	
	if (!TgtObj) {TgtObj = document;}
	if (this.IsUndefined(BubbleParam)) {BubbleParam = false;} 

	if (document.attachEvent)
	{
		TgtObj.attachEvent('on'+Action, Func);
	}
	else if (document.addEventListener)
	{
		TgtObj.addEventListener(Action, Func, BubbleParam); 
	}
	else {return false;}

	return true;
};	


/*jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj*/


efsMediaPlayerCLASS.prototype.CreateAFLAXSoundPlayer = function (DivId)
{
	
	this.aflax = new AFLAX("lib/aflax/aflax.swf", true);
//	this.soundObj = null;
	this.bIsPlaying = false;
	this.bPlayerLoaded = false;
	this.LoadedSong = false;  
	this.bStarted = false;

	this.SongPos = 0;
	this.SongLength = 0;
	this.SongPct = 0;
	
	
	this.FlashObj = this.aflax.addFlashToElement(DivId, 1, 1, "#FFFFFF", this.MyName+".InitSound", true);

}

/*jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj*/


efsMediaPlayerCLASS.prototype.InitSound = function ()
{
//	AFLAX.trace("Here")
//	alert('InitSound');
	
	this.soundObj = new AFLAX.FlashObject(this.aflax, "Sound");

//	AFLAX.trace(soundObj.id);
	
	this.bPlayerLoaded = true;
	this.soundObj.exposeFunction("loadSound", this.soundObj);		
	this.soundObj.exposeFunction("start", this.soundObj);		
	this.soundObj.exposeFunction("stop", this.soundObj);		
	this.soundObj.exposeProperty("position", this.soundObj);


	this.soundObj.exposeProperty("bytesLoaded");		
	this.soundObj.exposeProperty("bytesTotal");		
	this.soundObj.exposeProperty("length");		
	this.soundObj.exposeProperty("url");		

//AFLAX.trace('go rocks');

	
	this.soundObj.mapFunction("addEventHandler");
	if (this.CallBkReady) {this.CallBkReady();}  //flash created and ready for use

}

/*jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj*/
/*
 * 
 * @@@Bug: If you stop a file while it is loading and then you continue loading it....
 * the stopped status does not get reset
 * 
 */

efsMediaPlayerCLASS.prototype._UpdateProgress = function ()
{
	if (!this.bIsPlaying) {return;}

	var t = this.soundObj.getPosition();
	
	this.PrevPos = this.SongPos;
	
	this.SongPos = t/1000;
	this.SongPct = (this.SongLength <= 0) ? 0 : Math.round(this.SongPos/this.SongLength*100);
	
	
	if (this.bStarted && this.PrevPos < this.SongPos)
	{
		this.efsShowBusy('Hide');
		this.bStarted = false;
		if (this.CallBkStarted) {this.CallBkStarted();}
	}
	
	if (this.bEnableSongMonitor) 
	{
		setTimeout(this.MyName+"._UpdateProgress()", 1000);
		
		if (this.CallBkProgress) {this.CallBkProgress(this.SongPos, this.SongLength, this.SongPct);}
	}
	
	if (!this.bStarted  && this.PrevPos == this.SongPos)
	{
		//@@@Review: this is not entirly reliable, can fail in several ways. but is usful as a general indication
		if (((this.SongPos/this.SongLength*100) > 99 || this.SongPct == 0) || (!this.bStarted && this.StoppedCnt++ >= 5))
		{
			if (this.CallBkStopped) {this.CallBkStopped();}
		}
	}
	else
	{
		this.StoppedCnt = 0;
	}
}

/*jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj*/

efsMediaPlayerCLASS.prototype.PlaySound = function (Pos, SongFile, Length)
{
	if (!this.bPlayerLoaded)
	{
		alert('Flash Player has not loaded yet, please try again');
		return false;
	}
	
	if (this.bIsPlaying) 
	{
		//prevent multiple streams
		this.soundObj.stop();
		this.bIsPlaying = false;
	}  

	if (Pos < 0) {Pos = 0;}
	this.SongPos = Pos;
	
	this.bStarted = true;
			
	if (!this.IsUndefined(SongFile))  // && SongFile != this.LoadedSong
	{
		this.efsShowBusy('Show');
		if (this.CallBkLoading) {this.CallBkLoading();}
		this.soundObj.addEventHandler("onLoad", this.MyName+".PlaySound("+this.SongPos+")");
		this.soundObj.loadSound(SongFile, false);
		this.SongLength = this.IsUndefined(Length) ? -1 : Length;
		this.PrevPos = this.SongPos + 0.1;
	}
	else
	{
		this.soundObj.start(Pos);
		this.PrevPos = this.SongPos + 0.1;
		
		this.bIsPlaying = true;
		setTimeout(this.MyName+"._UpdateProgress()", 100);
	}
}

/*jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj*/


efsMediaPlayerCLASS.prototype.StopSound = function ()
{
	this.soundObj.stop();
	this.bStarted = false;
	this.PrevPos = this.SongPos;
	this.efsShowBusy('Hide');
	
	this._UpdateProgress();  //find out our current position so that Resume is possible
	this.bIsPlaying = false;
	if (this.CallBkStopped) {this.CallBkStopped();}
}


/*jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj*/

efsMediaPlayerCLASS.prototype.efsShowBusy = function ($StartStop)
{
	if (this.oBusyDisplay)
	{

		if ($StartStop == 'Show') 
        {
			this.oBusyDisplay.style.display = 'block';
        }
		
		 
		if ($StartStop == 'Show' || $StartStop == 'Move') 
        {
        	this.oBusyDisplay.style.left = DocMouseX + 15 + 'px';
        	this.oBusyDisplay.style.top  = DocMouseY - 45 + 'px';
        }
        else if ($StartStop == 'Hide') 
        {
        	this.oBusyDisplay.style.display = 'none';
        }		
	}

}

/*jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj*/

efsMediaPlayerCLASS.prototype.AddBusyDisplay = function ()
{
	var BusyId = 'dvBusy';
	this.oBusyDisplay = this.$id(BusyId);  
	
	
	if (!this.oBusyDisplay)
	{
		//can create the div here
		return;
	}
	
	this.AddEvent('mousemove', DocTrackMouse, document);
	//DocMoveWithMouse = true;
	
}

//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

var efsMediaPlayer = new efsMediaPlayerCLASS;
efsMediaPlayer.MyName = 'efsMediaPlayer';

