var Scroll = Class.create();
Object.extend(Scroll, {
	build: function(object, speed) {
		object = $(object);

		var outer = $Rel(object, "outer");
		var inner = $Rel(object, "inner");
		var up = $Rel(object, "up");
		var barouter = $Rel(object, "barouter");
		var barinner = $Rel(object, "barinner");
		var down = $Rel(object, "down");

		var _textContainerHeight = outer.offsetHeight;
		var _textHeight = inner.offsetHeight;
		var _barHeight = (_textHeight > _textContainerHeight) ? (_textContainerHeight * _barMaxHeight / _textHeight) : _barMaxHeight;
			_barHeight = (isNaN(_barHeight) || _barHeight < 0) ? 0 : _barHeight;

		var _maxOffsetTop = _textHeight - _textContainerHeight;
		var _maxBarTop = _barMaxHeight - _barHeight;

		if (barouter) {
			Event.observe(document, 'mousemove', Mouse.FindMousePosition, false);
			iebody = document.getElementsByTagName("body")[0];
			var _barMaxHeight = barouter.offsetHeight - parseInt(Element.getStyle(barouter, "padding-top")) - parseInt(Element.getStyle(barouter, "padding-bottom"));

			Position.relativize(barinner);
			if(parseInt(Element.getStyle(barinner, "margin-top")) > _maxBarTop) {
				barinner.style.marginTop = _maxBarTop + "px";
				outer.scrollTop = _maxOffsetTop;
			}

			Element.setStyle(barinner, { "margin-top": Math.round(_barHeight) + "px" });

			Event.observe(
				barinner, 
				'mousedown', 
				function(e) {
					var _barContainerTop = Scroller._findPos(document.getElementById(ids)).y + Math.abs(Scroller._mouse.y - document.getElementById(idb).offsetTop);

					var f = function(e) {
						Scroller._clearDocumentSelection();

						var _dY = Mouse.y - _barContainerTop;
						_dY += IE ? iebody.scrollTop : 0;

						if (_dY > _maxBarTop) _dY = _maxBarTop;
						if (_dY < 0) _dY = 0;

						var _y = _dY * _maxOffsetTop / _maxBarTop;
						_y = (_y > 0) ? _y : 0;

						outer.scrollTop = Math.round(_y);
						Element.setStyle(barinner, {"margin-top" : Math.round(_dY) + "px"});
						return true;
					}

					var g = function(e) {
						Event.stopObserving(document, 'mousemove', function() {}, false);
						Event.stopObserving(document, 'mouseup', function() {}, false);
					}

					Event.observe(document, 'mousemove', f, false);
					Event.observe(document, 'mouseup', g, false);
				},
				false
			);

			Event.observe(
				barinner, 
				"mouseup",
				function () {
					Event.stopObserving(document, 'mousemove', function() {}, false);
					Event.stopObserving(document, 'mouseup', function() {}, false);
				},
				false
			);
		}

		var fixedScroll = function (d) {
			outer.scrollTop = outer.scrollTop + d * speed;

			if (barinner) {
				var _dY = outer.scrollTop * _maxBarTop / _maxOffsetTop;
				_dY = Math.round(_dY);
				if (_dY > _maxBarTop) _dY = _maxBarTop;
				if (_dY < 0) _dY = 0;
				Element.setStyle(barinner, { "margin-top": _dY + "px" });
			}
		};

		Event.observe(
			up, 
			"mousedown",
			function () {
				if (up._t) clearInterval(up._t);
				if (down._t) clearInterval(down._t);
				up._t = setInterval(function() { fixedScroll(-1); }, 100);
			},
			false
		);

		Event.observe(
			up, 
			"mouseup",
			function () {
				if (up._t) clearInterval(up._t);
			},
			false
		);

		Event.observe(
			up, 
			"mouseout",
			function () {
				if (up._t) clearInterval(up._t);
			},
			false
		);


		Event.observe(
			down, 
			"mousedown",
			function () {
				if (up._t) clearInterval(up._t);
				if (down._t) clearInterval(down._t);
				down._t = setInterval(function() { fixedScroll(1); }, 100);
			},
			false
		);

		Event.observe(
			down, 
			"mouseup",
			function () {
				if (down._t) clearInterval(down._t);
			},
			false
		);

		Event.observe(
			down, 
			"mouseout",
			function () {
				if (down._t) clearInterval(down._t);
			},
			false
		);
	},

	/* <ClearSealections> */ 
	_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) { };
			}
		}
	}
	/* </ClearSealections> */ 
});
