// Author: Jim McCurdy, Face To Face Software, jmccurdy@facetofacesoftware.com
//
// A Javascript class that implements an MP3 client-side player using Flash
// This class wraps the functionality of PlayMP3Clip.swf
// EmbedFlash.js must be included before this file
// See http://www.adobe.com/cfusion/knowledgebase/index.cfm?id=50c96388 for more information about the sCrossDomainPolicyUrl
function CPlayMP3Clip(sSwfPath, sMP3Url, sCrossDomainPolicyUrl)
{
	var m_idFlashObject = null;
	var m_oPlayMP3Clip = null;
	var m_bFlashObjectFailure = false;
	var m_bFlashFunctionFailure = false;

	 //document.getElementById("spnPlayer4").innerHTML = 'CPlayMp31';
	 //document.getElementById("spnPlayer4").innerHTML = document.getElementById("spnPlayer4").innerHTML + "<br /> MP3 URL: " + sMP3Url;
	 //document.getElementById("spnPlayer4").innerHTML = document.getElementById("spnPlayer4").innerHTML + "<br /> sSwfPath: " + sSwfPath;

	// Create the Flash object that will host PlayMP3Clip.swf, and do all the MP3 playback
	var m_oFlash = new CEmbedFlash(sSwfPath, 'PlayMP3Clip.swf', '1'/*iWidth 150*/, '1'/*iHeight 600*/, '9'/*FlashVersion*/, true/*bUseExpressInstall*/);
	m_oFlash.addParam('allowScriptAccess', 'always');
	m_oFlash.addVariable('Url', sMP3Url);
	if (sCrossDomainPolicyUrl && sCrossDomainPolicyUrl != '')
	{
		// document.getElementById("spnPlayer").innerHTML = 'CPlayMp3 2';	
		m_oFlash.addVariable('PolicyUrl', sCrossDomainPolicyUrl);
	}
//	m_oFlash.addVariable('Start', '000');
//	m_oFlash.addVariable('End', '10000');
//	m_oFlash.addVariable('Loop', '0');
//	m_oFlash.addVariable('LoopMax', '4');
//	m_oFlash.addVariable('AutoPlay', '1');
//	m_oFlash.addVariable('Volume', '50');

	//document.getElementById("spnPlayer4").innerHTML = document.getElementById("spnPlayer4").innerHTML + '<br /> CPlayMp3 3 yayayayaayayay' + idFlashContainer;	
	// public
	this.Initialize = Initialize;
	function Initialize(idFlashContainer, idFlashObject)
	{
		// document.getElementById("spnPlayer").innerHTML = document.getElementById("spnPlayer").innerHTML + '<br /> CPlayMp3 4 ==' + idFlashContainer + '=== <br />';
		m_idFlashObject = idFlashObject;
		m_oFlash.Initialize(idFlashContainer, idFlashObject);
	}
	
	// public
	this.FlashIsUpToDate = FlashIsUpToDate;
	function FlashIsUpToDate()
	{
		return m_oFlash.IsUpToDate();
	}
	
	// public
	this.FlashIsInstalled = FlashIsInstalled;
	function FlashIsInstalled()
	{
		return m_oFlash.IsInstalled();
	}
	
	// public
	this.DoPlay = DoPlay;
	function DoPlay(bFromStart)
	{
		if (FlashOK() && FunctionOK('DoPlay', typeof(m_oPlayMP3Clip.DoPlay)))
		{
			// document.getElementById("spnPlayer4").innerHTML = document.getElementById("spnPlayer4").innerHTML + ' flash ok <br />';
			m_oPlayMP3Clip.DoPlay(bFromStart);
		}
		else 
		{
			//document.getElementById("spnPlayer4").innerHTML = document.getElementById("spnPlayer4").innerHTML + ' flash not ok <br />';
		}
	}

	// public
	this.DoStop = DoStop;
	function DoStop()
	{
		if (FlashOK() && FunctionOK('DoStop', typeof(m_oPlayMP3Clip.DoStop)))
			m_oPlayMP3Clip.DoStop();
	}

	// public
	this.SetUrl = SetUrl;
	function SetUrl(sUrl)
	{
		if (FlashOK() && FunctionOK('SetUrl', typeof(m_oPlayMP3Clip.SetUrl)))
			m_oPlayMP3Clip.SetUrl(sUrl);
	}

	// public
	this.SetPolicyUrl = SetPolicyUrl;
	function SetPolicyUrl(sCrossDomainPolicyUrl)
	{
		if (FlashOK() && FunctionOK('SetPolicyUrl', typeof(m_oPlayMP3Clip.SetPolicyUrl)))
			m_oPlayMP3Clip.SetPolicyUrl(sCrossDomainPolicyUrl);
	}

	// public
	this.SetStart = SetStart;
	function SetStart(iStart)
	{
		if (FlashOK() && FunctionOK('SetStart', typeof(m_oPlayMP3Clip.SetStart)))
		{
			m_oPlayMP3Clip.SetStart(iStart);
		}
	}

	// public
	this.SetEnd = SetEnd;
	function SetEnd(iEnd)
	{
		if (FlashOK() && FunctionOK('SetEnd', typeof(m_oPlayMP3Clip.SetEnd)))
			m_oPlayMP3Clip.SetEnd(iEnd);
	}

	// public
	this.SetLoop = SetLoop;
	function SetLoop(bLoop)
	{
		if (FlashOK() && FunctionOK('SetLoop', typeof(m_oPlayMP3Clip.SetLoop)))
			m_oPlayMP3Clip.SetLoop(bLoop);
	}

	// public
	this.SetLoopMax = SetLoopMax;
	function SetLoopMax(iLoopMax)
	{
		if (FlashOK() && FunctionOK('SetLoopMax', typeof(m_oPlayMP3Clip.SetLoopMax)))
			m_oPlayMP3Clip.SetLoopMax(iLoopMax);
	}

	// public
	this.SetAutoPlay = SetAutoPlay;
	function SetAutoPlay(bAutoPlay)
	{
		if (FlashOK() && FunctionOK('SetAutoPlay', typeof(m_oPlayMP3Clip.SetAutoPlay)))
			m_oPlayMP3Clip.SetAutoPlay(bAutoPlay);
	}

	// public
	this.SetVolume = SetVolume;
	function SetVolume(iVolume)
	{
		if (FlashOK() && FunctionOK('SetVolume', typeof(m_oPlayMP3Clip.SetVolume)))
			m_oPlayMP3Clip.SetVolume(iVolume);
	}

	// public
	this.GetLength = GetLength;
	function GetLength()
	{
		if (FlashOK() && FunctionOK('GetLength', typeof(m_oPlayMP3Clip.GetLength)))
			return m_oPlayMP3Clip.GetLength();
		return -1;
	}

	// public
	this.GetMetaData = GetMetaData;
	function GetMetaData(sField)
	{
		// sField must be one of the following:
		// "artist", "track", "comment", "songName", "album", "genre", "year"
		if (FlashOK() && FunctionOK('GetMetaData', typeof(m_oPlayMP3Clip.GetMetaData)))
			return m_oPlayMP3Clip.GetMetaData(sField);
		return '';
	}

	// private
	this.FlashOK = FlashOK;
	function FlashOK()
	{
			// document.getElementById("spnPlayer4").innerHTML = document.getElementById("spnPlayer4").innerHTML + ' fuck me <br />';

		if (!m_oPlayMP3Clip)
		{
			// document.getElementById("spnPlayer4").innerHTML = document.getElementById("spnPlayer4").innerHTML + ' fuck me2 <br />';
			m_oPlayMP3Clip = document.getElementById(m_idFlashObject);
		}
		if (m_oPlayMP3Clip)
		{
			// document.getElementById("spnPlayer4").innerHTML = document.getElementById("spnPlayer4").innerHTML + ' m_oPlayMP3Clip <br />';
			return true;
		}
		m_bFlashObjectFailure = true;
		return false;
	}

	// private
	this.FunctionOK = FunctionOK;
	function FunctionOK(sFunctionName, sFunctionType)
	{
		if (sFunctionType == 'function')
			return true;

		if (!m_bFlashFunctionFailure)
			alert('Could not access Flash function "' + sFunctionName + '"');
		
		m_bFlashFunctionFailure = true;
		return false;
	}
	// document.getElementById("spnPlayer").innerHTML = 'CPlayMp3 5';
}
