/***************************************************************************
	Scroller -Self Scrolling object.
	
	Two nested Div's with the outer one being the croping mask for the inner
	one which holds the content and scrolls. The inner Div is what gets 
	scrolled. For thoses that don't like scroll bars to mess with their layout.
	
	Version:
		v0.1, Monday, July 08, 2002
	
	Requires:
		UniDom
		MousePointer
		
	Author:
		Alexander Whillas

	Copyright (C) 2002 Taylor Square Designs
***************************************************************************/
// Some state cnstants that this object uses.
var NOT_SCROLLING = 0;
var SCROLLING_UP = -1;
var SCROLLING_DOWN = 1;

// Constructor
function Scroller (p_outter_div_id, p_inner_div_id, p_max_speed, p_default_direction, p_default_scrolling_speed, p_medium_speed_margin, p_top_speed_margin, p_looping) {
	this.frame = new UniDom(p_outter_div_id);
	this.content = new UniDom(p_inner_div_id, p_outter_div_id);
	
	this.max_speed = p_max_speed || 6;
	
	this.direction = this.defaultScrolling = p_default_direction || NOT_SCROLLING;
	this.speed = this.default_speed = p_default_scrolling_speed || 0;
	
	this.margin_medium = p_medium_speed_margin;
	this.margin_top = p_top_speed_margin;
	
	this.looping = p_looping || false;
	
	return this;
}


// Function called by the Animation object.
Scroller.prototype.move = function () {
	// If the mouse is indeed over the framed area (i.e. we're making our own mouse over)...
	if (this.frame.div.getX() <= mouse.getX() && mouse.getX() <= (this.frame.div.getX() + this.frame.div.width) && 
		this.frame.div.getY() <= mouse.getY() && mouse.getY() <= (this.frame.div.getY() + this.frame.div.height)) {

		// 'Position' of mouse over the object as a percentage of its hieght.
		position = Math.round(100 * ((mouse.getY() - this.frame.div.getY()) / this.frame.div.height));
		// This series of IF statment determines the state of the Scroller and thus what it does next.
		if (position > (100 - this.margin_medium)) {
			this.direction = SCROLLING_UP;
			if (position > (100 - this.margin_top)) {
				this.speed = this.max_speed;
			} else {
				this.speed = this.max_speed / 2;
			}
		} else if (position < this.margin_medium) {
			this.direction = SCROLLING_DOWN;
			if (position < this.margin_top) {
				this.speed = this.max_speed;
			} else {
				this.speed = this.max_speed / 2;
			}
		} else {
			this.direction = NOT_SCROLLING;
		}
		if ( this.isBottom() || this.isTop() ) this.direction = NOT_SCROLLING;
	} else {
		// Mouse is NOT over the box so assume default behavior.
		if (this.looping || this.isBottom() || this.isTop()) {
			this.defaultScrolling *= -1;
			this.direction = this.defaultScrolling		
		}
		this.direction = this.defaultScrolling;
		this.speed = this.default_speed;		
	}

	this.scroll();
}

// Content's bottom is at the frames bottom
Scroller.prototype.isBottom = function  () {
	return (this.frame.div.height >= (this.content.div.getY() + this.content.div.height) && this.direction == SCROLLING_UP);
}

// Contents top is at the frames top.
Scroller.prototype.isTop = function  () {
	return (this.content.div.getY() >= 0 && this.direction == SCROLLING_DOWN);
}

// Scroll! Increment the scroller by one step ('speed') in the 'direction'
Scroller.prototype.scroll = function () {
	this.content.moveBy(0, this.direction * this.speed);
}