/**
 * MenuScroll.js V 1.0
 * developed by goodRanking 2009
 * autor: M.Hoffmann
 * requires mootools.js V1.2
**/
window.addEvent('domready', function(){
	if($chk('menu3')){
		var menuScroll = new MenuScroll('menu3', 8, 102);
		menuScroll.initMS();
	}
});

function MenuScroll(menuId, maxView, step){
	// Properties
	this.menu = $(menuId);
	this.prev = $(menuId+'prev');
	this.next = $(menuId+'next');
	this.maxView = maxView;
	this.step = step;
	this.posLeft = 0;
	this.scrollIndex = 0;
	this.scrollDiff = 0;
	if($chk(this.menu)) this.menuLength = this.menu.getChildren("a").length;
	else this.menuLength = 0;
	this.FXscroll = new Fx.Scroll(menuId+'text', {
		wait: false,
		duration: 500,
		transition: Fx.Transitions.Quad.easeInOut
	});
	// Methods
	this.initMS = initMS;
	this.scrollLeft = scrollLeft;
	this.scrollRight = scrollRight;
}

function initMS(){
	var ms = this;
	var menu = this.menu;
	var FXscroll = this.FXscroll;
	if(this.maxView < this.menuLength){		
		// Cookie-Event auf alle menu3-Eintraege legen
		$each(this.menu.getChildren("a"), function(elm, index){
    		elm.addEvent('click', function(){
				Cookie.write("menuPos", (index+1));
				Cookie.write("scrollIndex", ms.scrollIndex);
			});
		});
		// Cookies loeschen bei allen links ausser menu3
		if(Cookie.read("scrollIndex")!=""){
			$each($$("a"), function(elm, index){
				switch(elm.getParent().get("id")){
					case 'menu3':
					break;
					
					default:
						elm.addEvent('click', function(){
							Cookie.dispose("scrollIndex");
						});
					break;
				}
			});
		}
		this.scrollIndex = this.menuLength - this.maxView;
		this.scrollDiff  = this.scrollIndex;
		menu.style.width = (this.menuLength * this.step)+"px";
		this.prev.style.display = "block";
		this.next.style.display = "block";
		this.next.addEvent('click', function(event){
			event = new Event(event).stop();
			ms.scrollRight(false);
		});
		this.prev.addEvent('click', function(event){
			event = new Event(event).stop();
			ms.scrollLeft(false);
		});
		// Cookie auslesen und ggf. scrollen
		if($chk(Cookie.read("scrollIndex"))){
			var scrollIndex = parseInt(Cookie.read("scrollIndex"));
			while(scrollIndex<this.scrollIndex){
				this.scrollRight(true);
			}
		}
		
	}
}

function scrollLeft(jump){
	if(this.scrollIndex<this.scrollDiff){
		var pos = (this.posLeft+this.step)+'px';
		this.posLeft = parseInt(pos);
		if(jump) this.FXscroll.set(Math.abs(parseInt(pos)),0);
		else this.FXscroll.start(Math.abs(parseInt(pos)),0);
		this.scrollIndex++;
	}
}

function scrollRight(jump){
	if(this.scrollIndex>0){
		var pos = (this.posLeft-this.step)+'px';
		this.posLeft = parseInt(pos);
		if(jump) this.FXscroll.set(Math.abs(parseInt(pos)),0);
		else this.FXscroll.start(Math.abs(parseInt(pos)),0);
		this.scrollIndex--;
	}
}
