﻿function setObjectOpacity(obj, iOpacity) {
    if (!obj) { return; }
    if (iOpacity < 0) { iOpacity = 0; }
    if (iOpacity > 0.99999) { iOpacity = 0.99999; }
    obj.style.filter = "alpha(opacity:" + iOpacity * 100 + ")";
    obj.style.opacity = iOpacity;
}

function getNow() {
    var n = new Date();
    return n.getTime();
}

function Banner(name)
{
    this.name = name;
    this.obj = null;
    this.link = null;
    this.images = Array();
    this.start = null;
    this.end = null;
    this.index = -1;
}

function BannerImage(path, href, target, text, delay, fade) {
    this.path = path;
    this.text = text;
    this.delay = delay;
    this.fade = fade;
    this.href = href;
    this.target = target;
}

var gBanners = Array();

gBanners.run = function () {
    var r = gBanners.ready;
    setTimeout("gBanners.run()", 10);
    gBanners.ready = true;
    if (!r) { return; }

    for (var b = 0; b < gBanners.length; b++) {
        gBanners[b].animate();
    }
}

Banner.prototype.addImage = function (i) { this.images.push(i); }
Banner.prototype.length = function () { return this.images.length; }
Banner.prototype.enable = function () {
    gBanners.push(this);
}

Banner.prototype.animate = function () {
    var n = getNow();
    var bChange = false;
    var t = this;

    if (t.index < 0) {
        t.phase = -1;
        t.index = Math.floor(Math.random() * t.length());
    }

    if (!t.obj) { t.obj = document.getElementById(t.name); }
    if (!t.link) { t.link = document.getElementById(t.name + "Link"); }

    if (!t.end || n >= t.end) {
        t.phase -= 1;
        if (t.phase < -1) {
            t.index += 1; if (t.index >= t.length()) { t.index = 0; }
            t.phase = 1;
        }
        bChange = true;
        t.start = n;
    }

    var i = t.images[t.index]; var o = t.obj; var l = t.link;

    if (l && l.href != i.href) { l.href = i.href; l.target = i.target; }
    if (o && o.src != i.path) { o.src = i.path; o.alt = i.text; o.target = i.target; }

    var phaseTime = (t.phase == 0) ? i.delay : i.fade;
    if (bChange) { t.end = t.start + phaseTime * 1000; }

    var iOpacity = 1.000;
    if (t.phase == -1) { iOpacity = (t.end - n) / (phaseTime * 1000); }
    if (t.phase == 1) { iOpacity = (n - t.start) / (phaseTime * 1000); }
    setObjectOpacity(o, iOpacity);
} 

