﻿function SetupRotatingBanner(base, banner, transition, pause, frame) {
    if (transition == null) { transition = 1; }
    if (pause == null) { pause = 5; }
    if (frame == null) { frame = 20; }

    if (typeof (base) == "string") { base = document.getElementById(base); }
    if (typeof (banner) == "string") { banner = document.getElementById(banner); }

    base.FrameLength = frame;

    base.Directions = { In: 1, Out: -1 }
    base.Direction = base.Directions.Out;

    base.TransitionLength = transition;
    base.PauseLength = pause;

    base.Banner = banner;

    base.Alpha = 1;
    base.Timer = new Date();
    base.Timeout = null;

    base.Playing = false;
    base.Paused = true;

    base.GetMoveBannerIndex = function (dir) {
        var current = this.CurrentBanner;
        if (this.BannerToShow != -1) { current = this.BannerToShow; }
        var move = current + dir;

        if (move > this.Banners.length - 1) { move = 0; }
        else if (move < 0) { move = this.Banners.length - 1; }

        return move;
    }

    base.Banners = new Array();
    base.CurrentBanner = 0;
    base.BannerToShow = -1;
    base.OnAddBanner = function () { }
    base.AddBanner = function (src, title, url, target) {
        var newBanner = new Banner(src, title, url, target);
        this.OnAddBanner(this.Banners.length, newBanner);
        this.Banners.push(newBanner);
    }
    base.GetCurrentBanner = function () {
        return this.Banners[this.CurrentBanner];
    }
    base.OnChangeBanner = function () { }
    base.NextBanner = function () {
        var str = this.BannerToShow;

        if (this.BannerToShow == -1) {
            this.CurrentBanner = this.GetMoveBannerIndex(1);
        }
        else {
            this.CurrentBanner = this.BannerToShow;
            this.BannerToShow = -1;
        }

        str += " " + this.BannerToShow;

        this.ChangeBanner();
    }
    base.ChangeBanner = function () {
        this.OnChangeBanner(this.CurrentBanner, this.GetCurrentBanner());
        this.ApplyCurrentBanner();
    }
    base.SetBanner = function (index) {
        this.BannerToShow = index;
        clearTimeout(this.Timeout);
        this.Update(true);
        this.OnChangeBanner(index, this.Banners[index]);
    }
    base.MoveBanner = function (dir) {
        var newIndex = this.GetMoveBannerIndex(dir);
        this.SetBanner(newIndex);
    }
//    base.ApplyCurrentBanner = function () {
//        var current = this.GetCurrentBanner();
//        this.Banner.style.backgroundImage = "url(" + current.Src + ")";
//        this.Banner.href = current.Url;
//        this.Banner.title = current.Title;
//        this.Banner.target = current.Target;
//    }

    base.ApplyCurrentBanner = function () {
        var current = this.GetCurrentBanner();
        this.Banner.style.backgroundImage = "url(" + current.Src + ")";
        this.Banner.href = current.Url;
        this.Banner.title = current.Title;
        this.Banner.target = current.Target;
        if (current.Url != '') {
            this.Banner.innerHTML = '<a href="' + current.Url + '" target="' + current.Target + '" title="' + current.Title + '"></a>';
        } else {
            this.Banner.innerHTML = '';
        }
        if (this.Banner.title != '') {
            document.getElementById("titleText" + this.id).innerHTML = current.Title;
        }
        //else {
        //document.getElementById("titleText" + this.id).innerHTML = '';
        //}
    }

    base.Start = function (index) {
        if (index != null) { this.CurrentBanner = index; }

        this.Playing = true;

        this.ApplyCurrentBanner();

        this.Timer = new Date();
        this.OnChangeBanner(this.CurrentBanner);
        this.Timeout = setTimeout("document.getElementById(\"" + this.id + "\").Update(true)", this.PauseLength * 1000);
    }
    base.Update = function (fromPause) {
        if (fromPause == true) {
            this.Timer = new Date();
        }

        this.Paused = false;

        var pause = false;

        var newTimer = new Date();
        var ofSec = (newTimer - this.Timer) / 1000;
        var diff = ofSec / this.TransitionLength;
        var newAlpha = this.Alpha + (diff * this.Direction);
        if (newAlpha <= 0) {
            this.Direction = 1;
            this.NextBanner();
            newAlpha = 0;
        }
        if (newAlpha > 1) {
            this.Direction = -1;
            newAlpha = 1;
            pause = true;
        }
        this.Alpha = newAlpha;

        this.Banner.style.opacity = this.Alpha;
        this.Banner.style.filter = "alpha(opacity=" + (this.Alpha * 100) + ")";

        this.Timer = newTimer;
        if ((this.BannerToShow == -1) && (pause)) {
            if (this.Playing) {
                clearTimeout(this.Timeout);
                this.Timeout = setTimeout("document.getElementById(\"" + this.id + "\").Update(true)", this.PauseLength * 1000);
            }
            this.Paused = true;
        }
        else {
            clearTimeout(this.Timeout);
            this.Timeout = setTimeout("document.getElementById(\"" + this.id + "\").Update()", this.FrameLength);
        }
    }
    base.Pause = function () {
        this.Playing = false;
        if (this.Paused) { clearTimeout(this.Timeout); }
    }
    base.Resume = function () {
        this.Playing = true;
        this.Timer = new Date();
        this.Update(true);
    }

    return base;
}

function Banner(src, title, url, target) {
    var temp = new Image();
    temp.src = src;

    this.Src = src;
    this.Title = title;
    this.Url = url;
    this.Target = target;
}

