var HorizontalSlideList = new Class({
	initialize: function(menu, options) {
		this.setOptions(this.getOptions(), options);
		
		this.menu = $(menu), this.current = this.menu.getElement('li.current');
	
		
		this.menu.getElements('li').each(function(item){
			item.addEvent('mouseover', function(){ this.moveBg(item); }.bind(this));
			item.addEvent('mouseout', function(){ this.moveBg(this.current); }.bind(this));
			//item.addEvent('click', function(event){ this.clickItem(event, item); }.bind(this));
		}.bind(this));
				
		
		
		
		this.itemSelectedOnLoad = false;
		if(this.current) {
		    this.setCurrent(this.current);
		    this.itemSelectedOnLoad = true;
		}
		else
		    this.menu.parentNode.addEvent('mouseout', function(e){ this.checkClear(e); }.bind(this));
	},
	
	setCurrent: function(el, effect){
		if (!this.back) {
			this.back = new Element('li').addClass('background').adopt(new Element('div').addClass('left')).injectInside(this.menu);
			this.back.fx = this.back.effects(this.options);
		}
		this.back.setStyles({left: (el.offsetLeft)+'px', width: (el.offsetWidth)+'px'});
		(effect) ? this.back.effect('opacity').set(0).start(1) : this.back.setOpacity(1);
		this.current = el;
	},
	
	getOptions: function(){
		return {
			transition: Fx.Transitions.sineInOut,
			duration: 500, wait: false,
			onClick: Class.empty
		};
	},

	clickItem: function(event, item) {
		if(!this.current) this.setCurrent(item, true);
		this.current = item;
		this.options.onClick(new Event(event), item);
	},

	moveBg: function(to) {
		//if(!this.current) return;
		
		if( !this.itemSelectedOnLoad ) {
		    $clear(this.clearFunc);
		    if(!this.current && to!=null && to.id != null ) {
		        this.setCurrent(to, true);
		        this.current = to;
		    }
		}
		this.back.fx.custom({
			left: [this.back.offsetLeft, to.offsetLeft],
			width: [this.back.offsetWidth, to.offsetWidth]
		});
	},
	
	checkClear: function(e) {
	    
	    if (window.event) 
	        e = window.event;  
	        
	    var srcEl = e.srcElement? e.srcElement : e.target; 
	    
	    //needs to be changed to be reusable, horizontalmenu should not be hardcoded
	    if( srcEl.id == "horizontalmenu"){
	        if(this.current)
                this.clearFunc = (function(){ this.clearCurrent(); }).delay(100, this);
	    }
	        
	},
	clearCurrent: function() {
	    if(this.current) {
	        this.current = null;
	        this.back.effect('opacity').set(1).start(0);
	    }
	}
});

HorizontalSlideList.implement(new Options);