/*  
 *  Slider Kit v1.6 (packed) - Sliding contents with jQuery
 *  http://www.kyrielles.net/sliderkit
 *  
 *  Copyright (c) 2010-2011 Alan Frog
 *  Licensed under the GNU General Public License
 *  See <license.txt> or <http://www.gnu.org/licenses/>
 *  
 *  Requires : jQuery v1.3+ <http://jquery.com/>
 * 
 */
(function (jQuery) {
    var o = function () {
            var n = this;
            this._init = function (c, d) {
                this.options = $.extend({}, this._settings, d);
                this.domObj = $(c);
                this.panels = $("." + this.options.cssprefix + "-panel", this.domObj);
                this.nav = $("." + this.options.cssprefix + "-nav", this.domObj);
                this.navClip = $("." + this.options.cssprefix + "-nav-clip", this.nav);
                this.allItems = this.panels.size();
                this.arePanels = this.allItems > 0 ? 1 : 0;
                this.isNavClip = this.navClip.size() > 0 ? 1 : 0;
                if (!this.arePanels && !this.isNavClip) {
                    this._errorReport("01", this.options.debug, 1)
                } else if (this.domObj.css("height") == "auto" && !this.options.freeheight) {
                    this._errorReport("02", this.options.debug, 1)
                }
                this.domObj.css("display", "block");
                this.currId = 0;
                this.prevId = 0;
                this.newId = 0;
                this.currPanel = null;
                this.prevPanel = 0;
                this.firstTime = 1;
                this.scrollActive = 0;
                this.isPlaying = null;
                this.changeOngoing = false;
                this.cssNames = {
                    cssSelected: this.options.cssprefix + "-selected",
                    cssActive: this.options.cssprefix + "-panel-active",
                    cssOld: this.options.cssprefix + "-panel-old",
                    cssBtnDisable: this.options.cssprefix + "-btn-disable",
                    cssPanelsWrapper: this.options.cssprefix + "-panels-wrapper",
                    cssBtnPause: this.options.cssprefix + "-pause-btn",
                    cssPosValue: "+"
                };
                if (this.isNavClip) {
                    this._buildNav()
                }
                this._buildControls();
                if (this.arePanels) {
                    this.panelsBag = $("." + this.options.cssprefix + "-panels", this.domObj);
                    if (this.options.panelfx == "sliding") {
                        this._wrapPanels()
                    }
                }
                this.lineScrollDo = !this.arePanels ? 1 : 0;
                if (this.options.mousewheel) {
                    this.domObj.mousewheel(function (a, b) {
                        b > 0 ? n.stepBackward() : n.stepForward();
                        return false
                    })
                }
                if (this.options.keyboard) {
                    this.domObj.keyup(function (a) {
                        if (a.keyCode == 37) {
                            n.stepBackward()
                        } else if (a.keyCode == 39) {
                            n.stepForward()
                        }
                    })
                }
                if (this.options.panelclick && this.arePanels) {
                    this.panelsBag.click(function () {
                        n.stepForward();
                        return false
                    })
                }
                this.changeWithId(this.options.start >= this.allItems ? this.allItems - 1 : this.options.start < 0 ? 0 : this.options.start, null);
                if (this.options.auto) {
                    this.autoScrollStart();
                    if (!this.isPlayBtn) {
                        this.domObj.hover(function () {
                            if (n.isPlaying != null) {
                                n.autoScrollStop()
                            }
                        }, function () {
                            n.autoScrollStart()
                        })
                    }
                }
                return this
            };
            this._settings = {
                cssprefix: "sliderkit",
                start: 0,
                auto: true,
                autospeed: 3000,
                mousewheel: false,
                keyboard: false,
                circular: false,
                shownavitems: 5,
                navitemshover: false,
                navclipcenter: false,
                navcontinuous: false,
                navscrollatend: false,
                navpanelautoswitch: true,
                navfx: "sliding",
                scroll: null,
                scrollspeed: 600,
                scrolleasing: null,
                panelfx: "fading",
                panelfxspeed: 700,
                panelfxeasing: null,
                panelfxfirst: "none",
                panelfxbefore: function () {},
                panelfxafter: function () {},
                panelbtnshover: false,
                panelclick: false,
                verticalnav: false,
                verticalslide: false,
                tabs: false,
                freeheight: false,
                fastchange: true,
                debug: false
            };
            this._errorReport = function (a, b, c) {
                if (b) {
                    alert("Slider Kit error! Code #" + a + " (see doc)")
                }
                if (c) {
                    return false
                }
            };
            this._buildNav = function () {
                this.navUL = $("ul", this.navClip);
                this.navLI = $("li", this.navUL);
                var c = this.navLI.size();
                if (this.arePanels && (c != this.allItems) && this.nav.size() == 1) {
                    this._errorReport("03", this.options.debug, 1)
                }
                if (this.options.tabs) {
                    this.options.shownavitems = this.allItems
                } else {
                    function getLImargin(a) {
                        attrVal = n.navLI.css(a);
                        if (attrVal != "auto" && a != "" && a != "0px") {
                            return parseInt(attrVal)
                        } else return 0
                    }
                    var d = this.options.verticalnav ? this.nav.height() : this.nav.width();
                    var e = this.navLI.outerWidth(true);
                    var f = this.navLI.outerHeight(true);
                    var g = getLImargin("margin-left") + getLImargin("margin-right");
                    var h = getLImargin("margin-top") + getLImargin("margin-bottom");
                    this.navLIsize = this.options.verticalnav ? f : e;
                    this.navULSize = this.navLIsize * c;
                    this.navClipSize = (this.options.shownavitems * this.navLIsize) - (this.options.verticalnav ? h : g);
                    this.cssPosAttr = this.options.verticalnav ? "top" : "left";
                    var i = this.options.verticalnav ? "height" : "width";
                    var j = this.options.verticalnav ? "width" : "height";
                    this.navLI.css({
                        width: this.navLI.width(),
                        height: this.navLI.height()
                    });
                    this.navUL.css(i, this.navULSize + "px");
                    this.navClip.css({
                        width: this.options.verticalnav ? e : this.navClipSize,
                        height: this.options.verticalnav ? this.navClipSize : f
                    });
                    if (this.options.navclipcenter) {
                        this.navClip.css(this.cssPosAttr, (d - this.navClipSize) / 2).css("margin", "0")
                    }
                    this.allItems = c;
                    if (this.allItems > this.options.shownavitems) {
                        this.scrollActive = true;
                        if (this.options.scroll == null || this.options.scroll < 0 || this.options.scroll > this.allItems) {
                            this.options.scroll = this.options.shownavitems
                        }
                        this.navBtns = $("." + this.options.cssprefix + "-nav-btn", this.nav);
                        if (this.navBtns.size() > 0) {
                            this._buildNavButtons()
                        }
                    }
                }
                if (this.options.navitemshover && this.arePanels) {
                    this.navLI.mouseover(function () {
                        n.changeWithId(getIndex(this, "li"), $(this))
                    })
                } else if (this.arePanels || this.options.navscrollatend) {
                    this.navLI.click(function () {
                        n.changeWithId(getIndex(this, "li"), $(this));
                        return false
                    })
                }
                function getIndex(a, b) {
                    return $(b, $(a).parent()).index(a)
                }
            };
            this._buildNavButtons = function () {
                this.scrollBtns = true;
                this.navBtnPrev = $("." + this.options.cssprefix + "-nav-prev", this.nav);
                this.navBtnNext = $("." + this.options.cssprefix + "-nav-next", this.nav);
                this.navBtnPrev.click(function () {
                    n.navPrev();
                    return false
                });
                this.navBtnNext.click(function () {
                    n.navNext();
                    return false
                });
                if (this.options.navcontinuous) {
                    this.navBtnPrev.mouseover(function () {
                        n.navPrev(true)
                    });
                    this.navBtnNext.mouseover(function () {
                        n.navNext(true)
                    });
                    this.navBtns.mouseout(function () {
                        n.navStopContinuous()
                    })
                }
                if (!this.options.circular) {
                    this.navBtnPrev.toggleClass(this.cssNames.cssBtnDisable)
                }
            };
            this._getNavPos = function () {
                this.navPos = this.options.verticalnav ? this.navUL.position().top : this.navUL.position().left;
                this.LIbefore = Math.ceil(Math.abs(this.navPos) / this.navLIsize);
                this.LIafter = Math.floor((this.navULSize - Math.abs(this.navPos) - this.navClipSize) / this.navLIsize);
                if (this.LIafter < 0) {
                    this.LIafter = 0
                }
            };
            this._buildControls = function () {
                this.playBtn = $("." + this.options.cssprefix + "-play-btn", this.domObj);
                this.gBtns = $("." + this.options.cssprefix + "-go-btn", this.domObj);
                this.isPlayBtn = this.playBtn.size() > 0 ? 1 : 0;
                this.goBtns = this.gBtns.size() > 0 ? 1 : 0;
                if (this.isPlayBtn) {
                    if (this.options.auto) {
                        this.playBtn.addClass(this.cssNames.cssBtnPause)
                    }
                    this.playBtn.click(function () {
                        if (n.playBtn.hasClass(n.cssNames.cssBtnPause)) {
                            n.playBtnPause()
                        } else {
                            n.playBtnStart()
                        }
                        return false
                    })
                }
                if (this.goBtns) {
                    this.goBtnPrev = $("." + this.options.cssprefix + "-go-prev", this.domObj);
                    this.goBtnNext = $("." + this.options.cssprefix + "-go-next", this.domObj);
                    if (this.options.panelbtnshover) {
                        this.gBtns.hide();
                        $("." + this.options.cssprefix + "-panels", this.domObj).hover(function () {
                            n.gBtns.fadeIn()
                        }, function () {
                            n.gBtns.fadeOut()
                        })
                    }
                    this.goBtnPrev.click(function () {
                        n.stepBackward($(this));
                        return false
                    });
                    this.goBtnNext.click(function () {
                        n.stepForward($(this));
                        return false
                    })
                }
            };
            this._wrapPanels = function () {
                this.panels.wrapAll('<div class="' + this.cssNames.cssPanelsWrapper + '"></div>');
                this.panelsWrapper = $("." + this.cssNames.cssPanelsWrapper, this.panelsBag);
                this.panelsWrapper.css({
                    "position": "relative"
                })
            };
            this._change = function (a, b, c, d, e) {
                if (e && this.isPlaying != null && this.isPlayBtn) {
                    this.playBtnPause()
                }
                if (a) {
                    if (a.hasClass(this.cssNames.cssBtnDisable)) {
                        return false
                    }
                }
                var f = 0;
                var g = $(":animated", this.options.fastchange ? this.nav : this.domObj).size() > 0 ? 1 : 0;
                if (!g) {
                    this.prevId = this.currId;
                    if (c == null && !d) {
                        this.currId = b == "-=" ? this.currId + 1 : this.currId - 1
                    } else if (c != null) {
                        c = parseInt(c);
                        this.currId = c < 0 ? 0 : c > this.allItems - 1 ? this.allItems - 1 : c;
                        var h = a ? a.parent().parent().hasClass(this.options.cssprefix + "-nav-clip") ? false : true : true
                    }
                    if (this.goBtns) {
                        this.gBtns.removeClass(this.cssNames.cssBtnDisable)
                    }
                    if (!this.options.circular) {
                        if (this.currId == -1) {
                            this.currId = 0;
                            f = 1
                        }
                        if (this.currId == 0 && this.goBtns) {
                            this.goBtnPrev.addClass(this.cssNames.cssBtnDisable)
                        }
                        if (this.currId == this.allItems) {
                            this.currId = this.allItems - 1;
                            f = 1
                        }
                        if (this.currId == this.allItems - 1) {
                            if (this.options.auto) {
                                this.autoScrollStop()
                            }
                            if (this.goBtns) {
                                this.goBtnNext.addClass(this.cssNames.cssBtnDisable)
                            }
                        }
                    } else if (!this.scrollActive) {
                        if (this.currId == this.allItems) {
                            this.currId = 0
                        }
                        if (this.currId == -1) {
                            this.currId = this.allItems - 1
                        }
                    }
                    if (this.scrollActive && !f) {
                        this._setNavScroll(d, b, h)
                    }
                    if (this.isNavClip) {
                        this.selectThumbnail(this.currId)
                    }
                    if (!(d && !this.options.navpanelautoswitch)) {
                        if (this.arePanels) {
                            this._animPanel(this.currId, b)
                        }
                    }
                    if (this.firstTime) {
                        this.firstTime = 0
                    }
                }
            };
            this._setNavScroll = function (a, b, c) {
                this._getNavPos();
                var d = a ? true : false;
                var e = 0;
                if (!a) {
                    var f = Math.abs(this.currId + 1 - this.LIbefore);
                    var g = this.options.shownavitems - f + 1;
                    var h = this.currId == 0 || this.currId == this.allItems - 1 ? 1 : 0;
                    if ((this.options.navscrollatend && (g == 1 || f == 1)) && !this.firstTime && !h) {
                        e = this.options.scroll - 1;
                        d = true
                    }
                    if (g == 0 || f == 0) {
                        d = true
                    }
                    if (c) {
                        var i = Math.abs(this.prevId - this.currId);
                        if (i > 0) {
                            e = i;
                            d = true
                        }
                    }
                    if (b == "") {
                        if (this.prevId == this.currId && !h) {
                            b = this.scrollWay == "-=" ? "+=" : "-="
                        } else {
                            b = this.prevId < this.currId ? "-=" : "+="
                        }
                    }
                    this.scrollWay = b
                }
                if (d) {
                    var j = e > 0 ? e : this.options.scroll;
                    var k = b == "-=" ? this.LIafter : this.LIbefore;
                    var l = k < j ? k : j;
                    var m = l * this.navLIsize;
                    this.newId = b == "-=" ? this.LIbefore + l : this.LIbefore - l + this.options.shownavitems - 1;
                    if ((b == "-=" && this.newId > this.currId) || (b == "+=" && this.newId < this.currId)) {
                        this.currId = this.newId
                    }
                    if (this.options.circular) {
                        if (this.LIbefore <= 0 && b == "+=") {
                            b = "-=";
                            this.currId = this.allItems - 1;
                            m = (this.LIafter / this.options.scroll) * (this.navLIsize * this.options.scroll)
                        } else if (this.LIafter == 0 && b == "-=") {
                            b = "+=";
                            this.currId = 0;
                            m = Math.abs(this.navPos)
                        }
                    }
                    this._animNav(b, m)
                }
            };
            this._animPanel = function (a, b) {
                this.currPanel = this.panels.eq(a);
                if (!this.currPanel.hasClass(this.cssNames.cssActive)) {
                    if (this.firstTime) {
                        this.panelTransition = this.options.panelfxfirst;
                        var c = 1
                    } else {
                        var d = this.options.freeheight && this.options.panelfx == "fading" ? "tabsfading" : "none";
                        this.panelTransition = this.options.freeheight ? d : this.options.panelfx
                    }
                    if (n.options.panelfxbefore) n.options.panelfxbefore();
                    this._panelTransitions[this.panelTransition](b, c)
                }
            };
            this._animNav = function (a, b) {
                var c = function () {
                        if (!n.options.circular && n.scrollBtns) {
                            n.navBtns.removeClass(n.cssNames.cssBtnDisable);
                            n._getNavPos();
                            if (n.LIbefore <= 0) {
                                n.navBtnPrev.addClass(n.cssNames.cssBtnDisable)
                            } else if (n.LIafter <= 0) {
                                n.navBtnNext.addClass(n.cssNames.cssBtnDisable)
                            }
                        }
                        if (n.scrollcontinue) {
                            setTimeout(function () {
                                n.scrollcontinue == "-=" ? n.navPrev() : n.navNext()
                            }, 0)
                        }
                    };
                this.navTransition = this.options.navfx;
                this._navTransitions[this.navTransition](a, b, c)
            };
            this._panelTransitions = {
                none: function (a, b) {
                    n.panels.removeClass(n.cssNames.cssActive).hide();
                    n.currPanel.addClass(n.cssNames.cssActive).show();
                    if (n.options.panelfxafter) {
                        n.options.panelfxafter()
                    }
                },
                sliding: function (a, b) {
                    if (a == "") {
                        a = n.prevPanel < n.currId ? "-=" : "+="
                    }
                    n.prevPanel = n.currId;
                    var c = a == "-=" ? "+" : "-";
                    var d = n.options.verticalslide ? "top" : "left";
                    var e = n.options.verticalnav ? n.domObj.height() : n.domObj.width();
                    var f = d == "top" ? {
                        top: a + e
                    } : {
                        left: a + e
                    };
                    n.oldPanel = $("." + n.cssNames.cssOld, n.domObj);
                    n.activePanel = $("." + n.cssNames.cssActive, n.domObj);
                    n.panels.css(d, "0");
                    n.oldPanel.removeClass(n.cssNames.cssOld).hide();
                    n.activePanel.removeClass(n.cssNames.cssActive).addClass(n.cssNames.cssOld);
                    n.currPanel.addClass(n.cssNames.cssActive).css(d, c + e + "px").show();
                    n.panelsWrapper.stop(true, true).css(d, "0").animate(f, n.options.panelfxspeed, n.options.panelfxeasing, function () {
                        if (n.options.panelfxafter) n.options.panelfxafter()
                    })
                },
                fading: function (a, b) {
                    if (b) {
                        n.panels.hide()
                    } else n.currPanel.css("display", "none");
                    $("." + n.cssNames.cssOld, n.domObj).removeClass(n.cssNames.cssOld);
                    $("." + n.cssNames.cssActive, n.domObj).stop(true, true).removeClass(n.cssNames.cssActive).addClass(n.cssNames.cssOld);
                    n.currPanel.addClass(n.cssNames.cssActive).animate({
                        "opacity": "show"
                    }, n.options.panelfxspeed, n.options.panelfxeasing, function () {
                        if (n.options.panelfxafter) {
                            n.options.panelfxafter()
                        }
                    })
                },
                tabsfading: function (a, b) {
                    n.panels.removeClass(n.cssNames.cssActive).hide();
                    n.currPanel.fadeIn(n.options.panelfxspeed, function () {
                        if (n.options.panelfxafter) {
                            n.options.panelfxafter()
                        }
                    })
                }
            };
            this._navTransitions = {
                none: function (a, b, c) {
                    var d = a == "-=" ? n.navPos - b : n.navPos + b;
                    n.navUL.css(n.cssPosAttr, d + "px");
                    c()
                },
                sliding: function (a, b, c) {
                    n.navUL.animate(n.cssPosAttr == "left" ? {
                        left: a + b
                    } : {
                        top: a + b
                    }, n.options.scrollspeed, n.options.scrolleasing, function () {
                        c()
                    })
                }
            };
            this.playBtnPause = function () {
                this.playBtn.removeClass(this.cssNames.cssBtnPause);
                this.autoScrollStop()
            };
            this.playBtnStart = function () {
                this.playBtn.addClass(n.cssNames.cssBtnPause);
                this.autoScrollStart()
            };
            this.autoScrollStart = function () {
                var a = this;
                this.isPlaying = setInterval(function () {
                    a._change(null, "-=", null, a.lineScrollDo, null)
                }, a.options.autospeed)
            };
            this.autoScrollStop = function () {
                clearTimeout(this.isPlaying);
                this.isPlaying = null
            };
            this.changeWithId = function (a, b) {
                this._change(b, "", a, 0, 1)
            };
            this.stepBackward = function (a) {
                this._change(a, "+=", null, n.lineScrollDo, 1)
            };
            this.stepForward = function (a) {
                this._change(a, "-=", null, n.lineScrollDo, 1)
            };
            this.navPrev = function (c) {
                if (c) {
                    n.scrollcontinue = "-="
                }
                this._change(this.navBtnPrev, "+=", null, 1, 1)
            };
            this.navNext = function (c) {
                if (c) {
                    n.scrollcontinue = "+="
                }
                this._change(this.navBtnNext, "-=", null, 1, 1)
            };
            this.navStopContinuous = function () {
                n.scrollcontinue = ""
            };
            this.selectThumbnail = function (a) {
                $("." + this.cssNames.cssSelected, this.navUL).removeClass(this.cssNames.cssSelected);
                this.navLI.eq(a).addClass(this.cssNames.cssSelected)
            };
            this.addTransition = function (a, b) {
                _transitions[a] = b
            }
        };
    $.fn.sliderkit = function (b) {
        if (this.length) {
            return this.each(function () {
                var a = new o();
                a._init(this, b);
                $.data(this, "sliderkit", a)
            })
        }
    }
})(jQuery);
