﻿

function BannerRotator() {
    this.Width = null;
    this.Height = null;
    this.Velocity = 3000;
    this.DivID = null;
    this.ContainerDiv = null;
    this.PortalWidth = null;
    this.PortalHeight = null;
    this.HAlign = null; // left, center, right
    this.VAlign = null; // top, middle, bottom
    this.BannerCount = null;
    this.VarName = null;
    this.IsMoving = false;
    this.MoveIntID = null;
    this.ButtonFadeInt = null;
    this.ButtonFadeDir = null;
    this.Directions = { "Horizontal": 0, "Vertical": 1 };
    this.ScrollDirection = this.Directions.Horizontal;
    this.MoveBeginTime = null;
    this.MoveBeginVal = null;
    this.MoveEndVal = null;
    this.MoveVelocity = null;
    this.MoveTime = null;
    
    this.PageBtnActiveClass = null;
    this.PageBtnInactiveClass = null;

    this.AutoScroll = false;
    this.AutoRewind = false;
    this.AutoScrollDelay = 5000;
    this.AutoScrollIntv = null;

    this.Refresh = 20; // milliseconds
    this.Banners = new Array();
    this.CurrentBanner = 1;

    this.Buttons = new Array();

    this.PagerButtons = null;

    this.DefinePagerButtons = function(BaseID, Qty) {
        this.PagerButtons = new Array();
        for (var i = 1; i <= Qty; i++) {
            this.PagerButtons[this.PagerButtons.length] = new PagerButton(BaseID + i);
        }
        this.ActivatePagerButton(0);
    }

    this.ActivatePagerButton = function(I) {
        for (i = 0; i < (this.PagerButtons.length); i++) {
            this.PagerButtons[i].El.className = this.PageBtnInactiveClass;
        }
        this.PagerButtons[I].El.className = this.PageBtnActiveClass;
    }
    
    function PagerButton(ID) {
        this.ID = ID;
        this.El = document.getElementById(ID);
    }

    this.AddButton = function(ID, Func, Display) {
        this.Buttons[this.Buttons.length] = new Button(ID, Func, Display);
    }

    this.Start = function() {
        exp = this.VarName + ".NextBanner()";
        this.AutoScrollIntv = setInterval(exp, this.AutoScrollDelay);
    }

    this.Stop = function() {
        clearInterval(this.AutoScrollIntv);
    }

    this.ShowButtons = function() {
        var isFading = this.ButtonFadeDir != null;
        this.ButtonFadeDir = 'up';
        if (!isFading)
            this.ButtonFadeInt = setInterval(this.VarName + '.FadeButtons()', 50);
    }

    this.HideButtons = function() {
        var isFading = this.ButtonFadeDir != null;
        this.ButtonFadeDir = 'down';
        if (!isFading)
            this.ButtonFadeInt = setInterval(this.VarName + '.FadeButtons()', 50);
    }

    this.FadeButtons = function() {
        var stillFading = false;
        for (var i in this.Buttons) {
            var b = this.Buttons[i];
            var o = parseFloat(b.Obj.style.opacity);
            if (isNaN(o)) {
                o = 0;
            }
            if (this.ButtonFadeDir == 'up') {
                if (o < 1) {
                    stillFading = true;
                    if (o == 0)
                        b.SetVisibility('visible');
                    b.Obj.style.opacity = o + 0.2;
                }
            }
            else {
                if (o > 0) {
                    stillFading = true;
                    b.Obj.style.opacity = o - 0.2;
                }
                else {
                    b.SetVisibility('hidden');
                }
            }
        }
        if (!stillFading) {
            this.ButtonFadeDir = null;
            clearInterval(this.ButtonFadeInt);
        }
    }



    this.MouseOver = function() {
        var exp = this.VarName + '.Stop()';
        this.Stop();
        this.ShowButtons();
    }

    this.MouseOut = function() {
        if (this.AutoScroll) {
            var exp = this.VarName + '.Start()';
            this.Start();
        }
        this.HideButtons();
    }

    this.AddBanner = function(ID) {
        this.Banners[this.Banners.length] = new Banner(ID);
    }

    this.FadeBanner = function(Index, Op, Seconds, Refresh) {
        var bnr = this.Banners[Index];
        if (bnr.IsFading)
            return;
        var co = parseFloat(bnr.Obj.style.opacity);
        var sign;
        (co < Op) ? sign = 1 : sign = -1;
        var frames = (Seconds * 1000) / Refresh;
        var d = (1 / frames) * sign;
        var exp = this.VarName + ".FadeBannerAction(" + Index + "," + Op + "," + d + ")";
        bnr.FadeIntID = setInterval(exp, Refresh);
        bnr.IsFading = true;
    }

    this.FadeBannerAction = function(Index, Op, D) {
        var bnr = this.Banners[Index];
        var co = parseFloat(bnr.Obj.style.opacity);
        if (Math.abs(Op - co) < Math.abs(D)) {
            bnr.Obj.style.opacity = Op;
            clearInterval(bnr.FadeIntID);
            bnr.IsFading = false;
        }
        else {
            bnr.Obj.style.opacity = co + D;
        }
    }

    this.SetLeft = function(Left) {
        this.ContainerDiv.style.left = Left + 'px';
    }

    this.SetTop = function(Top) {
        this.ContainerDiv.style.top = Top + 'px';
    }

    this.EaseInOut = function(x) {
        return (1 - Math.sin(Math.PI / 2 + x * Math.PI)) / 2;
    }


    this.CurrentPos = function() {
        // (1-sin(PI/2+x*PI))/2
        var ct = new Date().getTime();
        var et = ct - this.MoveBeginTime;
        var pd = et / (this.MoveTime * 1000);
        if (pd >= 1)
            return this.MoveEndVal;
        else {
            var md = this.MoveEndVal - this.MoveBeginVal;
            var mx = ((1 - Math.sin(Math.PI / 2 + (pd * Math.PI))) / 2);
            var l = (mx * md) + this.MoveBeginVal;

            return l;
        }
    }

    this.BannerHomePos = function(I) {
        var l;
        if (this.ScrollDirection == this.Directions.Horizontal)
            l = -(this.PortalWidth * I);
        else
            l = -(this.PortalHeight * I);
        return l;
    }

    this.MoveToBanner = function(I, V) {
        if (arguments.length == 1)
            V = this.Velocity;
        this.MoveVelocity = V;

        if ((I < 0) || (I >= this.BannerCount))
            return;
        var l = this.BannerHomePos(I);

        this.MoveTo(l, V);

        if (this.PagerButtons != null)
            this.ActivatePagerButton(I);
        this.CurrentBanner = I + 1;
    }

    this.NextBanner = function() {
        if (this.CurrentBanner < this.BannerCount) {
            this.CurrentBanner++;
            this.MoveToBanner(this.CurrentBanner - 1);
        }
        else {
            if (this.AutoRewind) {
                this.CurrentBanner = 1;
                this.MoveToBanner(0, this.RewindVelocity);
            }
        }
    }

    this.PrevBanner = function() {
        if (this.CurrentBanner > 1) {
            this.CurrentBanner--;
            this.MoveToBanner(this.CurrentBanner - 1);
        }
    }

    this.MoveTo = function(Pos, MoveVelocity) {

        this.MoveBeginTime = new Date().getTime();

        if (this.ScrollDirection == this.Directions.Horizontal)
            this.MoveBeginVal = parseFloat(this.ContainerDiv.style.left);
        else
            this.MoveBeginVal = parseFloat(this.ContainerDiv.style.top);
        if (this.MoveBeginVal == Pos)
            return;
        this.MoveEndVal = parseFloat(Pos);
        if (this.MoveBeginVal <= this.MoveEndVal)
            this.MoveVelocity = MoveVelocity;
        else
            this.MoveVelocity = -MoveVelocity;
        var exp = this.VarName + ".MoveToAction()";
        this.MoveIntID = setInterval(exp, this.Refresh);
    }

    this.MoveToAction = function() {
        var l = this.CurrentPos();
        if (this.ScrollDirection == this.Directions.Horizontal)
            this.ContainerDiv.style.left = l + 'px';
        else
            this.ContainerDiv.style.top = l + 'px';
        if (l == this.MoveEndVal)
            clearInterval(this.MoveIntID);
    }

    function Banner(ID) {
        this.ID = ID;
        this.Obj = document.getElementById(ID);
        //this.Obj.style.opacity = '1';
        this.FadeIntID = null;
        this.IsFading = false;

        this.SetOpacity = function(O) {
            this.Obj.style.opacity = O;
        }
    }

    function Button(ID, Func, Display) {
        this.ID = ID;
        this.Obj = document.getElementById(ID);
        this.Func = Func;
        this.Display = Display;

        this.SetVisibility = function(V) {
            this.Obj.style.visibility = V;
        }
    }

    this.ShowBannerInfo = function(I) {
        var b = this.Banners[I];
        alert(b.ID + ' - ' + b.Width + ' - ' + b.Height);
    }

}