// JavaScript Document

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; 

	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="0" cellSpacing="8" border="0" width="298">'; 
		/*for(var i in plst) { 
 			txt+='<tr><td><table width="288" cellPadding="0" cellSpacing="0" 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 + '" width="140"  height="79" title="Click to Play"></td>';
			txt+='<td onMouseover="ddrivetip(\'' + plst[i].author + '\',\'#211719\', 200)"; onMouseout="hideddrivetip()" class="plst_pad" width="100%">';
			txt+='<b>' + plst[i].title + '</b><br>';
			//txt+=plst[i].author + '<br>';
			//txt+='<a href="' + plst[i].link + '" target="_blank" title="' + plst[i].link + '" onclick="linkFlag=true"> fly 1</a><br>';
			//txt+='<a href="' + plst[i].link + '" target="_blank" title="' + plst[i].link + '" onclick="linkFlag=true"> fly 2</a><br>';
			//txt+='<a href="' + plst[i].link + '" target="_blank" title="' + plst[i].link + '" onclick="linkFlag=true"> fly 3</a><br>';
			//txt+=plst[i].description;
			txt+='</td></tr></table></td></tr>';
		}*/
		for(var j in plst)
		{
			txt += '<tr><td><table width="288" cellPadding="0" cellSpacing="0" id="itm' + j + '" onclick="if(! linkFlag) player.sendEvent(\'ITEM\',' + j + ');" ';
			txt += 'class="playlistlo" onmouseover="mover(this, ' + j + ')" onmouseout="mout(this, ' + j + ')">';
			txt += '<tr><td valign="top"><img src="' + plst[j].image + '" width="140" height="79" title="Click to Play"></td>';
			txt += '<td onMouseover="ddrivetip(\'' + plst[j].author + '\',\'#211719\', 200)"; onMouseout="hideddrivetip()" class="plst_pad" width="100%">';
			txt += '<b>' + plst[j].title + '</b><br>';
			if(plst[j].fly1link) 
				txt += '<a href="' + plst[j].fly1link + '"class="plst_ln" title="' + plst[j].fly1title + '" onclick="linkFlag=true">' + plst[j].fly1title + '</a><br>';
			if(plst[j].fly2link) 
				txt += '<a href="' + plst[j].fly2link + '"class="plst_ln" title="' + plst[j].fly2title + '" onclick="linkFlag=true">' + plst[j].fly2title + '</a><br>';
			if(plst[j].fly3link) 
				txt += '<a href="' + plst[j].fly3link + '"class="plst_ln" title="' + plst[j].fly3title + '" onclick="linkFlag=true">' + plst[j].fly3title + '</a><br>';
			if(plst[j].fly4link) 
				txt += '<a href="' + plst[j].fly4link + '"class="plst_ln" title="' + plst[j].fly4title + '" onclick="linkFlag=true">' + plst[j].fly4title + '</a><br>';
			if(plst[j].fly5link) 
				txt += '<a href="' + plst[j].fly5link + '"class="plst_ln" title="' + plst[j].fly5title + '" onclick="linkFlag=true">' + plst[j].fly5title + '</a><br>';
			if(plst[j].fly6link) 
				txt += '<a href="' + plst[j].fly6link + '"class="plst_ln" title="' + plst[j].fly6title + '" onclick="linkFlag=true">' + plst[j].fly6title + '</a><br>';
			txt += '</td></tr></table></td></tr>';
		}
		txt+='</table>'; //alert(txt);

		var tmp = document.getElementById("plstDat");
		if (tmp) { tmp.innerHTML = txt; }
	} else {
		setTimeout("printPlaylistData()",100);
	}	
}


function createPlayer(theFile, theAutostart) {
	var flashvars = {
		file:theFile,
		stretching:false,  
		autostart:theAutostart, 
		shuffle:false, 
		repeat:"list",
		skin:"videos/stylish.swf",
		backcolor:"333333",
		frontcolor:"cccccc",
		lightcolor:"ea9000"
	}

	var params = {
		allowfullscreen:"true", 
		allowscriptaccess:"always",
		allowcrossdomain:"true",
		wmode:"transparent"
	}

	var attributes = {
		id:"player1",  
		name:"player1"
	}

	swfobject.embedSWF("videos/player.swf", "placeholder1", "592", "342", "9.0.115", false, flashvars, params, attributes);
}


function init(xml_file) {
	//Drag.init(document.getElementById("playerhandle"), document.getElementById("playerroot"));	
	//Drag.init(document.getElementById("listhandle"), document.getElementById("listroot"));

	//document.getElementById("playerhandle").style.cursor = 'move';
	//document.getElementById("listhandle").style.cursor = 'move';

	createPlayer(xml_file, false); 
}

function changePlaylist() {
	if(document.getElementById("playlist_hdr").src == "images/header_recent_adventures.jpg") {
		 
	}
	else { 
		//init("videos/videos.xml");
		//document.getElementById("playlist_hdr").src = "images/header_recent_adventures.jpg";
		init("videos/videos_full.xml");
		document.getElementById("playlist_hdr").src = "images/header_full_episodes.jpg";
	}
}
