/**
* Hubbitus SlideShow
*
* @package slideShow
* @version 1.0
* @author	 Pahan-Hubbitus (Pavel Alexeev) <Pahan@Hubbitus.info>
* @copyright Copyright (c) 2009, Pahan-Hubbitus (Pavel Alexeev)
* @created 2009-07-03
**/

/**
* Mix-in object to track current element.
* Provide current/next element tracking and cycle it.
*
* Intended to extend Array object over Object.extend.
**/
next_track = {
	current_: 0
	,count_:0 // All amount of nextID call
	,nextID: function(){
	++this.count_;
			if (this.length == ++this.current_) this.current_ = 0;// Cycle
		return this.current_;
	}
	,currentID: function(){
		return this.current_;
	}
}

/**
* slideShow_images extended to track current/next
**/
Object.extend(slideShow_images, next_track);
Object.extend(
	slideShow_images
	,
	{
		currentSRC: function(){
		return this[this.currentID()];
		}
		,
		nextSRC: function(){
		return this[this.nextID()];
		}
	}
);

/**
* slideShow_effects extended to track current/next
**/
Object.extend(slideShow_effects, next_track);
Object.extend(
	slideShow_effects
	,
	{
		nextEffect: function(){
			return this[this.nextID()];
		}
		,
		currentEffect: function(){
			return this[this.currentID()];
		}
	}
)

////////////////////////////////////////////////////////////////////////////////
/**
* Constructor
*
* @param	imgs	Object(Array+)
* @param	basedir	string	Base directory with trailing slash!
* @param	effects	Object(Aray+) Array of effect closures.
* @param	slideBoxId	string. SlideBoxImg ID
**/
function slideShow(imgs, basedir, effects, slideBoxId){
var images_= imgs;
this.basedir = basedir;
var effects_ = effects;
var sid_ = slideBoxId; //SlideBoxImg ID

imgs.current_ = 0; // Start

	/**
	* Start slideshow
	*
	* @param	interval	integer Milliseconds interval between slides.
	* @return	nothing
	**/
	this.start = function(interval){
	//Image preloading
	var imPreloader = new Image();
	images_.each(function(img){
		imPreloader.src = basedir + img;
		}
	);
		// Default value for interval
		if (!interval) interval = 4000;
	this.timerId_ = setInterval(this.nextSlide, interval);
	}

	/**
	* Change slide
	**/
	this.nextSlide = function(){
	new (effects_.nextEffect().hide.effect)(
		effects_.currentEffect().hide.id
		,Object.extend( //Options
			effects_.currentEffect().hide.options || { }
			,{
				afterFinish: function(){
//				window.status = 'afterFinish():: ImageID: ' + images_.currentID() + '; ImageCurrentSRC: ' + images_.currentSRC() + '; effectsCurrentID: ' + effects_.currentID() + '; Total cycles: ' + effects_.count_;
				$(sid_).src = basedir + images_.nextSRC();
				/*
				* For stupid browsers who ignore "Cache-Control max-age" header and don't cache images give them time to change
				* image (it sent HTTP-request and got 304 Not Modifyed header, so it doues not require many time) before 
				* new effect started.
				* I think 1000ms should be enough
				**/
				setTimeout(
					function(){
						new (effects_.currentEffect().show.effect)(
							effects_.currentEffect().show.id
							,effects_.currentEffect().show.options || { }
						);
					}
					,1000
				);
				}
			}
		)
	);
	}

	/**
	* Stop running slideShow
	*
	* @return	nothing
	**/
	this.stop = function(){
	clearInterval(this.timerId_);
	}
}

// INIT
slideshow = new slideShow(slideShow_images, slideShow_imagesdir, slideShow_effects, slideBoxImg);
Event.observe(window, 'load', function (){ slideshow.start(slideShow_interval); });
