var mBanner = {

    oGallery : null,
    numSlides : 0,
    maxHeight : 0,
    oLayer : null,
    fIndex : 0,
    oNavigation : null,
    fTransition : null,
    fInterval : 5000,
    fSlideshow : 0,
    fPaused : false,

    create : function (oDiv) {

        this.oLayer = document.createElement("div");
        this.oLayer.style.position = 'relative';
        this.oLayer.style.top = '0px';
        this.oLayer.style.zIndex = 1;
        this.oLayer.style.left = '0px';
        this.oNavigation = document.createElement("div");
        this.oNavigation.className = 'FaceSlider';
        this.oNavigation = this.oNavigation.appendChild(document.createElement("ul"));
        this.oGallery = new Array();
        while (oDiv.childNodes.length) {
            var o = oDiv.childNodes[0];
            if (o.tagName && o.tagName.toLowerCase() == 'div') {
                this.oGallery.push(this.createSlide(o));
                this.oLayer.appendChild(oDiv.removeChild(o));
                this.numSlides++;
            } else {
                oDiv.removeChild(o);
            }
        }
        if (this.numSlides) {
            this.oLayer.style.height = this.maxHeight + 'px';
            oDiv.appendChild(this.oLayer);
            if (this.numSlides > 1) {
                oDiv.appendChild(this.oNavigation.parentNode);
                this.play();
            }
        }

    },

    createSlide : function (oElem) {

        var oRef = this;
        var oIndex = this.numSlides;
        oElem.style.display = 'block';
        if (this.maxHeight < oElem.offsetHeight) this.maxHeight = oElem.offsetHeight;
        oElem.onmouseover = function (e) {
            oRef.fPaused = true;
        }
        oElem.onmouseout = function (e) {
            oRef.fPaused = false;
        }
        oElem.style.position = 'absolute';
        oElem.style.zIndex = 1;
        oElem.style.top = '0px';
        oElem.style.width = '700px';
        oElem.style.left = (this.numSlides * 700) + 'px';
        var navElem = this.oNavigation.appendChild(document.createElement("li"));
        if (!this.numSlides) navElem.className = 'active';
        navElem = navElem.appendChild(document.createElement("a"));
        navElem.href = '#';
        navElem.appendChild(document.createTextNode(this.numSlides + 1));
        navElem.onclick = function (e) {
            return oRef.select(oIndex);
        }
        return oElem;

    },

    select : function (oId) {

        this.stop();
        this.show(oId, this.fIndex);
        return false;

    },

    play : function () {

        if (this.numSlides > 1) {
            if (!this.fSlideshow) {
                this.fSlideshow = setInterval("mBanner.play()", this.fInterval);
            } else {
                if (this.fPaused) return;
                var cur = this.fIndex;
                this.fIndex++;
                if (this.fIndex == this.numSlides) this.fIndex = 0;
                this.show(this.fIndex, cur);
            }
        }

    },

    stop : function () {

        if (this.fSlideshow) {
            clearInterval(this.fSlideshow);
            this.fSlideshow = 0;
        }

    },

    show : function (oId, oCurrent) {

        this.fTransition = {total: 20,current: 0,fps : 30,src: oCurrent,dst: oId};
        this.oNavigation.childNodes[oCurrent].className = '';
        setTimeout("mBanner.transition()", this.fTransition.fps);
        this.fIndex = oId;

    },

    transition : function () {

        if (this.fTransition.current < this.fTransition.total) {
            if (this.fTransition.src < this.fTransition.dst) {
                var dst = (this.fTransition.dst - this.fTransition.src) * 700
                var pos = (Math.round(this.cubicInOut(this.fTransition.current, 0, dst, this.fTransition.total)) + this.fTransition.src * 700) * -1;
            } else {
                var dst = (this.fTransition.src - this.fTransition.dst) * 700
                var pos = (this.fTransition.src * 700 - Math.round(this.cubicInOut(this.fTransition.current, 0, dst, this.fTransition.total))) * -1;
            }
            this.oLayer.style.left = pos + 'px';
            this.fTransition.current++;
            setTimeout("mBanner.transition()", this.fTransition.fps);
        } else {
            this.oLayer.style.left = (this.fIndex * 700 * -1) + 'px';
            this.oNavigation.childNodes[this.fIndex].className = 'active';
        }

    },

    cubicInOut : function (t, b, c, d) {

        if ((t /= d / 2) < 1) return c / 2 * t * t * t + b;
        return c / 2 * ((t -= 2) * t * t + 2) + b;

    }

}

function mbannerInitScripts() {

    if ((o = document.getElementById("mainBBlock"))) mBanner.create(o);

}

if (window.addActiveHook) {
    addActiveHook(mbannerInitScripts);
} else {
    window.onload = mbannerInitScripts;
}
