﻿(function($) {

	$.fn.dropDownMenu = function() {

		return this.each(function() {
			function menuHovered() {
				$Menu.children('li').each(function() {
					if ($(this).hasClass('hover'))
						return true;
				});
				return false;
			}

			var $Menu = $(this);

			$Menu.children('li').hover(
                function() {
                	$(this).addClass('hover');
                },
                function() {
                	$(this).removeClass('hover');
                }
            );
            
            // disable links with children
			$Menu.children('li').children('div').children('a').click(function() {
				if ($(this).parent().parent().find('ul').size() > 0)
					return false;
			});

			// if user clicks on anything other than the menu, collapse the menu
			$('*').click(function(e) {
				if ((this == e.target) && menuHovered()) {
					var clickInMenu = (event.target == $Menu[0] || ($(event.target).parents().index($Menu[0]) >= 0));
					if (!clickInMenu) {
						$Menu.children('li').removeClass('hover');
						return false;
					}
				}
			});
		});

	};
})(jQuery);

