var FrontPageFade = new Class({ Implements: Options, options: { container: $empty, prevButton: $empty, nextButton: $empty, directLinkContainer: $empty },
	position: 0,
	elements: new Array(),
	links: new Array(),
	selectedElement: $empty,
	timer: $empty,
	autoDelay: 5000,
	initialize: function (options) {
		this.setOptions(options);
		var els = this.options.container.getChildren('div');
		els.each(function (element, index) {
			if (element.hasClass("docRotatorElement")) {
				this.elements[this.elements.length] = element;
				this.options.directLinkContainer.adopt(new Element('a', {href: '#', html: this.elements.length}));
			}
		} .bind(this));

		this.links = this.options.directLinkContainer.getChildren('a');
		this.links.each(function (element, index) {
			element.addEvent('click', this.DirectLinkClicked.bind(this));
		} .bind(this));

		this.options.nextButton.addEvent('click', this.Next.bind(this));
		this.options.prevButton.addEvent('click', this.Previous.bind(this));

		this.Switch(this.position, true);
		//this.timer = this.AutoSwitch.bind(this).delay(this.autoDelay);
	},
	RestartTimer: function () {
		this.timer = $clear(this.timer);
		this.timer = this.AutoSwitch.bind(this).delay(this.autoDelay);
	},
	AutoSwitch: function () {
		this.position++;
		if (this.position == this.elements.length) {
			this.position = 0;
		}
		this.Switch(this.position);
	},
	Next: function (e) {
		this.position++;
		if (this.position == this.elements.length) {
			this.position = 0;
		}

		this.Switch(this.position);

		return false;
	},
	Previous: function (e) {
		this.position--;
		if (this.position < 0) {
			this.position = this.elements.length - 1;
		}

		this.Switch(this.position);

		return false;
	},
	Switch: function (selectedIndex, initial) {
		this.RestartTimer();
		this.links.each(function (element, index) {
			element.removeClass('current');
			if (index == selectedIndex) {
				element.addClass('current');
			}
		});

		this.elements.each(function (element, index) {
			//element.set('style', 'display: none');
			if (index == selectedIndex) {
				if (initial) {
					element.set('style', 'display: absolute');
				}
				else {
					//this.selectedElement.fade('out').get('tween').chain(function() { element.fade('in'); });
					this.selectedElement.fade('out');
					element.fade('in');
				}
				this.selectedElement = element;
			}
			else if (initial) {
				element.set('opacity', '0');
			}
		} .bind(this));
	},
	DirectLinkClicked: function (e) {
		if (e) {
			this.position = parseInt(e.target.get('text')) - 1;
			this.Switch(this.position);
		}
		return false;
	}
});

var Fade = new Class({
	Implements: Chain,
	initialize: function() {
		this.chain.apply(this, arguments);
	}
});

window.addEvent('domready', function() { if ($('featurerotator')) { var fade = new FrontPageFade({ container: $('featurerotator'), prevButton: $('featurerotatorBtnPrev'), nextButton: $('featurerotatorBtnNext'), directLinkContainer: $('featurerotatorCounter') }); } });




Fx.Tween = new Class({

	Extends: Fx.CSS,

	initialize: function(element, options) {
		this.element = this.subject = $(element);
		this.parent(options);
	},

	set: function(property, now) {
		if (arguments.length == 1) {
			now = property;
			property = this.property || this.options.property;
		}
		this.render(this.element, property, now, this.options.unit);
		return this;
	},

	start: function(property, from, to) {
		if (!this.check(arguments.callee, property, from, to)) return this;
		var args = Array.flatten(arguments);
		this.property = this.options.property || args.shift();
		var parsed = this.prepare(this.element, this.property, args);
		return this.parent(parsed.from, parsed.to);
	}

});

Element.Properties.tween = {

	set: function(options) {
		var tween = this.retrieve('tween');
		if (tween) tween.cancel();
		return this.eliminate('tween').store('tween:options', $extend({ link: 'cancel' }, options));
	},

	get: function(options) {
		if (options || !this.retrieve('tween')) {
			if (options || !this.retrieve('tween:options')) this.set('tween', options);
			this.store('tween', new Fx.Tween(this, this.retrieve('tween:options')));
		}
		return this.retrieve('tween');
	}

};

Element.implement({

	tween: function(property, from, to) {
		this.get('tween').start(arguments);
		return this;
	},

	fade: function(how) {
		var fade = this.get('tween'), o = 'opacity', toggle;
		how = $pick(how, 'toggle');
		switch (how) {
			case 'in': fade.start(o, 1); break;
			case 'out': fade.start(o, 0); break;
			case 'show': fade.set(o, 1); break;
			case 'hide': fade.set(o, 0); break;
			case 'toggle':
				var flag = this.retrieve('fade:flag', this.get('opacity') == 1);
				fade.start(o, (flag) ? 0 : 1);
				this.store('fade:flag', !flag);
				toggle = true;
				break;
			default: fade.start(o, arguments);
		}
		if (!toggle) this.eliminate('fade:flag');
		return this;
	},

	highlight: function(start, end) {
		if (!end) {
			end = this.retrieve('highlight:original', this.getStyle('background-color'));
			end = (end == 'transparent') ? '#fff' : end;
		}
		var tween = this.get('tween');
		tween.start('background-color', start || '#ffff88', end).chain(function() {
			this.setStyle('background-color', this.retrieve('highlight:original'));
			tween.callChain();
		} .bind(this));
		return this;
	}

});


