(function($) {
    $.fn.slideMenu = function(options) {
        var opts = $.extend($.fn.slideMenu.defaults, options);
        var $menu = $.fn.slideMenu;
        var $this = $(this);
        var $items = $('#' + $this.attr('id') + ' > li');
        var expandDistance = 0;

        $menu.expand = function(o) {
            $menu.close($items.eq($menu.expanded_index));

            SetListHeight(o);

            BuildMenuClass(true);

            $menu.expanded_index = o.attr('index');

            if ($('li', $menu.current).size() > 0) {
                $('.overlay', $menu.current).animate({ top: "+=" + (expandDistance - 1) }, opts.expand_speed);
                $menu.current.animate({ height: "+=" + expandDistance }, opts.expand_speed,
					function() {
					    $('ul', $menu.current).fadeIn(opts.fade_interval);
					});
            }

            if (opts.related_id != '') {
                if (typeof ($(opts.related_id).galleryView.showItem) == 'function' && arguments.length == 1)
                    $(opts.related_id).galleryView.showItem(o.attr('index'));
            }
        }

        $menu.close = function(o) {
            $menu.prev = o;

            if ($('li', $menu.prev).size() > 0) {
                $('.overlay', $menu.prev).animate({ top: "-=" + (expandDistance - 1) }, opts.close_speed);
                $menu.prev.animate({ height: "-=" + expandDistance }, opts.close_speed,
					function() {
					    $('#' + $this.attr('id') + ' ul').hide();
					    BuildMenuClass(false);
					});
            }
            else {
                BuildMenuClass(false);
            }

            if (o.attr('index') == $menu.expanded_index) $menu.expanded_index = -1;
        }

        return this.each(function() {
            MenuBuild();
            $items.hover(
				function() {
				    if (opts.related_id != '') $(document).stopTime("transition");
				    $menu.expand($(this));
				},
				function() {
				    $menu.close($(this));
				    BuildMenuClass(false);

				    if (opts.related_id != '') {
				        $(document).everyTime($(opts.related_id).galleryView.transition_interval, "transition", function() {
				            $(opts.related_id).galleryView.showNextItem(true);
				        });
				    }
				});
        });

        function MenuBuild() {
            IndexInit();
            
            $items.each(function() {
                $menu.current = $(this);
                $(this).addClass(BindName('closed')).append('<div class="overlay"></div>');
                $('.overlay', $(this)).addClass(BindName('closed') + 'overlay');
            });

            if (opts.default_expand) $menu.expand($items.eq(0));
        }

        function SetListHeight(o) {
            expandDistance = opts.item_height * $('li', o).size() + 12;
            $menu.current = o;
        }

        function BindName(strOpt) {
            return strOpt += opts.withIDclass ? $menu.current.attr('id') : '';
        }

        function DisposeName(strOpt) {
            return strOpt += opts.withIDclass ? $menu.prev.attr('id') : '';
        }

        function BuildMenuClass(s) {
            var mouseover = s;

            if (mouseover) {
                $menu.current.attr('class', '').addClass(BindName('expand'));
                $('.overlay', $menu.current).attr('class', '')
					.addClass('overlay').addClass(BindName('expand') + 'overlay')
            }
            else {
                $menu.prev.attr('class', '').addClass(DisposeName('closed'));
                $('.overlay', $menu.prev).attr('class', '')
					.addClass('overlay').addClass(DisposeName('closed') + 'overlay');
            }
        }
        function IndexInit() {
            $items.each(function(i) {
                $(this).attr('index', i);
            });
        }
    }

    $.fn.slideMenu.defaults = {
        fade_interval: 200,
        expand_speed: 300,
        close_speed: 200,
        item_height: 14,
        withIDclass: false,
        related_id: '',
        default_expand: false
    };
})(jQuery);
