var Scrollbar = new Class({

	options : {
		mode : 'vertical',
                sizeFixer : 0,
				container: false,
				wheelSpeed : 15,
				ieSizeFixer : false
		},
		
	initialize: function(element, containerHeight, bar, slider, options){
		this.setOptions(options);
		this.element = $(element);
		this.currentStep = 0;
		if(this.options.container){
			this.container = $(this.options.container);
			this.container.setStyles({
				'position' : 'relative',
				'height' : containerHeight + 'px',
				'overflow' : 'hidden',
                 'margin' : 0,
				 'width' : '490px'
			});
		}else{
		    this.container = new Element('div');
                this.container.setStyles({
				'position' : 'relative',
				'height' : containerHeight + 'px',
				'overflow' : 'hidden',
                'margin' : 0,
				'width' : '490px',
				'float':'left'
			});
		  //this.container.injectBefore(element);
		  this.container.inject(element, 'before')
		
		  this.container.grab(element);
		}
		this.element.addEvent('mousewheel', this.wheelScroll.bindWithEvent(this) );
			
		
		this.prop = '';
		var elSize, conSize;
		if(this.options.mode == 'vertical'){
			this.prop = 'margin-top';
			elSize = this.element.getSize().y + this.options.sizeFixer;
			
		}else{
			this.prop = 'margin-left';
			elSize = this.element.getSize().x + this.options.sizeFixer;
			
		}
		
		var diff = elSize - containerHeight;
		this.steps = diff;
  		var perShown = containerHeight / (elSize / 100);
		pershown = perShown.round();
  		if(perShown < 5) perShown = 5;
        if(perShown >= 100) $(bar).setStyle('display', 'none');
		
		$(slider).setStyle('height', perShown + '%');
		if(Browser.Engine.trident4 && this.options.ieSizeFixer) $(slider).setStyle('height', this.options.ieSizeFixer);
  		this.slider = new Slider(bar, slider, {
			mode:this.options.mode, 
			steps:diff,
			onChange:this.scrollElement.bind(this)
		});
		
	},
	
	wheelScroll: function(ev){
		var e = new Event(ev);
		e.stop();
		var w;
		if(e.wheel < 0){
			w = e.wheel * e.wheel;
			w = w + this.options.wheelSpeed;
		}else if(e.wheel > 0){
			w = e.wheel - (e.wheel * 2);
			w = w - this.options.wheelSpeed;
		}
		var s = w + this.currentStep;
		if(s < 0) s = 0;
		if(s > this.steps) s = this.steps;
		this.slider.set(s);
	},
	
	scrollElement: function(step){
		this.currentStep = step;
		step = step - (step * 2);
		if(isNaN(step)) return;
		this.element.setStyle(this.prop, step + 'px');
	}
});

Scrollbar.implement(new Options);
