var Carousel = (function() {
    var a = {
        init: function(b) {
            $(b).each(function() {
                a.newCarousel($(this))
            })
        },
        newCarousel: function(c) {
            var b = {
                settings: {
                    scroll_offset: -230
                },
                reset: function() {
                    b.model = new Xteam.Model({
                        current: 0
                    });
                    b.next_elem = c.find(".next").unbind("click").click(b.handleNextClick);
                    b.prev_elem = c.find(".prev").unbind("click").click(b.handlePrevClick);
                    b.slide_holder = c.find(".slide-holder");
                    b.slide_tray = c.find(".slide-tray");
                    if (!b.slide_tray.length) {
                        b.slide_tray = b.slide_holder.wrapInner("<div></div>").children().eq(0)
                    }
                    b.slides = b.slide_tray.children();
                    var d = b.slide_holder.width();
                    b.slide_holder.css("overflow", "hidden");
                    b.slide_tray.css({
                        position: "relative",
                        width: (d * b.slides.length)
                    });
                    b.slides.css({
                        "float": "left",
                        overflow: "hidden"
                    });
                    b.length = b.slides.length;
                    b.goto_slide(0)
                },
                goto_slide: function(f) {
                    while (f < 0) {
                        f += b.length
                    }
                    f %= b.length;
                    if (f == b.model.fields.current) {
                        return
                    }
                    var i = b.slides.eq(b.model.fields.current);
                    var e = b.slides.eq(f);
                    c.removeClass("at-first at-last");
                    if (f == 0) {
                        c.addClass("at-first")
                    } else {
                        if (f == b.length - 1) {
                            c.addClass("at-last")
                        }
                    }
                    var h = 500;
                    var d = c.hasClass("fixed_height");
                    if (!d) {
                        e.css({
                            height: "auto"
                        });
                        window.setTimeout(function() {
                            i.animate({
                                height: 1
                            },
                            {
                                duration: h,
                                complete: function() {
                                    i.removeClass("active")
                                }
                            });
                            $.scrollTo(b.slide_holder, {
                                offset: {
                                    top: b.settings.scroll_offset,
                                    left: 0
                                },
                                duration: h,
                                axis: "y"
                            })
                        },
                        Math.floor(h * 0.75))
                    }
                    var g = e.position().left;
                    b.slide_holder.animate({
                        scrollLeft: g
                    },
                    {
                        duration: h,
                        complete: function() {
                            if (!d) {
                                i.css({
                                    height: 1
                                })
                            }
                        }
                    });
                    b.model.update({
                        current: f
                    })
                },
                handleNextClick: function(d) {
                    d.preventDefault();
                    b.next()
                },
                next: function() {
                    b.goto_slide(b.model.fields.current + 1)
                },
                handlePrevClick: function(d) {
                    d.preventDefault();
                    b.prev()
                },
                prev: function() {
                    b.goto_slide(b.model.fields.current - 1)
                },
                hide: function(d) {
                    d.fadeOut()
                },
                at_first: function() { },
                at_last: function() {
                    c.addClass("at-last")
                }
            };
            b.reset();
            c.data("carousel", b);
            return b
        }
    };
    return a
})();
var CarouselPagination = function() {
    var b = function(c, d) {
        Xteam.DomView.apply(this, [c]);
        var e = ViewDefinitions.copy("carousel_pagination");
        DefinedView.apply(this, [e]);
        this.carousel_elm = d
    };
    b.prototype = $.extend({},
    Xteam.DomView.prototype, DefinedView.prototype, {
        updateCurrentPage: function(f) {
            var l = this.carousel_elm.data("carousel");
            var j = l.length;
            this.elm().setClass("has_next", f < j - 1);
            this.elm("previous_button").setClass("disabled", f == 0);
            this.elm("next_button").setClass("disabled", f >= j - 1);
            var h = this.model.fields.page_size;
            var i = this.carousel_elm.find("li.result").length;
            var d = (f * h) + 1;
            var k = Math.min(d + h - 1, i);
            this.model.update({
                start_result: d,
                end_result: k
            });
            var g = k + 1;
            var e = Math.min(g + h - 1, i);
            var c = k < i;
            if (c) {
                this.model.update({
                    next_start: g,
                    next_end: e
                })
            }
            this.elm().setClass("show_next_results", c)
        },
        setupEvents: function() {
            Xteam.DomView.prototype.setupEvents.call(this);
            var c = this;
            var d = this.carousel_elm.data("carousel");
            this.elm("next_button").add(this.elm("more_results_button")).click(function(e) {
                e.preventDefault();
                if (c.elm("next_button").hasClass("disabled")) {
                    return
                }
                d.next()
            });
            this.elm("previous_button").click(function(e) {
                e.preventDefault();
                if ($(this).hasClass("disabled")) {
                    return
                }
                d.prev()
            });
            d.model.addOwner(function(f, e) {
                if (f == "changed" && typeof e.current != "undefined") {
                    c.updateCurrentPage(e.current)
                }
                return true
            })
        }
    });
    var a = Xteam.Model;
    return {
        create: function(f) {
            var g = f.find("li.result").length;
            var e = f.find(".slide:eq(0) li.result").length;
            var h = {
                page_size: e,
                start_result: 1,
                end_result: e,
                total_results: g
            };
            var d = new a(h);
            var c = new b(d, f);
            c.updateCurrentPage(0);
            f.append(c.elm())
        }
    }
} ();
