// jQuery multi-tiered menu built off single <ul> node by Christopher Najewicz <cnajewicz@futuremediainteractive.com>

var buildMenu = function() {
	
	if(typeof(bInEditMode)!=='undefined' && bInEditMode) return false;
		
	var menuImageDir = '/img/menu/';

	// array of objects for top-level menu
	var menuImages = [ 
		{	"name":"undergraduateadmissions"	},
		{	"name":"graduateadmissions"			},
		{	"name":"academics"					},
		{	"name":"campus"						},		
		{	"name":"officesandresources"		},		
		{	"name":"athletics"					}		
	];
	
	 /*
		this will loop through all the menu items and append the max chars of each menu to the
		menuImages object
	 */	
	var findTheTallest = function()
	{
		var mainNav = '#mainnav';
		var tallest = 0;
		
			$(mainNav).css({'position':'absolute','top':'0','left':'-9999px'});
			
			$(mainNav+' ul').css({'display':'block'});
			$(mainNav+' li.topmost').each(function(count) {
				
				// important! we must reset this var for every topmost LI so that we can track tallest properly (chief)
				tallest = 0;
				
				if(count > (menuImages.length-1))
					{
						if(window.console) console.log('There was an error in buildMenu with too many main nav nodes');
						return false;
					}
			
					$(this).find('ul').each(function() {
						if($(this).height() > tallest) tallest = $(this).height()
					});
			
				menuImages[count].height = tallest;
			//DEBUGGIN
			//	if(window.console) console.log("menu #"+count+" = "+menuImages[count].height);
			}); // end of each for li.topmost
			
			$(mainNav).css({'position':'relative','top':'0','left':'0'});
			$('ul',mainNav+' li.topmost').css('display','none');
	}; // end of findTheTallest()

	// pre-load the images and their over-states
  for (var num = 0; num < menuImages.length; num++) {
		var tmpFileName = menuImageDir + 'nav_' + menuImages[num].name;
		menuImages[num].off = new Image();
		menuImages[num].off.src = tmpFileName + '.gif';
		menuImages[num].over = new Image();
		menuImages[num].over.src = tmpFileName + '-o.gif';
	}
	
	var swapImage = function(img) {

		var findImage = function(needle) {
			for(var num=0;num < menuImages.length; num++) {
				if( menuImages[num].over.src.match(needle) || menuImages[num].off.src.match(needle) )
					{ 	
						return num;	break;
					}
			}
			return 0;
		}; // end of findImage

		var imageIndex = findImage(img.attr('src'));
		
		if(img.attr('src').match('-o'))
			img.replaceWith(menuImages[imageIndex].off);
		else
			img.replaceWith(menuImages[imageIndex].over);
	}; // end of swapImage
	
	
	//pre-load the background image
	var tmpImage = new Image();
	tmpImage.src = menuImageDir + 'all_bgs.jpg';
		

	topMostHoverConfig = { // config for graphical top most menu items
		sensitivity: 3,
		interval: 100,
		timeout: 250,
		over: function() {
			var maxLevelItems = $(this).find('.level1 > li').length;
			var effectDelay = 100;
			var fatherUL = $('.level1', $(this)); 
			var thisIndex = $('#menu li.topmost').index(this);
			var theHeight = menuImages[thisIndex].height;
			var currentLevel = 1;
			
			swapImage($(this).find('img'));
			
			$(this).find('li').each(function() {
				if ($(this).find('ul:first > li').length > maxLevelItems)
					maxLevelItems = $(this).find('ul:first > li').length;
				
				$(this).hoverIntent(function() {
					$(this).addClass('hover');
					$(this).parent().attr('class').match(/level([1-4]{1})/);
					currentLevel = Number(RegExp.$1);
					fatherUL.removeClass('level'+String(currentLevel+1)+'_bg');
				}, // over
				function() {
					$(this).removeClass();
				}); // out
				
				$(this).find('ul').each(function() {
					var theLevel = $(this).attr('class').match(/2|3|4/);
					var levelBgClass = 'level' + theLevel + '_bg';
					var hoverConfig = // hover intent config for menu items
						{
							sensitivity: 8,
							interval: 200,
							over: function() {
								fatherUL.addClass(levelBgClass);
								$(this).parent().attr('class').match(/level([1-4]{1})/);
								currentLevel = Number(RegExp.$1);

								$(this).addClass('hover').find('.level'+theLevel).show('fast');
							}, // hover over
							timeout:250,
							out: function() {
								$(this).removeClass().find('.level'+theLevel).hide('fast',function() {
									if($(this).parent().parent().find('.hover').length === 0)
										{
											// hack for top level
											if(currentLevel==4) currentLevel--;
											fatherUL.removeClass('level'+(currentLevel+1)+'_bg');
										}
								});
							} // hover out
						}; // end of hoverConfig
					
					$(this).height(theHeight).parent().hoverIntent(hoverConfig);
				
				}); // end of each for UL item
			}); // end of each for LI item
	
			fatherUL.
				height('0px').
				css('padding-top', '0').
				css('padding-bottom', '0').
				addClass('level1').
				animate(
						{
							paddingTop: "10px",
							paddingBottom: "10px",
							height: theHeight + "px",
							opacity: 1
						},
						150,
						'swing',
						function() { $(this).addClass('level1_bg'); }
						); // hack for new jQuery 1.3 slideDown() end of animate

		}, // end of over() 

		out: function() {
			var fatherUL = $(this).find('.level1');
			swapImage($(this).find('img'));
			fatherUL
		.hide()
		.attr('class', 'level1');
		} // end of out()
	} // end of topMostHoverConfig 

	
	//find the tallest menu UL
	findTheTallest();

	// setup of nested UL menu
	$('.topmost').hoverIntent(topMostHoverConfig);

} // end of buildMenu()