function afns_video_event( video_id, video_title, video_eventname )
{
	var s = s_gi('theonionprod');
	// !no spaces allowed between vars!
	s.linkTrackVars='eVar20,events';
	s.linkTrackEvents='events13,events14';
	s.eVar20=video_title;
	s.events=video_eventname;
	s.tl(this,'o',video_id);
	s.events='';
}

var toolbar_size = 30;
var video_width_normal = 450;
var video_height_normal = 338+toolbar_size;
var video_width_big = 764;
var video_height_big = 573+toolbar_size;
var videoPlayer = null;
var video_name = "afnsVideo";
var video_title = document.title;
	
/*Flash function calls*/
function debug(msg){
	alert(msg);
}
function sendEvent(type,param) {

	if( thisMovie(video_name) ){
		thisMovie(video_name).sendEvent(type,param);
	}else{
		setTimeout("thisMovie(video_name).sendEvent('"+type+"', '"+param+"');",10);
	}
};
function loadFile(file) {
	thisMovie(video_name).loadFile(file);
};
function thisMovie(movieName) {

    if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
};
function setFullscreen( ){
	videoPlayer.toggleSize();
}
		
var STATE_PAUSED = 0;
var STATE_BUFFER = 1;
var STATE_PLAY = 2;
var STATE_COMPLETE = 3;

function getUpdate(type,pr1,pr2) {

//	window.status = type+ ": "+Math.round(pr1)+", "+Math.round(pr2);
	if( type == "time" ){
		videoPlayer.itemTime = pr1;
		if( videoPlayer.playing && videoPlayer.nextAction != null){
			var action = videoPlayer.nextAction;
			videoPlayer.nextAction = null;
			action();
		}
		videoPlayer.playing = true;
//		window.status = videoPlayer.currentItem+": "+videoPlayer.itemTime;
	}else if( type == "item" ){
		if( currentVideo["type"] == "preroll" ){
			//afns_video_event('video_ad_watched_' + video_title, video_title, 'events13');
		}else if( currentVideo["type"] == "video"){
			//video has been watched
			afns_video_event('video_watched_' + video_title, video_title, 'events13');
		}else if (currentVideo["type"] == "postroll" ){
			//ad2 has been watched
			afns_video_event('video_ad_watched_' + video_title, video_title, 'events14');
		}
		videoPlayer.currentItem = pr1;
	}else if( type == "state" ){
		videoPlayer.state = pr1;
		//alert(pr1+" "+pr2);
		if( pr1 == STATE_PLAY ){
		}
	}else if( type == "load" ){
	}else{
		//alert(type);
	}
//	var id = document.getElementById(type);
//	id.innerHTML = type+ ": "+Math.round(pr1);
//	pr2 == undefined ? null: id.innerHTML += ", "+Math.round(pr2);
};
var currentVideo;
function itemData(obj) {
	currentVideo = obj;
/*
	var nodes = "";
	for(var i in obj) { 
		nodes += "<li>"+i+": "+obj[i]+"</li>"; 
	}
	alert(nodes);
	*/
//	window.status=(nodes);
//	document.getElementById("data").innerHTML = nodes;
};

/*VideoPlayer object*/

function VideoPlayer(){	

	this.fullscreen = false;
	this.bufferTime = 3;
	this.width = video_width_normal;
	this.height = video_height_normal;
	this.playerWrapperId = "video_player";
	this.time = 0;
	this.image = "";
	this.currentItem = 0;
	this.itemTime = 0;
	this.bgcolor = "#FFF"
	this.playlist = null;
	this.autostart = "true";
	this.state = STATE_PAUSED;
	this.wrapper = document.getElementById(this.playerWrapperId);
	this.body = document.getElementById("content");
	this.body.originalClassname = this.body.className;
	this.playing = false;
	this.debugging=false;
	videoPlayer = this;
}
VideoPlayer.prototype.play = function(){
	this.playing = false;
	var buffer = this.bufferTime;
	var url = this.playlist;
	if( this.fullscreen ){
		url = this.playlist+"?quality=high";	
	}
	var flashVars = "file="+url+"&image="+this.image+"&bufferlength="+buffer+"&fullscreenmode="+false+"&debugging="+this.debugging;
	var FO = { 	movie: afns_base_url+"/themes/common/assets/videoplayer2/flvplayer.swf", 
				width: "100%", 
				height: "100%", 
				majorversion:"8", 
				build:"0", 
				bgcolor:this.bgcolor,
				swLiveConnect:"true",
				name:video_name,
				wmode:"transparent",
				id:video_name,
				xi:"true",
				ximovie:afns_base_url+"/themes/common/assets/videoplayer2/ufo.swf",
				flashvars: "showfsbutton=true&autostart="+this.autostart+"&enablejs=true&title="+escape(this.title)+"&"+flashVars
				};
//	document.getElementById(this.playerWrapperId).innerHTML = "";
	UFO.create(FO, this.playerWrapperId);
}

VideoPlayer.prototype.toggleSize = function(){
	this.syncItem = this.currentItem;
	this.syncTime = this.itemTime;
//	this.bufferTime = this.syncTime;
	this.nextAction = function(){videoPlayer.sync()};
	if( this.fullscreen ){
		this.resizeNormal();
	}else{
		this.resizeLarge();
	}
}

VideoPlayer.prototype.resizeNormal = function(){
	this.fullscreen = false;
	this.width = video_width_normal;
	this.height = video_height_normal;
	this.body.className = this.body.originalClassname;
	videoPlayer.body.style.marginTop = "1px"
	setTimeout('videoPlayer.body.style.marginTop = "0"',0);
//	loadFile(this.playlist);
	sendEvent("resize",0);
	this.node = this.wrapper.children[0];
	setTimeout('videoPlayer.node.focus()',0);
/*
	this.node.style.width=video_width_normal;
	this.node.style.height=video_height_normal;
*/
//	this.play();
}
VideoPlayer.prototype.resizeLarge = function(){
	this.fullscreen = true;
	this.width = video_width_big;
	this.height = video_height_big;
	this.body.className = this.body.originalClassname+" largeVideo";
	sendEvent("resize",1);
	this.node = this.wrapper.children[0];
	setTimeout('videoPlayer.node.focus()',0);
/*
	setTimeout('loadFile(this.playlist+"?quality=high")',100);
	this.node.style.width=video_width_big;
	this.node.style.height=video_height_big;
*/
//	this.play();
}
VideoPlayer.prototype.sync = function (){
	this.playing = false;
	if( this.currentItem != undefined && this.currentItem != this.syncItem){
		sendEvent("playitem",this.syncItem);
	}
//	this.nextAction = function(){videoPlayer.scrub()};
}
VideoPlayer.prototype.scrub = function(){
	var scrubTo = this.syncTime-1;
	if( this.currentTime != undefined ){
		scrubTo -= this.currentTime;
	}
	if( scrubTo > 0 ){
		sendEvent("scrub",scrubTo);
		this.bufferTime = scrubTo;
		//alert(scrubTo);
	}
//	alert(scrubTo);
//	this.bufferTime = 5;
}
function cleanupFP9IELeaks() {
  __flash_unloadHandler = function() {};
  __flash_savedUnloadHandler = function() {};
}
if (typeof window.attachEvent != "undefined" && UFO.uaHas("ieWin")) {
  window.attachEvent("onbeforeunload", cleanupFP9IELeaks);
}
