//
// Global variables
var sId = 'mediastatus';
var	pId = 'mediaplayer';
var albumList = new Array;
var pRemoteWaiting = false;
var pRemotePlayId;
var pRemotePlayURI;
var pRemotePlayLoop;


function FLE_mediaInit(MediaPlayerId,TextMediaStatusId,DefaultMedia)
{
	var result;
	
	if (TextMediaStatusId!=null) sId = TextMediaStatusId;
	if (MediaPlayerId!=null) pId = MediaPlayerId;

	var x = document.getElementById(pId);
	
	if (!x) { pId=null; return 1; }

	x.uiMode = "none";
	x.settings.autoStart  = true;
	if (DefaultMedia) x.URL = DefaultMedia;
	
	FLE_updateMediaReport(10);
}

function playRemotely()
// wait until the page has completely loaded before loading media
{
	if (document.readyState!='complete' || !document.hasFocus()) {
		pRemotePlayId = setTimeout("playRemotely();","500");
		return 1;
	}
	
	var x = document.getElementById(pId);
	if (!x) { pId=null; return 1; }
	
	x.URL=pRemotePlayURI;				
	if (pRemotePlayLoop.toLowerCase()=='repeat') x.settings.playCount = 100;
	x.controls.play();
	
}

function FLE_mediaPlay(mediaURI)
// Global function to play media file
//
// mediaURI - URI to media file
// loopTrack - endlessly loop track if "repeat"
// playAfterPageLoad - play only if page has completely loaded if "load"
// playPersistent - check again if page has completely loaded if "wait"
{
	var docStatus = document.readyState;
	
	if (!pId) return 1;
	
	var loopTrack = arguments[1] || '';
	playAfterPageLoad = arguments[2];
	playPersistent = arguments[3];
	
	if (pRemoteWaiting==true) clearTimeout(pRemotePlayId);

	if (playAfterPageLoad=='load' && docStatus!='complete')	{
		if (playPersistent=='wait') {
			pRemotePlayURI = mediaURI;
			pRemotePlayLoop = loopTrack;
			pRemotePlayId = setTimeout("playRemotely();","500");
		}
		return 1;
	}
	
	// play!
	x = document.getElementById(pId);
	x.URL=mediaURI;				
	x.controls.play();
	if (loopTrack.toLowerCase()=='repeat') x.settings.playCount = 100;

}

function FLE_mediaPause(mediaURI)
// Global function to play media file
//
// mediaURI - URI to media file
{
	if (!pId) return 1;

	var x = document.getElementById(pId);

	switch (x.playState) {
		case 2 : x.controls.play(); break;
		case 3 : x.controls.pause(); break;
		case 10 : x.controls.play(); break;
	}
}


function FLE_updateMediaReport(StateCode)
// update media status diplay
{
	var s = '';
	
	var x = document.getElementById(pId); // player
	var y = document.getElementById(sId); // media status report
	if (!x||!y) return 1;
	
	switch (StateCode) {
		case 1: s = "Stopped"; break;
	    case 2:	s = "Paused"; break;
			case 3: s = '<marquee id="marqueestatus" scrollamount="1" scrolldelay="75" truespeed>Now Playing ' + x.currentMedia.name + '</marquee>'; break;
	    case 6: s = "Buffering..."; break;
	    case 7: s = "Waiting..."; break;
	    case 8: s = "Media End"; break;
	    case 9: s = "Transitioning..."; break;
	    case 10: s = "Ready"; break;
	    default: s = "Unknown State";
	}

	y.innerHTML = s;
}


function FLE_handleMediaError()
// from WMPlayer10 SDK
{
	if (!pId) return 1;
	
	var strError = "";
    var max = x.error.errorCount
	
	var x = document.getElementById(pId)

    for (i=0;i<max;i++)
        strError += "Error: " + x.error.item(i).errorDescription + "\n";

    alert( strError ); // Display the error warning.

	x.error.clearErrorQueue();
}


function FLE_addAlbums(DisplayFirst,List)
// add albums <div class="trackset" id="ALBUM"> to global album list
{
	if (arguments.length < 2) return 0;
	
	for (i=1;i<arguments.length;i++) {
		albumList.push(arguments[i]);
	}
	
	if (DisplayFirst) {
		FLE_showAlbum(albumList[0]);
	}
}				


// shows specified album and hides all others
function FLE_showAlbum(AlbumId)
{
	var f = false;
	var k;
	
	// check if album is present in the list
	for(var i=0; i<albumList.length; i++) if (albumList[i]==AlbumId) { k=i; f=true; break; }
	
	if (f==true) {
		// hide all other albums first
		for(var i=0; i<albumList.length; i++) { 
			var x = document.getElementById(albumList[i]);
			if (albumList[i]!=AlbumId)
				// hide other albums
				{ x.style.display = "none"; }
			else {
				// toggle specified album
				if (x.style.display=="block")  
					{ x.style.display = "none"; }
				else
					{ x.style.display = "block"; }				
			}
		}
	}
}
					   
function FLE_launchVideo(theURL) {
	var x = window.open("/clientscripts/video.html?play="+theURL,"videoWin","width=325,height=300,left=200,top=200");
	x.focus();
}
						 
/* some conventions: 082305

SmallCapitalVariation - function parameters
smallFirstVariation - variables
ALLCAPS - constants

*/