var hoveey = {

  selected : '',

  reloadSelectedpage : function(e){

    $$('.cms_nav_menuitem_selected>a').each(function(e){
      e = e.next();
      if(e) // if there is a sub menu
      {
        // if the selection is not the current one, then hide it
        if(hoveey.selected != e)
        {
          if(hoveey.selected)
          {
            hoveey.selected.setStyle({left:'-9999em'});
          }
          // and show the clicked page
          e.setStyle({left:'auto'});

          // and set this back to the showing.
          hoveey.selected = e;
        }
      }
    });

  }


}

document.observe("dom:loaded", function() {
// Event.observe(window, 'load', function(){
  //console.log('Hoveey Loaded..');

  $$('.cms_menu_primary a+ul').each(function(e){
    e.observe('mouseout',function(i){
      hoveey.timout = setTimeout('hoveey.reloadSelectedpage()','1000');
    });
    e.observe('mouseover',function(i){
      clearTimeout(hoveey.timout);
    });
  });

  $$('.cms_menu_primary>a').each(function(e){

    e.observe('mouseout',function(i){
      hoveey.timout = setTimeout('hoveey.reloadSelectedpage()','1000');
    });

    //console.log('Beginning observe function for: ' + e.id);
    e.observe('mouseover',function(i){
      //console.log('Mouseover event fired for: ' + i.target.id);

      // clear any current countdowns
      clearTimeout(hoveey.timout);

      // get the events target
      i = i.target;

      // do nothing if this is the current menu
      if( hoveey.selected != i )
      {

        // if there is a current seletion, then hide it
        if(hoveey.selected)
          hoveey.selected.setStyle({left:'-9999em'});

        // get the ul (which is the parent of the sub menu item)
        hoveey.selected = i.next();

        if(hoveey.selected)
          // show the selected menu
          hoveey.selected.setStyle({left:'auto'});

      }

    });
  });

  // set the initial selected onpage load
  $$('.cms_nav_menuitem_selected>a').each(function(i){
    hoveey.selected = i.next();
  });
});

