var featuredStuff = {

	currentIndex: 0,
	timer: false,


	init: function ( elements, options ) {

		this.elements 			= elements;

		this.options 			= options || {};
		this.options.delay 		= this.options.delay || 5;

		this.currentIndex = elements.find( function ( el ) {
			return el.visible();
		}).previousSiblings().length;


		if ( $( this.options.backButton ) ) {
			$( this.options.backButton ).observe( 'click', function ( ev ) {
				Event.stop( ev );
				this.stopTimer();
				this.goBack();
			}.bind( this ));
		}


		if ( $( this.options.nextButton ) ) {
			$( this.options.nextButton ).observe( 'click', function ( ev ) {
				Event.stop( ev );
				this.stopTimer();
				this.goNext();
			}.bind( this ));
		}


		this.elements[ this.currentIndex ].siblings().invoke( 'hide' );

		this.startTimer();

	},


	atFirst: function () {

		return ( this.currentIndex == 0 );

	},


	atLast: function () {

		return ( this.currentIndex == ( this.elements.length - 1 ) );

	},


	change: function () {

		var c = this.elements[ this.currentIndex ];		//  Current element

		c.show().siblings().invoke( 'hide' );

		if ( $( this.options.indexUpdate ) ) {
			$( this.options.indexUpdate ).update( this.currentIndex + 1 );
		}

		if ( this.options.linkClass && $('featuredProductView') ) {

			var link = c.descendants().find( function ( el ) {
				return ( el.tagName.toLowerCase() == 'a' && el.hasClassName( this.options.linkClass ) );
			}.bind( this ));

			if ( link ) {
				$( this.options.viewButton ).href = link.href;
			}
		}

	},


	goBack: function () {

		if ( this.atFirst() ) {
			this.currentIndex = this.elements.length - 1;
		} else {
			this.currentIndex--;
		}

		this.change();

	},


	goNext: function () {

		if ( this.atLast() ) {
			this.currentIndex = 0;
		} else {
			this.currentIndex++;
		}

		this.change();

	},


	startTimer: function () {

		this.timer = new PeriodicalExecuter( this.goNext.bindAsEventListener( this ), this.options.delay );

	},


	stopTimer: function () {

    	//  Stop the timer (probably a click on a manual link)
		if ( this.timer !== false ) {
			this.timer.stop();
			this.timer = false;
		}

	}

}