var SlideList = new Class({
	initialize: function(menu, options) {
		this.setOptions({
		  transition: Fx.Transitions.sineInOut,
		  duration: 500,
		  direction: 'horizontal'
		}, options);
		
		this.options.wait = false;
		
		this.menu = $(menu); 
    	this.current = this.menu.getElement('li.current');
		
		this.menu.getElements('li').each(function(item){
		  item.addEvent('mouseover', function(){ 
			  this.moveBg(item); 
			  
			  if(this.timer){ $clear(this.timer); } 
		  }.bind(this));
		  
		  if(this.current){
		  	item.addEvent('mouseout', function(){
			  this.moveBg(this.current); 
			}.bind(this));
		  }
		  else{
			item.addEvent('mouseout', function(){
			  this.timer = setTimeout(function(){ this.back.fadeTo(0); }.bind(this), 1000);
			}.bind(this));
		  }
		}.bind(this));
				
		this.back = new Element('li').addClass('background').adopt(new Element('div').addClass('left')).injectInside(this.menu);
		
    	this.back.fx = new Fx.Morph(this.back, this.options);
		
		if(this.current) this.setCurrent(this.current);
		else{
			this.back.setOpacity(0);
		}
	},
	
	setCurrent: function(el){
		if(this.options.direction=='horizontal'){
		  this.back.setStyles({left: (el.getCoordinates().left)+'px', width: (el.offsetWidth)+'px'});
		}
		else{
		  this.back.setStyles({top: (el.offsetTop)+'px', width: (el.offsetWidth)+'px'});
		}
		this.current = el;
	},

	moveBg: function(to) {
		if(!this.current){
			this.current = this.menu.getElement('li');
			this.setCurrent(this.current);
		}
		
		this.back.fadeTo(1, function(){}, 200);
		
		if(this.options.direction=='horizontal'){
		  this.back.fx.start({
       		'left': [this.back.getCoordinates().left, to.getCoordinates().left],
       		'width': [this.back.offsetWidth, to.offsetWidth]
		  });
		}
		else{
		  this.back.fx.start({
		    'top' :[this.back.offsetTop, to.offsetTop],
		    'width': [this.back.offsetWidth, to.offsetWidth]
		  });
		}
	}
	
});
SlideList.implement(new Options, new Events);