var currentItem = -1; 
var previousItem = -1; 
var player = null;
function playerReady(thePlayer) {
	player = window.document[thePlayer.id];
	addListeners();
}
function addListeners() {
	if (player) { 
		player.addControllerListener("ITEM", "itemListener");
	} else {
		setTimeout("addListeners()",100);
	}
}
function itemListener(obj) { 
	if (obj.index != currentItem) {
 		previousItem = currentItem;
		currentItem = obj.index;

		var tmp = document.getElementById("itm");
		if (tmp) { 
			tmp.innerHTML = "current item: " + currentItem +
				"<br>previous item: " + previousItem;
		}
		getPlaylistData(currentItem);
	}
}
function getPlaylistData(theIndex) {
	var plst = null;
	plst = player.getPlaylist();

	if (plst) {
		var txt = '';
		txt += '<li><b>item number: </b>'+theIndex+':';
		txt += '<li><b>title: </b>'+plst[theIndex].title+'</li>';
		txt += '<li><b>author: </b>'+plst[theIndex].author+'</li>';
		txt += '<li><b>file: </b>'+plst[theIndex].file+'</li>';
		txt += '<li><b>image: </b>'+plst[theIndex].image+'</li>';
		txt += '<li><b>link: </b>'+plst[theIndex].link+'</li>';
		txt += '<li><b>description: </b>'+plst[theIndex].description+'</li>';
		txt += '</li>';

		var tmp = document.getElementById("plstDat");
		if (tmp) { tmp.innerHTML = txt; }

		txt = '';
		txt += '<a href="'+plst[theIndex].file+'" title="Download Current Song">Download Current Song</a>'
		tmp = document.getElementById("dwnlink");
    	if (tmp) { tmp.innerHTML = txt; }
	} 	
}
function deletePlayer(theWrapper, thePlaceholder, thePlayerId) { 
    swfobject.removeSWF(thePlayerId);
    var tmp=document.getElementById(theWrapper);
    if (tmp) { tmp.innerHTML = "<div id=" + thePlaceholder + "></div>"; }
}
function createPlayer(theWrapper, thePlaceholder, thePlayerId, theFile, thePlayerHeight, thePlayerWidth) {
    deletePlayer(theWrapper, thePlaceholder, thePlayerId);
	var flashvars = {
                    file:theFile, 
                    autostart:"false",
                    playlistsize:"100",
                    playlist:"bottom",
                    skin:"simple.swf",
                   backcolor:"0x313499",
                   frontcolor:"0xFFFFFF",
                   lightcolor:"0x00CCFF"
	}
	var params = {
	allowfullscreen:"true", 
	allowscriptaccess:"always"
	}

	var attributes = {
	id:thePlayerId,  
	name:thePlayerId
	}

	swfobject.embedSWF("player.swf", thePlaceholder, thePlayerWidth, thePlayerHeight, "9.0.115", false, flashvars, params, attributes);
}
