var Menue = new Class(
{
	Implements : [Events,Options],
  
	options    : {},
  menues     : new Array(),
  
	offset     : {
		'x'      : 10,
		'y'      : 15
  },
  
  initialize : function(options) {},
  
  add : function(options) {
		if(options.id && options.button) {
			if($('sub-'+options.id) != undefined) {
				var $m = $('sub-'+options.id);
			} else {
				var $m = null;
			}
	  
			if(options.link) {
				var $l = options.link;
				options.button.setStyle('cursor','pointer');
			} else {
				var $l = null;
			}
	  
	  var $menue = {
		'id'     : options.id,
		'button' : options.button,
		'menue'  : $m,
		'link'   : $l,
		'bover'  : false,
		'mover'  : false
	  };
	  
	  if($m !== null) {
	    var $this = this;
			
		$m.addEvents({
		  'mouseenter' : function() {
				$menue.mover = true;
		  }.bind($m),
			  
		  'mouseleave' : function() {
				$menue.mover = false;
			$this.isOut($menue);
		  }.bind($m)
		});
	  }
	  
	  this.prepareButton($menue);
	  this.menues.push($menue);
	} else {
		Hemmers.trace('no id / button');
	}
  },
  
  prepareButton : function(obj) {
    var $this = this;
	
	obj.button.addEvents({
	  'mouseenter' : function() {
	    obj.bover = true;
		$this.menue(obj);
		$this.mouseover(this);
	  }.bind(obj.button),
	  
	  'mouseleave'  : function() {
		obj.bover = false;
		
		if(obj.link !== null) {
		  $this.mouseout(this);
		} else {
		  $this.isOut(obj);
		}
	  }.bind(obj.button)
	});
	
	if(obj.link !== null) {
	  obj.button.addEvent('mousedown',function() {
	    window.location = obj.link; 
	  });
	}
  },
  
  isOut : function(obj) {
    var $this = this;
	
	if(obj.menue === null) {
	  return false;
	}
	
	var $t = (function() {
	  $clear($t);
	  
	  if(obj.mover === false && obj.bover === false) {
	    obj.menue.setStyle('display','none');
		$this.mouseout(obj.button);
	  }
	}).periodical(50);
	
	return true;
  },
  
  menue : function(obj) {
		if(obj.menue === null || obj.menue.getStyle('display') == 'block') {
			return false;
		}
		
		var $s = obj.button.getSize();
		
		var $x = ((obj.id-1)*$s.x)+this.offset.x;
		var $y = $s.y+this.offset.y;
		
		obj.menue.setStyles({
			'top'			: ($y+30)+'px',
			'left'		: $x+'px',
			'display' : 'block'
		});
	
		var $fx = new Fx.Morph(obj.menue,{
			'duration'   : 200,
			'transition' : Fx.Transitions.Sine.easeOut
		});
		
		$fx.start({'top':$y+'px'});
		
		return true;
  },
  
  mouseover : function(btn) {
		if(btn != undefined) {
			btn.setStyle('background-position','0px -100px');
		}
  },
  
  mouseout : function(btn) {
    if(btn != undefined) {
      btn.setStyle('background-position','0px 0px');
    }
  }
});
