window.addEvent('domready', function(){

  var ref = $('menu');
  var menus = ref.getElement("UL").getChildren();
  var pointer = $('menuhover');
  var c;
  var li;
  if (ref.getElement('LI.actif'))
    var d = ref.getElement('LI.actif').getCoordinates(ref);
  else
    var d = ref.getElement("LI").getCoordinates(ref);
  
  menus.removeClass("rollover");
  $$('UL.sousmenu').setStyle('display', 'block');
  $$('UL.sousmenu').fade('hide');
  
  
  pointer.getElement('.texte').setStyle("background-position", (- d.left + 5 )+'px 4px');
  menus.addEvent('mouseenter', function(){
    c = this.getCoordinates(ref);
    li = this;
    
    if (c.left == d.left)
    {
      if (li.getElement('UL.sousmenu'))
        li.getElement('UL.sousmenu').fade('in');
      return ;
    }
    pointer.getElement('.texte').tween('background-position', (- c.left + 5)+'px 4px');
    pointer.set('morph', {
      onComplete: function(){
        if (li.getElement('UL.sousmenu'))
          li.getElement('UL.sousmenu').fade('in');
      }
    });
    pointer.morph({
      'left': c.left - 5,
      'width': c.width - 6
    });
  });
  
  menus.addEvent('mouseleave', function(){
    pointer.set('morph', { onComplete: function(){ }});
    pointer.getElement('.texte').tween('background-position', (- d.left + 5 )+'px 4px');
    if (this.getElement('UL.sousmenu'))
      this.getElement('UL.sousmenu').fade('out');
    pointer.morph({
      'left': d.left - 5,
      'width': d.width - 6
    });
  });
  
});

