var currentItem = -1;

var currentListType = "playlist";
var currentListId = -1;
var currentFromList = true;

var noplaylist = false;

var _queue = new Array();

function initQueue()
{
    if (currentListType == "playlist") {
	if (document.getElementById("RandomPlay").checked){
		startPlaylistQueue(true);
	}
	else {
		startPlaylistQueue(false);		
	}
    }
    else if (currentListType == "houselist"){
	if (document.getElementById("RandomPlay").checked){
		startHouselistQueue(currentListId, true);
	}
	else {
		startHouselistQueue(currentListId, false);		
	}
    }
    else if (currentListType == "referrallist"){
	if (document.getElementById("RandomPlay").checked){
		startReferrallistQueue(currentListId, true);
	}
	else {
		startReferrallistQueue(currentListId, false);		
	}
    }

}

//Called after items are added to personal playlist
function rebuildQueue()
{
	if (currentListType != "playlist") {
		noplaylist = true;
	}

	if (document.getElementById("RandomPlay").checked) {
		rebuildPlaylistQueue(true, noplaylist);
	}
	else {
		rebuildPlaylistQueue(false, noplaylist);		
	}

}

function startPlaylistQueue(random) {
	
	var ajaxRequest = GetRequest()

	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var response = ajaxRequest.responseText;
			if (response != null)
			{
				setQueue(JSON.parse(response));
				currentItem = -1;	
				if (noplaylist)
					getDefaultHouseList();
				else
					playNextInQueue();
			}
		}
	}

	showRemoveAll();

	currentListType = "playlist";
	currentListId = -1;

	var queryString = "?func=playlistdata&random=" + random;
	ajaxRequest.open("GET", "index.php" + queryString, true);
	ajaxRequest.send(null);

}

function startHouselistQueue (listid, random) {

	var ajaxRequest = GetRequest()

	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var response = ajaxRequest.responseText;
			if (response != null)
			{
				setQueue(JSON.parse(response));
				currentItem = -1;	
				playNextInQueue();
			}
		}
	}

	hideRemoveAll();

	currentListType = "houselist";
	currentListId = listid;

	var queryString = "?func=houselistdata&random=" + random + "&listid=" + listid;
	ajaxRequest.open("GET", "index.php" + queryString, true);
	ajaxRequest.send(null);

}

function startReferrallistQueue (listid, random) {

	var ajaxRequest = GetRequest()

	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var response = ajaxRequest.responseText;
			if (response != null)
			{
				setQueue(JSON.parse(response));
				currentItem = -1;	
				playNextInQueue();
			}
		}
	}

	hideRemoveAll();

	currentListType = "referrallist";
	currentListId = listid;

	var queryString = "?func=playlistdata&random=" + random + "&referral=true&listid=" + listid;
	ajaxRequest.open("GET", "index.php" + queryString, true);
	ajaxRequest.send(null);

}

//Called after items are added to personal playlist
function rebuildPlaylistQueue(random, playnow)
{
	var ajaxRequest = GetRequest()
	var playnow = (playnow == null) ? false : playnow;


	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var response = ajaxRequest.responseText;
			if (response != null)
			{
				showRemoveAll();
				setQueue(JSON.parse(response));
				if (playnow) {
					currentItem = -1;
					playNextInQueue();
				}
			}
		}
	}

	currentListType = "playlist";
	currentListId = -1;

	var queryString = "?func=playlistdata&random=" + random;
	ajaxRequest.open("GET", "index.php" + queryString, true);
	ajaxRequest.send(null);

}

function setQueue(data)
{

	var tmpQueue = new Array();

	if (data.length == 0){
		noplaylist = true;
		return;
	}

	for(var i=0; i<data.length; i++) { 
		tmpQueue.push({brightcoveId: data[i].brightcoveID, playerId: data[i].playerID, publisherId: data[i].publisherID});
	}

	_queue = tmpQueue; 
	noplaylist = false;
}

function addToQueue(brightcoveID, playerID, publisherID)
{
	_queue.unshift({brightcoveId: brightcoveID, playerId: playerID, publisherId: publisherID});
}

function removeFromQueue(brightcoveId, playerId, publisherId) {

	for (i=0; i < _queue.length; i++)
	{
		if (_queue[i].brightcoveId == brightcoveId && _queue[i].playerId == playerId && _queue[i].publisherId == publisherId) {
			if (currentItem > i ) {
				currentItem--;
			}
			_queue.splice(i, 1)
			break;
		}
	}
}

function getCurrentPos(playerId, brightcoveId, publisherId) {

	for (i=0; i < _queue.length; i++)
	{
		if (_queue[i].brightcoveId == brightcoveId && _queue[i].playerId == playerId && _queue[i].publisherId == publisherId) {
			return i;
		}
	}
}

function playList(playerId, brightcoveId, publisherId, videoId) {

	currentFromList = true;
	
	play(playerId, brightcoveId, publisherId, videoId);
	
	currentItem = getCurrentPos(playerId, brightcoveId, publisherId);

}

function playVideo(playerId, brightcoveId, publisherId, videoId) {
	currentFromList = false;

	play(playerId, brightcoveId, publisherId, videoId);
}


function playNextInQueue() {	
		
	if (_queue.length <= currentItem + 1){
		currentItem = -1;
	}

	if (_queue.length > 0 ) {
		if (currentFromList) 
			currentItem++;

		playList(_queue[currentItem].playerId, _queue[currentItem].brightcoveId, _queue[currentItem].publisherId);		
	}
	else if (!noplaylist) {			   
		initQueue();
	}	
}

function play(playerId, brightcoveId, publisherId, videoId) {

	var vid_banner = document.getElementById('expandedBanner');
	
	if (videoId != undefined)
	{
		var ajaxRequest = GetRequest();
		var querystring = "?func=VideoPlayed&videoId=" + videoId;
		ajaxRequest.open("GET", "index.php" + querystring, true);
		ajaxRequest.send(null);
	}		
	  
    if (vid_banner != undefined) {
	    vid_banner.innerHTML = '';
	    vid_banner.style.display='none';		   
    }   
	
	
	if (playerId == '8724787001' && vid_banner != undefined){
	    vid_banner.style.display='block';		   
	}

	playBrightcoveVideo(publisherId, playerId, brightcoveId);
		
}

function playBrightcoveVideo(pubId, playerId, videoId) {
    //alert(pubId + "|" + playerId + "|" + videoId);    
    var videoArray = Array();
    videoArray[0] = pubId;
    videoArray[1] = playerId;
    videoArray[2] = videoId;
    document.getElementById("flexispot_0").flexIn("flexispot_0", "playVideo", videoArray);
}

 function setVideoDisplayName(displayName) {
 	var vid_title = document.getElementById('VideoTitle');
	vid_title.innerHTML = "<span class=\"VideoNote\">YOU ARE WATCHING:</span><br /><span class=\"VideoTitle\">" + displayName + "</span>";
}

//*********************************************************************
function flexOut(instanceName, commandName, commandParmsArray){
    //alert(commandName);
    if (commandName == "flexIsReady") {       
        initQueue();
    }
	else if (commandName == "videoComplete"){	    
	    playNextInQueue();		
	}
	else if (commandName == "videoTitleChange"){	    
	    setVideoDisplayName(commandParmsArray[0]);
	}
}
//*********************************************************************



