
ImageFader = Class.create();
ImageFader.prototype = {

	initialize: function(objName,img,dir,dispDuration,nextDuration) {
		this._objName = objName;
		this._img = $(img);
		this._dir = dir;
		this._disp = dispDuration;
		this._next = nextDuration;

		this._image_array = new Array();
		this._arr_len = 0;
		this._arr_ptr = 0;

		this.start_fader = this._start_fader.bind(this);
		this.appear_image = this._appear_image.bind(this);
		this.fade_image = this._fade_image.bind(this);

		// call the SAJAX method
		x_get_fader_images(this._dir, this._objName, this.start_fader);
	},

	_start_fader: function( imgs ) { // imgs is the array of .jpg or .gif filenames in the given directory
		for (var i in imgs) {
			this._image_array[i] = new Image();
			this._image_array[i].src = this._dir+imgs[i];
			this._arr_len = i;
		}
		this.appear_image();
	},

	_appear_image: function() {
		// Change the src of the image object on the page
		this._img.src = this._image_array[this._arr_ptr].src;
		
		// Call Effect.Appear and pass the callback function
		Effect.Appear(this._img);

		setTimeout(this.fade_image, this._disp);
	},

	_fade_image: function() {
		// Call Effect.Fade and pass callback function
		Effect.Fade(this._img);

		// Increase the pointer before we call the Effect.Fade
		if ( this._arr_ptr >= this._arr_len ) {
			this._arr_ptr = 0;
		} else {
			this._arr_ptr++;
		}

		setTimeout(this.appear_image, this._next);
	}

}