/** 
 * @description		Widget.Fader
 * @requires		prototype.js 1.6, effects.js 1.8
*/
if(typeof Widget == "undefined") Widget = {};

Widget.Fader = Class.create({
	initialize: function(img, list, options) {
		this.img = $(img);
		this.list = list;
		this.options = Object.extend({
			id: this.img.id,
			fadeInDuration: 2.5,
			fadeOutDuration: 1.5,
			displayDuration: 2.5,
			autoSize: false,
			autoStart: true,
			attributes: {},
			dir: "",
			beforeFade: null,
			startIndex: 0,
			builder: Widget.Fader.imageBuilder
		}, options || {});
		this.options.attributes["id"] = this.options.id;

		this.index = this.options.startIndex;
		this.container = $(this.img.parentNode);
		this.loadedObserver = this.loaded.bind(this);
		this.fadeInObserver = this.fadeIn.bind(this);
		this.nextObserver = this.next.bind(this);

		if(this.options.autoStart) {
			setTimeout(this.start.bind(this), this.options.displayDuration * 1000);
		}
	},

	start: function() {
		this.stopped = false;
		this.next();
	},

	stop: function() {
		this.stopped = true;
		try { clearTimeout(this.timeout); } catch(ex) { }
		try { Effect.Queues.get(this.options.id).each(function(effect) { effect.cancel() }) } catch(ex) { }
		if(this.oldImg) {
			this.img = this.oldImg;
			--this.index;
		}
		Element.setOpacity(this.img, 1);
	},

	next: function() {
		this.oldImg = this.img;
		if(this.stopped || this.list.length == 0) {
			return;
		}
		++this.index;
		if(this.index >= this.list.length) {
			this.index = 0;
		}
		this.img = this.options.builder(this, this.list[this.index], this.loadedObserver);

	},

	loaded: function() {
		Event.stopObserving(this.img, "load", this.loadedObserver);
		if(typeof this.options.beforeFade == "function") {
			this.options.beforeFade(this.oldImg, false);
		}
		new Effect.Opacity(this.oldImg, { duration: this.options.fadeOutDuration, from: 1.0, to: 0.0, queue: { scope: this.options.id } });
		this.timeout = setTimeout(this.fadeInObserver, this.options.fadeOutDuration * 1000);
	},

	fadeIn: function() {
		if(typeof this.options.beforeFade == "function") {
			this.options.beforeFade(this.img, true);
		}
		this.img.id = this.id;
		Element.setOpacity(this.img, 0);
		if(this.options.autoSize) {
			this.resize(this.img);
		}
		this.container.replaceChild(this.img, this.oldImg);
		this.oldImg = null;
		new Effect.Opacity(this.img, { duration: this.options.fadeInDuration, from: 0.0, to: 1.0, queue: { scope: this.options.id } });
		this.timeout = setTimeout(this.nextObserver, (this.options.fadeInDuration + this.options.displayDuration) * 1000);
	},

	resize: function(img) {
		var dim = this.container.getDimensions();
		dim.width -= parseInt(this.container.getStyle("padding-left")) +
			parseInt(this.container.getStyle("padding-right")) +
			parseInt(this.container.getStyle("border-left-width")) +
			parseInt(this.container.getStyle("border-right-width"));
		dim.height -= parseInt(this.container.getStyle("padding-top")) +
			parseInt(this.container.getStyle("padding-bottom")) +
			parseInt(this.container.getStyle("border-top-width")) +
			parseInt(this.container.getStyle("border-bottom-width"));

		var dw = dim.width / img.width;
		var dh = dim.height / img.height;
		var w1 = img.width * dh;
		var h1 = img.height * dw;

		if(dw > dh) {
			img.width = w1;
			img.height = dim.height;
		} else {
			img.width = dim.width;
			img.height = h1;
		}
	}
});

Widget.Fader.imageBuilder = function(fader, item, loaded) {
	var img = new Element("img", fader.options.attributes);
	img.observe("load", loaded);
	img.src = fader.options.dir + item;
	return img;
};

Widget.Fader.textBuilder = function(fader, item, loaded) {
	var div = new Element("div", fader.options.attributes).update(item);
	loaded.defer();
	return div;
};

var Fader = Widget.Fader;
