window.showDropDownMenu = null;
(function(){
    var Menu = null;
    $(function() {
	    Menu = document.createElement("UL");
	    Menu.className = "DropDownMenu";
        document.body.appendChild(Menu);
        $(document.body).click(function() { $(Menu).hide(); });
	
	    window.showDropDownMenu = function(x, y, items) {
	        $(Menu).empty();
	        $.each(items, function(i, item) {
	            var li = $("<li></li>").appendTo(Menu);
	            if(typeof item.text != "undefined") li.text(item.text);
	            if(typeof item.html != "undefined") li.html(item.html);
	            if(typeof item.checked != "undefined") li.addClass(item.checked ? "check on" : "check off");
				if(typeof item.onclick != "undefined") {
	                li.click(item.onclick)
	                .hover(
	                    function() { $(this).addClass("hl"); },
	                    function() { $(this).removeClass("hl"); }
	                );
				} else if(typeof item.url != "undefined") {
	                li.click(function() { window.location = item.url; })
	                .hover(
	                    function() { $(this).addClass("hl"); },
	                    function() { $(this).removeClass("hl"); }
	                );
				} else if(typeof item.separator != "undefined") {
					li.addClass("separator");
				} else {
					li.addClass("static");
				}
	        });
	        $(Menu)
	        	.css({left: x + "px", top: y + "px"})
	        	.show("fast", function() {
	        		if(x + $(Menu).width() > $(window).width())
	        			$(Menu).css({left: ($(window).width() - $(Menu).width() - 4) + "px"})
	        	});
	    };
	

    });

    jQuery.fn.attachDropdownMenu = function(menu) {
        return this
            .css("cursor", "pointer")
            .hover(
                function(){ $(this).addClass("yellow-highlight"); },
                function(){ $(this).removeClass("yellow-highlight");}
            )
            .click(function(event) {
                showDropDownMenu(event.pageX + 8, event.pageY + 8, menu);
                return false;
            });
    };

})();
