/*********************
//* jQuery Multi Level CSS Menu (horizontal)- By Cykooz
*********************/

(function($)
{
	$.fn.cykooz_menu = function(options)
	{
		options = $.extend({}, $.fn.cykooz_menu.defaults, options);
		
		var mainmenu = $(this);
		var headers = mainmenu.find('ul').parent();
		headers.each(function(i)
		{
			var header = $(this);
			var submenu = header.find('ul:eq(0)');
			var width = header.outerWidth();
			var height = header.innerHeight();
			var submenu_width = submenu.outerWidth();
			var is_top_header = header.parents('ul').length==1 ? true : false;
			header.addClass('has-submenu');
			submenu.css({top: is_top_header ? height+'px' : 0});
			header.hover(function(e)
			{
					var offsets = header.offset();
					var menuleft = is_top_header ? 0 : width;
					if (offsets.left + menuleft + submenu_width > $(window).width())
					{
						menuleft = is_top_header ? width - submenu_width - 1 : -width;
						header.addClass('left-submenu');
					}
					else
					{
						header.removeClass('left-submenu');
					}
					submenu.css({left: menuleft+'px'}).fadeIn(options.overduration);
			}, function(e)
			{
					submenu.fadeOut(options.outduration)
			}); //end hover
		}) //end $headers.each()
		mainmenu.find('li').hover(function()
		{
			$(this).addClass('hover');
		}, function()
		{
			$(this).removeClass('hover');
		});
		mainmenu.find('ul').css({display: 'none', visibility: 'visible'});
	};
	
	$.fn.cykooz_menu.defaults = 
	{
		overduration: 350,
		outduration: 100
	};
	
})(jQuery);

