window.addEvent('load', function() {
	new SlideList($E('ul', 'topmenu'), {transition: Fx.Transitions.linear, duration: 400});
});
var SlideList = 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.moveOut(this.current); }.bind(this));
			item.addEvent('click', function(event){ this.clickItem(event, item); }.bind(this));
		}.bind(this));

		this.back = new Element('li').addClass('background').adopt(new Element('div').addClass('left')).injectInside(this.menu);
		this.back.fx = this.back.effects(this.options);
		if(this.current) this.setCurrent(this.current);

	
	
			if(this.current) {
			// alert ("v14: " + this.back.offsetLeft );
				if (  this.back.offsetLeft <= 980) {
					this.back.fx.custom({
						left: [this.back.offsetLeft, this.back.offsetLeft]
					});
				
				}	
				else {
					// andere menu	
					this.back.fx.custom({
						left: [0, 0]
					});
				} // end else	
	
			}	

	
/*	
	// ACA
	alert ( "v12:" + this.back.offsetLeft );
	
	if(this.current ) {
	 "init current";
	}
	else {
	 "init not current";
	}
	
		if(this.current && this.back.offsetLeft > 500) {
			// andere menu	
			this.back.fx.custom({
				left: [0, 0]
			});
		}	

		else if(this.current ) {
			// andere menu	
			this.back.fx.custom({
				left: [this.back.offsetLeft, this.back.offsetLeft]
			});
		}	
*/		

	},

	setCurrent: function(el, effect){
 // alert ("setCurrent: " + el.offsetLeft);	
		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) {
	// alert ("clickItem...");
		if(!this.current) this.setCurrent(item, true);
		this.current = item;
		this.options.onClick(new Event(event), item);
	},

	moveBg: function(to) {
	// alert ("moveBg...");
		if(!this.current) return;
		this.back.fx.custom({
			left: [this.back.offsetLeft, to.offsetLeft]

		});
	},


	moveOut: function(to) {
	if(!this.current) {
		// alert ("not current");
	}
	else {
		    
		    if ( to.offsetLeft > 450 ) {
			 // alert ("andere menu"); // left: [this.back.offsetLeft, 0]

			this.back.fx.custom({
						left: [this.back.offsetLeft, 0]

			});

			return;

		    }

//  alert ("mouse out diese menu");
		    // diese Menu
			this.back.fx.custom({
				left: [this.back.offsetLeft, to.offsetLeft]

			});


		} // end else
	
	}



});

SlideList.implement(new Options);
