var m_sLastError = '';

function OnClipPlayFullSong()
{
	SetLoop();
	PlayMP3Clip.SetStart(0);
	PlayMP3Clip.SetEnd(0);
	PlayMP3Clip.DoPlay(true/*bFromStart*/);
}

function OnClipPlayFromStart()
{
	SetLoop();
	SetStartEnd();
	PlayMP3Clip.DoPlay(true/*bFromStart*/);
}

function OnClipPlay()
{
	//document.getElementById("spnPlayer2").innerHTML = document.getElementById("spnPlayer2").innerHTML + ' onclipplay ';
    document.getElementById("spnPlayer2").innerHTML = document.getElementById("spnPlayer2").innerHTML + ' onclipplay ';
	
	//SetLoop();
	SetStartEnd();
	//PlayMP3Clip.DoPlay(false/*bFromStart*/);
    PlayMP3Clip.DoPlay(true/*bFromStart*/);
}

function OnClipStop()
{
	PlayMP3Clip.DoStop();
}

function OnClipLoop(oLoopCheckbox)
{
	SetLoop(oLoopCheckbox);
}

function OnClipVolume(iVolumePercent)
{
	PlayMP3Clip.SetVolume(iVolumePercent);
}

// A CMobilizeClip callback; called OnMouseUp by the MobilizeClip UI
function OnClipChanged()
{
	SetStartEnd();
}

function SetLoop(oLoopCheckbox)
{
	if (!oLoopCheckbox)
		oLoopCheckbox = document.getElementById('idClipLoop');
	var bLoop = oLoopCheckbox.checked;
	PlayMP3Clip.SetLoop(bLoop);
}

function SetStartEnd()
{
	// Read the hidden variables from the MobilizeClip UI, and pass them on to the PlayMP3Clip object

	
	 var iStart = document.getElementById('lblUpdateBeginTimeFlash').value;
	 var iEnd = document.getElementById('lblUpdateEndTimeFlash').value;
	
	 if(navigator.appName.toLowerCase() == 'microsoft internet explorer')
     {
        document.getElementById("spnPlayer2").innerHTML = document.getElementById("spnPlayer2").innerHTML + 'IE';
        iStart = parseInt(iStart,10);
        iEnd =   parseInt(iEnd,10);
        //iStart = iEnd;
        //iEnd = (parseInt(document.getElementById('lblUpdateEndTimeFlash').value,10) - parseInt(document.getElementById('lblUpdateBeginTimeFlash').value,10)  ) + parseInt(document.getElementById('lblUpdateEndTimeFlash').value,10);
     }
    else
         document.getElementById("spnPlayer2").innerHTML = document.getElementById("spnPlayer2").innerHTML + 'NOT IE';
         
	 //var iStart = 5000;
	 //var iEnd = 10000;
	 
	 
	document.getElementById("spnPlayer2").innerHTML = document.getElementById("spnPlayer2").innerHTML + ' iStart: ' + iStart + ' iEnd: ' + iEnd;

	
	//var iStart = document.getElementById('idClipStart').value;
	//var iEnd = document.getElementById('idClipEnd').value;
	
	
	PlayMP3Clip.SetStart(iStart);
	PlayMP3Clip.SetEnd(iEnd);
}

// A PlayMP3Clip.swf callback
function OnLoadProgress(iLoadPercentage, iClipLength)
{
	var object = document.getElementById('idClipLength');
	if (object)
	{
		var sMsg = FormatTime(iClipLength, false/*bShowTenths*/) + (iLoadPercentage != 100 ? ' (' + iLoadPercentage + '%)' : '');
		object.innerHTML = sMsg + ' ' + m_sLastError;
//j		setTimeout( function() { object.innerHTML = sMsg; }, 0);
	}
}

// A PlayMP3Clip.swf callback
function OnPlayProgress(iPlayPosition, bEnd)
{
	var object = document.getElementById('idClipPlayPosition');
	if (object)
	{
		var sMsg = FormatTime(iPlayPosition, true/*bShowTenths*/);
		object.innerHTML = sMsg;
	}

	// Update the user interface
	MobilizeClip.SetPlayPosition(iPlayPosition);
}

// A PlayMP3Clip.swf callback
// See http://www.adobe.com/cfusion/knowledgebase/index.cfm?id=50c96388 if MetaData (ID3 tag) is undefined
function OnMetaDataAvailable()
{
	var object = document.getElementById('idClipMetaData');
	if (object)
	{
		var sMsg = '';
		var sData = '';
		sData = PlayMP3Clip.GetMetaData('artist');
		if (sData != '') sMsg += '  artist=' + sData 
		sData = PlayMP3Clip.GetMetaData('track');
		if (sData != '') sMsg += '  track=' + sData 
		sData = PlayMP3Clip.GetMetaData('comment');
		if (sData != '') sMsg += '  comment=' + sData 
		sData = PlayMP3Clip.GetMetaData('songName');
		if (sData != '') sMsg += '  song=' + sData 
		sData = PlayMP3Clip.GetMetaData('album');
		if (sData != '') sMsg += '  album=' + sData 
		sData = PlayMP3Clip.GetMetaData('genre');
		if (sData != '') sMsg += '  genre=' + sData 
		sData = PlayMP3Clip.GetMetaData('year'); 
		if (sData != '') sMsg += '  year=' + sData 

		object.innerHTML = sMsg;
	}
}

// A PlayMP3Clip.swf callback
function OnError(sError)
{
	if (m_sLastError != '') m_sLastError += '<br>';
	m_sLastError += sError;
	var object = document.getElementById('idClipLength');
	if (object)
		object.innerHTML = m_sLastError;
}

function FormatTime(iMilliSeconds, bShowTenths)
{
	iMilliSeconds = Math.round(iMilliSeconds);
	var iSeconds = Math.floor(iMilliSeconds / 1000);
	iMilliSeconds %= 1000;
	var iMinutes = Math.floor(iSeconds / 60);
	iSeconds %= 60;
	var sMinutes = (!iMinutes ? '0' : iMinutes);
	var sSeconds = (iSeconds < 10 ? '0' + iSeconds : iSeconds);
	var iTenths = Math.floor(iMilliSeconds / 100);
	var sTenths = (bShowTenths ? '.' + iTenths : '');
	return sMinutes + ':' + sSeconds + sTenths;
}

function addLoadEvent(fnOnload)
{
	if (typeof window.onload != 'function')
		window.onload = fnOnload;
	else
	{
		var fnOldOnload = window.onload;
		window.onload = function()
		{
			fnOldOnload();
			fnOnload();
		}
	}
}

var MobilizeClip = null;
var PlayMP3Clip = null;

function Initialize()
{
	//if (!MobilizeClip) || !PlayMP3Clip)
//	{
//document.getElementById("spnPlayer2").innerHTML = document.getElementById("spnPlayer2").innerHTML + ' I am returning';
//		return;
//	}
//document.getElementById("spnPlayer2").innerHTML = document.getElementById("spnPlayer2").innerHTML + ' IUnitialize2 ';

	//MobilizeClip.Initialize('idClipDisplayContainer', 'idClipStart', 'idClipEnd');
	PlayMP3Clip.Initialize('idPlayMP3ClipContainer', 'idPlayMP3ClipFlashObject');

	//fixPage();
}

/**
 * Fixes per UI or Backend requirements
 * 1. there's an annoying artifact from the object or embed tag generated by the
 * flash player that's fixable by this function by increasing it's width
 * 2. updates the ui when flash isn't installed
 * @return true if flash installed on page, false otherwise
 */
function fixPage()
{
	try
	{
		var bFlashIsInstalled = PlayMP3Clip.FlashIsInstalled();
		var bFlashIsUpToDate = PlayMP3Clip.FlashIsUpToDate();
		var bFlashOK = (bFlashIsInstalled && bFlashIsUpToDate);

		YAHOO.util.Dom.setStyle('idClipInfoFlashOK', 'display', (bFlashOK ? 'block' : 'none'));
		YAHOO.util.Dom.setStyle('idClipInfoFlashNotOK', 'display', (!bFlashOK ? 'block' : 'none'));
		YAHOO.util.Dom.setStyle('idClipInfoNoFlash', 'display', (!bFlashIsInstalled ? 'block' : 'none'));
		YAHOO.util.Dom.setStyle('idClipInfoOldFlash', 'display', (bFlashIsInstalled && !bFlashIsUpToDate ? 'block' : 'none'));
	}
	catch(e)
	{
		console.log( 'error in updating ui for fixPage(): ' + e.message );
	}
}

/**
 * constructs the mp3 clipper using the args object
 * @param Object args Contains the following indexes
 *     + 'swfPath' : string, the path th the PlayMP3Clip.swf file
 *     + 'previewUrl' : string, the full url to the mp3 file
 *     + 'crossDomainPolicyUrl' : string, the full url to the crossdomain XML policy file
 *     + 'imagesUrl' : string, the full url to the base dir of clipper images
 *     + 'trackSeconds' : track length in seconds
 */
function FactoryMP3MobilizeCrop(args)
{
// document.getElementById("spnPlayer").innerHTML = 'FactoryMP3MobilizeCrop(args); ' + args.swfPath + '; ' +  args.previewUrl + '; ' + args.crossDomainPolicyUrl + '; ' + args.imagesUrl + '; ' + args.trackSeconds;

// document.getElementById("spnPlayer2").innerHTML = 'here dammit';

    var swfPath = args.swfPath;
    var previewUrl = args.previewUrl;
    var crossDomainPolicyUrl = args.crossDomainPolicyUrl;
    var imagesUrl  = args.imagesUrl;
    var trackSeconds = args.trackSeconds;
    
    PlayMP3Clip = new CPlayMP3Clip(swfPath, previewUrl, crossDomainPolicyUrl);

// document.getElementById("spnPlayer2").innerHTML = PlayMP3Clip;

   /*
    MobilizeClip = new CMobilizeClip(
        trackSeconds * 1000, // length of the clip in milliseconds
        30000,				// length of the maximum selection in milliseconds
        imagesUrl + 'rt_clip/Background.gif',
        500, 70,			// width and height of the background image
        imagesUrl + 'rt_clip/HandleLeft.gif',
        imagesUrl + 'rt_clip/HandleRight.gif',
        16, 90,				// width and height of the handle image
        6,					// width of the overhang on each side of the handle in the image
        2, 80, 65, 60,		// width of the selection border, and the width of the length, start, and end labels
        OnClipChanged);
    */
	//if (window.isLoaded)
	//{
		Initialize(); 
		// document.getElementById("spnPlayer2").innerHTML = document.getElementById("spnPlayer2").innerHTML + ' first if ';
	//}
	//else
	//{	
	//addLoadEvent(Initialize); document.getElementById("spnPlayer2").innerHTML = 'second if ';
	//}
}
