(function($) {
    $.fn.marquis = function(opts) {
        $.fn.marquis.defaults = {
            currentSelector: '.marquis-slideshow-current',
            currentClass: 'marquis-slideshow-current',
            revealSelector: '.marquis-slideshow-reveal',
            revealClass: 'marquis-slideshow-reveal',
            revealDefinition: '<div class="marquis-slideshow-reveal"><div class="caption" /></div>',
            thumbMaskDefinition: '<div class="mask" />',
            thumbMaskSelector: '.mask',
            thumbHoverOpacity: '0.54',
            thumbHoverColor: '#ffffff',
            thumbActiveOpacity: '0.54',
            thumbActiveColor: '#b38807',
            captionTitleSelector: 'h2',
            captionSelector: '.caption',
            fullImageSelector: 'img.full'
        };
        var opts = $.extend({}, $.fn.marquis.defaults, opts);

        return this.each(function(idx) {
            var slideshow = $(this);
            var current = $(this).children(opts.currentSelector);
            var currentBg = current.css("background-image");
            var items = $(this).children("ul").children("li");

            PngFix(current.children(opts.captionSelector));

            items.click(
                function() {
                    if (slideshow.children(opts.currentSelector + ":animated").length) return;
                    var img = $(this).children(opts.fullImageSelector);
                    var captionTitle = $(this).children(opts.captionTitleSelector).clone();
                    var caption = $(this).children(opts.captionSelector).children().clone();
                    var reveal = $(opts.revealDefinition);
                    reveal.children(opts.captions).append(captionTitle).append(caption);
                    reveal.css({
                        backgroundImage: "url('" + img.attr("src") + "')"
                    }).addClass($(this).attr("class"));
                    reveal.insertBefore(current);
                    caption = reveal.children(opts.captions);
                    if (caption.children().length) {
                        PngFix(reveal.children(opts.captionSelector));
                    } else {
                        caption.remove();
                    }
                    current.fadeOut("fast", function() {
                        $(this).remove();
                        reveal.addClass(opts.currentClass).removeClass(opts.revealClass);
                        current = reveal;
                    });
                }
            ).append(opts.thumbMaskDefinition).children(opts.thumbMaskSelector).css("opacity", 0).hover(
                function() {
                    if ($(this).parent().hasClass("active")) return;
                    $(this).stop().css("background-color", opts.thumbHoverColor).fadeTo("fast", opts.thumbHoverOpacity);
                },
                function() {
                    if ($(this).parent().hasClass("active")) return;
                    $(this).stop().fadeTo("fast", 0);
                }
            ).click(function() {
                var li = $(this).parent();
                if (li.hasClass("active")) return;
                li.addClass("active");
                $(this).parent().siblings(".active").removeClass("active").children(opts.thumbMaskSelector).fadeTo("fast", 0, function() { $(this).css("background-color", opts.thumbHoverColor); });
                $(this).css("background-color", opts.thumbActiveColor).stop().fadeTo("fast", opts.thumbActiveOpacity);
            }).end().filter(".active").children(opts.thumbMaskSelector).css("background-color", opts.thumbActiveColor).fadeTo("fast", opts.thumbActiveOpacity);
        });
    };
})(jQuery);