var scrollbarContainers = new Array();
var scrollbarTimeouts = new Array();
var scrollbarDrags = new Array();
var useClipValues = new Array();

function initScrollbar(scrollingContent, scrollbarContainer, imgUp, imgDown, dontGoToTop, useClip, forcedArrowHeight) {
	var needScrollbar = false;
	if (typeof(scrollingContent) == 'string') {
		scrollingContent = document.getElementById(scrollingContent);
	}
	if (scrollingContent) {
		var scrollingContentId = scrollingContent.id;
		var tOrig = dontGoToTop ? parseInt(scrollingContent.style.marginTop) : false;
		scrollingContent.style.marginTop = '0px';
		unregisterAction(scrollingContentId);
		scrollbarContainers[scrollingContentId] = false;
		useClipValues[scrollingContentId] = useClip;
		if (typeof(scrollbarContainer) == 'string') {
			scrollbarContainer = document.getElementById(scrollbarContainer);
		}
		if (scrollbarContainer) {
			scrollbarContainer.style.display = 'none';
			scrollbarContainer.innerHTML = '';
			var scrollingContentContainer = scrollingContent.parentNode;
			if (scrollingContentContainer) {
				scrollingContentContainer.style.overflow = 'hidden';
				needScrollbar = scrollingContent.offsetHeight - scrollingContentContainer.clientHeight >= 0;
				if (needScrollbar) {
					scrollbarContainer.style.display = '';
					scrollbarContainers[scrollingContentId] = scrollbarContainer;
					var scrollbarWidth = scrollbarContainer.clientWidth;
					var buttons = new Array();
					buttons.push(new Array(imgUp, 1, false, 'scrollbar-arrow-up'));
					buttons.push(new Array(imgDown, -1, true, 'scrollbar-arrow-down'));
					var bar = document.createElement('div');
					bar.style.position = 'absolute';
					bar.style.width = scrollbarWidth + 'px';
					bar.style.height = scrollbarContainer.clientHeight + 'px';
					scrollbarContainer.appendChild(bar);
					for (var i = 0, length = buttons.length; i < length; i++) {
						var button = buttons[i];
						var imgButton = document.createElement('img');
						imgButton.style.position = 'absolute';
						imgButton.style.display = 'block';
						imgButton._scrollAttrs = button;
						imgButton.className = button[3];
						imgButton.onmousedown = function() {
							registerScrollAction(scrollingContentId, this._scrollAttrs[1] * 10, 0);
						};
						imgButton.onmouseup = function() {
							unregisterAction(scrollingContentId);
						};
						imgButton.onmouseout = function() {
							unregisterAction(scrollingContentId);
						};
						scrollbarContainer.appendChild(imgButton);
						if (forcedArrowHeight) {
							imgButton.src = button[0];
							imgButton.style.height = forcedArrowHeight + 'px';
							var p = scrollbarContainer;
							var h = forcedArrowHeight;
							bar.style.height = (parseInt(bar.style.height) - h) + 'px';
							if (imgButton._scrollAttrs[2]) {
								imgButton.style.marginTop = (p.clientHeight - h) + 'px';
							} else {
								bar.style.marginTop = h + 'px';
							}
						} else {
							imgButton.onload = function() {
								var p = this.parentNode;
								if (p) {
									var h = this.clientHeight;
									bar.style.height = (parseInt(bar.style.height) - h) + 'px';
									if (this._scrollAttrs[2]) {
										this.style.marginTop = (p.clientHeight - h) + 'px';
									} else {
										bar.style.marginTop = h + 'px';
									}
								}
								calculateScrollbar(scrollingContent);
							}
							imgButton.src = button[0];
						}
					}
					buttons = new Array();
					buttons.push(new Array('scrollbar-above', 50));
					buttons.push(new Array('scrollbar-scroller', 0));
					buttons.push(new Array('scrollbar-below', -50));
					for (var i = 0, length = buttons.length; i < length; i++) {
						var button = buttons[i];
						var span = document.createElement('span');
						span.className = button[0];
						span.style.position = 'absolute';
						span.style.width = scrollbarWidth + 'px';
						span.style.height = '0px';
						span.style.display = 'block';
						span.style.overflow = 'hidden';
						bar.appendChild(span);
						span._scrollAttrs = button;
						if (i == 1) {
							span.onmousedown = function(evt) {
								unregisterAction(scrollingContentId);
								startSliderDrag(scrollingContentId, evt);
							};
							span.onmouseup = function() {
								unregisterAction(scrollingContentId);
								document.onmousemove = function() {
								};
							};
							span.onmouseout = function(evt) {
								deselectText();
								if (!evt) {
									document.onmousemove = function(evt) {
										moveSlider(scrollingContentId, evt);
									};
									document.onmouseup = function() {
										unregisterAction(scrollingContentId);
										document.onmousemove = function() {
										};
										document.onmouseup = function() {
										};
									};
								}
							};
							span.onmousemove = function(evt) {
								moveSlider(scrollingContentId, evt);
							};
						} else {
							span.onmousedown = function() {
								registerScrollAction(scrollingContentId, this._scrollAttrs[1], 0);
							};
							span.onmouseup = function() {
								unregisterAction(scrollingContentId);
							};
//							span.onmouseout = function() {
//								unregisterAction(scrollingContentId);
//							};
//							span.onmousemove = function(evt) {
//								unregisterAction(scrollingContentId);
//							};
						}
					}
					if (forcedArrowHeight) {
						calculateScrollbar(scrollingContent);
					}
					scrollContentTo(scrollingContent, tOrig ? tOrig : 0);
					if (useClip && (scrollingContent.style.visibility == 'hidden')) {
						scrollingContent.style.visibility = 'visible';
					}
				}
			}
		}
	}
	return needScrollbar;
}

function unregisterAction(scrollingContentId) {
	var t = scrollbarTimeouts[scrollingContentId];
	if (t) {
		clearTimeout(t);
		scrollbarTimeouts[scrollingContentId] = false;
	}
	if (scrollbarDrags[scrollingContentId]) {
		scrollbarDrags[scrollingContentId] = false;
	}
}

function registerAction(scrollingContentId, action, timeout) {
	unregisterAction(scrollingContentId);
	scrollbarTimeouts[scrollingContentId] = setTimeout(action, timeout);

}

function registerScrollAction(scrollingContentId, offset, timeout) {
	registerAction(scrollingContentId, 'scrollContentBy(\'' + scrollingContentId + '\', ' + offset + ', true);', timeout);

}

function scrollContentBy(scrollingContent, offset, reRegister) {
	if (typeof(scrollingContent) == 'string') {
		scrollingContent = document.getElementById(scrollingContent);
	}
	var t = parseInt(scrollingContent.style.marginTop) + offset;
	scrollContentTo(scrollingContent, t);
	if (reRegister) {
		registerScrollAction(scrollingContent.id, offset, 50);
	}
}

function scrollContentTo(scrollingContent, t) {
	if (typeof(scrollingContent) == 'string') {
		scrollingContent = document.getElementById(scrollingContent);
	}
	if (t > 0) {
		t = 0;
	} else {
		var minT = scrollingContent.parentNode.offsetHeight - scrollingContent.offsetHeight;
		if (t < minT) {
			t = minT;
		}
	}
	scrollingContent.style.marginTop = t + 'px';
	var useClip = useClipValues[scrollingContent.id];
	if (useClip) {
		scrollingContent.style.clip = 'rect(' + (-t) + 'px, auto, ' + (scrollingContent.parentNode.clientHeight - t) + 'px, auto)';
	}
	calculateScrollbar(scrollingContent);
}

function calculateScrollbar(scrollingContent) {	
	if (typeof(scrollingContent) == 'string') {
		scrollingContent = document.getElementById(scrollingContent);
	}	
	var scrollbarContainer = scrollbarContainers[scrollingContent.id];	
	if (scrollbarContainer) {		
		var barParts = scrollbarContainer.getElementsByTagName('span');		
		if (barParts.length > 2) {			
			var h = barParts[0].parentNode.offsetHeight;
			var ratio = h / scrollingContent.offsetHeight;
			var h1 = Math.floor(- parseInt(scrollingContent.style.marginTop) * ratio);
			if( isNaN( h1 ) )
			{
				h1 = 0;
			}
			barParts[0].style.height = h1  + 'px';			
			barParts[1].style.marginTop = h1  + 'px';
			var h2 = Math.floor(scrollingContent.parentNode.offsetHeight * ratio);
			if( isNaN( h2 ) )
			{
				h2 = 0;
			}
			barParts[1].style.height = h2 + 'px';
			var h3 = h1 + h2;
			if (h3 > h) {
				h3 = h;
			}			
			barParts[2].style.marginTop = h3 + 'px';
			barParts[2].style.height = (h - h3) + 'px';
			var offset = h2 / ratio;
			barParts[0]._scrollAttrs[1] = offset;
			barParts[2]._scrollAttrs[1] = -offset;
		}
	}
}

function getMouseXY(evt) {
	if (!evt) {
		evt = window.event;
	}
	var xy = new Array(-1, -1);
	if (evt.pageX) {
		xy[0] = evt.pageX;
	} else if (evt.clientX) {
		xy[0] = evt.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
	}
	if (evt.pageY) {
		xy[1] = evt.pageY;
	} else if (evt.clientY) {
		xy[1] = evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	}
	return xy;
}

function startSliderDrag(scrollingContentId, evt) {
	var xy = getMouseXY(evt);
	var scrollingContent = document.getElementById(scrollingContentId);
	var scrollbarContainer = scrollbarContainers[scrollingContent.id];
	if (scrollbarContainer) {
		var barParts = scrollbarContainer.getElementsByTagName('span');
		if (barParts.length > 1) {
			var ratio = (scrollingContent.parentNode.offsetHeight - scrollingContent.offsetHeight) / (barParts[1].parentNode.offsetHeight - barParts[1].offsetHeight);
			var t = parseInt(scrollingContent.style.marginTop);
			scrollbarDrags[scrollingContentId] = new Array(xy[0], xy[1], ratio, t);
		}
	}
}

function moveSlider(scrollingContentId, evt) {
	var dragOptions = scrollbarDrags[scrollingContentId];
	if (dragOptions) {
		var xy = getMouseXY(evt);
		var dY = xy[1] - dragOptions[1];
		var ratio = dragOptions[2];
		var t = dragOptions[3];
		scrollContentTo(scrollingContentId, t + Math.floor(dY * ratio));
	}
}

function deselectText() {
	if (document.selection) {
		document.selection.empty();
	}
}

