// some variables to save
var currentPosition;
var currentRemaining;
var currentVolume;
var currentItem;
var currentState;
var currentLoad;
var currentXsize;
var currentYsize;
var currentFile;
var prevFile;
var startupPlaylistItem;
var playingFlag = false;
var playlistItems = new Array("What_are_biofuels", "Food_vs_Fuel", "The_next_generation", "Where_next", "Biopower", "Triumph_Through_Adversity");
var playlistfilename = "playlist.txt";
var playInt = 0;
var listenInt = 0;
var autoplayInt = 0;

var playerAutoPlayItemID = -1;

function setTracking(videoName){
	//alert("movie " + videoName + " is being loaded, can tag here (line 17, mediaplayer.js)");// we're loading the movie
	_hbPageView("/HOME_TOA","/Solutions/Saab/video_" + videoName);
}


// this function is caught by the JavascriptView object of the player.
function sendEvent(typ,prm) {
	thisMovie("mpl").sendEvent(typ,prm); 
};

function extractVideoName(fileName){
	var tmp = fileName.split("/");
	if (tmp.length > 0){
		return(tmp[tmp.length-1]);
	}else{
		return fileName;
	}
	
}


// these functions is called by the JavascriptView object of the player.
function getUpdate(typ,pr1,pr2,swf) {
	//alert("updating");
	if((typ == "state")&&(pr1 != undefined)) {
		currentState = pr1; 
		if((currentState=="0")&&( ! playingFlag)) {				
			playingFlag = true;
			if (startupPlaylistItem != ""){
				playStartupItem(); 	
			}
		}
	}
	if(typ == "time") { currentPosition = pr1; pr2 == undefined ? null: currentRemaining = Math.round(pr2); }
	else if(typ == "volume") { currentVolume = pr1; } 
	else if(typ == "item") { currentItem = pr1; setTimeout("getItemData(currentItem)",100);}
	else if(typ == "state") { currentState = pr1; }
	else if(typ == "load") { currentLoad = pr1; }
	else if(typ == "size") { currentXsize = "X=" + pr1; pr2 == undefined ? null: currentYsize = "Y=" + Math.round(pr2); } 
	
	if (prevFile != currentFile){
		setTracking(currentFile);
	}
	
	prevFile = currentFile;
};

function getItemData(idx) {
	
	var obj = thisMovie("mpl").itemData(idx);
	currentFile = obj["file"];
};


// These functions are caught by the feeder object of the player.
function loadFile(obj) { thisMovie("mpl").loadFile(obj); };

function addItem(obj,idx) { thisMovie("mpl").addItem(obj,idx); };

function removeItem(idx) { thisMovie("mpl").removeItem(idx); };

function getLength(swf) { return(thisMovie(swf).getLength()); };


// This is a javascript handler for the player and is always needed.
function thisMovie(movieName) {
	if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
};

function initPlayer(id, filename){
	playlistfilename = filename;
	playerAutoPlayItemID = id;
	playInt = setInterval(createPlayer, 1000);
}

function createPlayer(id, filename){
	clearInterval(playInt);
	/*var so = new SWFObject('flash/mediaplayer.swf','mpl','700','250','9');
	so.addParam('allowscriptaccess','always');
	so.addParam('allowfullscreen','true');
	so.addVariable('height','250');
	so.addVariable('width','700');
	so.addVariable('file','playlist.php?type=xml');
	so.addVariable('backcolor','0x125581');
	so.addVariable('frontcolor','0xFFFFFF');
	so.addVariable('lightcolor','0xFFFFFF');
	so.addVariable('screencolor','0x125581');
	so.addVariable("enablejs","true");
	so.addVariable("javascriptid","mpl");
	so.addVariable("volume","0");
	so.addVariable('displaywidth','450');
	so.addVariable('showdigits','false');
	so.addVariable('usefullscreen','false');
	so.addVariable('autoscroll','true');
	if (id == 0){
		so.addVariable("autostart","true");
	}else{
		so.addVariable("autostart","false");
		startupPlaylistItem = id;
	}
	so.write('player');*/
	var so = new SWFObject('player/player.swf','mpl','700','250','9');
	so.addParam('allowscriptaccess','always');
	so.addParam('allowfullscreen','true');
	so.addParam('flashvars','&file=mediaplaylist.php?filename=' + playlistfilename + '&playlistsize=290&streamscript=lighttpd&playlist=right&fullscreen=true&stretching=uniform&skin=http://www.independentonlinesolutions.com/saab/player/saabskin.swf');
	so.write('player');

	if (playerAutoPlayItemID != -1){
		autoplayInt = setInterval(playItem, 1500);
	}
	listenInt = setInterval(setListeners, 1000);
}


function setListeners(){
	var player = getPlayer();
	player.addModelListener("STATE","updateListener");
}

function test(updateListener) {
	clearInterval(listenInt);
	var newState = obj["newstate"];
	var oldState = obj["oldstate"];
	if (newState == "PLAYING" && (oldState != "PAUSED")){
		var player = getPlayer();
		var config = player.getConfig();
		//alert("now playing " + playlistItems[config["item"]]);
		setTracking(playlistItems[config["item"]]);
	}
};

function playItem(id){
	clearInterval(autoplayInt);
	var player = getPlayer();
	
	player.sendEvent('ITEM', playerAutoPlayItemID); 
}

function getPlayer() {
	if(navigator.appName.indexOf("Microsoft") != -1) {
		return window["mpl"];
	} else {
		return document["mpl"];
	}
};


function playStartupItem(){
	thisMovie("mpl").sendEvent('playitem',startupPlaylistItem);
}

function highlightVideo(id, highlight){
	if (highlight){
		var backcolor = "#FFFFFF";
		var fontcolor = "#000000";
	}else{
		var backcolor = "#FFFFFF";
		var fontcolor = "#000000";
	}
	document.getElementById("video_" + id).style.backgroundColor = backcolor;
	document.getElementById("video_" + id).style.color = fontcolor;
}

function gotoAndPlay(id, series){
	if (series == 3){
		loc = "videoS3.php?id=" + (id);	
	}else if (series == 2){
		loc = "videoS2.php?id=" + (id);	
	}else if (series == 1){
		loc = "videoS1.php?id=" + (id);	
	}else{
		loc = "video.php?id=" + (id);
	}
	window.location = loc;
}