// init 

$(document).ready (
	function() {
		window.hideTimer = null;
		window.activeElement = null;
		window.clearMenu = function(id, topLevel) {
			$(id).hide();
			$(topLevel).removeClass('showMenu');
		}

		// top level items
		$('#primary-nav').find('LI').each(
			function() {
				// if there is no corresponding menu to display, move on
				
				// show menus
				$(this).bind('mouseover', 
					function() {
						clearTimeout(window.hideTimer);
						
						var sId = $(this).attr('id');
						var oMenu = $('#sm-' + sId);
						var bHasMenu = oMenu.size() > 0;
						
						// clear former state
						if(window.activeElement) {
							window.clearMenu('#sm-' + window.activeElement, '#' + window.activeElement);
							//$('#' + window.activeElement).removeClass('showMenu');
						}
						
						if(bHasMenu) {
							oMenu.show();
							window.activeElement = sId;
						} else {
							window.activeElement = null;
						}
						$(this).addClass('showMenu');
						
					}
				);
				
				// hide menus 
				$(this).bind('mouseout', 
					function() {
						var sId = $(this).attr('id');
						if(window.clearMenu) {
							window.hideTimer = setTimeout("window.clearMenu('#sm-" + sId + "', '#" + sId + "')", 2000);
						}
					}
				);
				
			}
		);
		
		
		
		$('.secondary-nav').find('LI').each(
			function() {
				// mouseover sub menu 
				$(this).bind('mouseover', 
					function() {
						clearTimeout(window.hideTimer);
					}
				);
				
				// mouse off sub menu
				$(this).bind('mouseout', 
					function() {
						var sParentId = $(this).parent().parent().attr('id');
						var sTopLevelLI_Id = sParentId.replace('sm-', '');
						if(window.clearMenu) {
							window.hideTimer = setTimeout("window.clearMenu('#" + sParentId + "', '#" + sTopLevelLI_Id + "')", 2000);
						}
					}
				);
			}
		);
		
		// add mouse events to .rollover images
		$('.img_rollover').each (
			function() {
				$(this).bind('mouseover', 
					function() {
						var sImg = $(this);
						var sSrc = sImg.attr('src').replace(/(.jpg|.gif|.png)/, '_on$1');
						sImg.attr( {src: sSrc} );
					}
				);
				$(this).bind('mouseout',
					function() {
						var sImg = $(this);
						var sSrc = sImg.attr('src').replace(/_on./, '.');
						sImg.attr( {src: sSrc} );
					}
				);
			}
		)
		
		
	}
);