﻿function BannerManager(name, backdrop, link, controls) {
    this.Banners = new Array();

    this.name = name;

    this.transitionLength = 1;
    this.delayLength = 5;
    this.pauseLength = 10;

    this.backdrop = backdrop;
    this.link = link;
    this.controls = controls;

    this.oldBannerIndex = -1;
    this.newBannerIndex = -1;

    this.UserSelected = false;

    this.Delaying = true;
    this.Playing = true;

    this.Setup = function() {
        if (this.controls != null) {
            this.controls.className = "active";
            this.controls.innerHTML = "";
        }
    }
    this.PreSetup = function() { }
    this.PostSetup = function() { }

    this.Add = function(imgUrl, linkUrl) {
        var banner = new Banner(imgUrl, linkUrl);
        this.Banners.push(banner);

        if (this.controls != null) {
            var li = document.createElement("LI");
            var a = li.appendChild(document.createElement("A"));
            a.href = "";
            a.Index = this.Banners.length - 1;
            a.Manager = this;
            a.innerHTML = "<span>" + this.Banners.length + "</span>";
            a.onclick = function() { this.Manager.UserSelect(this.Index); return false; }
            this.controls.appendChild(li);

            banner.link = a;
        }
    }

    this.Timer = null;

    this.ClearTimer = function() {
        if (this.Timer != null) {
            clearInterval(this.Timer);
            this.Timer = null;
        }
    }

    this.Next = function(dir) {
        if (dir == null) { dir = 1; }
        this.ClearTimer();
        var nextIndex = this.newBannerIndex + dir;
        if (nextIndex > this.Banners.length - 1) { nextIndex = 0; }
        if (nextIndex < 0) { nextIndex = this.Banners.length - 1; }
        this.UserSelected = false;
        this.Select(nextIndex);
    }

    this.UserSelect = function(index) {
        this.UserSelected = true;
        this.Select(index);
    }
    this.Select = function(index) {
        this.oldBannerIndex = this.newBannerIndex;
        this.newBannerIndex = index;

        var oldBanner = this.Banners[this.oldBannerIndex];
        var newBanner = this.Banners[this.newBannerIndex];
        if (oldBanner.link != null) { oldBanner.link.className = ""; }
        if (newBanner.link != null) { newBanner.link.className = "sel"; }

        this.BeginTransition();
    }

    this.lastUpdated = null;
    this.UpdateTransition = function() {
        var frameLength = 0;
        if (this.lastUpdated != null) {
            frameLength = (new Date().valueOf() - this.lastUpdated.valueOf());
        }

        var oldAlpha = this.link.alpha;
        this.link.alpha += (frameLength / (this.transitionLength * 1000));
        if (this.link.alpha >= 1) {
            this.link.alpha = 1;
            this.EndTransition();
        }
        if ((this.link.alpha >= 0.5) && (oldAlpha < 0.5)) {
            this.link.href = this.Banners[this.newBannerIndex].linkUrl;
        }

        this.link.style.opacity = this.link.alpha;
        this.link.style.filter = "alpha(opacity=" + (this.link.alpha * 100) + ")";

        this.lastUpdated = new Date();
    }

    this.BeginTransition = function() {
        this.Delaying = false;

        this.lastUpdated = null;

        var oldBanner = this.Banners[this.oldBannerIndex];
        this.backdrop.style.backgroundImage = "url(" + oldBanner.imgUrl + ")";

        var newBanner = this.Banners[this.newBannerIndex];
        this.link.style.backgroundImage = "url(" + newBanner.imgUrl + ")";
        this.link.alpha = 0;

        this.UpdateTransition();
        this.ClearTimer();
        this.Timer = setInterval(this.name + ".UpdateTransition();", 1);
    }

    this.EndTransition = function() {
        this.ClearTimer();
        if (this.Playing) {
            if ((this.UserSelected) && (this.pauseLength > 0)) { this.Timer = setTimeout(this.name + ".Next();", this.pauseLength * 1000); return; }
            if ((!this.UserSelected) && (this.delayLength > 0)) { this.Timer = setTimeout(this.name + ".Next();", this.delayLength * 1000); }
        }

        this.Delaying = true;
    }

    this.Start = function() {
        this.Playing = true;
        this.Next(1);
    }
    this.Stop = function() {
        this.Playing = false;

        if (this.Delaying) { this.ClearTimer(); }
    }

    this.FirstSelect = function(index) {
        this.UserSelected = false;

        this.newBannerIndex = index;
        this.oldBannerIndex = index;

        var currentBanner = this.Banners[this.newBannerIndex];

        if (currentBanner.link != null) { currentBanner.link.className = "sel"; }

        this.link.href = currentBanner.linkUrl;
        this.backdrop.style.backgroundImage = "url(" + currentBanner.imgUrl + ")";

        this.EndTransition();
    }
}

function Banner(imgUrl, linkUrl) {
    this.imgUrl = imgUrl;
    this.linkUrl = linkUrl;
    this.link = null;

    this.preload = new Image();
    this.preload.src = imgUrl;

}

