/**
 * CoreFader
 *
 * Fades out a div, copies content from another div and
 * fades back in. Supports IE 6 and 7, Firefox 1.5+,
 * Mozilla and Opera 9+
 * 
 * Example use:
 * fader = new Fader('destination',100,10);
 * fader.fadeTo('source1');
 *
 * @author Kurt Aadnøy
 * @arg elementId string html element id of the master
 *      div
 * @arg interval int time in milliseconds between each
 *      fade step (recommended 50-100).
 * @arg fadeStepPercent int percent opacity change in
 *      each step.
 *
 */
function Fader(elementId, interval, fadeStepPercent) {
    this._object = document.getElementById(elementId);
    if(this._object == null) {
        return;
    }
    
    if(interval == null || interval <= 0 || interval > 10000) {
        alert('Illegal interval  [+0 - 10000].');
        return;
    }
    
    if(fadeStepPercent == null || fadeStepPercent < 1 || fadeStepPercent > 990) {
        alert('Illegal fadeStepPercent [1 - 99]%');
        return;
    }
    
    this._toObject = null;
    this._startOpacity = 100;
    this._endOpacity = 0;
    this._fadeOut = true;
    this._currentOpacity = null;
    this._intervalId = null;
    this._interval = interval;
    this._speed = fadeStepPercent/100;
    this._doFade = null;
    
    // Set doFade delegate
    this.setDoFadeFunction();
}

/**
 * This is the function used to start the fade process.
 * Set what hidden div (display:none) to copy content from.
 */
Fader.prototype.fadeTo = function(to) {
    this._toObject = document.getElementById(to);
    if(this._object == null || this._toObject == null) {
        return;
    }
    
    this._fadeOut = true;
    
    var objRef = this;
    this._intervalId = setInterval(function () { objRef.doFadeTick(); },this._interval);
}

Fader.prototype.fadeOut = function() {
    this._fadeOut = true;
    var objRef = this;
    this._intervalId = setInterval(function () { objRef.doFadeTick(); },this._interval);
}

Fader.prototype.fadeIn = function() {
    this._fadeOut = false;
    var objRef = this;
    this._intervalId = setInterval(function () { objRef.doFadeTick(); },this._interval);
}

// ============= START: Private functions

// Use this function to set a custom setOpacity functio
// to support more browers.
Fader.prototype.setDoFadeFunction = function() {
    switch(navigator.appName) {
        
        case 'Opera': 
        case 'Netscape':
            this._currentOpacity = 1;
            this._object.style.filter = this._currentOpacity;
            this._doSetOpacity = this.setOpacityMoz;
            break;
        
        default:   
            this._currentOpacity = 1;
            this._object.style.filter = this._currentOpacity;
            this._object.style.filter = "alpha(Opacity="+(this._currentOpacity*100)+")";
            this._doSetOpacity = this.setOpacityMSIE;
            break;            
    }
}

Fader.prototype.doFadeTick = function() {
    if(this._fadeOut) {
        if(this._currentOpacity > 0) {
            opacity = (Number(this._currentOpacity)-this._speed)*100;
            opacity = Math.round(opacity)/100;
            this._doSetOpacity(this._object, opacity);
        } else {
            if(this._toObject != null) {
                this._object.innerHTML = this._toObject.innerHTML;        
                this._fadeOut = false;
            } else {
                clearInterval(this._intervalId);
            }
        }
    } else {
        if(this._currentOpacity < 1) {
            opacity = (Number(this._currentOpacity)+this._speed)*100;
            opacity = Math.round(opacity)/100;
            this._doSetOpacity(this._object, opacity);
        } else {
            // end fade process when finished faded back in
            clearInterval(this._intervalId);
        }
    }
}

Fader.prototype.setOpacityMSIE = function(object, opacity) {    
    object.filters.alpha.opacity = (opacity*100);
    this._currentOpacity = opacity;
}

Fader.prototype.setOpacityMoz = function(object, opacity) {
    object.style.opacity = opacity;
    this._currentOpacity = opacity;
}
// ============= START: Private functions
// END: Fader
