(function ($) {
    $.fn.galleryView = function (h) {
        var j = $.extend($.fn.galleryView.defaults, h);
        var k;
        var l;
        var m = 0;
        var n;
        var o;
        var p;
        var q;
        var r;
        var t;
        var u = 0;
        var v = 0;
        var w;
        var f;
        var x;
        var y = false;
        var z = 20;
        var A = 5;
        var B = 2;
        var C;
        var D;
        var E;
        var F;

        function showItem(i) {
            $('img.nav-next').unbind('click');
            $('img.nav-prev').unbind('click');
            D.unbind('click');
            if (j.fade_panels) {
                E.fadeOut(j.transition_speed).eq(i % v).fadeIn(j.transition_speed)
            }
            if (w == 'strip') {
                C.stop();
                var b = getPos(D[i]).left - (getPos(F[0]).left + 2);
                var c = (b >= 0 ? '-=' : '+=') + Math.abs(b) + 'px';
                C.animate({
                    'left': c
                }, j.transition_speed, j.easing, function () {
                    if (i > v) {
                        i = i % v;
                        m = i;
                        C.css('left', '-' + ((j.frame_width + p) * i) + 'px')
                    } else if (i <= (v - strip_size)) {
                        i = (i % v) + v;
                        m = i;
                        C.css('left', '-' + ((j.frame_width + p) * i) + 'px')
                    }
                    if (!j.fade_panels) {
                        E.hide().eq(i % v).show()
                    }
                    u = i;
                    $('img.nav-prev').click(jumpToPrevItem);
                    $('img.nav-next').click(jumpToNextItem);
                    enableFrameClicking()
                })
            } else {
                F.stop();
                var d = getPos(D[i]);
                F.animate({
                    'left': (d.left - 2 + 'px')
                }, j.transition_speed, j.easing, function () {
                    if (!j.fade_panels) {
                        E.hide().eq(i % v).show()
                    }
                    $('img.nav-prev').click(jumpToPrevItem);
                    $('img.nav-next').click(jumpToNextItem);
                    enableFrameClicking()
                })
            }
            if ($('a', D[i])[0]) {
                F.unbind('click').click(function () {
                    var a = $('a', D[i]).eq(0);
                    if (a.attr('target') == '_blank') {
                        window.open(a.attr('href'))
                    } else {
                        location.href = a.attr('href')
                    }
                })
            }
        };

        function showNextItem() {
            if (++m == D.length) {
                m = 0
            }
            showItem(m)
        };

        function showPrevItem() {
            if (--m < 0) {
                m = D.length - 1
            }
            showItem(m)
        };

        function jumpToNextItem() {
            $(document).stopTime("transition");
            if (++m == D.length) {
                m = 0
            }
            showItem(m);
            $(document).everyTime(j.transition_interval, "transition", function () {
                showNextItem()
            })
        };

        function jumpToPrevItem() {
            $(document).stopTime("transition");
            if (--m < 0) {
                m = v - 1
            }
            showItem(m);
            $(document).everyTime(j.transition_interval, "transition", function () {
                showNextItem()
            })
        };

        function getPos(a) {
            var b = 0,
                top = 0;
            var c = a.id;
            if (a.offsetParent) {
                do {
                    b += a.offsetLeft;
                    top += a.offsetTop
                } while (a = a.offsetParent)
            }
            if (c == l) {
                return {
                    'left': b,
                    'top': top
                }
            } else {
                var d = getPos(f[0]);
                var e = d.left;
                var g = d.top;
                return {
                    'left': b - e,
                    'top': top - g
                }
            }
        };

        function enableFrameClicking() {
            D.each(function (i) {
                if ($('a', this).length == 0) {
                    $(this).click(function () {
                        $(document).stopTime("transition");
                        showItem(i);
                        m = i;
                        $(document).everyTime(j.transition_interval, "transition", function () {
                            showNextItem()
                        })
                    })
                }
            })
        };
        return this.each(function () {
            f = $(this);
            $('script').each(function (i) {
                var s = $(this);
                if (s.attr('src') && s.attr('src').match(/jquery\.galleryview/)) {
                    x = s.attr('src').split('jquery.galleryview')[0] + 'themes/'
                }
            });
            f.css('visibility', 'hidden');
            C = $('.filmstrip', f);
            D = $('li', C);
            E = $('.panel', f);
            l = f.attr('id');
            v = D.length;
            strip_size = Math.floor((j.panel_width - 84) / (j.frame_width + 5));
            if (strip_size >= v) {
                w = 'pointer';
                strip_size = v
            } else {
                w = 'strip'
            }
            n = j.panel_width;
            o = j.panel_height + j.frame_height + A + (j.show_captions ? z : A);
            p = Math.min((j.panel_width - 84 - (j.frame_width * strip_size)) / (strip_size + 1), 10);
            if (w == 'pointer') {
                q = (j.frame_width * v) + (p * (v))
            } else {
                q = (j.frame_width * v * 3) + (p * (v * 3))
            }
            r = 0;
            t = ((strip_size * j.frame_width) + ((strip_size - 1) * p));
            C.wrap('<div class="strip_wrapper"></div>');
            if (w == 'strip') {
                D.clone().appendTo(C);
                D.clone().appendTo(C);
                D = $('li', C)
            }
            if ($('.panel-overlay').length > 0) {
                E.append('<div class="overlay"></div>')
            }
            if (j.show_captions) {
                D.append('<div class="caption"></div>').each(function (i) {
                    $(this).find('.caption').html($(this).find('img').attr('title'))
                })
            }
            $('<img />').addClass('nav-next').attr('src', x + j.nav_theme + '/next.png').appendTo(f).css({
                'position': 'absolute',
                'cursor': 'pointer',
                'top': j.panel_height + A + ((j.frame_height - 22) / 2) + 'px',
                'right': '16px'
            }).click(jumpToNextItem);
            $('<img />').addClass('nav-prev').attr('src', x + j.nav_theme + '/prev.png').appendTo(f).css({
                'position': 'absolute',
                'cursor': 'pointer',
                'top': j.panel_height + A + ((j.frame_height - 22) / 2) + 'px',
                'left': '16px'
            }).click(jumpToPrevItem);
            f.css({
                'position': 'relative',
                'margin': '0',
                'background': j.background_color,
                'border': j.border,
                'width': n + 'px',
                'height': o + 'px'
            });
            E.css({
                'width': (j.panel_width - parseInt(E.css('paddingLeft').split('px')[0], 10) - parseInt(E.css('paddingRight').split('px')[0], 10)) + 'px',
                'height': (j.panel_height - parseInt(E.css('paddingTop').split('px')[0], 10) - parseInt(E.css('paddingBottom').split('px')[0], 10)) + 'px',
                'position': 'absolute',
                'top': '0px',
                'left': '0px',
                'overflow': 'hidden',
                'background': '#bebebe',
                'display': 'none'
            });
            C.css({
                'listStyle': 'none',
                'margin': '0',
                'padding': '0',
                'width': q + 'px',
                'position': 'absolute',
                'zIndex': '900',
                'top': '0',
                'left': '0',
                'height': (j.frame_height + 10) + 'px',
                'background': j.background_color
            });
            D.css({
                'float': 'left',
                'position': 'relative',
                'height': j.frame_height + 'px',
                'zIndex': '901',
                'marginTop': A + 'px',
                'marginBottom': '0px',
                'marginRight': p + 'px',
                'padding': '0',
                'cursor': 'pointer'
            });
            $('img', D).css({
                'border': 'none'
            });
            $('.strip_wrapper', f).css({
                'position': 'absolute',
                'top': j.panel_height + 'px',
                'left': ((n - t) / 2) + 'px',
                'width': t + 'px',
                'height': (j.frame_height + A + (j.show_captions ? z : A)) + 'px',
                'overflow': 'hidden'
            });
            $('.caption', f).css({
                'position': 'absolute',
                'top': j.frame_height + 'px',
                'left': '0',
                'margin': '0',
                'width': j.frame_width + 'px',
                'padding': '0',
                'color': j.caption_text_color,
                'textAlign': 'center',
                'fontSize': '16px',
                'height': z + 'px',
                'lineHeight': z + 'px'
            });
            $('.panel-overlay', E).css({
                'position': 'absolute',
                'zIndex': '999',
                'width': (j.panel_width - 20) + 'px',
                'height': j.overlay_height + 'px',
                'bottom': '0',
                'left': '0',
                'padding': '0 16px',
                'color': j.overlay_text_color,
                'fontSize': j.overlay_font_size
            });
            $('.panel-overlay a', E).css({
                'color': j.overlay_text_color,
                'textDecoration': 'underline',
                'fontWeight': 'bold'
            });
            $('.overlay', E).css({
                'position': 'absolute',
                'zIndex': '998',
                'width': j.panel_width + 'px',
                'height': j.overlay_height + 'px',
                'bottom': '0',
                'left': '0',
                'background': j.overlay_color,
                'opacity': j.overlay_opacity
            });
            $('.panel iframe', E).css({
                'width': j.panel_width + 'px',
                'height': (j.panel_height - j.overlay_height) + 'px',
                'border': '0'
            });
            var b = $('<div></div>');
            b.attr('id', 'pointer').appendTo(f).css({
                'position': 'absolute',
                'zIndex': '1000',
                'cursor': 'pointer',
                'top': getPos(D[0]).top - (B / 2) + 'px',
                'left': getPos(D[0]).left - (B / 2) + 'px',
                'height': j.frame_height - B + 'px',
                'width': j.frame_width - B + 'px',
                'border': B + 'px solid ' + (j.nav_theme == 'dark' ? 'black' : '#bebebe')
            });
            F = $('#pointer', f);
            var c = $('<img />');
            c.attr('src', x + j.nav_theme + '/pointer.png').appendTo($('#pointer')).css({
                'position': 'absolute',
                'zIndex': '1001',
                'top': '-' + (10 + B) + 'px',
                'left': ((j.frame_width / 2) - 10) + 'px'
            });
            enableFrameClicking();
            if (j.pause_on_hover) {
                $('.panel', f).mouseover(function () {
                    $(document).oneTime(500, "animation_pause", function () {
                        $(document).stopTime("transition");
                        y = true
                    })
                }).mouseout(function () {
                    $(document).stopTime("animation_pause");
                    if (y) {
                        $(document).everyTime(j.transition_interval, "transition", function () {
                            showNextItem()
                        });
                        y = false
                    }
                })
            }
            $('.panel', f).eq(0).show();
            if (w == 'strip') {
                C.css('left', '-' + ((j.frame_width + p) * v) + 'px');
                m = v
            }
            if ($('a', D[m])[0]) {
                F.click(function () {
                    var a = $('a', D[m]).eq(0);
                    if (a.attr('target') == '_blank') {
                        window.open(a.attr('href'))
                    } else {
                        location.href = a.attr('href')
                    }
                })
            }
            if (v > 1) {
                $(document).everyTime(j.transition_interval, "transition", function () {
                    showNextItem()
                })
            }
            f.css('visibility', 'visible')
        })
    };
    $.fn.galleryView.defaults = {
        panel_width: 400,
        panel_height: 300,
        frame_width: 80,
        frame_height: 80,
        overlay_height: 40,
        overlay_font_size: '1em',
        transition_speed: 400,
        transition_interval: 6000,
        overlay_opacity: 0.6,
        overlay_color: 'black',
        background_color: 'black',
        overlay_text_color: '#bebebe',
        caption_text_color: '#bebebe',
        border: '1px solid black',
        nav_theme: 'light',
        easing: 'swing',
        show_captions: false,
        fade_panels: true,
        pause_on_hover: false
    }
})(jQuery);