﻿///-class-/////////////////////////////////////////////////////////////////////
// Fader
///////////////////////////////////////////////////////////////////////////////
function Fader(ID) {
    ///-data-//////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////
    this._ID = ID;
    this._interval = '';
    this._opacity = 1.0;
    this._decrement = 0.2;
    this._items = new Array();
    this._itemIndex = 0;
    this._element0 = null;
    this._element1 = null;
    this._delay = 50;

    ///-methods-///////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////
    this.GetElements = function() {
        if (!this._element0 || !this._element1) {
            var index = this._itemIndex + 1;
            if (index >= this._items.length)
                index = 0;

            this._element0 = wGet(this._items[this._itemIndex]);
            this._element1 = wGet(this._items[index]);
        }

        return this._element0 && this._element1;
    };

    this.SetItemOpacity = function(Item, Value) {
        if (MSIEVersion > 5.5 && window.document.body.filters)
            Item.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + Math.round(Value * 100) + ')';
        else {
            Item.style.opacity = Value;
            Item.style.MozOpacity = Value;
        }
    };

    this.SetOpacity = function() {
        if (this.GetElements()) {
            this.SetItemOpacity(this._element0, this._opacity);
            this.SetItemOpacity(this._element1, (1.0 - this._opacity));
        }
    };

    this.SetZIndex = function() {
        for (var count = 0; count < this._items.length; ++count)
            if (wGet(this._items[count])) {
            if (count == this._itemIndex)
                wGet(this._items[count]).style.zIndex = 2;
            else
                wGet(this._items[count]).style.zIndex = 1;
        }
    };

    this.AddItem = function(ID) {
        this._items[this._items.length] = ID;
    };

    this.Animate = function() {
        if (this._interval != '') {
            if (this._delay) {
                --this._delay;
            }
            else {
                this._opacity -= this._decrement;

                if (this._opacity < 0.0) {
                    this._opacity = 1.0;

                    ++this._itemIndex;
                    if (this._itemIndex >= this._items.length)
                        this._itemIndex = 0;

                    this._element0 = null;
                    this._element1 = null;

                    this._delay = 50;

                    this.SetZIndex();
                }

                this.SetOpacity();
            }
        }
    };

    this.Start = function() {
        for (var count = 0; count < this._items.length; ++count) {
            var itemNext = wGet(this._items[count]);

            this.SetItemOpacity(itemNext, 0);

            if (itemNext)
                itemNext.style.visibility = 'visible';
        }

        this.SetOpacity();
        this.SetZIndex();

        if (this._interval == '')
            this._interval = setInterval(this._ID + '.Animate()', 50);
    };

    this.Stop = function() {
        if (this._interval != '')
            clearInterval(this._interval);

        this._interval = '';
    };

    this.ClickItem = function(Index) {
        this.Stop();

        if (this.GetElements()) {
            this.SetItemOpacity(this._element0, 0.0);
            this.SetItemOpacity(this._element1, 0.0);
        }

        this._element0 = null;
        this._element1 = null;

        this._itemIndex = Index;
        this.GetElements();

        this._delay = 100;
        this._opacity = 1.0;

        this.SetOpacity();

        this.SetZIndex();

        this.Start();
    };
}

var MSIEVersion = msieversion();

function wGet(id) {
    return document.getElementById(id);
}

function msieversion()
// return Microsoft Internet Explorer (major)
// version number, or 0 for others.
// This function works by finding the "MSIE "
// string and extracting the version number
// following the space, up to the decimal point
// for the minor version, which is ignored.
{
    var ua = window.navigator.userAgent
    var msie = ua.indexOf("MSIE ")
    if (msie > 0)      // is Microsoft Internet Explorer; return version number
        return parseInt(ua.substring(msie + 5, ua.indexOf(".", msie)))
    else
        return 0          // is other browser
}
