MenuItem2= Class.create();
MenuItem2.FrameTemplate = new Template('<iframe src="javascript: void(0);" id="#{id}" name="#{id}" style="filter: progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0); opacity: 0; display: none; left: 0px; top: 0px; position: absolute; width: #{width}px; height: #{height}px; z-index: #{zIndex}" frameborder="0" scrolling="no"></iframe>')


Object.extend(MenuItem2.prototype, {
  _parent: null,
  _iframe: null,
  _cache: null,
  _son:null,
  _current:null,
  _options:null,
	_effect:null,
  initialize: function(current, options, parent)
  {
	  	  
	this._current = current;
	this._parent = parent;
	this._son=$A();
	 
	this._options=options;
	var ul=current.down().next('ul');
	if(typeof(ul)!="undefined")
	{
		if(current.down().hasClassName('menu-trigger'))
    	{
			
			ul.hide();
			ul.setStyle({position: 'absolute', left: '0px', top: '0px'});
    		var dim = ul.getDimensions();
			dim.id = ul.id + '-frame';
			new Insertion.After(ul, MenuItem2.FrameTemplate.evaluate(dim));
			this._iframe = $(dim.id);
		
			this._iframe.style.zIndex = parent._options.baseZIndex++;
			ul.style.zIndex = parent._options.baseZIndex++;
   			Event.observe(current.down(), 'mouseover', this.showSubmenu.bindAsEventListener(this));
			var son=this._current.down('ul').immediateDescendants();
			
			this._cache=this._iframe.hide.bind(this._iframe);
			var tmpOption=null;
			for (var index = 0; index < son.length; ++index)
			{
				 tmpOption=Object.extend(this.getOptions(), this._options || {});
				 
				 tmpOption.direction=tmpOption.direction2;
				 this._son.push(new MenuItem2(son[index],tmpOption,this));
			}
		}
		else
		{
			Event.observe(current.down(), 'mouseover', this.hideBrother.bindAsEventListener(this));
		}		
	}
	else
	{
		Event.observe(current.down(), 'mouseover', this.hideBrother.bindAsEventListener(this));
	}
	
 
  },
	 getOptions: function(){
return { 
	direction: Menu.Directions.down,
	direction2: Menu.Directions.right,
	baseZIndex: 1000, 
	appearEffect:Effect.Appear,
	appearOptions:{duration:'.25'},
	disappearEffect:Effect.Fade };
},
  hideBrother: function(event)
  {
    
	var son=this._parent._son.without(this); 

	son.each(function(t){
		var ul=t._current.down().next('ul'); 
		if(typeof(ul)!="undefined" && ul.hasClassName('submenu') && ul.visible()) 
		{
			if(this._options.disappearEffect==null)
			{
				ul.hide();
				t._cache();
			}
			else
			{
				var tmpOption=t._options.appearOptions;
				tmpOption.afterFinish=t._cache;
				new Effect.Fade(ul, tmpOption);
			}
		}
		
	}.bind(this)); 
	//cache les fils
	this._son.each(function(t){
		t.hideBrother(t); 
	});
  },
  showSubmenu: function(event)
  {
	var _ele = this._current.up().down();
	while(_ele)	{
		_ele.down('a').removeClassName('currentHover');
		_ele = _ele.next();
	}
	this._current.down().addClassName('currentHover');
	
    Position.prepare();

    var pos = Position.positionedOffset(this._current.down());
    var dim = this._current.down().getDimensions();
		
    var o = null;
    if(this._options.direction == Menu.Directions.down)
    {
        if(this._parent._options.offsetTop)
        pos[1] += this._parent._options.offsetTop;
      o = {left: pos[0]+'px', top: pos[1]+dim.height+'px'};
    } //92,193
    else if(this._options.direction == Menu.Directions.right)
    {
      if(this._parent._options.offsetLeft)
        pos[0] += this._parent._options.offsetLeft;
      o = {left: pos[0]+dim.width+'px', top: pos[1]+'px'};
    }
    
    this._current.down().next('ul').setStyle(o);
    this._iframe.setStyle(o);
    this._iframe.show();
	if(this._options.appearEffect==null)
	{
		this._current.down().next('ul').show();
	}
	else
	{
		new Effect.Appear(this._current.down().next('ul'), this._options.appearOptions );
	}	
	this.hideBrother(this); 
  }
});


Menu = Class.create();
Menu.Directions = {down: 1, right: 2};

Object.extend(Menu.prototype, {
  _container: null,
  _son: null,
  _over:null,
  _timer:null,
  _cache:null,
  _options:null,
  initialize: function(container, options)
  {
  	this._cache = this._realHideSon.bind(this);
   
	this._container = $(container);
	this._options=Object.extend(this.getOptions(), options || {});
	
	this._son=$A();
	
	var son=this._container.immediateDescendants();
	for (var index = 0; index < son.length; ++index)
	{
  		 this._son.push(new MenuItem2(son[index], this._options,this));
	}
	Event.observe(this._container, 'mouseover', this.over.bindAsEventListener(this));
	Event.observe(this._container, 'mouseout', this.hideSon.bindAsEventListener(this));
  },
  
  getOptions: function(){
	return { 
		direction: Menu.Directions.down,
		direction2: Menu.Directions.right,
		baseZIndex: 1000, 
		appearEffect:Effect.Appear,
		appearOptions:{duration:'.25'},
		disappearEffect:Effect.Fade };
	},
  
  over: function(t)
  {
    if (this._over==0)
	    this._over = 1;
  	clearTimeout(this._timer);
  	this._timer = null;
  },
  hideSon: function(t)
  {		  
   	if (this._timer==null)
 	{
   		this._timer = setTimeout(this._cache, "250");
   		//outTimer();
  	}
  },
  _realHideSon: function(e)
  {	  
	
	
	$A($('menu').getElementsByClassName('mainmenu-item')).each(function(s) {
		s.down('a').removeClassName('currentHover');
	});

	/*
	while(_ele)	{
		_ele.down('a').removeClassName('currentHover');
		_ele = _ele.next();
	}
	*/


  	this._son.each(function(t){
		t.hideBrother(t); 
	}); 
 	this._over=0;
  }
});
