/* =========================================================

// scriptaculous innerfade

// ========================================================= */

var innerFade = Class.create();
innerFade.prototype = {
	options: {
		animationtype: 'fade',
		speed: 400,
		timeout: 200,
		type: 'sequence',
		containerheight: 'auto',
		runningclass: 'innerfade'
	},
	initialize: function(element, options) {
		if(!element.tagName && element.length && element.length == 1) {
			element = $(element[0]);
		} else if(element.tagName) {
			element = $(element);
		}
		Object.extend(this.options, options || {});

		if(!element.childElements) return;
		var elements = element.childElements();

		if(elements.length > 1) {

			element.setStyle({ position: 'relative' });

			element.setStyle({ height: this.options.containerheight });
			element.addClassName(this.options.runningclass);

			elements.each(function(child, i) {
				child.setStyle({
					zIndex: (elements.length - 1),
					position: 'absolute'
				})
			}.bind(this));

			if(this.options.type == 'sequence') {
				setTimeout(function(){
					this.next(elements, 1, 0);
				}.bind(this), this.options.timeout);
				$(elements[0]).show();
			} else if(this.options.type == 'random') {
				setTimeout(function(){
					do {
						current = Math.floor(Math.random() * (elements.length));
					} while(current == 0);
					this.next(elements, current, 0);
				}.bind(this), this.options.timeout);
				$(elements[0]).show();
			}
		}
	},
	next: function(elements, current, last) {
		if (this.options.animationtype == 'slide' ) {
			// $(elements[last]).slideUp(settings.speed, $(elements[current]).slideDown(settings.speed));
		} else if(this.options.animationtype == 'fade' ) {
			// $(elements[last]).fadeOut(settings.speed);
			// $(elements[current]).fadeIn(settings.speed);
			new Effect.Fade(elements[last], {
				duration: (this.options.speed / 1000)
			});
			new Effect.Appear(elements[current], {
				duration: (this.options.speed / 1000)
			});
		}

		if(this.options.type == 'sequence') {
			if((current + 1) < elements.length) {
				current = current + 1;
				last = current - 1;
			} else {
				current = 0;
				last = elements.length - 1;
			};
		} else if(this.options.type == 'random') {
			last = current;
			while(current == last) {
				current = Math.floor(Math.random() * (elements.length));
			};
		}
		setTimeout(function() {
			this.next(elements, current, last);
		}.bind(this), this.options.timeout);
	}
}
