﻿function FadeInLegend(legendID, framesCount, frameHeight) {
    var currentFrame = 1;
    var transitionWait = 30;
    var idleWait = 4000;
    var objLegend = null;
    var transitionTimerID = -1;
    var idleWaitTimerID = -1;
    var opacityLevel = 100;
    var opacityGain = -15;
    var opacityMin = 0;
    var opacityMax = 100;
    var supportsBlending = false;
    

    function InitTransition() {
        StopIdleTimer();

        if (supportsBlending) {
            StartTransitionTimer();
        }
        else {
            ShiftFrame();
            StartIdleTimer();
        }
    }

    function DoTransition() {
        StopTransitionTimer();

        if (opacityGain < 0) {
            opacityLevel = Math.max(opacityLevel + opacityGain, opacityMin);
            SetOpacity();
            if (opacityLevel == opacityMin) {
                ShiftFrame();
                opacityGain = (-1) * opacityGain;
            }
            StartTransitionTimer();
        }
        else if (opacityGain > 0) {
            opacityLevel = Math.min(opacityLevel + opacityGain, opacityMax);
            SetOpacity();
            if (opacityLevel == opacityMax) {
                opacityGain = (-1) * opacityGain;
                StartIdleTimer();
            }
            else {
                StartTransitionTimer();
            }
        }
    }

    function ShiftFrame() {
        if (currentFrame < framesCount) {
            currentFrame++;
            currentPos = -(frameHeight * (currentFrame - 1));
            objLegend.style.top = currentPos.toString() + "px";
        }
        else {
            currentFrame = 1;
            objLegend.style.top = "0px";
        }
    }

    function SetOpacity() {
        if (objLegend.style.opacity != null) {
            objLegend.style.opacity = (opacityLevel / 100).toString();
        }
        else if ((objLegend.style.filters != null) && (objLegend.style.filters.alpha != null)) {
            objLegend.style.filters.alpha.opacity = opacityLevel.toString();
        }
        else if (objLegend.style.filter != null) {
            objLegend.style.filter = "alpha(opacity=" + opacityLevel.toString() + ")";
        }
        else if (objLegend.style.MozOpacity != null) {
            objLegend.style.MozOpacity = (opacityLevel / 100).toString();
        }
    }

    function StartTransitionTimer() {
        StopTransitionTimer();
        transitionTimerID = self.setTimeout(DoTransition, transitionWait);
    }

    function StopTransitionTimer() {
        if (transitionTimerID != -1) {
            self.clearTimeout(transitionTimerID);
            transitionTimerID = -1;
        }
    }

    function StartIdleTimer() {
        StopIdleTimer();
        idleWaitTimerID = self.setTimeout(InitTransition, idleWait);
    }

    function StopIdleTimer() {
        if (idleWaitTimerID != -1) {
            self.clearTimeout(idleWaitTimerID);
            idleWaitTimerID = -1;
        }
    }

    function Initialize() {
        objLegend = GetDocumentElement(legendID);
        if (objLegend != null) {
            if (objLegend.style.opacity != null) {
                supportsBlending = true;
            }
            else if ((objLegend.style.filters != null) && (objLegend.style.filters.alpha != null)) {
                supportsBlending = true;
            }
            else if (objLegend.style.filter != null) {
                supportsBlending = true;
            }
            else if (objLegend.style.MozOpacity != null) {
                supportsBlending = true;
            }
            StartIdleTimer();
        }
    }

    Initialize();
    return this;
}
 




function ScrollingLengend(legendID, framesCount, frameWidth) {
    var currentFrame = 1;
    var relativeXPos = 0;
    var transitionWait = 40;
    var idleWait = 4000;

    var transitionTimerID = -1;
    var idleWaitTimerID = -1;

    var routeMap = [1, 1, 3, 4, 5, 6, 7, 8, 9, 12, 9, 8, 7, 6, 5, 4, 3, 1, 1];
    //var routeMap = [12, 12, 11, 10, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
    var framePos = null;
    var index = 0;

    var objLegend = null;

    function InitTransition() {
        StopIdleTimer();

        if (currentFrame == framesCount) {
            currentFrame = 1;
            objLegend.style.left = "0px";
        }
        
        framePos = new Array (routeMap.length);
        var xpos = -(frameWidth * (currentFrame - 1));
        var i = 0;
        for (i = 0; i < routeMap.length; i++) {
            xpos = xpos - ((frameWidth) * routeMap[i] / 100);
            framePos[i] = parseInt(xpos, 10);
        }
        index = 0;
        StartTransitionTimer();
    }

    function DoTransition() {
        StopTransitionTimer();

        if (index < framePos.length) {
            objLegend.style.left = framePos[index].toString() + "px";
            index++;
            StartTransitionTimer();
        }
        else {
            currentPos = -(frameWidth * currentFrame);
            objLegend.style.left = currentPos.toString() + "px";
            currentFrame++;
            StartIdleTimer();
        }
    }

    function StartTransitionTimer() {
        StopTransitionTimer();
        transitionTimerID = self.setTimeout(DoTransition, transitionWait);
    }

    function StopTransitionTimer() {
        if (transitionTimerID != -1) {
            self.clearTimeout(transitionTimerID);
            transitionTimerID = -1;
        }
    }

    function StartIdleTimer() {
        StopIdleTimer();
        idleWaitTimerID = self.setTimeout(InitTransition, idleWait);
    }

    function StopIdleTimer() {
        if (idleWaitTimerID != -1) {
            self.clearTimeout(idleWaitTimerID);
            idleWaitTimerID = -1;
        }
    }

    function Initialize() {
        objLegend = GetDocumentElement(legendID);
        StartIdleTimer();
    }


    Initialize();
    return this;
}

