var isdrag = false;
var y, ty;
//document.onselectstart = new Function('return false'); 

function Scroller() {
	// Public
	this.element = window.document.getElementById('scroll');
	this.scrollContent = window.document.getElementById('scrollContent');
	this.scrollBar = window.document.getElementById('scrollerBar');
	this.scrollBarThumb = window.document.getElementById('scrollerBarThumb');
	
	// Private
	this._timeoutID = null;
	this._firstClick = true;
	this._timeoutMilliseconds = 30;
	this._debug = new String;
	this._availScrollTop = this.scrollContent.offsetHeight - this.element.offsetHeight;
	this._actualScrollTop = 0;
	this._actualScrollTopPercent = 0;
	this._actualScrollThumbHeight = 0;
	this._availScrollBarTop = 0;
	this._isdrag = false;
	this._ie = document.all;
	this._nn6 = document.getElementById && !document.all;
	this._y = 0;
	this._ty = 0;
	
	// Construct operations
	if(this.scrollContent.offsetHeight <= this.element.offsetHeight) {
		if(window.document.getElementById('scroller')) {
			window.document.getElementById('scroller').style.display = 'none';
		}
	} else {
		if(window.document.getElementById('scroller')) {
			window.document.getElementById('scroller').style.display = 'block';
			// Resize scrollBarThumb
			this._actualScrollThumbHeight = Math.ceil((this.element.offsetHeight / this.scrollContent.offsetHeight) * 100)
			this.scrollBarThumb.style.height = Math.ceil(((this.scrollBar.offsetHeight * this._actualScrollThumbHeight) / 100)) - 3 + 'px';
			this._availScrollBarTop = Math.ceil(this.scrollBar.offsetHeight - ((this.scrollBar.offsetHeight * this._actualScrollThumbHeight) / 100));
		}
	}
	//this.scrollBarThumb.onmousedown = this.moveScrollBar;
}

Scroller.prototype.moveUp = function() {
	this.element.scrollTop = this.element.scrollTop - 5;
	// BEGIN actualScrollTop
	this._actualScrollTop = this.element.scrollTop;
	if(this._availScrollTop > 0) {
		this._actualScrollTopPercent = Math.round((this._actualScrollTop / this._availScrollTop) * 100);
		if(this._actualScrollTopPercent > 100) {
			this._actualScrollTopPercent = 100;
		}
		this.scrollBarThumb.style.top = Math.ceil(((this._availScrollBarTop / 100) * this._actualScrollTopPercent)) + 'px';
	}
	// END actualScrollTop
	//clearInterval(this._timeoutID);
	if(this._firstClick) {
		this._timeoutMilliseconds = 300;
		this._firstClick = false;
	} else {
		this._timeoutMilliseconds = 30;
	}
	
	this._timeoutID = window.setTimeout('SCROLL.moveUp()', this._timeoutMilliseconds);
}
  
Scroller.prototype.moveDown = function(scrollTopValue) {
	this.element.scrollTop = this.element.scrollTop + 5;
	// BEGIN actualScrollTop
	this._actualScrollTop = this.element.scrollTop;
	if(this._availScrollTop > 0) {
		this._actualScrollTopPercent = Math.round((this._actualScrollTop / this._availScrollTop) * 100);
		if(this._actualScrollTopPercent > 100) {
			this._actualScrollTopPercent = 100;
		}
		this.scrollBarThumb.style.top = Math.ceil(((this._availScrollBarTop / 100) * this._actualScrollTopPercent)) + 'px';
	}
	// END actualScrollTop
	//clearInterval(this._timeoutID);
	if(this._firstClick) {
		this._timeoutMilliseconds = 300;
		this._firstClick = false;
	} else {
		this._timeoutMilliseconds = 30;
	}
	
	this._timeoutID = window.setTimeout('SCROLL.moveDown()', this._timeoutMilliseconds);
}

Scroller.prototype.clearTimeoutID = function() {
	clearInterval(this._timeoutID);
	this._firstClick = true;
}

Scroller.prototype.debug = function() {
	this._debug = 'Veľkosť elementu scroll: ' + this.element.offsetWidth + ' x ' + this.element.offsetHeight + '\n';
	this._debug += 'Veľkosť elementu scrollContent: ' + this.scrollContent.offsetWidth + ' x ' + this.scrollContent.offsetHeight + '\n';
	this._debug += 'Možný posun: ' + this._availScrollTop + '\n';
	this._debug += 'Aktuálny scrollTop: ' + this._actualScrollTop + '\n';
	this._debug += 'Aktuálny scrollTop v percentách: ' + this._actualScrollTopPercent + '\n';
	this._debug += 'Možný posun scroll baru: ' + this._availScrollBarTop + '\n';
	alert(this._debug);
}

function dragScrollBar(e) {
	if(isdrag) {
		//window.document.body.style.background = 'red';
		var topPos;	
		!e ? e = window.event : null;
		topPos = ty + e.clientY - y;
		//window.status = topPos;
		//window.statusbar = topPos;
		topPos < 0 ? topPos = 0 : '';
		topPos > SCROLL._availScrollBarTop ? topPos = SCROLL._availScrollBarTop : '';
		if(topPos >= 0 && topPos <= SCROLL._availScrollBarTop) {
			window.document.getElementById('scrollerBarThumb').style.top = topPos + 'px';
			//SCROLL.element.scrollTop = topPos;
			//SCROLL.element.scrollTop = (SCROLL._availScrollTop * Math.ceil((100 * topPos) / SCROLL._availScrollBarTop) * 100);
			SCROLL.element.scrollTop = Math.round(SCROLL._availScrollTop * topPos / SCROLL._availScrollBarTop);
		}
	}
}

function releaseDrag() {
	isdrag = false;
	window.document.body.style.cursor = 'Default';
	document.onselectstart = new Function('return true');
	function newds(e){return true;}
	document.onmousedown = newds;
}

function moveScrollBar(e) {
	isdrag = true;
	ty = parseInt(window.document.getElementById('scrollerBarThumb').style.top + 0);
	!e ? e = window.event : null;
	y = e.clientY;
	window.document.onmousemove = dragScrollBar;
	window.document.onmouseup = releaseDrag;
	window.document.body.style.cursor = 'n-resize';
	//window.document.getElementById('scrollerBarThumb').onmouseout = releaseDrag;
	
	document.onselectstart = new Function('return false');
	function ds(e){return false;}
	document.onmousedown = ds;
}

var SCROLL;
window.onload = function() {
	SCROLL = new Scroller;
	window.document.getElementById('scrollerBarThumb').onmousedown = moveScrollBar;
}