var VScroller = Class.create();
VScroller.prototype = {
  initialize: function(element, upBT, downBT) {
    this.element = $(element);
	this.visibleArea = $(this.element.parentNode.id);
	this.upEl = $(upBT);
	this.downEl = $(downBT);
	this.distance = this.visibleArea.getHeight()*0.9;
	this.element.setStyle({position: 'relative'});
	//this.element.setStyle({top: '0px'});
	
	this.allLinks = getElementsByClassName(this.element, 'a', "current");
	if(this.allLinks.length == 1){
		this.current = $(this.allLinks[0]);
		var outerpos = this.element.cumulativeOffset();
		var pos = this.current.cumulativeOffset();
		pos[0] -= outerpos[0];
		pos[1] -= outerpos[1];
		pos[0] -= this.visibleArea.getWidth()/2;
		//pos[0] *= -1;
		pos[1] *= -1;
		pos[1] += 155;
		new Effect.Move(this.element.id, {x:0, y:pos[1], mode: 'relative'});
	}
	
    this.eventGoup = this.startScrollup.bindAsEventListener(this);
	this.eventGodown = this.startScrolldown.bindAsEventListener(this);
    
    Event.observe(this.upEl, 'click', this.eventGoup);
	Event.observe(this.downEl, 'click', this.eventGodown);
  },
  destroy: function() {
    Event.stopObserving(this.upEl, 'click', this.eventGoup);
	Event.stopObserving(this.downEl, 'click', this.eventGodown);
  },
  startScrollup: function(event) {
    $move_y = this.distance;
	this.scrollEffect = new Effect.Move(this.element.id, {x:0, y:$move_y, mode: 'relative'});
	this.scrollEffect.targObj = this;
	this.scrollEffect.went = $move_y;
	this.scrollEffect.finish = this.doneScroll;
  },
  startScrolldown: function(event) {
    $move_y = this.distance*-1;
	this.scrollEffect = new Effect.Move(this.element.id, {x:0, y:$move_y, mode: 'relative'});
	this.scrollEffect.targObj = this;
	this.scrollEffect.went = $move_y;
	this.scrollEffect.finish = this.doneScroll;
  },
  doneScroll: function(){
	var outerpos = this.targObj.visibleArea.cumulativeOffset();
	var innerpos = this.targObj.element.cumulativeOffset();
	
	$toplimit = outerpos[1];
	$bottomlimit = outerpos[1] + this.targObj.visibleArea.getHeight();
	
	$topside = innerpos[1];
	
	//Firefox is pissing me off
	if(this.targObj.visibleArea.getHeight() == this.targObj.element.getHeight()){
		var lastlinkpos = this.targObj.lastlink.cumulativeOffset();
		$bottomtside = lastlinkpos[1] + this.targObj.lastlink.getHeight();
	}else{
		$bottomside = innerpos[1] + this.targObj.element.getHeight();
	}
	
	if(($bottomside - $topside) <= this.targObj.visibleArea.getHeight()){
		$die = true;
	}else{
		$die = false;
	}
	
	//too small
	if($die){
		$y_move = this.went *-1;
		new Effect.Move(this.targObj.element.id, {x:0, y:$y_move, mode: 'relative', duration: 0.25});
	}
	//too far forward
	if(!$die & ($bottomside < $bottomlimit) & !($bottomside > $bottomlimit)){
		$y_move = $bottomlimit - $bottomside;
		//alert($x_move);
		new Effect.Move(this.targObj.element.id, {x:0, y:$y_move, mode: 'relative', duration: 0.25});
	}
	//too far back
	if(!$die & ($topside > $toplimit) & !($topside < $toplimit)){
		$y_move = $toplimit - $topside;
		//alert($x_move);
		new Effect.Move(this.targObj.element.id, {x:0, y:$y_move, mode: 'relative', duration: 0.25});
	}
  }
}
