// Setup empty array
var fadeObjectArray = new Array();
fadeObjectCount = 0;
var timerSet=0;
var fadeTimerSpeed=10;

// This is an object containing
// fade target info and the control
function FadeObject(_ctrl,_id)
{
    this.ctrl = _ctrl;
    this.id = _id;
    this.fadeTarget = 100;
    this.fadeCurrent = 100;
    this.SetOpacity = function(opacity)
    {
        this.fadeCurrent = opacity;
        this.ctrl.style.opacity=this.fadeCurrent/100.0;
        this.ctrl.style='filter:alpha(opacity=' + this.fadeCurrent + ')';
    };
}

// Add a new control into the fade array
function AddFadeObject(ctrl)
{
    obj = new FadeObject(ctrl,fadeObjectCount);
    fadeObjectArray[fadeObjectCount++] = obj;
}

// Set the provided control opacity to the 
// percentage given (0-100)
function SetCtrlOpacity(ctrl,o)
{
    var obj = GetFadeObjectByControl(ctrl);
    if(obj!=null) obj.fadeCurrent = o;
    ctrl.style.opacity=o/100.0;
    //ctrl.style="filter:alpha(opacity=" + o + ")";
}

function GetFadeObjectByControl(findCtrl)
{
    var i;
    // Find the control in the array
    for(i=0;i<fadeObjectCount;i++)
    {
        if(fadeObjectArray[i].ctrl.id == findCtrl.id)
        {
            return fadeObjectArray[i];
        }
    }
    
    // Not found, return null
    return null;    
}

function DoFade()
{
    var timerContinue=0;
    var i;

    for(i=0;i<fadeObjectCount;i++)
    {
        var obj = fadeObjectArray[i];
        if(obj.fadeTarget!=obj.fadeCurrent)
        {
            if( obj.fadeTarget>obj.fadeCurrent)
                obj.fadeCurrent+=2;
            else
                obj.fadeCurrent-=2;
            
            //obj.SetOpacity(obj.fadeCurrent);
            SetCtrlOpacity(obj.ctrl,obj.fadeCurrent);
            
            // Flag the timer to continue
            timerContinue=1;
        }
    }
    
    // If we still need to animate, reset the timer
    if(timerContinue==1)
    {   
        setTimeout(DoFade,fadeTimerSpeed);
        timerSet=1;
    }
    else
    {
        timerSet=0;
    }
}

// THis is called from the client page
// it starts fading depending on the mode
// 1: Fade this control only (DEFAULT)
// 2: Fade all controls except this one
// 3: Fade all controls (ctrl is ignored)
function StartFade(mode, id, target)
{
    switch(mode)
    {
        case 2: // Fade all controls except this one
            for(i=0;i<fadeObjectCount;i++)
            {
                var obj = fadeObjectArray[i];
                if(i!=id)
                    obj.fadeTarget=target;
                else
                    obj.fadeTarget=100;
            }
            if(timerSet==0) setTimeout(DoFade,fadeTimerSpeed);
            break;
        case 3: // Fade all controls (ctrl is ignored)
            for(i=0;i<fadeObjectCount;i++)
            {
                var obj = fadeObjectArray[i];
                obj.fadeTarget=target;
            }
            if(timerSet==0) setTimeout(DoFade,fadeTimerSpeed);
            break;
        default: // this control only
            var obj = fadeObjectArray[id];
            obj.fadeTarget=target;
            if(timerSet==0) setTimeout(DoFade,fadeTimerSpeed);
            break;
    }
}
