$(function() {		

	navigation_storeDropdownHeight();

	$("#navigation > ul > li").add(".sideNavItem").hover(function() { //mousein
		var childMenu = $(this).find(".child-menu");
		childMenu.css("visibility","visible");
		childMenu.stop().animate({  height: childMenu.attr("height")   }, "fast");
	}, function() { //mouseout
		var childMenu = $(this).find(".child-menu");
		childMenu.stop().animate({  height: 0	}, "fast", function() {  $(this).css("visibility","hidden");  });
	});	

	//Attach hover handlers to #orgnavi in ie and other browsers
	if(ie) { //the bad...
		//fix a ie bug where .navi001 doesnt display bumbers, refresh padding to solve issue
		$("#orgnavi .navi001").css("paddingLeft","1");
		$("#orgnavi .paanavigointi").hover(function(){
			var hoverElementsGroup = $(this).children(".bumber").add(  $(this).children(".contentBasket")  );
			hoverElementsGroup.children(".hover").css("visibility","visible");
			hoverElementsGroup.children(".normal").css("visibility","hidden");
		},function(){
			var hoverElementsGroup = $(this).children(".bumber").add(  $(this).children(".contentBasket")  );
			hoverElementsGroup.children(".hover").css("visibility","hidden");
			hoverElementsGroup.children(".normal").css("visibility","visible");
		});
	}
	else { 
		$("#orgnavi .hover").css("visibility","visible");
		$("#orgnavi .paanavigointi").hover(function(){
			var hoverElementsGroup = $(this).children(".bumber").add(  $(this).children(".contentBasket")  );
			hoverElementsGroup.children(".hover").stop().animate({opacity: 1},"fast");
			hoverElementsGroup.children(".normal").stop().animate({opacity: 0},"fast");
		},function(){
			var hoverElementsGroup = $(this).children(".bumber").add(  $(this).children(".contentBasket")  );
			hoverElementsGroup.children(".hover").stop().animate({opacity: 0},"fast");
			hoverElementsGroup.children(".normal").stop().animate({opacity: 1},"fast");	
		});
	}
	
	/*Set selected childMenu's of side navigation in open stance and remove all hover triggers
	  Also set list bullet in accordance if item has children. This is achieved by activating the
	  .hover image, which should be associated with a downward arrow via background-image-css*/
	var selDropdown = $(".sideNavItem .child-menu .selected").parents(".child-menu"); //find the .child-menu-ul which has been selected
	if (selDropdown.size() == 1) { //if a selected dropdown-menu has been found
		selDropdown.height( selDropdown.attr("height") ).css("visibility","visible");
		selDropdown.parent().parent().unbind("mouseover").unbind("mouseout");
		var cfC = selDropdown.parent().parent().children(".crossfadeContainer");
		if(ie) {
			cfC.children(".hover").css("visibility","visible");
			cfC.children(".normal").css("visibility","hidden");
		}
		else {
			cfC.children(".hover").css("opacity","1");
			cfC.children(".normal").css("opacity","0");
		}
	}
});

/** Iterates given collection and sums up individual elements heights
 * @param root a jQuery object to iterate 
 * @oak */
function getListHeight(root) {
	var h = 0;
	$(root).children().each(function() {
		h += $(this).height();
	});
	return h;
}

/** 
 * find every .child-menu, calculates the sum of heights of its children and stores it to be used in the dropdown animation 
 */
function navigation_storeDropdownHeight() {
	$(".child-menu").each(function(){   //store each .child-menu-elements height
		$(this).css("height","").parent().css("top", $(this).height());
		$(this).attr(  "height", getListHeight($(this))  ).css("height","0");
		
		//resize dropdown width according to widest element
/*		var maxW = Math.max(  $(this).width() , $(this).siblings(".selectedChild").width()  );
		$(this).width(maxW);
		$(this).siblings(".selectedChild").width(maxW);*/
	});
}
