// VIDEO LISTENER VARIABLES
var currentState = "NONE"; 
var previousState = "NONE"; 
var player = null;
function cacheHack(){
	return "?t=" +new Date().getTime();
}
function playerReady(thePlayer) {
	player = document.getElementById(thePlayer.id);
	addListeners();
}
// ADD LISTENER FOR VIDEO
function addListeners() {
	if (player) { 
		player.addModelListener("STATE", "stateListener");
	} else {
		setTimeout("addListeners()",100);
	}
}
// LISTEN FOR VIDEO TO STOP PLAYING AND GO TO NEXT VIDEO
function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
	currentState = obj.newstate; 
	previousState = obj.oldstate; 
	if ((currentState == "COMPLETED")&&(previousState == "PLAYING")) {
		// Doing nothing
	}
}
// JQUERY READY
$(document).ready(function(){			   
	// count vidoes and assign them numbers
	var count = $(".item").size();
	if(count < 3){
		$("#vidscroll #vid_playlist ").css("height","auto");
	}
	var first =  $(".item:first").attr("rel");
	var flashvars = {
	'title':document.title,
	'overstretch':'true',
	'skin':'',
	'bufferlength':1,
	'autostart':'false',
	'icons':'false',
	'image':'/events/homeland/graphics/placeholder.jpg',
	'controlbar':'over',
	'dock':'true',
	'playlist':'none',
	'file':first,
	'stretching':'fill',
	'screencolor':'0xFFFFFF',
	'plugins':'',
	'sharing.link':window.location
	};
	var params = {
	'wmode':'opaque',
	'allowScriptAccess':'always',
	'allowfullscreen':'true',
	'quality':'high',
	'play':'true'
	};
	var attributes = {
	id:"player1",  
	name:"player1"
	}
	//swfobject.removeSWF("player1");
	swfobject.embedSWF("/events/homeland/busintro.swf"+ cacheHack(), "placeholder", "458", "260", "9.0.0", "/styles/expressInstall.swf",flashvars, params, attributes);
	$(".item").each(function (i) {
		// get current number
		var vidnum = $(".item").index(this) + 1;
		// add attributes to <a> link
		$(this).attr({
			tabindex: vidnum + 1,
			title: "Video " + vidnum
		});
		// set span for .item .number
		$(this).find(".number").text(vidnum + " of " + count);
	});
	// click on a video item in playlist
	$(".item").click(function(){
		$("#vid_playlist a").removeClass("selected");					  
		$(this).addClass("selected");
		// SCROLL TO VIDEO THAT IS SELECTED
		$('#vid_playlist')[0].scrollTo(".selected");
		var flashvars = {
		'title':document.title,
		'overstretch':'true',
		'skin':'',
		'bufferlength':1,
		'autostart':'true',
		'icons':'false',
		'image':'/events/homeland/graphics/placeholder.jpg',
		'controlbar':'over',
		'dock':'true',
		'stretching':'fill',
		'screencolor':'0xFFFFFF',
		'plugins':'',
		'sharing.link':window.location
		};
		var params = {
		'wmode':'opaque',
		'allowScriptAccess':'always',
		'allowfullscreen':'true',
		'quality':'high',
		'play':'true'
		};
		var attributes = {
			id:"player1",  
			name:"player1"
		}
		var vidplay = $(this).attr("rel");
		if(vidplay.match(".flv")){
			flashvars.file = vidplay;
		}
		// removes current playing flash if one exists
		if($("#placeholder").length<1 && flashvars.file){
			swfobject.removeSWF("player1");
			$("#vid_active").append('<div id="placeholder"></div>');
		}
		if(flashvars.file){
			//swfobject.removeSWF("player1");
			swfobject.embedSWF("/styles/longtail-vid/player-licensed-viral.swf"+ cacheHack(), "placeholder", "458", "260", "9.0.0", "/styles/expressInstall.swf",flashvars, params, attributes);
		}
	});
	// scrolling video playlist
	$('#vid_playlist').jScrollPane({
		scrollbarWidth: 19,
		dragMinHeight:67,
		dragMaxHeight:67,
		scrollbarMargin:0,
		animateTo:true
	});
	// set last item to have no border
	$(".item:last").addClass("last");
	$(".item:first").addClass("selected");
});
