// JavaScript Document
var jq_menu_triggerCount = 0;
(function($){
	$.fn.menu = function(opts)
	{
		opts = jQuery.extend({
								'animation' : 'none',				//animation.  slideDown, show, 'none', or 'false'
								'hideAnimation' : 'reverse',		//animation played when dropdown is hidden.  revers, slideUp, hide, 'none', or 'false'
								'speed' : 0,						//speed of animation
								'perColumn' : false,					//number of items that should be in a single column.  if false, no column code is run.
								'hoverClass' : ''					//class applied to parent element
							 }, opts);
		return this.each(function(){
			var $trigger = $(this).addClass('j-menuTrigger').attr('triggerid', jq_menu_triggerCount);
			jq_menu_triggerCount++;
			var $parent = $trigger.parents(':first').css('position', 'relative');
			var $menu = $trigger.next('ul');
			if($menu.length == 0)
			{
				$menu = $('ul', $trigger);
			}
			var removeHoverClass = opts.hoverClass ? !$trigger.hasClass(opts.hoverClass) : false;
			if($menu.length == 0 || $('li', $menu).length == 0)
			{
				return;
			}
			var menuHTML = '';
			var curColumn = false;
			if(!isNaN(opts.perColumn) && opts.perColumn > 0)
			{
				$parent.addClass('j-menu-' + Math.ceil($('li', $menu).length / opts.perColumn));
				curColumn = 1;
				menuHTML += '<div class="j-menu-column j-menu-column-1">';
			}
			$('li', $menu).each(function(i){
				var html = $(this).html();
				menuHTML += html
				if(curColumn != false && (i + 1) % opts.perColumn == 0)
				{
					curColumn++;
					menuHTML += '</div><div class="j-menu-column j-menu-column-' + curColumn + '">';
				}
				//$(this).addClass('j-menuItem-column-' + curColumn);
			});
			if(!isNaN(opts.perColumn) && opts.perColumn > 0)
			{
				menuHTML += '<div style="clear:both;"></div></div>';
			}

			$menu.wrap('<div class="j-menuDrop" style="display:none; top:' + $trigger.outerHeight() + 'px; position:absolute;" />');
			$menu = $menu.parents('.j-menuDrop:first');
			$menu.html(menuHTML);

			if(opts.hideAnimation != 'slideUp' && opts.hideAnimation != 'hide')
			{
				opts.hideAnimation = 'reverse';
			}		

			if(opts.animation != 'slideDown' && opts.animation != 'show')
			{
				opts.animation = 'none';
				if(opts.hideAnimation == 'reverse')
				{
					opts.hideAnimation = 'none';
				}
			}
			else if(opts.animation == 'slideDown' && opts.hideAnimation == 'reverse')
			{
				opts.hideAnimation = 'slideUp';
			}
			else if(opts.animation == 'show' && opts.hideAnimation == 'reverse')
			{
				opts.hideAnimation = 'hide';
			}

			$.data($menu, 'info', {
				   					'isOverTrigger' : false,
									'isOverMenu' : false,
									'hideTimer' : false,
									'currentTrigger' : false,
									'closeDrop' : function()
													{
														if(!$.data($menu, 'info').isOverMenu || force === true)
														{
															if(opts.hideAnimation != 'none' && opts.speed != 0)
															{
																$menu[opts.hideAnimation](opts.speed);
															}
															else
															{
																$menu.hide();
															}
															if(removeHoverClass)
															{
																$trigger.removeClass(opts.hoverClass);
															}
														}
														$.data($menu, 'info').hideTimer = false;
													}
								} );
			$trigger.hover(
				function()
				{
					$.data($menu, 'info').currentTrigger = $(this).attr('triggerid');
					$.data($menu, 'info').isOverTrigger = true;
					if(opts.animation != 'none' && opts.speed != 0)
					{
						$menu[opts.animation](opts.speed);
					}
					else
					{
						$menu.show();
					}
					if(opts.hoverClass && removeHoverClass)
					{
						$trigger.addClass(opts.hoverClass);
					}
				},
				function()
				{
					$.data($menu, 'info').isOverTrigger = false;
					if($.data($menu, 'info').hideTimer)
					{
						clearTimeout($.data($menu, 'info').hideTimer);
					}
					$.data($menu, 'info').hideTimer = setTimeout($.data($menu, 'info').closeDrop, 250);
				}
			);
			$menu.hover(
				function()
				{
					$.data($menu, 'info').isOverMenu = true;
					if(opts.animation != 'none' && opts.speed != 0)
					{
						$menu[opts.animation](opts.speed);
					}
					else
					{
						$menu.show();
					}
				},
				function()
				{
					$.data($menu, 'info').isOverMenu = false;
					if($.data($menu, 'info').hideTimer) clearTimeout($.data($menu, 'info').hideTimer);
					$.data($menu, 'info').hideTimer = setTimeout($.data($menu, 'info').closeDrop, 250);
				}
			);
						

		});
	}
})(jQuery);
