// scrollingLayer.js

var scrollingLayers = new Object();

function scrollingLayer(id, top, clipTop, clipRight, clipBottom, clipLeft) {
	// Attributes
	this.id = id;
	this.style = document.getElementById(id).style;
	this.clipTop = clipTop;
	this.clipRight = clipRight;
	this.clipBottom = clipBottom;
	this.clipLeft = clipLeft;
	this.top = top;
	this.height = document.getElementById(id).offsetHeight;
	
	// Position the layer on construction
	this.style.clip = "rect(" + this.clipTop + "px " + 
	                   this.clipRight + "px " + 
					   this.clipBottom + "px " + 
					   this.clipLeft + "px)";
	this.style.top = this.top + "px";
	
	// Methods
	this.scroll = scroll;
	this.stopScroll = stopScroll;
}


function scroll (amount, time) {
	doScroll(this.id, amount, time);
}

function stopScroll () {
	if (this.time) {
		clearTimeout(this.time);
	}
}

function doScroll (id, amount, time) {
	// BEGIN DEBUG
	//alert('DEBUG: id=' + id);
	// END DEBUG
	var lyr = getScrollingLayer(id);
	// BEGIN DEBUG
	//alert('DEBUG: lyr.id=' + lyr.id);
	// END DEBUG
	
	
	lyr.clipTop += amount;
	lyr.clipBottom += amount;
	lyr.top -= amount;
	if (lyr.clipTop < 0 || lyr.clipBottom > lyr.height) {
		lyr.clipTop -= amount;
		lyr.clipBottom -= amount;
		lyr.top += amount;
		return;
	}
	
	lyr.style.clip = "rect(" + lyr.clipTop + "px " + 
	                   lyr.clipRight + "px " + 
					   lyr.clipBottom + "px " + 
					   lyr.clipLeft + "px)";
	lyr.style.top = lyr.top + "px";
	
	var timeoutCmd = 'doScroll(\'' + lyr.id + '\', ' + amount + ', ' + time + ')';
	// BEGIN DEBUG
	//alert('DEBUG: timeoutCmd=' + timeoutCmd);
	// END DEBUG	
	lyr.time = setTimeout(timeoutCmd, time);
}

function registerScrollingLayer(lyr) {
	scrollingLayers[lyr.id] = lyr;
}

function getScrollingLayer(id) {
	return scrollingLayers[id];
}

