var rotator = new Class({
	options: {
		timer: 2000,
		pause: false,
		transition: false,
		random: false,
		traverse: false,
		elements: []
	},
	initialize: function(options) {
		this.setOptions(options);
		this.addRotators(this.options.elements);
	},
	addRotators: function(elements) {
		$$(elements).each(function(e) {
			pause = this.options.pause;
			timer = this.options.timer;
			transition = this.options.transition;
			duration = this.options.duration;
			duration = this.options.duration;
			traverse = this.options.traverse;

			wrapper = new Element('div');
			wrapper.innerHTML = e.innerHTML;
			wrapper.style.position = 'relative';
			e.innerHTML = '';
			wrapper.inject(e);

			objects = new Array();
			effects = new Array();

			$$('#' + e.id + ' div.rotator-object').each(function(el) {
				el.style.position = 'absolute';
				el.style.top = '0';
				el.style.left = '0';
				el.setStyle('z-index', 9997);

				effect = new Fx.Tween(el);
				effect.set('opacity', 0);
				effects.push(effect);

				objects.push(el);
			});

			objects[0].setStyle('z-index', 9998);
			effects[0].set('opacity', 1);

			current = 0;
			direction = 1;

			rotate = function() {
				if (objects.length > 1) {
					if (current == objects.length || current < 0) {
						if (traverse) {
							direction = direction * -1;
							current = current + direction;
						} else {
							current = 0;
						}
					}

					next = current + direction;
					previous = current - direction;

					if (next >= objects.length) {
						if (traverse) {
							next = objects.length - 1;
						} else {
							next = 0;
						}
					}

					if (next < 0) {
						next = 0;
					}

					if (previous < 0) {
						if (traverse) {
							previous = 0;
						} else {
							previous = objects.length - 1;
						}
					}

					if (previous >= objects.length) {
						previous = objects.length - 1;
					}

					objects[current].setStyle('z-index', 9998);

					if (next != current) {
						objects[next].setStyle('z-index', 9999);
					}

					if (previous != current) {
						objects[previous].setStyle('z-index', 9997);
					}

					if (previous != current) {
						effects[previous].set('opacity', 0);
					}

					if (transition && next != current) {
						effects[next].start('opacity', 0, 1);
					} else {
						effects[next].set('opacity', 1);
					}

					current = current + direction;
				}
			}

			loop = rotate.periodical(timer);

			e.addEvent('mouseenter', function() {
				if (pause == true) {
					$clear(loop);
				}
			});

			e.addEvent('mouseleave', function() {
				if (pause == true) {
					loop = rotate.periodical(timer);
				}
			});
		}, this);
	}
});
rotator.implement(new Options);

window.addEvent('domready', function() {
	new rotator({
		timer: 1500,
		transition: true,
		traverse: true,
		elements: [$('rotator')]
    });
});