var p3SlideShow = {

    oldWinOnLoad: false,
    inits: new Array(),
    faders: new Object(),

    init: function (configs) {
        var fader;

        if (this.inits) {
            this.inits[this.inits.length] = configs;

        } else {
            fader = new this.Fader(configs);

            if (fader != false && !this.faders[fader.id]) {
                this.faders[fader.id] = fader;
                window.setTimeout(function () {    fader.next(); }, fader.viewTime);
            }
        }
    },

    start: function () {
        this.oldWinOnLoad = window.onload;

        window.onload = function () {
            if (typeof(p3SlideShow.oldWinOnLoad) == "function") {
                p3SlideShow.oldWinOnLoad();
            }
            p3SlideShow.onload();
        };
    },

    onload: function () {
        var i, fader, e = document.createElement("link");
        e.type = "text/css";
        e.rel = "stylesheet";
        e.href = "fader-framework/fader-framework.css";
        document.getElementsByTagName("head")[0].appendChild(e);
		
        fader = this.inits;
        delete this.inits;

        for (i = 0; i < fader.length; i++) {
            this.init(fader[i]);
        }
    },

    Fader: function (configs) {
        if (!configs.id || !document.getElementById(configs.id)
            || p3SlideShow.faders[configs.id]
            || configs.images.length < 2) {

            return new Boolean(false);
        }

        var i, original = document.getElementById(configs.id);

        this.id = configs.id;
        this.images = new Array();
        this.counter = false;

        this.element = document.createElement("span");
        this.element.className = "p3fader";
        original.parentNode.replaceChild(this.element, original);

        for (i = 0; i < configs.images.length; i++) {
            this.images[i] = document.createElement("img");
            this.images[i].src = configs.images[i];
            this.images[i].width = configs.w;
            this.images[i].height = configs.h;
            this.images[i].alt = "";

            if (i == 0) {
                this.element.appendChild(this.images[i]);
            }
        }

        this.fade = function (step) {
            var fader = this, imgs = this.element.getElementsByTagName("img");

            step = (!step) ? 0 : step;

            imgs[1].style.opacity = step/100;
            imgs[1].style.filter = "alpha(opacity=" + step + ")"; // IE?

            step = step + 2;

            if (step <= 100) {
                window.setTimeout(function () { fader.fade(step); }, 1);
            } else {
                imgs[1].className = "";
                this.element.removeChild(imgs[0]);
                window.setTimeout(function () { fader.next(); }, 2000);
            }
        };

        this.next = function () {
            this.counter = (this.counter < this.images.length -1) ? this.counter +1 : 0;

            this.element.appendChild(this.images[this.counter]);
            this.images[this.counter].className = "next";
            this.fade();
        };
    }
};

p3SlideShow.start();
