// JavaScript Document
var ScrollBar = new Class({
						   
			Implements:[Options, Events],
			
			options:{
				
				content:Element,
				scrollbar:Element,
				handle:Element,
				horizontal:false,
				handleResize:false,
				ignoreMouse:false
			},
			
			initialize: function(options){
				this.setOptions(options);
				this.content = this.options.content;
				
				this.scrollbar = this.options.scrollbar;
				
				this.handle = this.options.handle;
				
				if(this.makeScrollBar())
					this.makeSlider();
				this.listen2Mouse();
			},
			
			update:function(){
				
				if(this.slider)this.slider.detach();
				this.stopListening();
				if(this.makeScrollBar())
					this.makeSlider();
				else
					this.slider = false;
				this.listen2Mouse();
				
			},
			
			makeScrollBar:function(){
				if(this.options.horizontal){
    			if(this.content.getScrollSize().x/this.content.getSize().x<=1){
    				
    				this.hide();
    				
    				return false;
    			}
    			else if(this.options.handleResize){
      			//this.scrollbar.setStyles({'width':this.content.getSize().x+'px'});
      			this.handle.setStyles({'width':this.scrollbar.getSize().x/this.content.getScrollSize().x*this.content.getSize().x+'px'});
    			}
  			}else{
  				
  				if(this.content.getScrollSize().y/this.content.getSize().y<=1){
  					this.hide();
  					return false;
  				}
					else if(this.options.handleResize){
      			//scrollbar.setStyles({'height':content.getSize().y+'px'});
      			this.handle.setStyles({'height':this.scrollbar.getSize().y/this.content.getScrollSize().y*this.content.getSize().y+'px'});
    			}
  			}
  			
  			this.show();
  			
  			return true;
			},
			
			show:function(){
				this.disabled = false;
				this.scrollbar.setStyles({'display': 'block', 'visibility':'visible'});
  			this.handle.setStyles({'display': 'block', 'visibility':'visible', 'cursor':'pointer'});
  			this.content.addClass('withScroll');
			},
			
			hide:function(){
				this.disabled = true;
				if(this.content.hasClass('widthScroll'))this.content.removeClass('withScroll');
				this.scrollbar.setStyles({'display': 'none', 'visibility':'hidden'});
  			this.handle.setStyles({'display': 'none', 'visibility':'hidden'});
  			
			},
			
			makeSlider:function(){
				
				var steps = (this.options.horizontal?(this.content.getScrollSize().x - this.content.getSize().x):(this.content.getScrollSize().y - this.content.getSize().y));
				this.slider = new Slider(this.scrollbar, this.handle, {
					steps: steps,
					mode: (this.options.horizontal?'horizontal':'vertical'),
					onChange: function(step){
						
						var x = (this.options.horizontal?step:0);
						var y = (this.options.horizontal?0:step);
						this.content.scrollTo(x,y);
					}.bind(this)
				}).set(0);
				
			},
			
			listen2Mouse:function(){
				if(this.options.ignoreMouse || this.disabled)return;
				$$(this.content, this.scrollbar).addEvent('mousewheel', function(e){
					e = new Event(e).stop();
					var step = this.slider.step - e.wheel * 30;
					this.slider.set(step);
				}.bind(this));
			},
			stopListening:function(){
				$$(this.content, this.scrollbar).removeEvents('mousewheel');
			}
});
						