﻿$(function() {
    $.fn.dropdownmenu = function(options) {
        var options = options || {};
        var hideClass = "hide";
        var $ddl = $(this);
        var $lis = $ddl.find("li");
        options.selected = options.selected || ":first"; // selector string for selected item
        var $sel = $lis.filter(options.selected);

        if ($sel.size() == 0) { // if no match for the selected item is found, select the first item
            options.selected = ":first";
            $sel = $lis.filter(options.selected);
        }

        var $otherItems = $lis.not(options.selected);
        var hideItems = function() { $otherItems.addClass(hideClass); };
        var showItems = function() { $otherItems.removeClass(hideClass); };

        hideItems();
        $sel.find("a").click(function() {
            showItems();
            return false;
        });

        $ddl.mouseleave(hideItems);

        return this;
    }
});
