(function ($) {
    $.fn.gallery = function () {
        return this.each(function () {
            var $this = $(this),
                $images = $this.find('.imageBlock'),
                $controls = $this.find('.thumbs a'),
                $imageWrap,
                $container;
            
            // Prepare extra markup
            $imageWrap = $images.wrapAll('<div class="gallery-image-wrap" />')
                .parent().css({
                    position: 'absolute'
                });
            // Prepare offset calculation
            $container = $imageWrap.wrap('<div class="gallery-image-frame" />')
                .parent().css({
                    overflow: 'hidden',
                    height: $this.height(),
                    position: 'relative'
                });
            // Remove accessibility features (i.e. scrollbar)
            $this.css('overflow', 'auto');
            // Wire controls
            $controls.click(function (e) {
                var $target,
                    position;
                
                e.preventDefault();
                $target = $(this.hash, $imageWrap);
                if ($target.length === 0) {
                    // target not found in this div, do nothing
                    return;
                }
                position = $target.position();
                // bs, 20110905 Added ie8 workaround
                // (I know feature detection is better, but what the heck does IE8 get wrong here?)
                if ($.browser.msie && parseInt($.browser.version) === 8) {
                    position.top -= 1;
                }
                /* bs, 20110902 Kicked animation
                $imageWrap.animate(
                    {
                        marginTop: parseInt($imageWrap.css('margin-top'), 10)
                            - position.top
                    }
                );
                */
                $imageWrap.css(
                    {
                /* bs, 20110905 Replaced with absolute positioning
                        marginTop: parseInt($imageWrap.css('margin-top'), 10)
                            - position.top
                */
                        top: - position.top
                    }
                );
            });
        });
    };
}(jQuery));

