// javascript used on jukebox
//


window.onload=function()
{
	// check if DOM is available
	if(!document.getElementById || !document.createTextNode){return;}

	var flashWorks=!document.getElementById('flashwarning');
    if (flashWorks)	{
		document.getElementById('listswap').style.display='block';
		document.getElementById('currentplaylist').style.display='block';
	}
}

// This is called by JW player when it is instantiated. 
function playerReady(obj) {
	var id = obj['id'];
	var version = obj['version'];
	var client = obj['client'];
//	alert('the videoplayer '+id+' has been instantiated ');
	player = document.getElementById(id);
	player.addControllerListener("ITEM","onItemChange");
	player.addControllerListener("PLAYLIST","onPlaylistChange");
	//showPlaylistInfo();
	//printPlaylistData();
//    alert('frontcolor='+player.getConfig().frontcolor);
//    alert('title=');
};

function printPlaylistData() {
	var plst = null;
	plst = player.getPlaylist();
	if (plst) {
		var txt = '';
		for(var itm in plst) { 
			txt += '<li>'+itm+':</li>';
			txt += '<ul>';
			txt += '<li>'+plst[itm].title+'</li>';
			txt += '<li>'+plst[itm].author+'</li>';
			txt += '<li>'+plst[itm].file+'</li>';
			txt += '</ul><br>';
		}
		var tmp = document.getElementById("plstDat");
		if (tmp) { tmp.innerHTML = txt; }
	} else {
		setTimeout("printPlaylistData()",100);
	}	
}

function showPlaylistInfo()	{
	plst=player.getPlaylist();
	if (plst) {
		alert("got playlist");
//	    alert('title='+player.getPlaylist()[1].title);
	    alert('title='+player.getPlaylist().title);
		}
	else setTimeout("showPlaylistInfo()",100);
}

// called when the user changes playlist item selection.
function onItemChange(obj)	{
//	alert("index="+obj.index); 
    nowPlaying(obj.index);	
}

// called when playlist changes
function onPlaylistChange(obj)	{
    nowPlaying(0);	
}

function writePlayerObject(playlist)	{
	//writeXSPFPlayer(songname);
	writeJWFlashPlayer(playlist);		
}

// Load JW Player for audio (no display) with playlist (aka jukebox)
function writeJWPlayerAudioPlayList()	{
	writeJWFlashPlayer(350,180,"#ffffff","/config_jukebox.xml","/pl_default.xspf");
}

// Load JW Player for audio (no display) for a single song (no playlist)
function writeJWPlayerAudioSingle(songname)	{
	writeJWFlashPlayer(350,20,"#ffffff","/config_singlesong_player.xml","/songs/audio/"+songname+".mp3");
}

// Load JW Player for single video 
function writeJWPlayerVideoSingle(vidname,cfgname)	{
//	writeJWFlashPlayer(480,290,"#ffffff","/video/"+cfgname+".xml","/video/"+vidname);
// use default width/height.
   writeJWPlayerVideoSized(480,290,vidname,cfgname);
}

// Load JW Player for single video 
function writeJWPlayerVideoSized(width,height,vidname,cfgname)	{
	writeJWFlashPlayer(width,height,"#ffffff","/video/"+cfgname+".xml","/video/"+vidname);
}


// Load JW Player
function writeJWFlashPlayer(width,height,bgcolor,config,file)	{
	so = new SWFObject("/player.swf", "embeddedplayer", width, height, "9", bgcolor);
	so.addParam('allowscriptaccess','always');
	so.addParam('allowfullscreen','true');
	so.addVariable("config", config); // XML config file
	so.addVariable("file", file);	  // initial media file or playlist

    so.write("playercontainer");
}

// update the playlist
function plSwap(playlist,listtitle)	{
	if (playlist=='pl_ready') {
		alert('Ha. Ha. Yeah, doesn\'t everybody!?!? Now, please try to be a little '
		+'more serious for a minute and I\'ll load a few pieces that are about as '
		+'"ready-to-use" as their gonna get.');
	}		
	player.sendEvent("LOAD","/"+playlist+".xspf");
	player.sendEvent("ITEM",0);  // needed for scroll bar redraw.
	document.getElementById("playlist_title").firstChild.nodeValue=listtitle;	
}

// update the "Current Selection" info. 
function nowPlaying(np_index)	{
	nowplayingtext=player.getPlaylist()[np_index].title;
	if (!nowplayingtext) nowplayingtext="None";
	document.getElementById("nowplaying_title").firstChild.nodeValue=nowplayingtext;
}

var player;

function spit(vomit)	{
	alert ('we just spit '+vomit);
}



