/* Avs Menu System */

$(document).ready(function() {
	
	// Menu Container Setting
	var menuSpeed = parseInt($("#MenuSpeed").attr("value"));
	var menuAnimation = $("#MenuStyle").attr("value");
	var menuItemHover = $("#MenuHover").attr("value");
	
	//Menu Item Settings
	var menuItemBGDefault = "#ffffff";
	var menuItemBGHover = "#dddddd";
	
	var menuItemBorderDefault = "#ffffff";
	var menuItemBorderHover = "#aaaaaa";
	
	// Menu Container Hovers
	
	// The fade in and out - Needs to be slightly slower otherwise looks jerky and unprofessional
	if(menuAnimation == "fade") {
			// Assign hover funnctions to each element
			$('.MenuItemHover').hover(function() {
				// Reset the opacity of the menus incase to overcome animation not finishing bug
			   $("#" + this.id + " > .MenuSubCont").css("opacity","1");
			   $("#" + this.id + " > .MenuSubCont").css("MozOpacity","1");
			   $("#" + this.id + " > .MenuSubCont").css("filter","alpha(opacity=100");
				
				$("#" + this.id + " > .MenuSubCont").fadeIn({queue:false, duration: menuSpeed}); // FADE IN!
			},
			function () {
				$("#" + this.id + " > .MenuSubCont").fadeOut({queue:false, duration: menuSpeed}); // OH CRAP! FADE OUT!
			});
	}
	
	// The slide down and up - looks the best.. works when fast, very fluid
	if(menuAnimation == "slide") {
		$('.MenuItemHover').each(function(e) {
			
			// Set the original height value to enable hover function to access it and reset the css height if the slide animation is ended before it completes
			if( ($(this).children(".MenuSubCont:first").height() ) != null) {
				$(this).attr("origHeight", $(this).children(".MenuSubCont:first").height());
			}
			
			// Assign the hover functions to each element
			$(this).hover(function() {
				$(this).children(".MenuSubCont:first").height(parseInt($(this).attr("origHeight"))); // Reset the div to be displayed to its original height to overcome a bug caused by the animation stopping before it compeltes
				$(this).children(".MenuSubCont:first").slideDown({queue:false, duration: menuSpeed, easing: "easeInOutCirc"}); // Transformers! ROLL OUT!
				//alert($("#" + this.id + " > .MenuSubCont").offset().left);
			},
			function () {
				$(this).children(".MenuSubCont:first").slideUp({queue:false, duration: menuSpeed, easing: "easeInOutCirc"}); // TRANSFORMERS! I MESSED UP! ROLL IN!
			});
		});
	}
	
	// The slide down and up - looks the best.. works when fast, very fluid
	if(menuAnimation == "slideUL") {
		$('.MenuItemHover').each(function(e) {
			
			// Set the original height value to enable hover function to access it and reset the css height if the slide animation is ended before it completes
			/*
			if( ($(this).children("ul.MenuItemCont:first").height() ) != null) {
				$(this).attr("origHeight", $(this).children("ul.MenuItemCont:first").height());
			}
			*/
			
			// Assign the hover functions to each element
			$(this).hover(function() {
				//$(this).children("ul.MenuItemCont:first").slideDown({duration: menuSpeed, easing: "easeInOutCirc"}) // Transformers! ROLL OUT!
				//$(this).children("ul.MenuItemCont:first").height(parseInt($(this).attr("origHeight"))); // Reset the div to be displayed to its original height to overcome a bug caused by the animation stopping before it compeltes
				$(this).children("ul.MenuItemCont:first").animate({height:'show'}, menuSpeed );
			},
			function () {
				//$(this).children("ul.MenuItemCont:first").slideUp({duration: menuSpeed, easing: "easeInOutCirc"}) // Transformers! ROLL OUT!
				$(this).children("ul.MenuItemCont:first").animate({height:'hide'}, menuSpeed );
			});
		});
	}
	
	// Menu Item Hovers
	if(menuItemHover == "on") { // Only activate the leet hover if you want it
		$('.MenuItem').each(function(e) {
			// Assing hovers to each element within the menu
			$(this).hover(function() {
				//$("#" + this.id + " > a").animate({color: "#ffffff"}, 200); // Un comment this to animate text color
				$(this).animate({backgroundColor: menuItemBGHover, borderTopColor: menuItemBorderHover, borderBottomColor: menuItemBorderHover}, {queue: false, duration: menuSpeed});
			},
			function () {
				//$("#" + this.id + " > a").animate({color: "#000000"}, 200); // Un comment this to animate text color
				$(this).animate({backgroundColor: menuItemBGDefault, borderTopColor: menuItemBorderDefault, borderBottomColor: menuItemBorderDefault}, {queue: false, duration: menuSpeed});
			});
		});
	}
});

