function videoPlayer(e, movieurls, w, h, instanceId, autoplay) {
	autoplay = autoplay==null?false:autoplay;
	var fVersion = parseInt(getFlashVersion().split(',')[0]);
	if(fVersion <= 8) {
		var so = new SWFObject(Router.videoPlayer7, "videoObject"+instanceId, w, h, "8", "#000000");
		so.addVariable("movieurl", movieurls + (autoplay==true?"&autoplay":""));
		so.addParam("menu", "false");
		so.addParam("allowFullScreen", "true");
		so.addParam("allowScriptAccess", "always");
		so.useExpressInstall(Router.expressInstaller);
		so.write(e);
		
	}	else {
		var so = new SWFObject(Router.videoPlayer, "videoObject"+instanceId, w, h, "9", "#000000");
		so.addVariable("movieurl", movieurls + (autoplay==true?"&autoplay":""));
		so.addParam("menu", "false");
		so.addParam("allowFullScreen", "true");
		so.addParam("allowScriptAccess", "always");
		so.useExpressInstall(Router.expressInstaller);
		so.write(e);
	}
}

function skipTo(i, instanceId) {
	instanceId = instanceId==null?1:instanceId;
	$("#videoObject"+instanceId).get(0).skipTo(i);
}

function loadVideo(url, instanceId) {
	instanceId = instanceId==null?1:instanceId;
	$("#videoObject"+instanceId).get(0).loadVideo(url);
}

function getFlashVersion() {
	// ie
	try {
		try {
			// avoid fp6 minor version lookup issues
			// see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
			var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
			try { axo.AllowScriptAccess = 'always';	} 
			catch(e) { return '6,0,0'; }				
		} catch(e) {}
		return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1];
	// other browsers
	} catch(e) {
		try {
			if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){
				return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1];
			}
		} catch(e) {}		
	}
	return '0,0,0';
}

$(document).ready(function(){
	$("div.video_preview a[href*=javascript]").click(function(){
		var yOffset = $(".videoplayer").offset().top;
		$("html").animate({scrollTop: yOffset-15}, 500, "linear");
	});
	
});

/*
	
Videos Page Slider Controls
	
*/

//Global vars
var ptVideoSliders = new Array();
var ptVideoInfoBoxTimeout;
var ptVideoInfoBoxHovered = false;

function PTVideoSlider(btnFwd, btnBack, slide) {
	this.btnFwd = btnFwd;
	this.btnBack = btnBack;
	this.slideArea = slide;
	this.position = 0;
	this.numItems = $("li", this.slideArea).length;
	this.slideArea.width(this.numItems * 172 + 10);
	var o = this;
	
	if(this.numItems < 6) {
		this.btnFwd.addClass("disabled");
	} else {
		this.btnFwd.click(function(){o.forward()});
		this.btnBack.click(function(){o.back()});
	}
	
}

PTVideoSlider.prototype.forward = function() {
	if(this.position > -(this.numItems/5-1)) {
		this.position--;
		this.slideArea.animate({
			left: this.position*5*172
		}, 600);
	}
	if(this.position >= Math.round(-(this.numItems/5))) {
		this.btnFwd.addClass("disabled");
		this.btnBack.removeClass("disabled");
	} else {
		this.btnFwd.removeClass("disabled");
		this.btnBack.removeClass("disabled");
	}
}

PTVideoSlider.prototype.back = function() {
	if(this.position < 0) {
		this.position++;
		this.slideArea.animate({
			left: this.position*5*172
		}, 600);
	}
	
	if(this.position == 0) {
		this.btnBack.addClass("disabled");
		this.btnFwd.removeClass("disabled");
	} else {
		this.btnFwd.removeClass("disabled");
		this.btnBack.removeClass("disabled");
	}
}

$(document).ready(function(){
	//Hide hover-infobox
	$("#infobox").hide();	
	//Attach interaction for Video Sliders
	var i = 0;
	$("div.video-list-title").each(function() {
		ptVideoSliders.push(new PTVideoSlider($("div.btn-forward", this), $("div.btn-backward", this), $("ul.video-list").eq(i)));
		i++;
	});
	
	//Attach interaction for infobox
	$("ul.video-list li").hover(
		function() { //Over
		var elm = $(this);
		
		//Set contents
		$("#infobox h3").text($("h3", elm).text());
		$("#infobox h3 + span").text($("h3 + span", elm).text());
		$("#infobox p.description").html($("p.description", elm).html());
		
		var ibHeight = $("#infobox").height();
		var isAbove = (elm.offset().top + 69 + ibHeight) > ($(window).height() + $(window).scrollTop());
		if(isAbove)
			$("#infobox").addClass("above");
		else
			$("#infobox").removeClass("above");
			
		
		//Position
		if(isAbove) {
			$("#infobox").css({
				top: elm.offset().top - ibHeight + 15,
				left: elm.offset().left - 20
			});
		} else {
			$("#infobox").css({
				top: elm.offset().top + 69,
				left: elm.offset().left - 20
			});
		}
		
		//Fade in
		clearTimeout(ptVideoInfoBoxTimeout);
		ptVideoInfoBoxTimeout = window.setTimeout(function() {
			$("#infobox").stop().css("display", "block").fadeTo(180, 1);
		}, 200);
	}, function() { //Out
		var elm = this;
		clearTimeout(ptVideoInfoBoxTimeout);
		ptVideoInfoBoxTimeout = window.setTimeout(function() {
			if(!ptVideoInfoBoxHovered)
				$("#infobox").stop().fadeTo(180, 0, function(){$(this).css("display", "none")});
		}, 100);
	});
	
	$("#infobox").hover(
		function() { //Over
			ptVideoInfoBoxHovered = true;
		},
		function() { //Out
			ptVideoInfoBoxHovered = false;
			ptVideoInfoBoxTimeout = window.setTimeout(function() {
				$("#infobox").stop().fadeTo(180, 0, function(){$(this).css("display", "none")});
			}, 100);
		});
});

