﻿
// FloatAlert Settings
var iFloatAlert_TimerDelay = 10;
var iFloatAlert_FadeDeley = 500;
var iFloatAlert_OpacityStep = 10;

var oFloatAlert_Nodes = new Array();
var oFloatAlert_Ticker;

function FloatAlert_Node()
{
    this.DIV;
    this.DelayCount;
    this.Opacity;
    this.OpacityStep;
}

function FloatAlert_AddFade(oDIV, iLength)
{
    var i;
    var bFoundDiv = false;

    for (i = 0; i < oFloatAlert_Nodes.length; i++)
    {
        if (oDIV == oFloatAlert_Nodes[i].DIV)
        {
            bFoundDiv = true;

            oFloatAlert_Nodes[i].DelayCount = iLength;
            oFloatAlert_Nodes[i].Opacity = 80;
            oFloatAlert_Nodes[i].OpacityStep = iFloatAlert_OpacityStep;
        }
    }


    if (bFoundDiv == false)
    {
        oNode = new FloatAlert_Node;


        if (!iLength)
        {
            iLength = iFloatAlert_TimerDelay;
        }


        oNode.DIV = oDIV;
        oNode.DelayCount = iLength;
        oNode.Opacity = 80;
        oNode.OpacityStep = iFloatAlert_OpacityStep;

        oFloatAlert_Nodes[oFloatAlert_Nodes.length] = oNode;

        if (!oFloatAlert_Ticker)
        {
            oFloatAlert_Ticker = setTimeout("FloatAlert_FadeOuts();", iFloatAlert_TimerDelay);
        }

        FloatAlert_SetOpacity(oNode.DIV, oNode.Opacity)
        oFloatAlert_FloatDiv.appendChild(oNode.DIV);
    }
}

function FloatAlert_SetOpacity(oElem, iOpacity)
{
    var iErrorOnLine = 0;

    try
    {
        oElem.style.opacity = (iOpacity / 100);

        iErrorOnLine = 2;
        oElem.style.MozOpacity = (iOpacity / 100);

        iErrorOnLine = 4;
        oElem.style.KhtmlOpacity = (iOpacity / 100);

        iErrorOnLine = 6;
        oElem.style.filter = "alpha(opacity=" + iOpacity + ")";
    }
    catch (oErr)
    {
        JSError('FloatAlert_SetOpacity', oErr + '\n\n iErrorOnLine=' + iErrorOnLine);
    }
}


function FloatAlert_FadeOuts()
{
    var i = 0;

    while (i < oFloatAlert_Nodes.length)
    {
        if (oFloatAlert_Nodes[i].DelayCount > 0)
        {
            oFloatAlert_Nodes[i].DelayCount--;

            i++;
        }
        else if (oFloatAlert_Nodes[i].Opacity - oFloatAlert_Nodes[i].OpacityStep <= 0)
        {

            DeleteElement(oFloatAlert_Nodes[i].DIV);

            oFloatAlert_Nodes.splice(i, 1);
        }
        else
        {
            oFloatAlert_Nodes[i].Opacity -= oFloatAlert_Nodes[i].OpacityStep;

            FloatAlert_SetOpacity(oFloatAlert_Nodes[i].DIV, oFloatAlert_Nodes[i].Opacity);

            i++;
        }
    }

    if (oFloatAlert_Nodes.length > 0)
    {
        oFloatAlert_Ticker = setTimeout("FloatAlert_FadeOuts();", iFloatAlert_TimerDelay);
    }
    else
    {
        clearTimeout(oFloatAlert_Ticker);
        oFloatAlert_Ticker = null;
    }

}

var oFloatAlert_FloatDiv;
function FloatAlert(sText, iLength, iWidth,oDIV_Old)
{
    if (!sText)
    {
        sText = '{undefined}';
    }
    else if (sText == '')
    {
        sText = '{nothing}';
    }

    if (!oFloatAlert_FloatDiv)
    {
        oFloatAlert_FloatDiv = document.createElement('div');

        oFloatAlert_FloatDiv.style.position = 'fixed';
        oFloatAlert_FloatDiv.style.top = '2px';
        oFloatAlert_FloatDiv.style.right = '2px';
        oFloatAlert_FloatDiv.style.overflow = 'visible';
        oFloatAlert_FloatDiv.style.zIndex = '1';

        document.forms[0].appendChild(oFloatAlert_FloatDiv);
    }

    if (!iLength)
    {
        iLength = 500;
    }


    var oDIV;

    if (oDIV_Old)
    {
        oDIV = oDIV_Old;
    }
    else
    {
        oDIV = document.createElement('div');

        oDIV.style.textAlign = 'left';
        oDIV.align = 'right';
        oDIV.style.position = 'absolute';
        oDIV.style.margin = '2px 2px 2px 2px';
        oDIV.style.padding = '3px 3px 3px 3px';


        if (oFloatAlert_FloatDiv.childNodes.length > 0)
        {
            var oLastNode = oFloatAlert_FloatDiv.childNodes[oFloatAlert_FloatDiv.childNodes.length - 1];
            var iRight = parseInt(replace(oLastNode.style.right, 'px', '')) + oLastNode.offsetWidth + 2;

            oDIV.style.right = iRight + 'px'
        }
        else
        {
            oDIV.style.right = '2px';
        }

    }


    if (!iWidth)
    {
        oDIV.style.width = 'auto';
    }
    else
    {
        oDIV.style.width = iWidth + 'px';
    }

    oDIV.className = 'FloatAlert';
    oDIV.innerHTML = '<span>' + sText + '</span>';


    FloatAlert_AddFade(oDIV, iLength, 1, true);

    return oDIV;

}

