var player = null;
var currentItem = -1;
var preBuffer = false;
var playerIndex = 0;

function playerReady(obj) {
	player = document[obj.id];
	player.addControllerListener('ITEM', 'itemMonitor');
	player.addModelListener('LOADED', 'loadedMonitor');

	//...to play a specific item using getQueryParamValue('item')
	if((playerIndex != null) && (playerIndex < player.getPlaylist().length)) {
		player.sendEvent('ITEM', (playerIndex - 1));
		playerIndex = null;
	}
};

function itemMonitor(obj) {
	if(currentItem != obj.index) {
		//...global for use in other functions
		currentItem = obj.index;
		preBuffer   = false;
	}
};

function loadedMonitor(obj) {
	if(((obj.loaded / obj.total) > .5) && (!preBuffer)) {
		var playlist = player.getPlaylist();
		var url = playlist[currentItem + 1]['file'].replace("../","");
		//added replace to remove the ../ from the array (needed to keep videos on the root)
		preBuffer = true;
		sendRequest(url);
	}
};

function sendRequest(url) {
	$.get(url, function(){
		preBuffer = false;
	});
}

function createPlayer() {
	var flashvars = {
		file:"images/BB_Color_Bars.jpg",
		image:"images/BB_Color_Bars.jpg",
		autostart:"true",
		bufferlength:"1",
		icons:"false",
		controlbar:"none",
		playlist:"none",
		playlistsize:"0",
		shuffle:"false",
		stretching:"exactfit",
		repeat:"list"
	}

	var params = {
		allowfullscreen:"false",
		allowscriptaccess:"always",
		wmode:"opaque"
	}

	var attributes = {
		id:"player1",
		name:"player1"
	}

	swfobject.embedSWF('flash/player.swf', 'videoPlayer', '320', '243', '9.0.124', false, flashvars, params, attributes);
}

$(window).load(function() {
	createPlayer();

	$("#blenderButtons").click(function(e){
		player.sendEvent('ITEM', 0);
		player.sendEvent('LOAD',loadMovies());
	})
	
	$("#headerWrap").click(function(){//dont think we are using this
		player.sendEvent('ITEM', 0);//resets to the beginning
		player.sendEvent('LOAD',videoList);//reloads the playlist
	});
});

function loadMovies(){
	var videoList = new Array();
	var randomVideoNum;
	var frame = $('#frame').attr('value');
	var totalVideos = vidArray[frame].files.length;
	var vidMax = Math.min(10,totalVideos);
	var numOfVideos = getRandom(Math.max(1,(vidMax-4)),vidMax,numOfVideos,videoList,0);

	for(i=0;i<numOfVideos;i++){
		randomVideoNum = getRandom(0,(vidArray[frame].files.length-1),randomVideoNum,videoList,((totalVideos<100)?((totalVideos<20)?0:2):10));
		videoList[i] = {
			file:"../videos/"+vidArray[frame].name+"/"+vidArray[frame].files[randomVideoNum],
			image:"images/BB_Color_Bars.jpg",
			type:"video",
			num:randomVideoNum
		}
	}

	videoList[numOfVideos] = {
			file:"../videos/BB_Color_Bars.flv",
			image:"images/BB_Color_Bars.jpg",
			type:"video",
			num:0
	};

	//console.log(videoList);

	return videoList;
}

function getRandom(vidMin,vidMax,previousNum,videoList,range){
	var num;

	num = Math.round(Math.random() * (vidMax - vidMin) + vidMin);

	num = ( ((videoList.length > 1)?videoList.in_array(num):false) || (Math.abs(num-previousNum) <= range) )?getRandom(vidMin,vidMax,previousNum,videoList,range):num;

	return num
}

//Customized the in_array to account for the object num (normal in_array doesnt have the .num in it)
Array.prototype.in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i].num == p_val) {
			return true;
		}
	}
	return false;
}