// JavaScript Document

"use strict";

Function.prototype.bind = function (object) {
    var method = this;
    return function () {
        method.apply(object, arguments);
    };
}

/* X marks the spot */
var X = (function () {

    var library = {

        'isIE': (function () {

            return /*@cc_on!@*/false;

        }()),

        'isIE6': (function () {

            return false/*@cc_on || @_jscript_version < 5.7 || (@_jscript_version==5.7 && navigator.userAgent.toLowerCase().indexOf("msie 6.") !== -1) @*/;

        }()),

        'AddEvent': function (node, name, callback) {

            if (node.addEventListener) {
                node.addEventListener(name, callback, false);
            } else if (node.attachEvent) {
                node.attachEvent('on' + name, callback);
            } else {
                return false;
            }

            return true;

        },

        'SetAlpha': function (node, alpha) {
            node.style.opacity = alpha;
            if (X.isIE) {
                node.style.filter = 'alpha(opacity=' + (alpha * 100) + ')';
            }
            return;
        },

        'Slideshow': function (node, delay, speed) {

            var i;

            this.node = node;
            this.delay = delay;
            this.steps = Math.round(speed / 10);
            this.slide_now = 0;

            if (X.isIE6) {
                // compensate for IE 6's lack of child selectors
                for (i = 0; i < this.node.children.length; i++) {

                    this.node.children[i].style.position = 'absolute';
                    this.node.children[i].style.top = 0;
                    this.node.children[i].style.left = 0;
                    this.node.children[i].style.display = 'none';
                    X.SetAlpha(this.node.children[i], 0);

                    if (i == 0) {
                        this.node.children[0].style.display = 'block';
                        X.SetAlpha(this.node.children[0], 1);
                    }

                };
            }

            this.timer_advance = setTimeout(this.advance.bind(this), this.delay);

            return this;

        }

    };

    library.Slideshow.prototype.advance = function () {

        this.slide_next = this.slide_now + 1;

        if (this.slide_next == this.node.children.length) {

            this.slide_next = 0;

        }

        this.node.children[this.slide_next].style.display = 'block';

        this.step = 0;
        this.timer_transition = setInterval(this.transition.bind(this), 10);

    }

    library.Slideshow.prototype.transition = function () {

        this.step++;

        X.SetAlpha(this.node.children[this.slide_now], (1 - (this.step / this.steps)));
        X.SetAlpha(this.node.children[this.slide_next], (this.step / this.steps));

        if (this.step === this.steps) {

            clearInterval(this.timer_transition);

            this.node.children[this.slide_now].style.display = 'none';

            this.slide_now = this.slide_next;

            this.timer_advance = setTimeout(this.advance.bind(this), this.delay);

        }

    }

    return library;

}());

/* FooBar namespace */
var FB = (function () {

    var library = {

        'init': function () {

            this.slideshow = new X.Slideshow(document.getElementById('slideshow'), 5000, 500);

            return this;

        }

    };

    return library;

}());

X.AddEvent(window, 'load', FB.init);
