﻿(function($) {
	jQuery.fn.DropMenu = function(options) {

		var defaults = {
			subMenu: undefined,
			delayTime: 100,
			allowClick: true
		};

		var options = $.extend(defaults, options);
		var timerID = 0;
		var me = $(this);

		options.subMenu.css({ left: me.offset().left + 'px', top: (parseInt(me.offset().top) + 31).toString() + 'px', position: 'absolute' });

		// Determines if can click on main menu or not
		//
		me.click(function() {
			return options.allowClick;
		});

		// Display menu when on mouse over and wire the mouse out to hide menu
		//
		me.mouseover(function() {
			options.subMenu.slideDown('fast');
			if (timerID != 0)
				clearTimeout(timerID);
			timerID = 0;

			me.mouseout(function() {
				if (timerID == 0) {
					timerID = setTimeout(function() {
						if (timerID != 0)
							clearTimeout(timerID);
						timerID = 0;
						options.subMenu.slideUp('fast');
					}, options.delayTime);
				}
			});

			options.subMenu.mouseover(function() {
				if (timerID != 0)
					clearTimeout(timerID);
				timerID = 0;
				me.unbind('mouseout');
			});

			options.subMenu.mouseout(function() {
				if (timerID == 0) {
					timerID = setTimeout(function() {
						if (timerID != 0)
							clearTimeout(timerID);
						timerID = 0;
						options.subMenu.slideUp('fast');
					}, options.delayTime);
				}
			});
		});

		return me;
	};
})(jQuery);


