var currentItem = -1;
var previousItem = -1;
var currentLength = 0;
var currentState = 'NONE'; 
var previousState= 'NONE'; 
var linkFlag = false;

var player = null;
function playerReady(thePlayer) {
	player = window.document[thePlayer.id];
	addListeners();
	printPlaylistData();
}


function addListeners() {
	if (player) { 
		player.addControllerListener("ITEM", "itemListener");
		player.addModelListener("STATE", "stateListener");
	} else {
		setTimeout("addListeners()",100);
	}
}


function itemListener(obj) { 
	if (obj.index != currentItem) {
 		previousItem = currentItem;
		currentItem = obj.index; 		
		setItemStyle(currentItem);	
	}
}


function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
	currentState = obj.newstate; 
	//previousState = obj.oldstate; 



if(currentState == 'PLAYING') {  
var txtitol = document.getElementById("titolo_video");
var txtempo = document.getElementById("tempo_video");
var txdescr = document.getElementById("descrizione_video");
plstm = player.getPlaylist();

var txttitolo ='';
var txttempo ='';
var txtdescr ='';

txttitolo = plstm[currentItem].title;
txttempo = plstm[currentItem].author;
txtdescr = plstm[currentItem].link;

txtitol.innerHTML = txttitolo+' <i><font face="Verdana" size="2" color="#000000">'+txttempo+'</font></i>';
 
txdescr.innerHTML = txtdescr;
}


if(currentState == 'COMPLETED') { 
fleXcrollTo("mycustomscroll3","2s","40px",true);
}


	var tmp = document.getElementById("itm" + currentItem);
	if ((currentState != previousState)||(currentItem != previousItem)) {  
		previousState = currentState; 
		setItemStyle(currentItem);	
	} 
}


function mover(obj, idx) { 
	if (idx == currentItem) { obj.className = 'playinghi'; }
	else  { obj.className = 'playlisthi'; }
}


function mout(obj, idx) {
	linkFlag = false;
	if (idx == currentItem) { obj.className = 'playinglo'; }
	else  { obj.className = 'playlistlo'; } 
}


function setItemStyle(idx) {
	var count = getLength(); 
	for(var i=0; i<count; i++) { 
		var tmp = document.getElementById("itm" + i);
		if (tmp) { 
			if(i == idx) {
				 if(currentState == 'PLAYING') { tmp.className = 'playinglo'; } //'playinghi';
				else { tmp.className = 'playlisthi'; } //'playinglo';
			} else { tmp.className = 'playlistlo'; }
		}
	}
	var tmp = document.getElementById("plstwrapper");
	tmp.scrollTop = idx * 80;
}


function getLength() { currentLength = player.getPlaylist().length; return(currentLength); };


function printPlaylistData() {
	var plst = null;
	plst = player.getPlaylist();
	if (plst) {
		var txt='<table cellPadding="2" width="98%">'; 
		for(var i in plst) { 
 			txt+='<tr><td><table width="98%" id="itm' + i + '" onclick="if(! linkFlag) player.sendEvent(\'ITEM\',' + i + ');" ';
 			txt+='class="playlistlo" onmouseover="mover(this, ' + i + ')" onmouseout="mout(this, ' + i + ')">';
			txt+='<tr><td><img src="' + plst[i].image.replace('video_','ico_video_') + '" width="60"  height="50" title="Click to Play"></td>';
			txt+='<td width="98%" valign=middle><p>';
			txt+=plst[i].title + '<br>';
			txt+='<b>' + plst[i].author + '</b><br>';
			//txt+='<a href="' + plst[i].link + '" target="_blank" title="' + plst[i].link + '" onclick="linkFlag=true"> more info</a><br>';
			//txt+=plst[i].description;
			txt+='</p></td></tr></table></td></tr>';
		}
		txt+='</table>'; //alert(txt);

		var tmp = document.getElementById("plstDat");
		if (tmp) { tmp.innerHTML = txt; CSBfleXcroll('mycustomscroll3'); }
	} else {
		setTimeout("printPlaylistData()",100);




	}	
}




AW=screen.availWidth;
AH=screen.availHeight;

var largh ="";
var altez ="";

		if((AW<1048) && (AH<800)){
                largh=460;
                altez=309;
		}else{
                largh=700;
                altez=393;
		}







function createPlayer(theFile, theAutostart) {



	var flashvars = {
		file:theFile,  
		autostart:theAutostart, 
		shuffle:false, 
		repeat:"list",
                stretching:"uniform",
                backcolor:"#F5F5F5",
                frontcolor:"#ff9933",
                lightcolor:"#ff9933",

                screencolor:"#FFFFFF",
                captions:"subtitle_xml.php",
                plugins:"accessibility-1"
	}

	var params = {
		allowfullscreen:"true", 
		allowscriptaccess:"always",
        bgcolor:              '#FFFFFF'
	}

	var attributes = {
		id:"player1",  
		name:"player1"
	}

	swfobject.embedSWF("http://www.tuscanytv.tv/mediaplayer4290.swf", "placeholder1", largh, altez, "9", false, flashvars, params, attributes);
}
