/*
 * jquery.ui.potato.menu
 * 
 * Copyright (c) 2009 makoto_kw (makoto.kw@gmail.com)
 * Dual licensed under the new BSD licenses.
 * 
 * Version: 1.0
 */
(function($)
{
	$.fn.extend
	({
		ptMenu:function(arg)
		{
			var defaltOption =
			{
				vertical:false,
				menuItemSelector: 'li',
				menuGroupSelector: 'ul',
				firstClass:'potato-menu',
				menuItemClass:'potato-menu-item',
				menuGroupClass:'potato-menu-group',
				verticalClass:'potato-menu-vertical',
				holizontalClass:'potato-menu-holizontal',
				hasVerticalClass:'potato-menu-has-vertical',
				hasHolizontalClass:'potato-menu-has-holizontal',
				showDuration: 350,
				hideDuration: 100
			}
			
			var option = (typeof(arg)!='string') ? $.extend(defaltOption,arg) : $.extend(defaltOption,{});
			var $menu = $(this).addClass(option.firstClass).addClass((option.vertical) ? option.verticalClass : option.holizontalClass);
			var $menuItems = $menu.find(option.menuItemSelector).addClass(option.menuItemClass);
			
			$menuItems.each(function()
			{
				if($(this).parent().attr('class') != '')
				{
					$(this).hover
					(
						function(e)
						{
							$(this).find('span').fadeIn('fast');
							$(this).addClass('menu-item-hover');					
						},
						function(e)
						{
							$(this).find('span').fadeOut('fast');
							$(this).removeClass('menu-item-hover');
						}
					);
				}
				else
				{					
					$(this).hover
					(
						function(e)
						{
							$(this).addClass('sub-item-hover');
						},
						function(e)
						{
							$(this).removeClass('sub-item-hover');
						}
					);
				}
			});
			
			var $menuGroups = $menu.find(option.menuGroupSelector).addClass(option.menuGroupClass);
			$menuGroups.parent().each(function(index)
			{
				var bottom = $(this).parent(option.menuGroupSelector+'.'+option.firstClass).length == 1 && !option.vertical;
				var $menuGroup = $(this).addClass((bottom) ? option.hasVerticalClass : option.hasHolizontalClass)
					.children(option.menuGroupSelector+':first').addClass(option.verticalClass);
					
				$(this).hover
				(
					function(e)
					{
						if($menuGroup.is(':hidden'))
						{
							var offset = (bottom) ? {left:'0',top:''} : {left:$(this).width()+'px',top:'0'};
							$menuGroup.css({left:offset.left,top:offset.top}).fadeIn(option.showDuration);
						}
					},
					function(e)
					{
						if($menuGroup.is(':visible'))
						{
							$menuGroup.fadeOut(option.hideDuration);
						}
					}
				);
				
				$(this).find('.head').click(function()
				{
					$(this).parent().click();
				});
				
				$(this).click(function()
				{
					
				});
			});
			
			// Center the whole menu (sum of the width of each element devided by 2. Using that as a negative margin)
			//var margin_left = ($(this).width()/2);
			
			// HARD OVERRIDE
			var margin_left = 422;
			
			$(this).css('margin-left','-'+margin_left+'px');
			if(jQuery.browser.msie)
			{
				var new_width = ($(this).parent().parent().width()/2) - margin_left;
				var new_width = $(this).parent().parent().width() - new_width;
				$(this).css('width',new_width+'px');
			}

			
			$menu.find('a[href^="#"]').click(function()
			{
				//$menuGroups.fadeOut(option.hideDuration);
				//return ($(this).attr('href')=='#') ? false : true;
			});
			
			return this;
		}
	})
})(jQuery);