/**
 * Single Drop Down Menu 1.1
 * August 22, 2009
 * Corey Hart @ http://www.codenothing.com
 */
;(function($){
 	// Define the function
	$.fn.singleDropMenu = function(options){
		return this.each(function(){
			// Default Settings
			var $obj = $(this), timer, menu,
				settings = $.extend({
					timer: 500,
					parentMO: 'non-404',
					childMO: 'non-404',
                                        divClass: 'multi-menu',
                                        id: 'multi-ddm'
				}, options||{}, $.metadata ? $obj.metadata() : {});

			// Run Menu
			$obj.children('li').mouseover(function(){
//                                settings.id = $(this).attr('id');
				// Clear any open menus
				closemenu();
				// Open nested list
                                
                                $oA = $(this).children('a');
                                $oA.addClass(settings.parentMO);

                                var oA_offset = $oA.offset();
                                var left = oA_offset.left;
                                var width = $oA.outerWidth();
                                var top = oA_offset.top + $oA.outerHeight();
                                var rel = $oA.attr("rel");
                                settings.rel = rel;
                                

//                                alert($('#'+rel).attr("id"));
                                $oDiv = $('#'+rel).show().css({'top': top, 'left': left});
                                if ($oDiv.outerWidth() < width) {
                                    $oDiv.css('width', width);
                                }

			}).mouseout(function(){
				// Prevent auto close
				menu = $(this);
				timer = setTimeout(closemenu, settings.timer);
			});

                        $('.'+settings.divClass).mouseover(function(){
                            clearTimeout(timer);
                        });
                        $('.'+settings.divClass).mouseout(function(){
				// Prevent auto close
				menu = $(this);
				timer = setTimeout(closemenu, 500);
			});

			// Dropped Menu Highlighting
			$('.'+settings.divClass+' span').hover(function(){
				$(this).addClass(settings.childMO);
			}, function(){
				$(this).removeClass(settings.childMO);
			});

			// Closes any open menus when mouse click occurs anywhere else on the page
			$(document).click(closemenu);

			// Function to close set menu
			function closemenu(){
                            if (menu && timer){
                                    $('.'+settings.divClass).hide();
                                    $('#'+settings.id).children('li').children('a').removeClass(settings.parentMO)
                                    clearTimeout(timer);
                                    menu = false;
                            }
			}
		});
	};
})(jQuery);
