/*
Nav script 2.0
Key to speed in IE is using fewer listeners.
Always unregister listeners when possible, and stop events as soon as they are trapped properly.

about scriptaculous' morph:
overflow must be set to 'hidden' on the UL tag for it to work (why?)
*/
/*
function loadNavListener () {
	document.observe('dom:loaded', function s() {
	    $$("li.mainMenu").each ( function s(x) {
	      // add listener
	      Event.observe(x, 'mouseover', navOver, false);	
	      x.firstChild.style.textDecoration = "none";
		});
	});
}

function hideMenu (listObj) {
	// set main menu background image to default
	listObj.style.backgroundImage = "";
    // hide sub menu
    listObj.down('ul.subMenu').style.display = "none";
    listObj.down('ul.subMenu').style.height = "0";
}

function showMenu (listObj) {
    var bg_file = "nav_over_bg.gif";
    // set the main menu item background image to highlight
    listObj.style.backgroundImage = "url('/images/" + bg_file + "')";
    // display sub menu
	listObj.down('ul.subMenu').style.display = "block";
	new Effect.Morph(listObj.down('ul.subMenu').id , {																			
	    style:'height:' + (((listObj.down('ul.subMenu').descendants().length)/2)*22) + "px",  // line height 16, top padding 2, bottom padding 3, border bottom 1
		duration:0.2
	});
	
}

function navOver (e) {
  // find the main menu item object
  var obj = Event.element(e).up('li.mainMenu');
  
  // loop through the main items, if its the one triggering the event, show its submenu, otherwise hide the other submenus
  $$('li.mainMenu').each (function s(x) {
    if (x.id == obj.id) {
	  // menu highlight
	  showMenu (x);
	  Event.stopObserving(x,'mouseover',navOver);
	  Event.observe(document.body, 'mouseover', navOut);
	} else {
	  if (x.style.backgroundImage.length) {
	      hideMenu (x);
	      Event.observe(x,'mouseover',navOver);
	  }
	}
  });
  
  Event.stop(e);
}

function navOut (e) {
  var theElement = Event.element(e);
  if (theElement.descendantOf('menuContainer') == false) {
      var menuObjs = $$("li.mainMenu");
      // hide the menus
      menuObjs.each( function s(x) {
	      hideMenu(x);
		  Event.observe(x, 'mouseover', navOver);
	  });	
      Event.stop(e);
  }
}


*/
function hideMenu (aObj) {
	// set main menu background image to default
	
	aObj.style.backgroundImage = "url('/images/nav_bg.png')";
    // hide sub menu
    $("SubMenu_" + aObj.id.split("_")[1]).style.display = "none";
    //listObj.down('ul.subMenu').style.height = "0";
    //$('search_cat').show();
}

function showMenu (aObj) {
    // set the main menu item background image to highlight
    // slightly different than the home page
    aObj.style.backgroundImage = "none";
    aObj.style.backgroundColor = "#3F9800";
    var theID = aObj.id.split("_")[1];
    /*
    if ((theID == "2") || (theID == "1")) {
	    $('search_cat').hide();
    }
    */
    // display sub menu
	 $("SubMenu_" + theID).style.display = "block";	
	/*
	new Effect.Morph(listObj.down('ul.subMenu').id , {																			
	    style:'height:' + (((listObj.down('ul.subMenu').descendants().length)/2)*22) + "px",  // line height 16, top padding 2, bottom padding 3, border bottom 1
		duration:0.2
	});
	*/
}

function navOver (e) {
  // find the main menu item object
  var obj = Event.element(e);
  var itemToShow;
  
  // loop through the main items, if its the one triggering the event, show its submenu, otherwise hide the other submenus
  $$('.mainMenuLink').each (function s(x) {
    if (x.id == obj.id) {
	  // menu highlight
	  itemToShow = x;
	  
	} else {
	  
	  if (x.style.backgroundImage.length > 0) {
	      hideMenu (x);
	      Event.observe(x,'mouseover',navOver);
	  }
	  
	}
  });
  
  showMenu (itemToShow);
  Event.stopObserving(itemToShow);
  Event.observe(document.body, 'mouseover', navOut);
  
  Event.stop(e);
}

function navOut (e) {
  var theElement = Event.element(e);
  if (theElement.descendantOf('topnav') == false) {
      var menuObjs = $$(".mainMenuLink");
      // hide the menus
      menuObjs.each( function s(x) {
	      hideMenu(x);
		  Event.observe(x, 'mouseover', navOver);
	  });	
      Event.stop(e);
  }
}

/* the main loader */
	
var column = 236;
var widths = new Array (2.3,1,1.5,1.01);
	
document.observe('dom:loaded', function s() {
	var lp = 0;
	var linksPerColumn = 9;
	var verticalOffset = 6;
	var horizontalOffset = 12;
	var linkHeight = 21;
	var columnWidth = 170;
	
    $$(".mainMenuLink").each ( function t(x) {
	    var topPos = 0;
	    var leftPos = 0;
	    var lp2 = 0;
  	    // first we juggle
  	    var theUL = x.up().down('ul');  	    
  	    theUL.childElements().each ( function v(y) {
	  	    y.style.position = "absolute";
	  	    topPos = (lp2 - (Math.floor(lp2/linksPerColumn) * linksPerColumn)) * linkHeight + verticalOffset;
	  	    leftPos = horizontalOffset + Math.floor(lp2/linksPerColumn) * columnWidth;
	  	    y.style.top = topPos + "px";
	  	    y.style.left = leftPos + "px";
	  	    lp2++;	   	    
   	    });
	    
	    
	  // add listener
      Event.observe(x, 'mouseover', navOver, false);	
      x.style.textDecoration = "none";
	      
      var theLI = $("SubMenu_" + x.id.split("_")[1]);
      theLI.style.left = (lp * column) + "px";	      
      theLI.style.width = (Math.floor(widths[lp] * (column))) + "px";
	      
      lp = lp + 1;
	});
	
	
	// top free shipping logo loader
	//$("freeShipMap").observe('click', headerShippingPopup);	
	
});