// some variables to save
var currentPosition;
var currentVolume;
var currentItem;

//FIXME only needed fro lib/svplayer.php
//var subvideo_url = "http://dev.subvideo.tv";
//var subvideo_url = "http://subvideo.tv";

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

function scrubTo() {
	 var seekTimeField = $('seekTime').value;
	  subvideo_timer(seekTimeField, totalTime);
	  sendEvent('scrub',seekTimeField);
}

function playfrom(offset) {
	playpause('play', 1);
	setTimeout('scrub(' + offset+')', 2000);
}
function playfrom_svplayer(offset) {
        playpause('play', 1);
        setTimeout('scrubsvplayer(' + offset+')', 2500);
}

function scrub(offset) {
	  sendEvent('scrub',currentPosition + offset);
	  var newposition = currentPosition;
	  if (currentPosition + offset <= 0) {
		  newposition = 0;
	  }
	  if (currentPosition + offset > totalTime) {
		  newposition = totalTime;
	  };
	  // alert(currentPosition);
	  subvideo_timer(newposition, totalTime);
}

function scrubsvplayer(offset, totalTime) {
	  sendEvent('scrub',offset);
	  var newposition = offset;
	  if (offset <= 0) {
		  newposition = 0;
	  }
	  if (offset > totalTime) {
		  newposition = totalTime;
	  };
	  // alert(currentPosition);
	  //subvideo_timer(newposition, totalTime);
	  if ($("loading")) {
		var doo = $("loading");
	        doo.style.visibility = 'hidden';
	  }
}
function jump(video, point)   {
	//make_player(video);
	//alert(video);
	if (video) {
		so.addVariable('file',video);
		so.addVariable('bufferlength', point+10);
		so.write('flvplayer');
		setTimeout('playfrom_svplayer('+point+');',500);
	}
	/////loadFile(video); 

	return false;
}


function make_player(video)  {
	/* <![CDATA[ */
	//so = new SWFObject('http://subvideo.tv/player/mediaplayer.swf','mpl','400','304','7');
	so = new SWFObject(subvideo_url+'player/mediaplayer.swf','mpl','400','304','7');
	so.addParam('allowfullscreen','true');
	so.addParam('allowscriptaccess','always');
	//so.addVariable('skin', 'http://dev.subvideo.tv/player/Kleur.swf');
	//////WARMING>>> use  urlencode for: ? -> %3F  | = -> %3D | & -> %26
	so.addVariable("file",video);
	so.addVariable('height',304);
	so.addVariable('width',400);
	// this 2 variable are the screen size. The rest is the space for the playlist.
	so.addVariable("displaywidth", "400");
	so.addVariable('displayheight','286');/// to hide controls: 310
	so.addVariable('overstretch','fit');
	so.addVariable('showdigits','true');
	so.addVariable('showicons','false');
	so.addVariable('showvolume','true');
	//so.addVariable('shuffle','false');
	so.addVariable('enablejs','true');
	so.addVariable('logo', '../images/sv_mini.png');
	//so.addVariable("autoscroll","true");
	//so.addVariable("thumbsinplaylist","false");
	//so.addVariable("repeat","false");

	so.write('flvplayer');
}

function getUpdate(typ,pr1,pr2,pid) {
	  if (typ == 'state') {
	//	  if (pr1) {
	//		  //activate_tab('context');// showing context TAB after press play
	//		  playpause('play', 0);
	//	  } else {
	//		  playpause('pause', 0);
	//	  }
	  } else if (typ == "time") {
		  currentPosition = pr1;
		  subvideo_timer((pr1), totalTime); // Main timer
	  } else if (typ == 'stop') {
		  //alert("stopped man!");
	  } else if (typ == "volume") {
		  currentVolume = pr1;
	  } else if(typ == "item") {
		  currentItem = pr1;
		  setTimeout("getItemData(currentItem)",100);
	  }

	var id = document.getElementById(typ);
	id.innerHTML = typ+ ": "+Math.round(pr1);
	pr2 == undefined ? null: id.innerHTML += ", "+Math.round(pr2);
	if(pid != "null") {
		document.getElementById("pid").innerHTML = "(received from the player with id <i>"+pid+"</i>)";
	}
};

// 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 getItemData(idx) {
	var obj = thisMovie("mpl").itemData(idx);
	var nodes = "";
	for(var i in obj) {
		nodes += "<li>"+i+": "+obj[i]+"</li>";
	}
	document.getElementById("data").innerHTML = nodes;
};

// 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];
	}
}


/* <![CDATA[ */
var so = new SWFObject('player/mediaplayer.swf','mpl','500','304','7');

so.addParam('allowfullscreen','true');
so.addParam('allowscriptaccess','always');

//////WARMING>>> use  urlencode for: 
//   ? -> %3F
//   = -> %3D
//   & -> %26
if (edit == 'yes') {
	var url_all = subvideo_url + '/lib/playlist_generator.php%3Fid=' + video_id + '%26edit=yes%26sv=' + sv ;
} else {
	var url_all = subvideo_url + '/lib/playlist_generator.php%3Fid=' + video_id + '%26sv=' + sv;
	//alert(url_all);
}
so.addVariable("file",url_all);


//so.addVariable('file', video_url);
//so.addVariable("file",subvideo_url + "/lib/playlist_generator.php?sv=114%26id=301");

//so.addVariable('usecaptions','true');
//so.addVariable('captions', subvideo_url + '/lib/import_sub.php?sv=' + sv + '&amp;action=bd2tt');
//so.addVariable('captions',subvideo_url+'/test.xml');

//so.addVariable('type','flv');
//so.addVariable('autostart','true');

so.addVariable('height',304);
so.addVariable('width',500);
// this 2 variable are the screen size. The rest is the space for the playlist.
so.addVariable("displaywidth", "400");
so.addVariable('displayheight','286');/// to hide controls: 310
so.addVariable('overstretch','fit');
so.addVariable('showdigits','true');
so.addVariable('showicons','false');
so.addVariable('showvolume','true');
so.addVariable('shuffle','false');
so.addVariable('enablejs','true');
so.addVariable('logo', subvideo_url + '/images/sv_mini.png');
so.addVariable("autoscroll","true");
so.addVariable("thumbsinplaylist","false");
so.addVariable("repeat","false");

//so.addVariable("image","http://www.jeroenwijering.com/upload/peterjones.jpg");

so.write('flvplayer');
//thisMovie('mpl').playpause('play', 1);
//sendEvent('scrub',12);
//setTimeout("playfrom(11);",4000);
//thisMovie('mpl').sendEvent('scrub', 12)



