var IE = (document.all) ? true : false;

var Scroller = {
	build: function(ido, idt, ids, idb, idl, idr, sped, resize) {
		// - - - tengo q sacar la altura q tiene por si es un recálculo (ie bug) - - -
		var btn_up = document.getElementById(idl);
		var btn_down = document.getElementById(idr);

		document.onmousemove = Mouse.FindMousePosition;
		if (!IE) {
			document.captureEvents(Event.MOUSEMOVE);
			document.captureEvents(Event.MOUSEUP);
		}

		var _barMaxHeight = document.getElementById(ids).offsetHeight;
		var _textContainerHeight = document.getElementById(ido).offsetHeight;
		var _textHeight = document.getElementById(idt).offsetHeight;
		var _barHeight = (_textHeight > _textContainerHeight) ? (_textContainerHeight * _barMaxHeight / _textHeight) : _barMaxHeight;

		if (resize)	_barHeight = (isNaN(_barHeight) || _barHeight < 0) ? 0 : _barHeight;
		else		_barHeight = document.getElementById(idb).offsetHeight;
		
		var _maxOffsetUp = _textHeight - _textContainerHeight;
		var _maxBarUp = _barMaxHeight - _barHeight;

		document.getElementById(idb).style.position = "relative";
		if(parseInt(document.getElementById(idb).style.top) > _maxBarUp) {
			document.getElementById(idb).style.top = _maxBarUp + "px";
			document.getElementById(ido).scrollTop = _maxOffsetUp;
		}
			
		document.getElementById(idb).style.height =  Math.round(_barHeight) + "px";
		var btn_bar = document.getElementById(idb);

		btn_bar.onmousedown = function() {
			var _barContainerUp = Scroller._findPos(document.getElementById(ids)).y + Math.abs(Mouse.y - Scroller._findPos(document.getElementById(idb)).y);

			document.onmousemove = function(e) {
				Mouse.FindMousePosition(e);
				Scroller._clearDocumentSelection();

				var _dX = Mouse.y - _barContainerUp;

				if (_dX > _maxBarUp) _dX = _maxBarUp;
				if (_dX < 0) _dX = 0;

				var _x = _dX * _maxOffsetUp / _maxBarUp;
				_x = (_x > 0) ? _x : 0;

				document.getElementById(ido).scrollTop = Math.round(_x);
				document.getElementById(idb).style.top = Math.round(_dX) + "px";
				return true;
			};

			document.onmouseup = function() {
				document.onmousemove = Mouse.FindMousePosition;
				document.onmouseup = function() { return true; };
				return true;
			};
		};

		btn_bar.onmouseup = function() {
			document.onmousemove = Mouse.FindMousePosition;
			document.onmouseup = function() { return true; };
			return true;
		};

		var btn_up = document.getElementById(idl);
		var btn_down = document.getElementById(idr);
		var friccion = 4;

		if (btn_up) {
			btn_up.onmousedown = function() {
				var f = function() {
					Scroller._clearDocumentSelection();

					var _dX = parseInt(document.getElementById(idb).style.top) - sped;
					if (_dX > _maxBarUp) _dX = _maxBarUp;
					if (_dX < 0) _dX = 0;

					var _x = _dX * _maxOffsetUp / _maxBarUp;
					_x = (_x > 0) ? _x : 0;

					document.getElementById(ido).scrollTop = Math.round(_x);
					document.getElementById(idb).style.top = Math.round(_dX) + "px";
					return true;
				};

				var _timer = setInterval(f, 10);
				f();

				document.onmouseup = function() {
					document.onmousemove = Mouse.FindMousePosition;
					document.onmouseup = function() { return true; };
					clearInterval(_timer);
					return true;
				};
			};
		}

		if (btn_down) {
			btn_down.onmousedown = function() {
				var f = function() {
					Scroller._clearDocumentSelection();

					var _dX = parseInt(document.getElementById(idb).style.top) + sped;
					if (_dX > _maxBarUp) _dX = _maxBarUp;
					if (_dX < 0) _dX = 0;

					var _x = _dX * _maxOffsetUp / _maxBarUp;
					_x = (_x > 0) ? _x : 0;

					document.getElementById(ido).scrollTop = Math.round(_x);
					document.getElementById(idb).style.top = Math.round(_dX) + "px";
					return true;
				};

				var _timer = setInterval(f, 10);
				f();

				document.onmouseup = function() {
					document.onmousemove = Mouse.FindMousePosition;
					document.onmouseup = function() { return true; };
					clearInterval(_timer);
					return true;
				};
			};			
		}

		// ini: actualizo la barra
		var _x = document.getElementById(ido).scrollTop;
		var _dX = _x * _maxBarUp / _maxOffsetUp;
		_dX = (_dX > _maxBarUp) ? _maxBarUp : _dX;
		_dX = (_dX < 0) ? 0 : _dX;
		_dX = _dX ? _dX : 0;

		document.getElementById(idb).style.top = Math.floor(_dX) + "px";
		// fin: actualizo la barra
	},

	// <ClearSelections>
	_clearDocumentSelection: function() {
		if (document.selection)	{
			if (document.selection.clear) document.selection.clear();
			else if (document.selection.empty) document.selection.empty();
		} else {
			if (window.getSelection) {
				try{
					window.getSelection().collapse();
					window.getSelection().removeAllRanges();
				} catch(e) { };
			}
		}
	},
	// </ClearSelections>

	// <AbsolutePosition>
	_findPos: function (obj) {
		var rv = new Object();
			rv.x = rv.y = 0;

		if (obj.offsetParent) {

			rv.x = obj.offsetLeft;
			rv.y = obj.offsetTop;
			
			while (obj = obj.offsetParent){
				if (obj.nodeType == 1){
				rv.x += obj.offsetLeft;
				rv.y += obj.offsetTop;
				}
			}
		}
		return rv;
	}
	// </AbsolutePosition>
}

