(function($) {
    var q;
    q = $.fn.galleria = function(k) {
        if (!q.hasCSS()) {
            return false
        }
        $.historyInit(q.onPageLoad);
        var l = {
            insert: '.galleria_container',
            history: true,
            clickNext: true,
            onImage: function(a, b, c) {},
            onThumb: function(a) {}
        };
        var m = $.extend(l, k);
        for (var i in m) {
            if (i) {
                $.galleria[i] = m[i]
            }
        }
        var n = ($(m.insert).is(m.insert)) ? $(m.insert) : jQuery(document.createElement('div')).insertBefore(this);
        var o = $(document.createElement('div')).addClass('galleria_wrapper');
        var p = $(document.createElement('span')).addClass('caption');
        n.addClass('galleria_container').append(o).append(p);
        return this.each(function() {
            $(this).addClass('galleria');
            $(this).children('li').each(function(i) {
                var b = $(this);
                var c = $.meta ? $.extend({},
                m, b.data()) : m;
                c.clickNext = $(this).is(':only-child') ? false: c.clickNext;
                var d = $(this).find('a').is('a') ? $(this).find('a') : false;
                var e = $(this).children('img').css('display', 'none');
                var f = d ? d.attr('href') : e.attr('src');
                var g = d ? d.attr('title') : e.attr('title');
                var j = new Image();
                if (c.history && (window.location.hash && window.location.hash.replace(/\#/, '') == f)) {
                    b.siblings('.active').removeClass('active');
                    b.addClass('active')
                }
                $(j).load(function() {
                    $(this).attr('alt', e.attr('alt'));
                    var a = d ? d.find('img').addClass('thumb noscale').css('display', 'none') : e.clone(true).addClass('thumb').css('display', 'none');
                    if (d) {
                        d.replaceWith(a)
                    }
                    if (!a.hasClass('noscale')) {
                        var w = Math.ceil(e.width() / e.height() * b.height());
                        var h = Math.ceil(e.height() / e.width() * b.width());
                        if (w < h) {
                            a.css({
                                height: 'auto',
                                width: b.width(),
                                marginTop: -(h - b.height()) / 2
                            })
                        } else {
                            a.css({
                                width: 'auto',
                                height: b.height(),
                                marginLeft: -(w - b.width()) / 2
                            })
                        }
                    } else {
                        window.setTimeout(function() {
                            a.css({
                                marginLeft: -(a.width() - b.width()) / 2,
                                marginTop: -(a.height() - b.height()) / 2
                            })
                        },
                        1)
                    }
                    a.attr('rel', f);
                    a.attr('title', g);
                    a.click(function() {
                        $.galleria.activate(f)
                    });
                    a.hover(function() {
                        $(this).addClass('hover')
                    },
                    function() {
                        $(this).removeClass('hover')
                    });
                    b.hover(function() {
                        b.addClass('hover')
                    },
                    function() {
                        b.removeClass('hover')
                    });
                    b.prepend(a);
                    a.css('display', 'block');
                    c.onThumb(jQuery(a));
                    if (b.hasClass('active')) {
                        $.galleria.activate(f)
                    }
                    e.remove()
                }).error(function() {
                    b.html('<span class="error" style="color:red">Error loading image: ' + f + '</span>')
                }).attr('src', f)
            })
        })
    };
    q.nextSelector = function(a) {
        return $(a).is(':last-child') ? $(a).siblings(':first-child') : $(a).next()
    };
    q.previousSelector = function(a) {
        return $(a).is(':first-child') ? $(a).siblings(':last-child') : $(a).prev()
    };
    q.hasCSS = function() {
        $('body').append($(document.createElement('div')).attr('id', 'css_test').css({
            width: '1px',
            height: '1px',
            display: 'none'
        }));
        var a = ($('#css_test').width() != 1) ? false: true;
        $('#css_test').remove();
        return a
    };
    q.onPageLoad = function(a) {
        var b = $('.galleria_wrapper');
        var c = $('.galleria img[rel="' + a + '"]');
        if (a) {
            if ($.galleria.history) {
                window.location = window.location.href.replace(/\#.*/, '') + '#' + a
            }
            c.parents('li').siblings('.active').removeClass('active');
            c.parents('li').addClass('active');
            var d = $(new Image()).attr('src', a).addClass('replaced');
            b.empty().append(d);
            b.siblings('.caption').text(c.attr('title'));
            $.galleria.onImage(d, b.siblings('.caption'), c);
            if ($.galleria.clickNext) {
                d.css('cursor', 'pointer').click(function() {
                    $.galleria.next()
                })
            }
        } else {
            b.siblings().andSelf().empty();
            $('.galleria li.active').removeClass('active')
        }
        $.galleria.current = a
    };
    $.extend({
        galleria: {
            current: '',
            onImage: function() {},
            activate: function(a) {
                if ($.galleria.history) {
                    $.historyLoad(a)
                } else {
                    q.onPageLoad(a)
                }
            },
            next: function() {
                var a = $(q.nextSelector($('.galleria img[rel="' + $.galleria.current + '"]').parents('li'))).find('img').attr('rel');
                $.galleria.activate(a)
            },
            prev: function() {
                var a = $(q.previousSelector($('.galleria img[rel="' + $.galleria.current + '"]').parents('li'))).find('img').attr('rel');
                $.galleria.activate(a)
            }
        }
    })
})(jQuery);
jQuery.extend({
    historyCurrentHash: undefined,
    historyCallback: undefined,
    historyInit: function(a) {
        jQuery.historyCallback = a;
        var b = location.hash;
        jQuery.historyCurrentHash = b;
        if (jQuery.browser.msie) {
            if (jQuery.historyCurrentHash === '') {
                jQuery.historyCurrentHash = '#'
            }
            $("body").prepend('<iframe id="jQuery_history" style="display: none;"></iframe>');
            var c = $("#jQuery_history")[0];
            var d = c.contentWindow.document;
            d.open();
            d.close();
            d.location.hash = b
        } else if ($.browser.safari) {
            jQuery.historyBackStack = [];
            jQuery.historyBackStack.length = history.length;
            jQuery.historyForwardStack = [];
            jQuery.isFirst = true
        }
        jQuery.historyCallback(b.replace(/^#/, ''));
        setInterval(jQuery.historyCheck, 100)
    },
    historyAddHistory: function(a) {
        jQuery.historyBackStack.push(a);
        jQuery.historyForwardStack.length = 0;
        this.isFirst = true
    },
    historyCheck: function() {
        if (jQuery.browser.msie) {
            var a = $("#jQuery_history")[0];
            var b = a.contentDocument || a.contentWindow.document;
            var c = b.location.hash;
            if (c != jQuery.historyCurrentHash) {
                location.hash = c;
                jQuery.historyCurrentHash = c;
                jQuery.historyCallback(c.replace(/^#/, ''))
            }
        } else if ($.browser.safari) {
            if (!jQuery.dontCheck) {
                var d = history.length - jQuery.historyBackStack.length;
                if (d) {
                    jQuery.isFirst = false;
                    var i;
                    if (d < 0) {
                        for (i = 0; i < Math.abs(d); i++) {
                            jQuery.historyForwardStack.unshift(jQuery.historyBackStack.pop())
                        }
                    } else {
                        for (i = 0; i < d; i++) {
                            jQuery.historyBackStack.push(jQuery.historyForwardStack.shift())
                        }
                    }
                    var e = jQuery.historyBackStack[jQuery.historyBackStack.length - 1];
                    if (e !== undefined) {
                        jQuery.historyCurrentHash = location.hash;
                        jQuery.historyCallback(e)
                    }
                } else if (jQuery.historyBackStack[jQuery.historyBackStack.length - 1] === undefined && !jQuery.isFirst) {
                    if (document.URL.indexOf('#') >= 0) {
                        jQuery.historyCallback(document.URL.split('#')[1])
                    } else {
                        c = location.hash;
                        jQuery.historyCallback('')
                    }
                    jQuery.isFirst = true
                }
            }
        } else {
            c = location.hash;
            if (c != jQuery.historyCurrentHash) {
                jQuery.historyCurrentHash = c;
                jQuery.historyCallback(c.replace(/^#/, ''))
            }
        }
    },
    historyLoad: function(a) {
        var b;
        if (jQuery.browser.safari) {
            b = a
        } else {
            b = '#' + a;
            location.hash = b
        }
        jQuery.historyCurrentHash = b;
        if (jQuery.browser.msie) {
            var c = $("#jQuery_history")[0];
            var d = c.contentWindow.document;
            d.open();
            d.close();
            d.location.hash = b;
            jQuery.historyCallback(a)
        } else if (jQuery.browser.safari) {
            jQuery.dontCheck = true;
            this.historyAddHistory(a);
            var e = function() {
                jQuery.dontCheck = false
            };
            window.setTimeout(e, 200);
            jQuery.historyCallback(a);
            location.hash = b
        } else {
            jQuery.historyCallback(a)
        }
    }
});