/*
* Crossfade
* Version 2.0 28/02/2007
* 
* Copyright (c) 2007 Millstream Web Software http://www.millstream.com.au
* 
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* * 
*/

var Crossfade = Class.create();

Crossfade.prototype = {
	_queue : 0,
	loaded : false,
	initialize : function(elm, options) {
		this.elm = $(elm);
		this.counter = 0;
		this.prevSlide = null;
		this.options = Object.extend(Crossfade.defaults, options || {});
		this.slides = this.elm.immediateDescendants();
		this.elm.makePositioned();
		var self = this;
		if(this.options.random){
			this.slides.sort(function(a,b){
				return self.rndm(-1,1);
			});
		}
		this.slides.each(function(s,i){
			$(s).setStyle({opacity:(i === 0 ? 1 : 0),visibility:'visible'});
			if(i === 0 || i === 1) {
				self.loadSlide(s);
			}
		});
		setTimeout(this.start.bind(this),this.rndm(100,this.options.interval*1000));
	},
	start: function() {
		this.cycle()
		return new PeriodicalExecuter(this.cycle.bind(this), this.options.interval); 
	},
	cycle: function() {
		this.counter += 1;
		if(this.counter > this.slides.length){
			this.counter = 1;
			this.loaded = true;
		}
		var prevSlide = this.slides[this.counter-1];
		var nextSlide = this.slides[(this.counter === this.slides.length ? 0 : this.counter)]; //(this.counter >= this.slides.length) ? 0 : this.counter
		
		if(!this.loaded) {
			this.loadSlide(nextSlide);
			this.loadSlide(this.slides[this.counter+1]);
		}
		var opt = this.options;
		var self = this;
		var fade = new Effect.Parallel([new Effect.Fade(prevSlide ,{sync:true}),
			new Effect.Appear(nextSlide,{sync:true})],
			{duration: opt.duration, queue : 'crossfade'}
		);
	},
	loadSlide : function(slide){
		var loaders = [], img;
		slide = $(slide);
		loaders = Selector.findChildElements(slide, ['a.load']);
		if(loaders.length && loaders[0].href !== ''){
			img = '<img src="' + loaders[0].href + '" />';
			$(loaders[0]).replace(img);
		}
	},
	rndm : function(min, max){
		return Math.floor(Math.random() * (max - min + 1) + min);
	}
};
Crossfade.defaults = {
	autoStart : true,
	random : false,
	selectors : ['.crossfade'],
	interval : 5,
	duration : 2
};
Crossfade.setup = function(options) {
	Object.extend(Crossfade.defaults,options);
};
Crossfade.load = function() {
	if(Crossfade.defaults.autoStart) {
		Crossfade.defaults.selectors.each(function(s){
			$$(s).each(function(c){
				return new Crossfade(c);
			});
		});
	}
};

if(window.FastInit) {
	FastInit.addOnLoad(Crossfade.load);
} else {
	Event.observe(window, 'load', Crossfade.load);
}
