(function($) {
	jQuery.extend( jQuery.easing,
	{
		easeUltraMenu: function (x, t, b, c, d) {
			return -c *(t/=d)*(t-2) + b;
		}
	});
	
	$.fn.ultraMenu = function(settings) {
	
		var config = {'speed': 300, 'pathToOgg':'', 'pathToMP3':''};
	
		if (settings) $.extend(config, settings);
		
		this.each(function() {		
			$(this).find('>ul>li').each(function() {
				if ($(this).find('ul>li>ul').length > 0) {
					var colcount=$(this).find('>ul>li').length;
					$(this).children('ul').addClass('multicol').css('width',colcount*188+'px');
					$(this).find('>ul>li').each(function() {
						$(this).children('a').eq(0).addClass('first');
					});
					var blockright = $(this).position().left+$(this).children('ul').width();
					if (blockright>960) {
						$(this).children('ul').css('left', -$(this).position().left+'px');
					}
				};
				$(this).data('myheight', $(this).children('ul').height());
				
				if (config.pathToOgg!='' & config.pathToMP3!="") {
					$('body').append("<audio class='menuaudio' preload='auto'><source src='"+config.pathToOgg+"' type='audio/ogg' /><source src='"+config.pathToMP3+"' type='audio/mpeg' /></audio>");
				}
				
			});
			
			var audio=$('.menuaudio');			
		
			$(this).children('ul').children('li').hover(function() {
				$(this).children('ul').hide();
				$(this).children('em').css({top:-90,opacity:0});
				$(this).addClass('hover');
				$(this).children('ul').stop(true,true).css({'height':0}).animate({'height': $(this).data('myheight')}, {'duration':config.speed, 'easing':'easeUltraMenu'});
				$(this).children('em').animate({top:-80, opacity:.9},{'duration':config.speed, 'easing':'easeUltraMenu'});
				
				if (audio.length > 0) {
					if (!!(audio.get($(this).index()).canPlayType)) {
						audio.get($(this).index()).pause();
						audio.get($(this).index()).currentTime=0;
						audio.get($(this).index()).play();
					}
				}
				
			}, function() {
				$(this).removeClass('hover');
			});
		});
		return this;
	};
})(jQuery);
