/************* Start Splash Bubble Code ********************/

var max = 0;
var timeout = null;
var splashDivArray = null;
var previousIndex = 0;
var nextIndex = 0;
var currentDOMObject;
var nextDOMObject;
var firstTime=true;
var insertionIndex = 0;
var currentDiv = null;
var nextDiv = null;
var debug = false;
var disabled = false;
var firstMove = true;
var isPlaying = false;
var pauseButton = null;
var playButton = null;
var buttonsvisible = false;

/************* Player Controls ********************/

function klawplay(){
	if(disabled){
		if(debug){
			alert('play called on disabled player');
		}
		return;
	}

	if(firstTime){
		init();
	}else{
		if(isPlaying){
			progressIndices();
		}
		
		resetDivs();
		showNextDiv();
	}
	
	divTimeout();
	
	if(!isPlaying){
		togglePausePlay();
	}
	if(!buttonsvisible){
		wweshowButtons();
	}
}

function wweshowButtons(){
	var buttons = document.getElementById("buttons1");
	buttons.style.display="inline";
	buttonsvisible = true;
	
}

function klawpause(){
	if(disabled){
		if(debug){
			alert('pause called on disabled player');
		}
	return;
	}
	
	if(timeout != null){
		window.clearTimeout(timeout);
		timeout = null;
		firstMove=true;
		if(isPlaying){
			togglePausePlay();
		}
	}
}

function klawback(){
	if(disabled){
		if(debug){
			alert('back called on disabled player');
		}
		return;
	}

	klawpause();
	regressIndices();
	resetDivs();
	showNextDiv();
}

function klawforward(){
	if(disabled){
		if(debug){
			alert('forward called on disabled player');
		}
		return;
	}

	klawpause();
	progressIndices();
	resetDivs();
	showNextDiv();

}

function init(){
	firstTime=false;
	resetDivs();
	getPausePlayButtons();
}


/**************************** Splash Helper Functions ***************************/
function addSplashDiv(splashDiv){
	splashDivArray[insertionIndex] = splashDiv;
	insertionIndex++;
}

function getPausePlayButtons(){
	pauseButton = document.getElementById("pause");
	playButton = document.getElementById("play");
}

function togglePausePlay(){
	if(!isPlaying){
		pauseButton.style.display="inline";
		playButton.style.display="none";
		isPlaying = true;
	}else{
		playButton.style.display="inline";
		pauseButton.style.display="none";
		isPlaying = false;
	}
}

function progressIndices(){
	previousIndex = nextIndex;
	if((previousIndex + 1) < max){
		nextIndex = previousIndex + 1;
	}else{
		nextIndex = 0;
	}
}

function resetDivs(){
	nextDiv = splashDivArray[nextIndex];
	previousDiv = splashDivArray[previousIndex];
}
  
function divTimeout(){
	var delay = nextDiv.delay;
	if(isNaN(delay)){
		if(debug){
			alert('delay for timeout is not a number');
		}
		disable();
		return;
	}

	timeout = window.setTimeout("klawplay()",delay);
}

function regressIndices(){
	previousIndex = nextIndex;

	if((previousIndex - 1) >= 0){
		nextIndex = previousIndex - 1;
	}else{
		nextIndex = max -1;
	}
}

function showNextDiv(){
	var previousDivSortOrder = previousDiv.sortOrder;
	var nextDivSortOrder = nextDiv.sortOrder;
  	
	if(isNaN(previousDivSortOrder) || isNaN(nextDivSortOrder)){
		if(debug){
			alert('sort order returned by div is not a number');
		}
		disable();
		return;
  	}
  	
  	previousDOMObject = document.getElementById("bubble_left_" + previousDivSortOrder);
  	nextDOMObject = document.getElementById("bubble_left_" + nextDivSortOrder);
  	
  	if(previousDOMObject == null || nextDOMObject == null){
  		if(debug){
  			alert('unable to retrieve one of the DOM Objects');
  		}
  		disable();
  		return;
  	}
    
    previousDOMObject.className = "left offscreen";
    nextDOMObject.className = "left";
}

function disable(){
	disabled = true;
	if(timeout != null){
		window.clearTimeout(timeout);
	}
}

/************************* Helper Object Encapsulates properties for each Div*******************/

function splashDiv(delay,sortOrder){
	var defaultDelay = 3;
	if(delay == "-1"){
		delay = defaultDelay;
	}
	
	this.delay = delay * 1000;
	this.sortOrder = sortOrder;
}