var FeaturedImages = Class.create();



FeaturedImages.prototype = {

  timerSpeed: 55,   // how often the timer will run, the lower the number the faster it will run

  timerIncrement: 1, // how much the time will go up each time

  timerMax: 100,     // when timer reaches this it will reset

  timerCurrent: 0,   // the value the timer is currently at

  timerFadeAt: 100,  // value that the timer will start to fade out at, this must be an exact value

  timerCurrent: 0,



	initialize: function() {

    this.totalImages = $('featured_pics').select('.featured_pic').size();

    if (this.totalImages <= 1) return;

	  this.currentImageId = Math.floor(this.totalImages*Math.random());

    // console.log("this.totalImages: "+this.totalImages);

    // console.log("this.currentImageId: "+this.currentImageId);

    $('featured_pics').select('.featured_pic').invoke('hide');

    $('feature_'+this.currentImageId).show();

    this.timer();

	},



	timer: function() {

		this.timerCurrent += this.timerIncrement;

		if(this.timerCurrent == this.timerFadeAt) {

			this.hideCurrentImage();

		}

		if(this.timerCurrent >= this.timerMax) {

			this.next();

			this.timerCurrent = 0;

		} 

		setTimeout(function() {

		  this.timer();

		}.bind(this), this.timerSpeed);

	},



	next: function() {

		this.advanceCurrentImageId();

		this.showCurrentImage();

	},



	advanceCurrentImageId: function() {

    // console.log("advanceCurrentImageId: "+this.currentImageId);

		this.currentImageId = this.currentImageId + 1;

	  if (this.currentImageId == this.totalImages) this.currentImageId = 0;

	},



	hideCurrentImage: function() {

    // console.log("hideCurrentImage: "+this.currentImageId);

		Effect.Fade($('feature_'+this.currentImageId), {duration:2.5});

	},



	showCurrentImage: function() {

    // console.log("showCurrentImage: "+this.currentImageId);

		Effect.Appear($('feature_'+this.currentImageId), {duration:2.5});

	}

};



document.observe('dom:loaded', function() {

  new FeaturedImages();

});