window.addEvent("domready", function() {
  var small = 233;
  var normal = 305;
  var huge = 377;
  
  // make sure yellow is huge if url has an anchor named return
  if(document.location.hash.substring(1) == "return") {
    $('yellow').removeClass('normal');
    $('yellow').addClass('huge');
    
    $('green').removeClass('normal');
    $('green').addClass('small');
  }
  
  // select menuboxes
  var menuboxes = $$("#menu .menubox");
  
  // store original widths of menuboxes
  var p = [];
  menuboxes.each(function(menubox, i) {
    p[i] = menubox.getStyle("width").toInt();
  });
  
  // store previously hidden nav element
  var hiddenNav;
  
  var fx = new Fx.Elements(menuboxes, {wait: false, duration: 300, transition: Fx.Transitions.Sine.easeInOut});
  menuboxes.each(function(menubox, i) {  
  	menubox.addEvent("mouseenter", function(event) {
  		var o = {};
  		// o[1] = width of hovered element (e.g. yellow)
  		o[i] = {width: [menubox.getStyle("width").toInt(), huge]};
  		menuboxes.each(function(other, j) {
  		  // applies to all non hovered menuboxes
  			if(i != j) {
  			  // width of other menuboxes (e.g. green)
  				var w = other.getStyle("width").toInt();
  				// unless small, set non hovered menuboxes (e.g. o[2] i.e. yellow) to small
  				if(w != small) {
  				    o[j] = {width: [w, small]};
  				}				
  				// hide nav
  				var nav = other.getChildren()[0];
  				if(nav.getStyle("visibility") == "visible") {
    				nav.setStyle("visibility", "hidden");
    				hiddenNav = nav;
  				}
  			}
  		});
		if(window.ie6) {
		    // simulate :hover (enter)
		    menubox.getChildren()[0].setStyle("visibility", "visible");
		}  		
  		fx.start(o);
  	});
  });

  menuboxes.addEvent("mouseleave", function(event) {
  	var o = {};
  	menuboxes.each(function(menubox, i) {
  	  // set to original width
  		o[i] = {width: [menubox.getStyle("width").toInt(), p[i]]};
      	
      	if(window.ie6 && menubox.className != "menubox huge") {
    	    // simulate :hover (leave)
    	    menubox.getChildren()[0].setStyle("visibility", "hidden");
    	}  		
  	});
  	if(hiddenNav) {
  	    hiddenNav.setStyle("visibility", "visible");
  	}
  	fx.start(o);
  });
});
