var Cycler = new Class({
    Implements: Options,
    options:
	{
        duration: 1000,
		interval: 3000,
		children: 'img',
		controls: true
    },

    initialize: function(container, options){
        this.setOptions(options);
        this.container = $(container);
		this.elements = this.container.getChildren(this.options.children);

		if (this.elements.length < 2 || !this.elements.length)
		{
			return;
		}


		this.elements.each(function(elt, idx){
			elt.set('tween', {duration: this.options.duration, fps: 20, transition: 'linear'});
			$(elt).setStyles({
				'z-index': this.elements.length - idx,
				'position': 'absolute',
				'top' : 0,
				'left' : 0,
				'opacity' : (idx > 0) ? 0 : 1,
				'visibility' : (idx > 0) ? 'hidden' : 'visible'
			});
		}, this);

		if (this.options.controls)
		{
			this.pausebutton = new Element('div', {
				'class': 'pausebutton'
			})
			.setStyles({
					'display' : 'block',
					'position' : 'absolute',
					'z-index' : this.elements.length + 3000,
					'height': 64,
					'top': -64,
					'width': '100%',
					'cursor': 'pointer'
			})
			.inject(this.container)
			.addEvents({
				'click': function()
				{
					if (this.started == 1)
					{
						this.pause();
					}
					else
					{
						this.start();
					}
				}.bind(this)
			});

			this.container.addEvents({
				'mouseenter': function()
				{
					this.pausebutton.morph({'top': 0, 'opacity':0.7});
				}.bind(this),

				'mouseleave': function()
				{

				this.pausebutton.morph({'top': -64, 'opacity':0});
				}.bind(this)
			})



		}

		this.container.setStyles({
			'position':'relative',
			'overflow':'hidden',
			'height': (this.elements[0]) ? this.elements[0].getHeight() : 'auto',
			'width': (this.elements[0]) ? this.elements[0].getWidth() : 'auto'
		});

		this.currentItem = 0;


	},

	start: function()
	{
		if (this.elements.length < 2 || !this.elements.length)
		{
			return;
		}
		
		if (this.options.controls && this.pausebutton)
		{
			this.pausebutton.removeClass('paused');
		}
		this.started = 1;
		$clear(this.timer);
		this.timer = this.nextItem.periodical(this.options.interval, this);
	},

	pause: function()
	{
		if (this.elements.length < 2 || !this.elements.length)
		{
			return;
		}
		
		if (this.options.controls && this.pausebutton)
		{
			this.pausebutton.addClass('paused');
		}
		this.started = 0;
		$clear(this.timer);
	},

	stop: function()
	{
		this.pause();
		this.started = 0;
	},

	scan: function()
	{
		$$('.cycler').each(function(elt, i){
			new Cycler($(elt)).start();
		});
	},



	nextItem: function()
	{
		if (this.elements.length == 0) return;
		if (this.currentItem + 1 == this.elements.length)
		{
			this.elements[0].fade('in');
			this.elements[this.currentItem].fade.delay(this.options.duration, this.elements[this.currentItem], 'hide');
			this.currentItem = 0;

		}
		else
		{
			this.elements[this.currentItem].fade('out');
			this.currentItem++;
			this.elements[this.currentItem].fade('show');
		}

	}

});


var Fader = new Class({
    Implements: Options,
    options:
	{
        duration: 1000,
		interval: 3000,
		children: 'img'
    },

    initialize: function(container, options){
        this.setOptions(options);
        this.container = $(container);
		this.elements = this.container.getChildren(this.options.children);

		if (this.elements.length < 2)
		{
			return;
		}


		this.elements.each(function(elt, idx){
			elt.set('tween', {duration: this.options.duration, fps: 20, transition: 'linear'});
			$(elt).setStyles({
				'z-index': this.elements.length - idx,
				'position': 'absolute',
				'top' : 0,
				'left' : 0,
				'opacity' : (idx > 0) ? 0 : 1,
				'visibility' : (idx > 0) ? 'hidden' : 'visible'
			});
		}, this);


	this.container.setStyles({
			'position':'relative',
			'overflow':'hidden',
			'height': (this.elements[0]) ? this.elements[0].getHeight() : 'auto',
			'width': (this.elements[0]) ? this.elements[0].getWidth() : 'auto'
		});


		this.currentItem = 0;


	},

	start: function()
	{
		this.started = 1;
		$clear(this.timer);
		this.timer = this.nextItem.periodical(this.options.interval, this);
	},



	stop: function()
	{
		this.pause();
		this.started = 0;
	},

	nextItem: function()
	{
		if (this.elements.length == 0) return;
		if (this.currentItem + 1 == this.elements.length)
		{
			this.elements[0].fade('in');
			this.elements[this.currentItem].fade.delay(this.options.duration, this.elements[this.currentItem], 'hide');
			this.currentItem = 0;

		}
		else
		{
			this.elements[this.currentItem].fade('out');
			this.currentItem++;
			this.elements[this.currentItem].fade('show');
		}
	}

});
