// JavaScript Document
//http://www.dynamicdrive.com/dynamicindex2/crawler/index.htm

var previewTimeout;

var previewContinuous = function(preview,tickerData,previewFreq){
	this.preview = preview;
	this.list = tickerData;
	
	this.previewI = 0;
	
	this.previewOn = false;
	
	this.pFreq = previewFreq;//default miliseconds between transition
	this.currentFreq = previewFreq;//the current time may be overwritten by a specific transition

	//START CONTROL FUNCTIONS
	//These are the functions that should be used for external control, like buttons.
	this.startPreview = function(backwards,delay){//backwards(bool):whether the preview should move left to right),delay(int)[optional]:miliseconds to wait before starting
		if(typeof(delay) == 'undefined'){
			delay = 0;
		}
		//clearTimeout(this.previewTimeout);
		this.previewOn = true;
		this.preview.previewBackwards = backwards;
		//var prev = this;
		//this.previewTimeout = setTimeout(function(){prev.startTransition();},delay);
		this.currentFreq = delay;
		this.next();
	};
	
	this.stopPreview = function(){
		this.previewOn = false;
	};
	
	this.togglePreview = function(backwards){
		if(typeof(backwards) == 'undefined'){
			backwards = this.preview.previewBackwards;
		}
		if(this.previewOn){
			this.stopPreview();
		}else{
			this.startPreview(backwards);
		}
	}
	//END CONTROL FUNCTONS
	
	this.startTransition = function(){
		if(!this.previewOn){
			return;
		}
		var prevContin = this;

		if(this.preview.previewBackwards){
			this.previewI--;
			if(this.previewI < 0){
				this.previewI = this.list.length-1;
			}
		}else{
			this.previewI++;
			if(this.previewI >= this.list.length){
				this.previewI = 0;
			}
		}
		this.currentFreq = this.pFreq;
		if(!isNaN(this.list[this.previewI].time)){
			this.currentFreq = this.list[this.previewI].time;
		}
		
		if(typeof(this.list[this.previewI].onload) == "function"){
			var onload = this.list[this.previewI].onload;
			var callBack = function(){onload(),prevContin.next();};
		}else{
			var callBack = function(){prevContin.next()};
		}
		
		this.preview.preview(this.list[this.previewI].spread,callBack);
	}
	this.next = function(){//do not call directly. use startPreview instead.
		clearTimeout(this.previewTimeout);
		if(this.previewOn){
			var prev = this;
			this.previewTimeout = setTimeout(function(){prev.startTransition();},this.currentFreq);//,time
		}
	}
}

function toggleButtonState(button,previewManager,backwards){
	previewManager.togglePreview(backwards);
	if(previewManager.previewOn){
		button.className = "previewPauseButton";
	}else{
		button.className = "previewPlayButton";
	}
}
function showImageTransition(image,previewSlideshow,transitionObject,endFunction){
	if(typeof(endFunction) != 'function'){
		endFunction = function(){};
	}
	previewSlideshow.stopPreview();
	transitionObject.preview(image,endFunction);
};
var SlideTransition = function(bgID,previewFreqTransistion,pWidth,pSlide,activeContentBox){
	
	this.bgID = bgID;//text id of dom backgrounds
	this.bgElLeft = document.getElementById(this.bgID);//direct reference to background
	this.bgElRight = document.getElementById(this.bgID + "2");//direct reference to background
	
	this.newImage;
		
	this.previewTimeout;
	this.pFreqT = previewFreqTransistion;//time between ticks during tranistion
	this.slide = pSlide;//pixels to slide each tick during transition
	
	this.previewWidth = pWidth;
	this.previewSlide = 0;

	this.activeContentBox = activeContentBox;//DOM element that will hold any related buttons/links/mouse events objects. must be clear each transition.
	
	this.preview = function(newImage,callbackFunction){
		//this.previewBackwards = backwards;
		this.callbackFunction = callbackFunction;
		/*this.bgElLeft.innerHTML = '';
		this.bgElRight.innerHTML = '';*/
		this.activeContentBox.innerHTML = '';
		
		if(this.previewBackwards){
			this.bgElLeft.style.backgroundImage='url('+newImage+')';
		}else{
			this.bgElRight.style.backgroundImage='url('+newImage+')';
		}
		this.newImage = newImage;
		this.previewSlide = this.previewWidth;
		this.previewTransition();
	};
	this.previewTransition = function(){
		this.previewSlide-=this.slide;
		if(this.previewSlide < 0){
			this.previewSlide = 0;
		}
		
		if(this.previewBackwards){
			this.bgElLeft.style.width = (this.previewWidth - this.previewSlide) + 'px';
			this.bgElRight.style.width = this.previewSlide + 'px';
		}else{
			this.bgElRight.style.width = (this.previewWidth - this.previewSlide) + 'px';
			this.bgElLeft.style.width = this.previewSlide + 'px';
		}
		if(this.previewSlide){
			var prev = this;
			this.previewTimeout = setTimeout(function(){prev.previewTransition();},this.pFreqT);
		}else{
			this.previewEndTransition();
		}
	}
	this.previewEndTransition = function(){
		var currentBG;
		if(this.previewBackwards){
			this.bgElRight.style.width = this.previewWidth + 'px';
			this.bgElLeft.style.width = '0px';
			this.bgElRight.style.backgroundImage='url('+this.newImage+')';
			currentBG = this.bgElRight;
		}else{
			this.bgElLeft.style.width = this.previewWidth + 'px';
			this.bgElRight.style.width = '0px';
			this.bgElLeft.style.backgroundImage='url('+this.newImage+')';
			currentBG = this.bgElLeft;
		}
		this.callbackFunction(currentBG);
	}
}
