FadeApi = function(NumberOfImages, StartNumber, ID, PauseTime, FadeLoopTime) {
    // Store constructor parameters
    this.NumberOfImages = NumberOfImages;
    this.CurrentItem = null;
    this.NextItem = null;
    this.CurrentNumber = StartNumber;
    this.StartNumber = StartNumber;
    this.ID = ID;
    this.nextValue = 0;
    this.Currentvalue = 1;
    this.timeout = null;
    this.PauseTime = PauseTime;
    this.isFading = false;
    if (FadeLoopTime != null)
        this.FadeLoopTime = FadeLoopTime;
    else
        this.FadeLoopTime = 4;

   

    // Hook the onLoad event
    addOnLoadHandler(this, this.onLoad);
    return this;
}

FadeApi.prototype.onLoad = function()
{
	this.CurrentItem = document.getElementById(this.ID +this.CurrentNumber);
	if(this.NumberOfImages > this.CurrentNumber)
	{
		this.NextItem = document.getElementById(this.ID +(this.CurrentNumber+1));
		var obj = this;
		this.timeout = setTimeout(function () { obj.Fade(); }, this.PauseTime);
	}
	
	
	var obj = this;
	this.timeout = setTimeout(function () { obj.Fade(); }, this.PauseTime);
	
}

FadeApi.prototype.Prev = function() {
    if (this.isFading == false) {

        if (this.CurrentNumber == 0) {
            this.CurrentNumber = 5;
        }
        if (this.CurrentNumber != 1) {
            this.NextItem = document.getElementById(this.ID + (this.CurrentNumber - 1));
            this.CurrentNumber = (this.CurrentNumber - 2);
        } else {
            this.NextItem = document.getElementById(this.ID + this.NumberOfImages);
            this.CurrentNumber = (this.NumberOfImages - 1);
        }
        this.Fade();
    }
}

FadeApi.prototype.Next = function() {
  if (this.isFading == false) {
    this.Fade();
    }
  
}

FadeApi.prototype.Fade = function() {
   
    var obj = this;
     this.isFading = true;
    this.CurrentItem.style.display = "inline";
    this.NextItem.style.display = "inline";
    this.nextValue = this.nextValue + 0.010000000000000;
    this.Currentvalue = this.Currentvalue - 0.010000000000000;
    var num = new Number(this.Currentvalue);

    if (this.CurrentItem.filters != null) {
        this.NextItem.filters.alpha.opacity = (100 * this.nextValue);
        this.CurrentItem.filters.alpha.opacity = (100 * num.toFixed(10));
    }
    else if (this.CurrentItem.style.MozOpacity != null) {
        this.NextItem.style.MozOpacity = this.nextValue
        this.CurrentItem.style.MozOpacity = num.toFixed(10)
    }
    else if (this.CurrentItem.style.KhtmlOpacity != null) {
        this.NextItem.style.KhtmlOpacity = this.nextValue
        this.CurrentItem.style.KhtmlOpacity = num.toFixed(10)
    }

    if (this.nextValue <= 1) {
        this.timeout = setTimeout(function() { obj.Fade(); }, this.FadeLoopTime);
    }
    else {
        this.CurrentNumber++;
        this.nextValue = 0;
        this.Currentvalue = 1;
        this.CurrentItem.style.display = "none";
        
        this.CurrentItem = document.getElementById(this.ID + this.CurrentNumber);
        this.NextItem = document.getElementById(this.ID + (this.CurrentNumber + 1));
        
        if (this.CurrentNumber == this.NumberOfImages) {
            this.CurrentItem = document.getElementById(this.ID + this.CurrentNumber);
            this.NextItem = document.getElementById(this.ID + "1");
            this.CurrentNumber = 0;
        }
        else {
            this.CurrentItem = document.getElementById(this.ID + this.CurrentNumber);
            this.NextItem = document.getElementById(this.ID + (this.CurrentNumber + 1));
        }
        obj = this;
        this.isFading = false;
        clearTimeout(this.timeout);
       this.timeout = setTimeout(function () { obj.Fade(); },this.PauseTime);
    }
}
