var Scroller = function(id, options) {
	options = {
		step: (options && options.step) ? options.step : 2,
		timing: (options && options.timing) ? options.timing : 50,
		direction: (options && options.direction=='horizontal') ? 'horizontal' : 'vertical',
		onInit: (options && options.onInit) ? options.onInit : function() {}
	};
	if (window==this) {
		return new Scroller(id, options);
	}
	
	var offset = options.direction=='horizontal' ? 'offsetWidth' : 'offsetHeight';
	var top    = options.direction=='horizontal' ? 'left' : 'top';
	
	var stepping = 0;
	
	var timer = null;
	
	this.up = function() {
		var that = this;
		stepping = -options.step;
		timer = window.setInterval( function() { that.scroll(); }, options.timing);
	};
	
	this.down = function() {
		var that = this;
		stepping = options.step;
		timer = window.setInterval( function() { that.scroll(); }, options.timing);
	};
	
	this.stop = function() {
		stepping = 0;
		window.clearInterval(timer);
	};
	
	this.faster = function() {
		stepping += (stepping>0) ? options.step : -options.step;
	};
	
	this.init = function() {
		var oldonresize = window.onresize;
		var that = this;
		if (window.onresize === null || typeof window.onresize != 'function') {
			window.onresize = function() { that.updateScrollarea(); };
		} else {
			window.onresize = function() {
				oldonresize();
				that.updateScrollarea();
			};
		}
		that.updateScrollarea();
		options.onInit();
	};
	
	this.updateScrollarea = function() {
		var that = this;
		var nodeBox = document.getElementById(id);
		if (nodeBox) nodeBox.style.overflow = 'hidden';
		
		var nodeData = document.getElementById(id+'data');
		if (nodeBox && nodeData) {
			var visible = parseInt(nodeBox[offset],10);
			var content = parseInt(nodeData[offset],10);
			
			var hide = content <= visible;
			
			if (hide) {
				nodeData.style[top] = '0px';
			} else {
				var contentTopStr = nodeData.style[top];
				if (contentTopStr === '') contentTopStr = '0px';
				var contentTop = parseInt(contentTopStr,10);
				if (contentTop < visible - content) nodeData.style[top] = (visible - content)+'px';
			}
			var node = document.getElementById(id+'up') || document.getElementById(id+'left');
			if (node) {
				node.style.display = (hide ? 'none' : 'block');
				node.onmouseover = function() { that.up(); };
				node.onmouseout = function() { that.stop(); };
				node.onclick = function() { that.faster(); return false; };
			}
			node = document.getElementById(id+'down') || document.getElementById(id+'right');
			if (node) {
				node.style.display = (hide ? 'none' : 'block');
				node.onmouseover = function() { that.down(); };
				node.onmouseout = function() { that.stop(); };
				node.onclick = function() { that.faster(); return false; };
			}
		}
	};
	
	this.scroll = function() {
		var nodeBox = document.getElementById(id);
		var nodeData = document.getElementById(id+'data');
		if (!nodeBox || !nodeData) {
			if (!nodeBox) alert('scrolling() could not find id ('+id+')');
			if (!nodeData) alert('scrolling() could not find id ('+id+'data)');
			stepping = 0;
			this.stop();
			return;
		}
		var visible = parseInt(nodeBox[offset],10);
		var content = parseInt(nodeData[offset],10);
		var contentTopStr = nodeData.style[top];
		if (contentTopStr === '') contentTopStr = '0px';
		var contentTop = parseInt(contentTopStr,10);
		contentTop -= stepping;
		
		var lastStep = false;
		if (contentTop > 0) {
			contentTop = 0;
			lastStep = true;
		} else if (contentTop < visible - content) {
			contentTop = visible - content;
			lastStep = true;
		}
		nodeData.style[top] = contentTop+'px';
		if (lastStep) this.stop();
	};
	
	this.init();
};


var initScrollarea = Scroller;