var EMenu = new Class({
    Implements: [Events, Options],
    
    options: {
        autocalc: true,
        image_speed: 400,
        menu_speed: 400
    },
    
    
    initialize: function(id, options){
        this.setOptions(options);
        this.over = false;
        var menu = $(id), 
            first_list_lis = menu.getFirst().getChildren(),
	        count = first_list_lis.length,
	        width = menu.getStyle('width').toInt(),
            li_width = Math.floor(width / count),		
            img_opacity = new Fx.Tween(menu.getElement('.bild'), 'opacity', {
                link: 'cancel',
                duration: this.options.image_speed
            });
					
        menu.getFirst().addEvents({
            mouseenter: function(){
                img_opacity.start(0.7);
            },
            mouseleave: function(){
                img_opacity.start(1);			
            }
        });
        

				if (first_list_lis[0]){
        	first_list_lis[0].addClass('first');
        }
        first_list_lis.each(function(el){
            
            if (this.options.autocalc) {
                var new_width = li_width;
                
                $each(el.getStyles('border-left', 'border-right', 'padding-left', 'padding-right', 'margin-left', 'margin-right'), function(val){
                    new_width -= val.toInt();
                });
                
                el.setStyle('width', new_width);
            }
            
            var second_list = el.getElement('ul');
            if (second_list){
            	//second_list.setOpacity(1).getFirst().addClass('first');
				second_list.getFirst().addClass('first');
            }

			
			if (menu.getElement('.active ul')) { 
			var active_opacity = new Fx.Tween(menu.getElement('.active ul'), 'opacity', {
                link: 'cancel',
                duration: 0
            });	
			}

            
            if (second_list){
	            var fx = new Fx.Tween(second_list, 'opacity', {
  	              link: 'cancel',
    	            duration: this.options.menu_speed
      	      });
      	    }
			
            
            var that = this;
			
			
            
            el.addEvents({
                mouseenter: function(event){
					if (menu.getElement('.active ul')) { 
					if (menu.getElement('.active ul') != el.getElement('ul')) { active_opacity.start(0) };
					}
					
					if (fx){
                    fx.start(1).chain(function(){
                        that.fireEvent('onMenuShow', this);					
                        // (function(){that.over = true;}).delay(50); // delay prevents race conditions
                    }.bind(this));
                    this.addClass('hover');		
					
                }
                },
                mouseleave: function(){
				//if (menu.getElement('.active ul') != el.getElement('ul')) { 				
						if (fx){
                    fx.start(0).chain(function(){
                        if (that.over) {
                            that.fireEvent('onMenuHide', this);
                            that.over = false;
                        }
                    }.bind(this));
             }
                    this.removeClass('hover');
					if (menu.getElement('.active ul')) { 
					active_opacity.start(0);
					}
					// if (menu.getElement('.active ul')) { active_opacity.start(1); }
                //}
				}
				
            });
            
        }.bind(this));
    }
});