var xMenu = Class.create();
xMenu.prototype = {
	
	timeout : null,
	id : null,
	object : null,
	position : null,
	height : null,
	menuObject : null,
	menuItems : null,
	
	initialize: function(elt, mnu) {

  		this.object = elt;
  		this.menuItems = mnu;
  		this.position = this.object.viewportOffset();
  	    var menuObject = new Element('div', {'class': 'dropmenu'});
  	    	  
        menuObject.setStyle({
          zIndex  : '100',
          display : 'none'
        });
		
		this.menuItems.each(function(item) {
  		      a = new Element(item.type, item.attributes).update(item.content);
  		      menuObject.insert({bottom: a});
	    });
	    this.menuObject = menuObject;
  	    
  	    $(document.body).insert({bottom:this.menuObject});
  		    		
  		this.mouseoverlistener = this.showPopup.bindAsEventListener(this);
  		this.mouseoutlistener = this.hidePopup.bindAsEventListener(this);

  		this.clicklistener = this.hidePopup.bindAsEventListener(this);
  		this.clicklistener2 = this.hide.bindAsEventListener(this);
		  
		Event.observe(this.object, "click", this.mouseoverlistener);
  		Event.observe(document, "click", this.clicklistener2);
  		Event.observe(this.object, "mouseover", this.mouseoverlistener);
  		Event.observe(this.object, "mouseout", this.mouseoutlistener);
  		Event.observe(this.menuObject, "mouseover", this.mouseoverlistener);
  		Event.observe(this.menuObject, "mouseout", this.mouseoutlistener);
  	},
    showPopup : function(e){
        
      this.height = this.object.getHeight();	
  	  this.position = this.object.cumulativeOffset();

  	  this.menuObject.setStyle({
          position: 'absolute', 
          top : this.position[1] + this.height + 'px',
          left : this.position[0] + 'px',
          zIndex : 9999
  	  });
   	  clearTimeout(this.timeout);
      if(this.menuObject.style.display == 'none'){
  				    var self = this;
              this.timeout = setTimeout(function(){new Element.show(self.menuObject); },200); 
  		}
    },
    hidePopup : function(){
      if(this.menuObject.style.display == 'none'){
          clearTimeout(this.timeout);
      }else{
         var self = this;
         this.timeout = setTimeout(function(){new Element.hide(self.menuObject); },400);
	  }
    },
    hide : function(){
        Element.hide(this.menuObject);
    }
}
