/**
 * Init jQuery.SS(IMG_ELEMENT,IMAGE_NAMES_ARRAY,TIMEOUT)
 */

(function($) {
    function SS(el, array, timeout) {
        if (array == null || array.length <= 1) return;
        this.init(el, array, timeout);
    }


    SS.prototype = {
        SS_switch:function () {
            if (this.curIdx >= this.images.length) this.curIdx = 0;

            var img = this.images[this.curIdx];

            if (img.width) {
                var dow = (img.width / img.height) > (this.maxw / this.maxh);
                if (dow) {
                    var h = this.maxw * img.height / img.width;
                    this.off.css({
                        width: this.maxw,
                        height: h,
                        top:(this.maxh - h) / 2,
                        left:0
                    })
                } else {
                    var w = this.maxh * img.width / img.height;
                    this.off.css({
                        width: w,
                        height: this.maxh,
                        top:0,
                        left:(this.maxw - w) / 2
                    })
                }

            }

            this.off.attr("src", img.src);

            this.on.fadeOut(this.timeout / 4);
            this.off.fadeIn(this.timeout / 4);

            this.curIdx++;

            var t = this.on;
            this.on = this.off;
            this.off = t;


            var self = this;
            window.setTimeout(function() {
                self.SS_switch();
            }, this.timeout + (this.timeout / 4));
        },

        init:function(el, array, timeout) {
            this.el = $(el);
            this.maxw = $(el).width();
            this.maxh = $(el).height();
            this.curIdx = 0;
            this.timeout = timeout;

            var images = new Array();
            jQuery.each(array, function() {
                var img = new Image();
                img.src = this;
                images[images.length] = img;
            });
            this.images = images;

            this.el.wrap("<div class=\"ssWrapper\"></div>");
            $(".ssWrapper").css({
                width: $(el).width(),
                height: $(el).height(),
                overflow: "hidden",
                position: "relative"
            });

            var el2 = $(document.createElement("IMG"));
            el2.css({
                display:"none"
            });


            this.el.after(el2);
            this.on = this.el;
            this.off = el2;

            $(".ssWrapper img").css({
                position: "absolute",top:0,left:0
            });


            var self = this;
            window.setTimeout(function() {
                self.SS_switch();
            }, this.timeout);
        }
    }
            ;
    $.fn.ss = function(array, timeout) {
        return this.each(function() {
            new SS(this, array, timeout);
        });
    };
})
        (jQuery);