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

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

		this._image_array = new Array();
		this._image_array2 = 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
		if(imgNames == ''){
			x_get_fader_images(this._dir, this._objName, this.start_fader);
		}
		else {
			this._image_array2 = imgNames.split(";");
			this.start_fader(this._image_array2);
		}
	},

	_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() {
		
		if ( this._arr_ptr > this._image_array.length - 1 ) {
			this._arr_ptr = 0;			
		}
		
		// Change the src of the image object on the page
		try{
			this._img.src = this._image_array[this._arr_ptr].src;
		}
		catch(err){
			txt="There was an error on this page.\n\n";
			txt+="Error description: " + err.description + "\n\n";
			txt+="Pointer: " + this._arr_ptr + "\n\n";
			txt+="MaxPointer: " + this._image_array.length + "\n\n";
			txt+="Click OK to continue.\n\n";
//			alert(txt);
		}
		// 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 - 1 ) {
			this._arr_ptr = 0;			
		} else {
			this._arr_ptr++;
		}

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

}
