// JavaScript Document
function SumoScroll(id){
	
	this.stepsRange = 100;
	this.stepsStep = 5;
	
	var wrapper = $('wrapper_'+id);
	var content = $('content_'+id);
	var area = $('area_'+id);
	
	this.ch = content.getCoordinates().height;
	this.vh = wrapper.getCoordinates().height;
	this.sh = area.getCoordinates().height;
	
	this.kh = this.vh * this.sh / this.ch;
	
	if (this.ch <= this.vh) return;
	else {
		$('scrollbar_'+id).setStyle('visibility', 'visible');
	}
	
	$('knob_'+id).setStyle('height', this.kh+'px');
	
	this.stepHeight = this.stepsStep * this.ch / this.stepsRange;
	
	this.objScroll = new Fx.Scroll('wrapper_'+id, {
		wait: false,
		duration: 0,
		offset: {'x': 0, 'y': 0},
		transition: Fx.Transitions.Quad.easeInOut
	});
	
	Slider.prototype.objSumoScroll;
	Slider.prototype.setSumoScroll = function(objSumoScroll){
		this.objSumoScroll = objSumoScroll;
	}
	this.objSlide = new Slider($('area_'+id), $('knob_'+id), {	
		steps: this.stepsRange,	
		mode: 'vertical',	
		onChange: function(step){
			this.currentStep = step;
			if (this.objSumoScroll!=null){
				this.objSumoScroll.setCurrentStep(step);
				this.objSumoScroll.scrollToCurrentStep();
			}
		}
	}).set(0);
	this.objSlide.setSumoScroll(this);
	
	
	
	/*** wheel management ***/
	Element.Events.extend({
		'wheelup': {
			type: Element.Events.mousewheel.type,
			map: function(event){
				event = new Event(event);
				if (event.wheel >= 0) this.fireEvent('wheelup', event)
			}
		},
	 
		'wheeldown': {
			type: Element.Events.mousewheel.type,
			map: function(event){
				event = new Event(event);
				if (event.wheel <= 0) this.fireEvent('wheeldown', event)
			}
		}
	});
	
	
	
	/* scroll wheel */
	$('wrapper_'+id).addEvents({
		'wheelup': function(e) {
			e = new Event(e).stop();
		},
	 
		'wheeldown': function(e) {
			e = new Event(e).stop();
		}
	});
	
	content.objSumoScroll = this;
	content.addEvent('mousewheel', function(event) {
			event = new Event(event);
		 
			/* Mousewheel UP */
			if (event.wheel > 0) {
				this.objSumoScroll.scrollUp();
			} 
			/* Mousewheel DOWN */
			else if (event.wheel < 0) {
				this.objSumoScroll.scrollDown();
			}
		});
	
	
	
	
	
}

SumoScroll.prototype.vh;
SumoScroll.prototype.ch;
SumoScroll.prototype.kh;
SumoScroll.prototype.sh;

SumoScroll.prototype.stepsRange;
SumoScroll.prototype.stepsStep;
SumoScroll.prototype.stepHeight;
SumoScroll.prototype.currentStep;

SumoScroll.prototype.objScroll;
SumoScroll.prototype.objSlide;

SumoScroll.prototype.getObjScroll = function(){
	return this.objScroll;
}



SumoScroll.prototype.scrollDown = function(){
	if (this.currentStep==null) this.currentStep = 0;
	this.objSlide.set(this.currentStep + this.stepsStep);
}

SumoScroll.prototype.scrollUp = function(){
	if (this.currentStep==null) this.currentStep = 0;
	this.objSlide.set(this.currentStep - this.stepsStep);
}


SumoScroll.prototype.setCurrentStep = function(step){
	this.currentStep = step;
}

SumoScroll.prototype.scrollToCurrentStep = function(){
	// this.objScroll.scrollTo(0, parseInt(this.currentStep) * (this.ch - this.vh + (this.vh / 3)) / this.stepsRange);
	this.objScroll.scrollTo(0, parseInt(this.currentStep) * (this.ch - this.vh + (this.vh / 3)) / this.stepsRange);
}


