/*
 * jCarousel - Riding carousels with jQuery
 * http://sorgalla.com/jcarousel/
 *
 * Copyright (c) 2006 Jan Sorgalla (http://sorgalla.com)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Built on top of the jQuery library
 * http://jquery.com
 *
 * Inspired by the "Carousel Component" by Bill Scott
 * http://billwscott.com/carousel/
 */
(function (c) {
    var d = {
        vertical: false,
        rtl: false,
        start: 1,
        offset: 1,
        size: null,
        scroll: 3,
        visible: null,
        animation: "normal",
        easing: "swing",
        auto: 0,
        wrap: null,
        initCallback: null,
        reloadCallback: null,
        itemLoadCallback: null,
        itemFirstInCallback: null,
        itemFirstOutCallback: null,
        itemLastInCallback: null,
        itemLastOutCallback: null,
        itemVisibleInCallback: null,
        itemVisibleOutCallback: null,
        buttonNextHTML: "<div></div>",
        buttonPrevHTML: "<div></div>",
        buttonNextEvent: "click",
        buttonPrevEvent: "click",
        buttonNextCallback: null,
        buttonPrevCallback: null,
        itemFallbackDimension: null
    },
        b = false;
    c(window).bind("load.jcarousel", function () {
        b = true
    });
    c.jcarousel = function (l, g) {
        this.options = c.extend({}, d, g || {});
        this.locked = false;
        this.autoStopped = false;
        this.container = null;
        this.clip = null;
        this.list = null;
        this.buttonNext = null;
        this.buttonPrev = null;
        this.buttonNextState = null;
        this.buttonPrevState = null;
        if (!g || g.rtl === undefined) {
            this.options.rtl = (c(l).attr("dir") || c("html").attr("dir") || "").toLowerCase() == "rtl"
        }
        this.wh = !this.options.vertical ? "width" : "height";
        this.lt = !this.options.vertical ? (this.options.rtl ? "right" : "left") : "top";
        var q = "",
            n = l.className.split(" ");
        for (var k = 0; k < n.length; k++) {
            if (n[k].indexOf("jcarousel-skin") != -1) {
                c(l).removeClass(n[k]);
                q = n[k];
                break
            }
        }
        if (l.nodeName.toUpperCase() == "UL" || l.nodeName.toUpperCase() == "OL") {
            this.list = c(l);
            this.container = this.list.parent();
            if (this.container.hasClass("jcarousel-clip")) {
                if (!this.container.parent().hasClass("jcarousel-container")) {
                    this.container = this.container.wrap("<div></div>")
                }
                this.container = this.container.parent()
            } else {
                if (!this.container.hasClass("jcarousel-container")) {
                    this.container = this.list.wrap("<div></div>").parent()
                }
            }
        } else {
            this.container = c(l);
            this.list = this.container.find("ul,ol").eq(0)
        }
        if (q !== "" && this.container.parent()[0].className.indexOf("jcarousel-skin") == -1) {
            this.container.wrap('<div class=" ' + q + '"></div>')
        }
        this.clip = this.list.parent();
        if (!this.clip.length || !this.clip.hasClass("jcarousel-clip")) {
            this.clip = this.list.wrap("<div></div>").parent()
        }
        this.buttonNext = c(".jcarousel-next", this.container);
        if (this.buttonNext.size() === 0 && this.options.buttonNextHTML !== null) {
            this.buttonNext = this.clip.after(this.options.buttonNextHTML).next()
        }
        this.buttonNext.addClass(this.className("jcarousel-next"));
        this.buttonPrev = c(".jcarousel-prev", this.container);
        if (this.buttonPrev.size() === 0 && this.options.buttonPrevHTML !== null) {
            this.buttonPrev = this.clip.after(this.options.buttonPrevHTML).next()
        }
        this.buttonPrev.addClass(this.className("jcarousel-prev"));
        this.clip.addClass(this.className("jcarousel-clip")).css({
            overflow: "hidden",
            position: "relative"
        });
        this.list.addClass(this.className("jcarousel-list")).css({
            overflow: "hidden",
            position: "relative",
            top: 0,
            margin: 0,
            padding: 0
        }).css((this.options.rtl ? "right" : "left"), 0);
        this.container.addClass(this.className("jcarousel-container")).css({
            position: "relative"
        });
        if (!this.options.vertical && this.options.rtl) {
            this.container.addClass("jcarousel-direction-rtl").attr("dir", "rtl")
        }
        var m = this.options.visible !== null ? Math.ceil(this.clipping() / this.options.visible) : null;
        var p = this.list.children("li");
        var r = this;
        if (p.size() > 0) {
            var f = 0,
                h = this.options.offset;
            p.each(function () {
                r.format(this, h++);
                f += r.dimension(this, m)
            });
            this.list.css(this.wh, (f + 100) + "px");
            if (!g || g.size === undefined) {
                this.options.size = p.size()
            }
        }
        this.container.css("display", "block");
        this.buttonNext.css("display", "block");
        this.buttonPrev.css("display", "block");
        this.funcNext = function () {
            r.next()
        };
        this.funcPrev = function () {
            r.prev()
        };
        this.funcResize = function () {
            r.reload()
        };
        if (this.options.initCallback !== null) {
            this.options.initCallback(this, "init")
        }
        this.setup()
    };
    var a = c.jcarousel;
    a.fn = a.prototype = {
        jcarousel: "0.2.7"
    };
    a.fn.extend = a.extend = c.extend;
    a.fn.extend({
        setup: function () {
            this.first = null;
            this.last = null;
            this.prevFirst = null;
            this.prevLast = null;
            this.animating = false;
            this.timer = null;
            this.tail = null;
            this.inTail = false;
            if (this.locked) {
                return
            }
            this.list.css(this.lt, this.pos(this.options.offset) + "px");
            var f = this.pos(this.options.start, true);
            this.prevFirst = this.prevLast = null;
            this.animate(f, false);
            c(window).unbind("resize.jcarousel", this.funcResize).bind("resize.jcarousel", this.funcResize)
        },
        reset: function () {
            this.list.empty();
            this.list.css(this.lt, "0px");
            this.list.css(this.wh, "10px");
            if (this.options.initCallback !== null) {
                this.options.initCallback(this, "reset")
            }
            this.setup()
        },
        reload: function () {
            if (this.tail !== null && this.inTail) {
                this.list.css(this.lt, a.intval(this.list.css(this.lt)) + this.tail)
            }
            this.tail = null;
            this.inTail = false;
            if (this.options.reloadCallback !== null) {
                this.options.reloadCallback(this)
            }
            if (this.options.visible !== null) {
                var h = this;
                var j = Math.ceil(this.clipping() / this.options.visible),
                    g = 0,
                    f = 0;
                this.list.children("li").each(function (k) {
                    g += h.dimension(this, j);
                    if (k + 1 < h.first) {
                        f = g
                    }
                });
                this.list.css(this.wh, g + "px");
                this.list.css(this.lt, -f + "px")
            }
            this.scroll(this.first, false)
        },
        lock: function () {
            this.locked = true;
            this.buttons()
        },
        unlock: function () {
            this.locked = false;
            this.buttons()
        },
        size: function (f) {
            if (f !== undefined) {
                this.options.size = f;
                if (!this.locked) {
                    this.buttons()
                }
            }
            return this.options.size
        },
        has: function (g, h) {
            if (h === undefined || !h) {
                h = g
            }
            if (this.options.size !== null && h > this.options.size) {
                h = this.options.size
            }
            for (var f = g; f <= h; f++) {
                var k = this.get(f);
                if (!k.length || k.hasClass("jcarousel-item-placeholder")) {
                    return false
                }
            }
            return true
        },
        get: function (f) {
            return c(".jcarousel-item-" + f, this.list)
        },
        add: function (l, q) {
            var m = this.get(l),
                h = 0,
                g = c(q);
            if (m.length === 0) {
                var p, k = a.intval(l);
                m = this.create(l);
                while (true) {
                    p = this.get(--k);
                    if (k <= 0 || p.length) {
                        if (k <= 0) {
                            this.list.prepend(m)
                        } else {
                            p.after(m)
                        }
                        break
                    }
                }
            } else {
                h = this.dimension(m)
            }
            if (g.get(0).nodeName.toUpperCase() == "LI") {
                m.replaceWith(g);
                m = g
            } else {
                m.empty().append(q)
            }
            this.format(m.removeClass(this.className("jcarousel-item-placeholder")), l);
            var o = this.options.visible !== null ? Math.ceil(this.clipping() / this.options.visible) : null;
            var f = this.dimension(m, o) - h;
            if (l > 0 && l < this.first) {
                this.list.css(this.lt, a.intval(this.list.css(this.lt)) - f + "px")
            }
            this.list.css(this.wh, a.intval(this.list.css(this.wh)) + f + "px");
            return m
        },
        remove: function (f) {
            var g = this.get(f);
            if (!g.length || (f >= this.first && f <= this.last)) {
                return
            }
            var h = this.dimension(g);
            if (f < this.first) {
                this.list.css(this.lt, a.intval(this.list.css(this.lt)) + h + "px")
            }
            g.remove();
            this.list.css(this.wh, a.intval(this.list.css(this.wh)) - h + "px")
        },
        next: function () {
            if (this.tail !== null && !this.inTail) {
                this.scrollTail(false)
            } else {
                this.scroll(((this.options.wrap == "both" || this.options.wrap == "last") && this.options.size !== null && this.last == this.options.size) ? 1 : this.first + this.options.scroll)
            }
        },
        prev: function () {
            if (this.tail !== null && this.inTail) {
                this.scrollTail(true)
            } else {
                this.scroll(((this.options.wrap == "both" || this.options.wrap == "first") && this.options.size !== null && this.first == 1) ? this.options.size : this.first - this.options.scroll)
            }
        },
        scrollTail: function (f) {
            if (this.locked || this.animating || !this.tail) {
                return
            }
            this.pauseAuto();
            var g = a.intval(this.list.css(this.lt));
            g = !f ? g - this.tail : g + this.tail;
            this.inTail = !f;
            this.prevFirst = this.first;
            this.prevLast = this.last;
            this.animate(g)
        },
        scroll: function (g, f) {
            if (this.locked || this.animating) {
                return
            }
            this.pauseAuto();
            this.animate(this.pos(g), f)
        },
        pos: function (E, k) {
            var n = a.intval(this.list.css(this.lt));
            if (this.locked || this.animating) {
                return n
            }
            if (this.options.wrap != "circular") {
                E = E < 1 ? 1 : (this.options.size && E > this.options.size ? this.options.size : E)
            }
            var B = this.first > E;
            var G = this.options.wrap != "circular" && this.first <= 1 ? 1 : this.first;
            var J = B ? this.get(G) : this.get(this.last);
            var D = B ? G : G - 1;
            var H = null,
                C = 0,
                z = false,
                I = 0,
                F;
            while (B ? --D >= E : ++D < E) {
                H = this.get(D);
                z = !H.length;
                if (H.length === 0) {
                    H = this.create(D).addClass(this.className("jcarousel-item-placeholder"));
                    J[B ? "before" : "after"](H);
                    if (this.first !== null && this.options.wrap == "circular" && this.options.size !== null && (D <= 0 || D > this.options.size)) {
                        F = this.get(this.index(D));
                        if (F.length) {
                            H = this.add(D, F.clone(true))
                        }
                    }
                }
                J = H;
                I = this.dimension(H);
                if (z) {
                    C += I
                }
                if (this.first !== null && (this.options.wrap == "circular" || (D >= 1 && (this.options.size === null || D <= this.options.size)))) {
                    n = B ? n + I : n - I
                }
            }
            var u = this.clipping(),
                y = [],
                h = 0,
                w = 0;
            J = this.get(E - 1);
            D = E;
            while (++h) {
                H = this.get(D);
                z = !H.length;
                if (H.length === 0) {
                    H = this.create(D).addClass(this.className("jcarousel-item-placeholder"));
                    if (J.length === 0) {
                        this.list.prepend(H)
                    } else {
                        J[B ? "before" : "after"](H)
                    }
                    if (this.first !== null && this.options.wrap == "circular" && this.options.size !== null && (D <= 0 || D > this.options.size)) {
                        F = this.get(this.index(D));
                        if (F.length) {
                            H = this.add(D, F.clone(true))
                        }
                    }
                }
                J = H;
                I = this.dimension(H);
                if (I === 0) {
                    throw new Error("jCarousel: No width/height set for items. This will cause an infinite loop. Aborting...")
                }
                if (this.options.wrap != "circular" && this.options.size !== null && D > this.options.size) {
                    y.push(H)
                } else {
                    if (z) {
                        C += I
                    }
                }
                w += I;
                if (w >= u) {
                    break
                }
                D++
            }
            for (var r = 0; r < y.length; r++) {
                y[r].remove()
            }
            if (C > 0) {
                this.list.css(this.wh, this.dimension(this.list) + C + "px");
                if (B) {
                    n -= C;
                    this.list.css(this.lt, a.intval(this.list.css(this.lt)) - C + "px")
                }
            }
            var q = E + h - 1;
            if (this.options.wrap != "circular" && this.options.size && q > this.options.size) {
                q = this.options.size
            }
            if (D > q) {
                h = 0;
                D = q;
                w = 0;
                while (++h) {
                    H = this.get(D--);
                    if (!H.length) {
                        break
                    }
                    w += this.dimension(H);
                    if (w >= u) {
                        break
                    }
                }
            }
            var o = q - h + 1;
            if (this.options.wrap != "circular" && o < 1) {
                o = 1
            }
            if (this.inTail && B) {
                n += this.tail;
                this.inTail = false
            }
            this.tail = null;
            if (this.options.wrap != "circular" && q == this.options.size && (q - h + 1) >= 1) {
                var A = a.margin(this.get(q), !this.options.vertical ? "marginRight" : "marginBottom");
                if ((w - A) > u) {
                    this.tail = w - u - A
                }
            }
            if (k && E === this.options.size && this.tail) {
                n -= this.tail;
                this.inTail = true
            }
            while (E-- > o) {
                n += this.dimension(this.get(E))
            }
            this.prevFirst = this.first;
            this.prevLast = this.last;
            this.first = o;
            this.last = q;
            return n
        },
        animate: function (j, f) {
            if (this.locked || this.animating) {
                return
            }
            this.animating = true;
            var g = this;
            var h = function () {
                    g.animating = false;
                    if (j === 0) {
                        g.list.css(g.lt, 0)
                    }
                    if (!g.autoStopped && (g.options.wrap == "circular" || g.options.wrap == "both" || g.options.wrap == "last" || g.options.size === null || g.last < g.options.size || (g.last == g.options.size && g.tail !== null && !g.inTail))) {
                        g.startAuto()
                    }
                    g.buttons();
                    g.notify("onAfterAnimation");
                    if (g.options.wrap == "circular" && g.options.size !== null) {
                        for (var l = g.prevFirst; l <= g.prevLast; l++) {
                            if (l !== null && !(l >= g.first && l <= g.last) && (l < 1 || l > g.options.size)) {
                                g.remove(l)
                            }
                        }
                    }
                };
            this.notify("onBeforeAnimation");
            if (!this.options.animation || f === false) {
                this.list.css(this.lt, j + "px");
                h()
            } else {
                var k = !this.options.vertical ? (this.options.rtl ? {
                    right: j
                } : {
                    left: j
                }) : {
                    top: j
                };
                this.list.animate(k, this.options.animation, this.options.easing, h)
            }
        },
        startAuto: function (g) {
            if (g !== undefined) {
                this.options.auto = g
            }
            if (this.options.auto === 0) {
                return this.stopAuto()
            }
            if (this.timer !== null) {
                return
            }
            this.autoStopped = false;
            var f = this;
            this.timer = window.setTimeout(function () {
                f.next()
            }, this.options.auto * 1000)
        },
        stopAuto: function () {
            this.pauseAuto();
            this.autoStopped = true
        },
        pauseAuto: function () {
            if (this.timer === null) {
                return
            }
            window.clearTimeout(this.timer);
            this.timer = null
        },
        buttons: function (h, g) {
            if (h == null) {
                h = !this.locked && this.options.size !== 0 && ((this.options.wrap && this.options.wrap != "first") || this.options.size === null || this.last < this.options.size);
                if (!this.locked && (!this.options.wrap || this.options.wrap == "first") && this.options.size !== null && this.last >= this.options.size) {
                    h = this.tail !== null && !this.inTail
                }
            }
            if (g == null) {
                g = !this.locked && this.options.size !== 0 && ((this.options.wrap && this.options.wrap != "last") || this.first > 1);
                if (!this.locked && (!this.options.wrap || this.options.wrap == "last") && this.options.size !== null && this.first == 1) {
                    g = this.tail !== null && this.inTail
                }
            }
            var f = this;
            if (this.buttonNext.size() > 0) {
                this.buttonNext.unbind(this.options.buttonNextEvent + ".jcarousel", this.funcNext);
                if (h) {
                    this.buttonNext.bind(this.options.buttonNextEvent + ".jcarousel", this.funcNext)
                }
                this.buttonNext[h ? "removeClass" : "addClass"](this.className("jcarousel-next-disabled")).attr("disabled", h ? false : true);
                if (this.options.buttonNextCallback !== null && this.buttonNext.data("jcarouselstate") != h) {
                    this.buttonNext.each(function () {
                        f.options.buttonNextCallback(f, this, h)
                    }).data("jcarouselstate", h)
                }
            } else {
                if (this.options.buttonNextCallback !== null && this.buttonNextState != h) {
                    this.options.buttonNextCallback(f, null, h)
                }
            }
            if (this.buttonPrev.size() > 0) {
                this.buttonPrev.unbind(this.options.buttonPrevEvent + ".jcarousel", this.funcPrev);
                if (g) {
                    this.buttonPrev.bind(this.options.buttonPrevEvent + ".jcarousel", this.funcPrev)
                }
                this.buttonPrev[g ? "removeClass" : "addClass"](this.className("jcarousel-prev-disabled")).attr("disabled", g ? false : true);
                if (this.options.buttonPrevCallback !== null && this.buttonPrev.data("jcarouselstate") != g) {
                    this.buttonPrev.each(function () {
                        f.options.buttonPrevCallback(f, this, g)
                    }).data("jcarouselstate", g)
                }
            } else {
                if (this.options.buttonPrevCallback !== null && this.buttonPrevState != g) {
                    this.options.buttonPrevCallback(f, null, g)
                }
            }
            this.buttonNextState = h;
            this.buttonPrevState = g
        },
        notify: function (f) {
            var g = this.prevFirst === null ? "init" : (this.prevFirst < this.first ? "next" : "prev");
            this.callback("itemLoadCallback", f, g);
            if (this.prevFirst !== this.first) {
                this.callback("itemFirstInCallback", f, g, this.first);
                this.callback("itemFirstOutCallback", f, g, this.prevFirst)
            }
            if (this.prevLast !== this.last) {
                this.callback("itemLastInCallback", f, g, this.last);
                this.callback("itemLastOutCallback", f, g, this.prevLast)
            }
            this.callback("itemVisibleInCallback", f, g, this.first, this.last, this.prevFirst, this.prevLast);
            this.callback("itemVisibleOutCallback", f, g, this.prevFirst, this.prevLast, this.first, this.last)
        },
        callback: function (k, n, f, l, j, h, g) {
            if (this.options[k] == null || (typeof this.options[k] != "object" && n != "onAfterAnimation")) {
                return
            }
            var o = typeof this.options[k] == "object" ? this.options[k][n] : this.options[k];
            if (!c.isFunction(o)) {
                return
            }
            var p = this;
            if (l === undefined) {
                o(p, f, n)
            } else {
                if (j === undefined) {
                    this.get(l).each(function () {
                        o(p, this, l, f, n)
                    })
                } else {
                    var q = function (r) {
                            p.get(r).each(function () {
                                o(p, this, r, f, n)
                            })
                        };
                    for (var m = l; m <= j; m++) {
                        if (m !== null && !(m >= h && m <= g)) {
                            q(m)
                        }
                    }
                }
            }
        },
        create: function (f) {
            return this.format("<li></li>", f)
        },
        format: function (k, h) {
            k = c(k);
            var g = k.get(0).className.split(" ");
            for (var f = 0; f < g.length; f++) {
                if (g[f].indexOf("jcarousel-") != -1) {
                    k.removeClass(g[f])
                }
            }
            k.addClass(this.className("jcarousel-item")).addClass(this.className("jcarousel-item-" + h)).css({
                "float": (this.options.rtl ? "right" : "left"),
                "list-style": "none"
            }).attr("jcarouselindex", h);
            return k
        },
        className: function (f) {
            return f + " " + f + (!this.options.vertical ? "-horizontal" : "-vertical")
        },
        dimension: function (j, k) {
            var h = j.jquery !== undefined ? j[0] : j;
            var g = !this.options.vertical ? (h.offsetWidth || a.intval(this.options.itemFallbackDimension)) + a.margin(h, "marginLeft") + a.margin(h, "marginRight") : (h.offsetHeight || a.intval(this.options.itemFallbackDimension)) + a.margin(h, "marginTop") + a.margin(h, "marginBottom");
            if (k == null || g == k) {
                return g
            }
            var f = !this.options.vertical ? k - a.margin(h, "marginLeft") - a.margin(h, "marginRight") : k - a.margin(h, "marginTop") - a.margin(h, "marginBottom");
            c(h).css(this.wh, f + "px");
            return this.dimension(h)
        },
        clipping: function () {
            return !this.options.vertical ? this.clip[0].offsetWidth - a.intval(this.clip.css("borderLeftWidth")) - a.intval(this.clip.css("borderRightWidth")) : this.clip[0].offsetHeight - a.intval(this.clip.css("borderTopWidth")) - a.intval(this.clip.css("borderBottomWidth"))
        },
        index: function (f, g) {
            if (g == null) {
                g = this.options.size
            }
            return Math.round((((f - 1) / g) - Math.floor((f - 1) / g)) * g) + 1
        }
    });
    a.extend({
        defaults: function (f) {
            return c.extend(d, f || {})
        },
        margin: function (h, g) {
            if (!h) {
                return 0
            }
            var f = h.jquery !== undefined ? h[0] : h;
            return a.intval(c.css(f, g))
        },
        intval: function (f) {
            f = parseInt(f, 10);
            return isNaN(f) ? 0 : f
        }
    });
    c.fn.jcarousel = function (h) {
        if (typeof h == "string") {
            var f = c(this).data("jcarousel"),
                g = Array.prototype.slice.call(arguments, 1);
            return f[h].apply(f, g)
        } else {
            return this.each(function () {
                c(this).data("jcarousel", new a(this, h))
            })
        }
    }
})(jQuery);

