function RoyalSlider(a, b) {
    if ("ontouchstart" in window) {
        this.hasTouch = true
    } else {
        this.hasTouch = false
    }
    this.slider = $(a);
    var c = this;
    this.defaults = {
        directionNavEnabled: false,
        directionNavAutoHide: false,
        slideTransitionSpeed: 400,
        slideTransitionEasing: "easeInOutSine",
        captionAnimationEnabled: true,
        captionShowEffects: ["fade", "moveleft"],
        captionMoveOffset: 20,
        captionShowSpeed: 400,
        captionShowEasing: "easeOutCubic",
        captionShowDelay: 200,
        controlNavEnabled: false,
        controlNavThumbs: false,
        slideshowEnabled: false,
        slideshowDelay: 5000,
        slideshowPauseOnHover: true,
        welcomeScreenEnabled: true,
        welcomeScreenShowSpeed: 1000,
        minSlideOffset: 20,
        beforeSlideChange: function () {},
        afterSlideChange: function () {},
        loadingComplete: function () {}
    };
    this.settings = $.extend({}, this.defaults, b);
    if (this.hasTouch) {
        this.settings.directionNavAutoHide = false
    }
    this.isSlideshowRunning = false;
    this._dc = this.slider.find(".royalSlidesContainer");
    this._swr = this._dc.wrap('<div class="royalWrapper"/>').parent();
    this.slides = this._dc.find(".royalSlide");
    this.sliderWidth = 0;
    this.sliderHeight = 0;
    this.slideshowTimer = '';
    this.numSlides = this.slides.length;
    this.currentSlideId = 0;
    this.lastSlideId = -1;
    this.isAnimating = true;
    this._currentDragPosition = 0;
    this._lastpdr = 0;
    this._timecap = [];
    this._tx = 0;
    this._xmsa = 0;
    this._startMouseY = 0;
    this._endish = 0;
    this._dis = false;
    this._wsping = false;
    this._hois = false;
    if (this.settings.slideshowEnabled) {
        if (this.settings.slideshowDelay > 0) {
            if (this.settings.slideshowPauseOnHover) {
                this.slider.hover(function () {
                    c._hois = true;
                    c.stopSlideshow()
                }, function () {
                    c._hois = false;
                    c.resumeSlideshow()
                })
            }
        }
    }
    var d = "";
    if ($.browser.msie) {
        if (parseInt($.browser.version) == 7) {
            this.isIE7 = true;
            this._dc.css("cursor", "move")
        } else {
            this.isIE7 = false
        }
    }
    if ($.browser.opera) {
        _grabCursor = "move";
        _brabbingc = "move"
    } else {
        if ($.browser.mozilla) {
            _grabCursor = "-moz-grab";
            _brabbingc = "-moz-grabbing"
        } else {
            _grabCursor = "url(http://www.delive.se/wp-content/themes/delive/images/cursors/grab.png), move";
            _brabbingc = "url(http://www.delive.se/wp-content/themes/delive/images/cursors/grabbing.png), move"
        }
        _grabCursor = $.browser.mozilla ? "-moz-grab" : "url(http://www.delive.se/wp-content/themes/delive/images/cursors/grab.png), move";
        _brabbingc = $.browser.mozilla ? "-moz-grabbing" : "url(http://www.delive.se/wp-content/themes/delive/images/cursors/grabbing.png), move"
    }
    this._setGrabCursor();
    if (this.settings.controlNavEnabled) {
        if (!c.settings.controlNavThumbs) {
            var e = $('<div class="royalControlNavOverflow"><div class="royalControlNavContainer"><div class="royalControlNavCenterer"></div></div></div>')
        } else {
            var e = $('<div class="royalControlNavOverflow royalThumbs"><div class="royalControlNavContainer"><div class="royalControlNavCenterer"></div></div></div>')
        }
        var f = e.find('.royalControlNavCenterer');
        var g = 0;
        this.slides.each(function () {
            if (c.settings.controlNavThumbs) {
                f.append("<a href='#' class='royalThumb' style='background-image:url(" + $(this).attr("data-thumb") + ");'/>")
            } else {
                f.append('<a href="#"/>')
            }
            g++
        });
        this.navItems = f.children();
        this.navItems.eq(0).addClass("current");
        this.navItems.eq(this.navItems.length - 1).addClass("last");
        this._swr.after(e)
    }
    if (this.settings.captionAnimationEnabled) {
        this._swr.find(".royalCaption").css("display", "none")
    }
    if (this.settings.directionNavEnabled) {
        this._swr.after("<a href='#' class='arrow left'/>");
        this._swr.after("<a href='#' class='arrow right'/>");
        this.arrowLeft = this.slider.find("a.arrow.left");
        this.arrowRight = this.slider.find("a.arrow.right");
        if (this.arrowLeft.length < 1 || this.arrowRight.length < 1) {
            this.settings.directionNavEnabled = false
        } else if (this.settings.directionNavAutoHide) {
            this.arrowLeft.hide();
            this.arrowRight.hide();
            this.slider.hover(function () {
                c.arrowLeft.fadeIn("fast");
                c.arrowRight.fadeIn("fast")
            }, function () {
                c.arrowLeft.fadeOut("fast");
                c.arrowRight.fadeOut("fast")
            })
        }
    }
    this.resizeTimer = null;
    $(window).bind('resize', function () {
        if (this.resizeTimer) clearTimeout(resizeTimer);
        this.resizeTimer = setTimeout(function () {
            c._oe()
        }, 100)
    });
    this._oe();
    if (this.settings.welcomeScreenEnabled) {
        var h = this.slides.eq(0).css("background-image");
        if (h != "" && h != "none") {
            var i = new Image();
            $(i).load(function () {
                c.settings.loadingComplete.call(c);
                c.slider.find(".royalLoadingScreen").fadeOut(c.settings.welcomeScreenShowSpeed, function () {
                    $(this).remove()
                });
                setTimeout(function () {
                    c._lucky()
                }, c.settings.welcomeScreenShowSpeed + 100)
            });
            h = h.substring(4, h.length - 1);
            if (h.charAt(0) == '"' || h.charAt(0) == "'") {
                h = h.substring(1, h.length - 1)
            }
            i.src = h
        } else {
            var j = this.slider.find(".royalLoadingScreen");
            if (j) {
                c.settings.loadingComplete.call(c);
                j.fadeOut(c.settings.welcomeScreenShowSpeed, function () {
                    $(this).remove()
                });
                setTimeout(function () {
                    c._lucky()
                }, c.settings.welcomeScreenShowSpeed + 100)
            } else {
                setTimeout(function () {
                    c._lucky();
                    c.slider.find(".royalLoadingScreen").remove();
                    c.settings.loadingComplete.call(c)
                }, 100)
            }
        }
    }
}
RoyalSlider.prototype = {
    goTo: function (a) {
        if (!this.isAnimating) {
            this.isAnimating = true;
            var b = this;
            this._dc.unbind("mousedown").unbind("touchstart").unbind("MozTouchDown");
            if (this.settings.directionNavEnabled) {
                this.arrowRight.unbind("click");
                this.arrowLeft.unbind("click")
            }
            if (this.settings.controlNavEnabled) {
                this.navItems.unbind("click");
                this.navItems.eq(this.currentSlideId).removeClass('current');
                this.navItems.eq(a).addClass("current")
            }
            this.lastSlideId = this.currentSlideId;
            this.currentSlideId = a;
            this.settings.beforeSlideChange.call(this);
            this.stopSlideshow();
            this._dc.animate({
                left: -this.currentSlideId * this.sliderWidth
            }, this.settings.slideTransitionSpeed, this.settings.slideTransitionEasing, function () {
                b._lucky()
            })
        }
    },
    prev: function () {
        if (this.currentSlideId <= 0) {
            this.goTo(this.numSlides - 1)
        } else {
            this._moveSlideLeft()
        }
    },
    next: function () {
        if (this.currentSlideId >= this.numSlides - 1) {
            this.goTo(0)
        } else {
            this._rsmov()
        }
    },
    resumeSlideshow: function () {
        if (this.settings.slideshowEnabled) {
            if (this.slideshowTimer == '' && !this._hois) {
                var a = this;
                this.slideshowTimer = setInterval(function () {
                    a.next()
                }, this.settings.slideshowDelay);
                this.isSlideshowRunning = true
            }
        }
    },
    stopSlideshow: function () {
        if (this.settings.slideshowEnabled) {
            clearInterval(this.slideshowTimer);
            this.slideshowTimer = '';
            this.isSlideshowRunning = false
        }
    },
    _setGrabCursor: function () {
        if (!this.isIE7) {
            this._dc.css("cursor", _grabCursor)
        }
    },
    _setGrabbingCursor: function () {
        if (!this.isIE7) {
            this._dc.css("cursor", _brabbingc)
        }
    },
    _rsmov: function () {
        if (this.currentSlideId < this.numSlides - 1) {
            this.goTo(this.currentSlideId + 1)
        } else {
            this.goTo(this.currentSlideId)
        }
    },
    _moveSlideLeft: function () {
        if (this.currentSlideId > 0) {
            this.goTo(this.currentSlideId - 1)
        } else {
            this.goTo(this.currentSlideId)
        }
    },
    _oe: function () {
        var a = parseInt(this._swr.width());
        var b = parseInt(this._swr.height());
        if (a != this.sliderWidth || b != this.sliderHeight) {
            this.sliderWidth = a;
            this.slides.each(function () {
                $(this).css({
                    height: b + "px",
                    width: a + "px"
                })
            });
            this._dc.css({
                "left": -this.currentSlideId * this.sliderWidth,
                width: this.sliderWidth * this.numSlides
            })
        }
    },
    _onNavItemClick: function (e) {
        this.goTo($(e.currentTarget).index())
    },
    _ondst: function (e) {
        if (!this._dis) {
            var a;
            if (this.hasTouch) {
                var b = e.originalEvent.touches;
                if (b && b.length > 0) {
                    a = b[0]
                } else {
                    return false
                }
            } else {
                a = e;
                e.preventDefault()
            }
            if (this.slideshowTimer != '') {
                this.wasSlideshowPlaying = true;
                this.stopSlideshow()
            } else {
                this.wasSlideshowPlaying = false
            }
            this._setGrabbingCursor();
            this._dis = true;
            var c = this;
            $(document).bind("mousemove touchmove MozTouchMove", function (e) {
                c._ondgme(e)
            });
            $(document).bind("mouseup touchend MozTouchRelease", function (e) {
                c._updd(e)
            });
            this._tx = parseInt(this._dc.css("left"));
            this._xmsa = a.clientX;
            this._startMouseY = a.clientY;
            this._endish = parseInt(this._dc.css("left"))
        }
        return false
    },
    _ondgme: function (e) {
        var a;
        if (this.hasTouch) {
            var b = e.originalEvent.touches;
            if (b.length > 1) {
                return false
            }
            a = b[0];
            if (Math.abs(a.clientY - this._startMouseY) + 10 > Math.abs(a.clientX - this._xmsa)) {
                return false
            }
            e.preventDefault()
        } else {
            a = e;
            e.preventDefault()
        }
        this._lastpdr = this._currentDragPosition;
        var c = a.clientX - this._xmsa;
        if (this._lastpdr != c) {
            this._currentDragPosition = c
        }
        if (c != 0) {
            if (this.currentSlideId == 0) {
                if (c > 0) {
                    c = Math.sqrt(c) * 5
                }
            } else if (this.currentSlideId == (this.numSlides - 1)) {
                if (c < 0) {
                    c = -Math.sqrt(-c) * 5
                }
            }
            this._dc.css("left", this._tx + c)
        }
        return false
    },
    _updd: function (e) {
        if (this.wasSlideshowPlaying) {
            this.resumeSlideshow()
        }
        if (this._dis) {
            this._dis = false;
            this._setGrabCursor();
            this.endPos = parseInt(this._dc.css("left"));
            this.isdrag = false;
            $(document).unbind("mousemove").unbind("touchmove").unbind("MozTouchMove").unbind("mouseup").unbind("touchend").unbind("MozTouchRelease");
            if (this.endPos == this._endish) {
                return
            }
            if (this._endish - this.settings.minSlideOffset > this.endPos) {
                if (this._lastpdr < this._currentDragPosition) {
                    this.goTo(this.currentSlideId);
                    return false
                }
                this._rsmov()
            } else if (this._endish + this.settings.minSlideOffset < this.endPos) {
                if (this._lastpdr > this._currentDragPosition) {
                    this.goTo(this.currentSlideId);
                    return false
                }
                this._moveSlideLeft()
            } else {
                this.goTo(this.currentSlideId)
            }
        }
        return false
    },
    _lucky: function () {
        var a = this;
        this.resumeSlideshow();
        if (this.settings.controlNavEnabled) {
            this.navItems.bind("click", function (e) {
                e.preventDefault();
                a._onNavItemClick(e)
            })
        }
        this._dc.bind("mousedown touchstart MozTouchDown", function (e) {
            a._ondst(e)
        });
        if (this.settings.directionNavEnabled) {
            this.arrowRight.bind('click', function (e) {
                e.preventDefault();
                a.next()
            });
            this.arrowLeft.bind('click', function (e) {
                e.preventDefault();
                a.prev()
            })
        }
        if (this.settings.captionAnimationEnabled && this.lastSlideId != this.currentSlideId) {
            if (this.lastSlideId != -1) {
                this.slides.eq(this.lastSlideId).find(".royalCaption").css("display", "none")
            }
            setTimeout(function () {
                a._showCaption(this.currentSlideId)
            }, 10)
        }
        this.isAnimating = false;
        this.settings.afterSlideChange.call(this)
    },
    _showCaption: function (e) {
        var f = this.slides.eq(this.currentSlideId).find(".royalCaption");
        if (f.length) {
            f.css("display", "block");
            var g = this;
            var h;
            var j = f.children();
            var k;
            var l;
            var m;
            var n;
            var o;
            var p;
            var r;
            var s;
            var t;
            var u;
            var v;
            var w;
            if (this._timecap.length > 0) {
                for (var a = this._timecap.length; a > -1; a--) {
                    clearTimeout(this._timecap.splice(a, 1))
                }
            }
            for (var i = 0; i < j.length; i++) {
                h = $(j[i]);
                n = {};
                k = false;
                l = false;
                o = "";
                if (h.attr("data-show-effect") == undefined) {
                    p = this.settings.captionShowEffects
                } else {
                    p = h.attr("data-show-effect").split(" ")
                }
                for (var q = 0; q < p.length; q++) {
                    if (k && l) {
                        break
                    }
                    m = p[q].toLowerCase();
                    if (!k && m == "fade") {
                        k = true;
                        n['opacity'] = 1
                    } else if (l) {
                        break
                    } else if (m == "movetop") {
                        o = "margin-top"
                    } else if (m == "moveleft") {
                        o = "margin-left"
                    } else if (m == "movebottom") {
                        o = "margin-bottom"
                    } else if (m == "moveright") {
                        o = "margin-right"
                    }
                    if (o != "") {
                        n['moveProp'] = o;
                        n['moveStartPos'] = parseInt(h.css(o));
                        l = true
                    }
                }
                s = parseInt(h.attr("data-move-offset"));
                if (isNaN(s)) {
                    s = this.settings.captionMoveOffset
                }
                t = parseInt(h.attr("data-delay"));
                if (isNaN(t)) {
                    t = g.settings.captionShowDelay * i + 10
                }
                u = parseInt(h.attr("data-speed"));
                if (isNaN(u)) {
                    u = g.settings.captionShowSpeed
                }
                v = h.attr("data-easing");
                if (v == undefined) {
                    v = g.settings.captionShowEasing
                }
                r = {};
                if (l) {
                    w = n.moveProp;
                    if (w == "margin-right") {
                        w = "margin-left";
                        r[w] = n.moveStartPos + s + "px"
                    } else if (w == "margin-bottom") {
                        w = "margin-top";
                        r[w] = n.moveStartPos + s + "px"
                    } else {
                        r[w] = n.moveStartPos - s + "px"
                    }
                }
                if (k) {
                    r.opacity = 0
                }
                h.hide().css(r);
                if (l) {
                    r[w] = n.moveStartPos + "px"
                }
                if (k) {
                    r.opacity = 1
                }
                this._timecap.push(setTimeout((function (a, b, c, d) {
                    return function () {
                        a.show().animate(b, c, d)
                    }
                })(h, r, u, v), t))
            }
        }
    }
};
