jQuery(document).ready(function() {

	/*
	    Menu
	*/
	jQuery("div#menu ul").superfish({
		speed: 500,
		animation: {opacity:'show',height:'show'}, 
		hideAnimation: {opacity:'hide',height:'hide'}, 
		autoArrows: false, 
		delay: 500,
		onBeforeShow: function() {
			
			/// Start positioning the shadow behind the opening submenu.
			
			var $activeSubMenu = jQuery(this);
			var $submenuShadow = $activeSubMenu.prev("div.submenu_shadow");
			
			var left = $activeSubMenu.css('left');
			var marginLeft = $activeSubMenu.css('margin-left');
			var marginTop = $activeSubMenu.css('margin-top');
			
			if (marginTop) {
				marginTop = (marginTop.replace("px", "") * 1 + 10) + "px";
			} else {
				marginTop = "auto";
			}
			
			// Give the shadow the same height as the above lying submenu
			$submenuShadow.height($activeSubMenu.height());
			
			$submenuShadow.css({
				'left': left,
				'margin-left': marginLeft,
				'margin-top': marginTop
			});
			
			// Animate the shadow the same time as the menu
			$submenuShadow.animate({opacity:'show',height:'show'}, 600, function(){});
			
		}
		
	});
	
	jQuery('div#menu ul').children('li').each(
	    function() {
			if( jQuery(this).parent().parent().attr('id') == 'menu') {
			    var itemMiddlePosition = (jQuery(this).width() / 2);
			    var defaultWidth = (jQuery('div#menu li ul').first().width() / 2);
			    var widthResult = (itemMiddlePosition - defaultWidth) + 4;
	
			    if(jQuery.browser.msie && jQuery.browser.version == '7.0')
			    {
			    	var ieWidth = (jQuery(this).width());
			    	var widthResult = (widthResult - ieWidth);
	
			    	jQuery(this).find('ul:first').css('margin-top', 33 +'px');
			    }
				
				// Position the submenu (the -3 is for the extra pixels the '+'-char in the parent menu adds)
			    jQuery(this).find('ul:first').css('margin-left', (widthResult * 1 - 3) +'px');
			}
	    }
	);
	
});
