// JavaScript Document

	var cScrollBar = new Class({
		/* implementiert folgende klassen */
		Implements: [Options],

		/* setzt optionen 
		maxThumbSize=kleiste grösse des schiebereglers
		wheel wie sich das mausrad verhält*/
		options: {
			styleGesamtDiv : {left:'0'},
			scrollerStart: 0,
			minScrollerSize: 10,
			wheel: 30,
			mausRad: true
		},

		initialize: function(gesamtDiv,content, scrolleBar, scroller, goBack, goOneback, goNext, goOneNext, options ){
			this.setOptions(options);
			this.gesamtDiv = $(gesamtDiv);
			this.content = $(content);
			this.scrollbar = $(scrolleBar);
			this.scroller = $(scroller);
			this.goBack = $(goBack);
			this.goOneback = $(goOneback);
			this.goNext = $(goNext);
			this.goOneNext = $(goOneNext);
			this.beginner = true;
			this.ausrichtung = false;
			this.mode = 'vertical',
			this.slider = false;
			this.scrollerMaxSize = false;
		},
		
		update: function(){
			this.setScrollerStyle();
			var steps = (this.ausrichtung ? (this.content.getScrollSize().x - this.content.getSize().x) : (this.content.getScrollSize().y - this.content.getSize().y));
			if( this.ausrichtung && (this.content.getScrollSize().x > this.content.getSize().x) ){
				var isScroller = true;
			}
			else if (this.content.getScrollSize().y > this.content.getSize().y ){
				var isScroller = true;
			}
			else{
				var isScroller = false;
			}
			
			if( isScroller ){
				
				this.gesamtDiv.setStyles(this.options.styleGesamtDiv);
				var mode = this.mode;
				var ausrichtung = this.ausrichtung;
				var content = this.content;
				
				this.slider = new Slider(this.scrollbar, this.scroller,{
						steps: steps,
						mode: mode,
						onChange: function(step){
							var scrollerX = (ausrichtung ? step : 0);
							var scrollerY = (ausrichtung ? 0 : step);
							content.scrollTo(scrollerX,scrollerY);
						}
					}).set(this.scrollerStart);
				 
				this.docLeave();
				if( this.options.mausRad ) this.mausWheel();
				if( this.goBack ) { alert='hallo'; this.setGoBack();}
				if( this.goOneback ) this.setGoOneBack();
				if( this.goNext ) this.setGoNext();
				if( this.goOneNext ) this.setGoOneNext();
				this.setOverflow();
				this.setScrollerStyle();
			}
			
		},
		
		mausWheel: function(){
			var slider = this.slider;
			var wheel = this.options.wheel;
			$$(this.content, this.scrollbar).addEvent('mousewheel', function(e){	
				e = new Event(e).stop();
				if(parseInt(slider.step)!=((slider.step)-0))  { 
				 	var step = wheel;
				}
				else{
					var step = slider.step - e.wheel * wheel;
				}
				slider.set(step);					
			});
		},
		
		setGoBack: function(){
			var slider = this.slider;
			$(this.goBack).addEvent('mousedown', function(e){	
				e = new Event(e).stop();
				slider.set(0);					
			});
		},
		
		setGoOneBack: function(){
			var slider = this.slider;
			var wheel = this.options.wheel;
			var ein = false;
			$(this.goOneback).addEvent('mousedown', function(e){	
						ein = setInterval(function(){
						var step = slider.step - wheel;
						slider.set(step);
					},70);
			});
			
			$(this.goOneback).addEvent('mouseup', function(e){	
					clearInterval(ein);
			});
		},
		
		setGoNext: function(){
			var slider = this.slider;
			var ende = this.contentScrollSize;
			$(this.goNext).addEvent('mousedown', function(e){	
				e = new Event(e).stop();
				slider.set(ende);					
			});
		},
		
		setGoOneNext: function(){
			var slider = this.slider;
			var wheel = this.options.wheel;
			var ein = false;
			$(this.goOneNext).addEvent('mousedown', function(e){	
						ein = setInterval(function(){
						
						var step = slider.step + wheel;
						if( !step ) step = wheel;
						slider.set(step);
					},70);
			});
			
			$(this.goOneNext).addEvent('mouseup', function(e){	
					clearInterval(ein);
			});
		},
		
		goPosition:function(step){
			this.slider.set(step);
			
		},
		
		docLeave: function(){
			var slider = this.slider;
			$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
		},
		
		setAusrichtung: function(type){
			if( type == true ) {
				this.mode = 'horizontal'
				this.ausrichtung =  true;
			}
			else{
				this.mode = 'vertical'
				this.ausrichtung =  false;
			}
		},
		
		setOverflow: function(){
			this.content.setStyle('overflow','hidden');
		},
		
		setScrollerStyle : function(){
			if(this.ausrichtung){
				var scrollStyle = 'width';
				this.contentSize = this.content.offsetWidth;
				this.contentScrollSize = this.content.scrollWidth;
				this.scrollerMaxSize = this.scrollbar.offsetWidth;
			}
			else{
				var scrollStyle = 'height';
				this.contentSize = this.content.offsetHeight;
				this.contentScrollSize = this.content.scrollHeight;
				this.scrollerMaxSize = this.scrollbar.offsetHeight;
			}
			
			
			
			var contentRatio = this.contentSize / this.contentScrollSize;
			var scrollerSize = (this.scrollerMaxSize * contentRatio).limit(this.options.minScrollerSize, this.scrollerMaxSize);
			this.scroller.setStyle(scrollStyle,scrollerSize);
		}
		
		
	});

