(function(b) {
    b.extend({
        svUltimateSlider: {
            transitions: {
                fade: {
                    duration: 800,
                    delay: 0,
                    css: {
                        opacity: 0
                    },
                    columns: 1,
                    rows: 1,
                    order: "normal",
                    easing: "linear",
                    slide: "current"
                }
            }
        }
    });
    b.fn.svUltimateSlider = function(p) {
        var g = b.extend({},
        {
            delay: 3E3,
            type: "fade",
            onend: null,
            onstart: null
        },
        p);
        this.each(function() {
            var a = b(this);
            a.attr("sv_cur", 0);
            a.attr("sv_ani", 0);
            a.attr("sv_timer", setTimeout(function() {
                n(a)
            },
            g.delay));
            b("li", this).each(function(f) {
                f == 0 ? b(this).css({
                    zIndex: 2,
                    position: "absolute"
                }) : b(this).css({
                    zIndex: 0,
                    position: "absolute"
                })
            });
            b(g.prev).click(function() {
                if (a.attr("sv_ani") == 0) {
                    var f = a.attr("sv_cur") - 1;
                    if (f < 0) f = b("li", a).length - 1;
                    clearTimeout(a.attr("sv_timer"));
                    n(a, f)
                }
            });
            b(g.next).click(function() {
                if (a.attr("sv_ani") == 0) {
                    clearTimeout(a.attr("sv_timer"));
                    n(a)
                }
            });
            b("li", b(g.controls)).each(function(f) {
                b(this).click(function() {
                    if (a.attr("sv_ani") == 0) {
                        clearTimeout(a.attr("sv_timer"));
                        n(a, f)
                    }
                })
            });
            // a.hover(function() {
            //                 a.attr("sv_ani") == 0 && clearTimeout(a.attr("sv_timer"))
            //             },
            //             function() {
            //                 a.attr("sv_ani") == 0 && a.attr("sv_timer", setTimeout(function() {
            //                     n(a)
            //                 },
            //                 g.delay))
            //             })
        });
        var n = function(a, f) {
            if (a.attr("sv_ani") == 0 && a.attr("sv_cur") != f) {
                a.attr("sv_ani", 1);
                clearTimeout(a.attr("sv_timer"));
                var i = a.attr("sv_cur");
                i = isNaN(f) ? i < b("li", a).length - 1 ? parseInt(i) + 1: 0: f;
                g.onstart && g.onstart(i);
                var h;
                if (b.isArray(g.type)) {
                    isNaN(a.attr("sv_type")) && a.attr("sv_type", 0);
                    h = g.type[parseInt(a.attr("sv_type"))];
                    parseInt(a.attr("sv_type")) + 1 >= g.type.length ? a.attr("sv_type", 0) : a.attr("sv_type", parseInt(a.attr("sv_type")) + 1)
                } else h = g.type;
                h = b.svUltimateSlider.transitions[h] ? b.extend({},
                b.svUltimateSlider.transitions.fade, b.svUltimateSlider.transitions[h]) : b.svUltimateSlider.transitions.fade;
                var c;
                c = [];
                cols = h.columns;
                rows = h.rows;
                for (var o = h.slide == "next" ? b(b("li", a)[i]) : b(b("li", a)[a.attr("sv_cur")]), l = a.width() / cols, j = a.height() / rows, m = b("<li/>").css({
                    width: a.width(),
                    height: a.height(),
                    overflow: "hidden",
                    position: "relative",
                    top: 0,
                    display: "block",
                    zIndex: 3
                }), d = 0; d < rows; d++) for (var e = 0; e < cols; e++) {
                    c[d * cols + e] = b("<div/>").css({
                        width: l + 1,
                        height: j + 1,
                        overflow: "hidden",
                        position: "absolute",
                        left: l * e,
                        top: j * d
                    });
                    if (h.slide == "next") {
                        var k = {};
                        k.width = c[d * cols + e].css("width");
                        k.height = c[d * cols + e].css("height");
                        k.opacity = 1;
                        k.top = c[d * cols + e].css("top");
                        k.left = c[d * cols + e].css("left");
                        c[d * cols + e].data("sv_cssdata", k).animate(h.css, 0);
                        c[d * cols + e].data("sv_divwidth", c[d * cols + e].css("width"));
                        c[d * cols + e].css({
                            width: 0
                        })
                    }
                    k = b("<div/>").html(o.html()).css({
                        marginTop: -(j * d),
                        marginLeft: -(l * e),
                        width: l,
                        height: j
                    });
                    c[d * cols + e].append(k);
                    m.append(c[d * cols + e])
                }
                c = [m, c];
                a.append(c[0]);
                b("li", b(g.controls)).removeClass("active");
                b(b("li", b(g.controls))[i]).addClass("active");
                if (h.slide != "next") {
                    b(b("li", a)[i]).css({
                        zIndex: 2
                    });
                    b(b("li", a)[a.attr("sv_cur")]).css({
                        zIndex: 0
                    })
                }
                q(a, c[1], c[0], i, h)
            }
        },
        q = function(a, f, i, h, c) {
            var o = 0,
            l = f.length;
            del = c.delay;
            tt = c.duration;
            for (var j = 0; j < l; j++) {
                if (c.easing == undefined || !jQuery.easing[c.easing]) c.easing = "swing";
                if (o < l - 1) if (c.order == "random") {
                    o++;
                    var m = f[Math.floor(Math.random() * f.length)];
                    f = jQuery.grep(f,
                    function(d) {
                        return d != m
                    });
                    m.delay(del * j, "svUltimateSlider").queue("svUltimateSlider",
                    function(d) {
                        var e = c.slide == "next" ? b(this).data("sv_cssdata") : c.css;
                        c.slide == "next" && b(this).css({
                            width: b(this).data("sv_divwidth")
                        });
                        b(this).animate(e, tt, c.easing);
                        d()
                    }).dequeue("svUltimateSlider")
                } else f[o++].delay(del * j, "svUltimateSlider").queue("svUltimateSlider",
                function(d) {
                    var e = c.slide == "next" ? b(this).data("sv_cssdata") : c.css;
                    c.slide == "next" && b(this).css({
                        width: b(this).data("sv_divwidth")
                    });
                    b(this).animate(e, tt, c.easing);
                    d()
                }).dequeue("svUltimateSlider");
                else {
                    m = c.order == "random" ? f[0] : f[o++];
                    clearInterval(a.attr("sv_timer"));
                    m.delay(del * j, "svUltimateSlider").queue("svUltimateSlider",
                    function(d) {
                        var e = c.slide == "next" ? b(this).data("sv_cssdata") : c.css;
                        c.slide == "next" && b(this).css({
                            width: b(this).data("sv_divwidth")
                        });
                        b(this).animate(e, tt, c.easing,
                        function() {
                            i.remove();
                            b(b("li", a)[h]).css({
                                zIndex: 2
                            });
                            b(b("li", a)[a.attr("sv_cur")]).css({
                                zIndex: 0
                            });
														var p = 1;
                            if (g.onend)
 															 p = g.onend(h);
                            a.attr("sv_cur", h);
														if (p) {
															a.attr("sv_timer", setTimeout(function() {
	                                n(a)
	                            },	
															g.delay));
	                            a.attr("sv_ani", 0)
														}
                            
                        });
                        d()
                    }).dequeue("svUltimateSlider")
                }
            }
        };
        return this
    }
})(jQuery);
