var SlideTickInterval = 33;

var Slideshows = new Array();
var SlideshowsMeta = new Array();
var Controllers = new Array();

function SlideshowsTick() {
	for( i = 0; i < Controllers.length; i++ ) {
		var Controller = Controllers[i];	
		if( Controller.NextSlideshow != null ) {
			Controller.CurrentSlideshow = Controller.NextSlideshow;
			Controller.NextSlideshow = null;
		}
		
		var Running = false;
		if( Controller.Playing ) {
			Running = true;
			if( Controller.CurrentSlideshow != null && Slideshows[Controller.CurrentSlideshow] != null ) {
				if( Controller.PauseTicks < 1 ) {
					Controller.FadeTicks--;
					if( Controller.FadeTicks < 1 ) {
						Controller.HideSpinner();
					
						Controller.FadeTicks = Controller.FadeTicksMax;
						Controller.PauseTicks = Controller.PauseTicksMax;

						var CurrentSlide = Slideshows[Controller.CurrentSlideshow].slide;
						CurrentSlide++;
						if( CurrentSlide >= Slideshows[Controller.CurrentSlideshow].count ) {
							CurrentSlide = 0;
							SlideshowsMeta[Controller.CurrentSlideshow].DoCallbackOnEnd( Controller.CurrentSlideshow );
						}
						Slideshows[Controller.CurrentSlideshow].slide = CurrentSlide;
						
						Controller.ShowSlideB = !Controller.ShowSlideB;
						if( Controller.ShowSlideB ) {
//							console.log( 'Slide B' ); 
							Controller.SlideA.style.zIndex = 20;
							Controller.SlideB.style.zIndex = 21;
							Controller.SlideA.style.opacity = 1;
							Controller.SlideB.style.opacity = 1;
							Controller.SlideA.style.backgroundImage = 'url(' + Slideshows[Controller.CurrentSlideshow].slides[CurrentSlide].file + ')';
							if( Controller.TextA )
								Controller.TextA.innerHTML = Slideshows[Controller.CurrentSlideshow].slides[CurrentSlide].text;
						} else {
//							console.log( 'Slide A' ); 
							Controller.SlideA.style.zIndex = 21;
							Controller.SlideB.style.zIndex = 20;
							Controller.SlideA.style.opacity = 1;
							Controller.SlideB.style.opacity = 1;
							Controller.SlideB.style.backgroundImage = 'url(' + Slideshows[Controller.CurrentSlideshow].slides[CurrentSlide].file + ')';
							if( Controller.TextB )
								Controller.TextB.innerHTML = Slideshows[Controller.CurrentSlideshow].slides[CurrentSlide].text;
							else if( Controller.TextA )
								Controller.TextA.innerHTML = Slideshows[Controller.CurrentSlideshow].slides[CurrentSlide].text;
						}
					}
					var Opacity = Controller.FadeTicks / Controller.FadeTicksMax;
					if( Controller.ShowSlideB )
						Controller.SlideB.style.opacity = Opacity;
					else
						Controller.SlideA.style.opacity = Opacity;
				} else
					Controller.PauseTicks--;
			}
		}
	}
	
	if( Running )
		window.setTimeout( 'SlideshowsTick()', SlideTickInterval );
}

function SlideshowMeta() {
	this.Loaded = false;
	this.CallbackOnEnd = null;
	this.CallbackOnEndScope = null;
	
	this.DoCallbackOnEnd = function( i_CurrentSlideshow ) {
		if( this.CallbackOnEnd != null ) {
			if( this.CallbackOnEndScope != null )
				this.CallbackOnEnd.call( this.CallbackOnEndScope, i_CurrentSlideshow );
			else
				this.CallbackOnEnd( i_CurrentSlideshow );
		}
	}
}

function SlideshowController( i_SlideA, i_SlideB, i_TextA, i_TextB ) {
	this.FadeTimerMax = 2.0;
	this.PauseTimerMax = 5.5;
	
	this.FadeTicksMax = Math.round( this.FadeTimerMax * 1000 / SlideTickInterval );
	this.PauseTicksMax = Math.round( this.PauseTimerMax * 1000 / SlideTickInterval );

	this.CurrentSlideshow = null;
	this.NextSlideshow = null;
	
	this.ShowSlideB = 0;
	this.FadeTicks = 0;
	this.PauseTicks = 0;
	
	this.SlideA = i_SlideA;
	this.SlideB = i_SlideB;
	this.TextA = i_TextA;
	this.TextB = i_TextB;
	
	this.Playing = false;
	
	this.Spinner = null;
	this.SpinnerCount = 0;
	
	this.ShowSpinner = function( Count ) {
		if( Count )
			this.SpinnerCount = Count;
		else
			this.SpinnerCount++;
		
		if( this.Spinner != null )
			this.Spinner.style.visibility = 'visible';
	}
	
	this.HideSpinner = function() {
		this.SpinnerCount--;
		
		if( this.SpinnerCount < 1 ) {
			this.SpinnerCount = 0
			if( this.Spinner != null )
				this.Spinner.style.visibility = 'hidden';
		}
	}
	
	this.onGetSlideshowResponse = function( httpResponse ) {
		this.HideSpinner();

		var Response = JSON.parse( httpResponse );
		Slideshows[Response.id] = Response;
		
		SlideshowsMeta[Response.id].Loaded = true;
	}
	
/* public */

	this.JSONRequestSlideshow = function( Id, Category, Limit ) {
		var Request = '/hslide.json.php?id=' + Id;
		if( Category ) Request += '&cat=' + Category;
		if( Limit ) Request += '&limit=' + Limit;
		AsyncHTTPRequest( Request, this.onGetSlideshowResponse, this );
		
		SlideshowsMeta[Id] = new SlideshowMeta();
		
		this.ShowSpinner();
	}

	this.SetNextSlideshow = function( Id ) {
		this.NextSlideshow = Id;
		this.ShowSpinner( 1 );
		if( this.CurrentSlideshow != null ) this.ShowSpinner();
	}
	
	this.SetCallbackOnEnd = function( Id, i_CallbackFunction, i_Scope ) {
		SlideshowsMeta[Id].CallbackOnEnd = i_CallbackFunction;
		SlideshowsMeta[Id].CallbackOnEndScope = i_Scope;
	}
	
	this.SetSpinner = function( i_Spinner ) {
		this.Spinner = i_Spinner;
	}
	
	this.Play = function() {
		this.Playing = true;
		window.setTimeout( 'SlideshowsTick()', SlideTickInterval );
	}
	
	this.Pause = function() {
		this.Playing = false;
	}
	
	this.Rewind = function() {
		if( this.CurrentSlideshow != null && Slideshows[this.CurrentSlideshow] != null )
			Slideshows[this.CurrentSlideshow].slide = 0;
	}
	
	this.Previous = function() {
		if( this.CurrentSlideshow != null && Slideshows[this.CurrentSlideshow] != null ) {
			var CurrentSlide = Slideshows[this.CurrentSlideshow].slide;
			CurrentSlide = CurrentSlide - 2;
			if( CurrentSlide < 0 )
				CurrentSlide += Slideshows[this.CurrentSlideshow].count;
			Slideshows[this.CurrentSlideshow].slide = CurrentSlide;
		}
		this.PauseTicks = 0;
		this.Playing = true;
	}

	this.Next = function() {
		this.PauseTicks = 0;
		this.Playing = true;
	}
	
	Controllers.push( this );
}
