
$(function(){
	if (typeof document.body.scrollTop == "undefined")
		return;
	
	$("#main div.contentWrapper div.box div.text")
		.wrap('<div class="textWrapper"></div>')
	$("#main div.contentWrapper div.box div.textWrapper")
		.each(function(){
			var box = $("div.text", this).get(0);
			var vH = $(box).height();
			if ($(box).css("overflow") != "auto") return;
			if (box.scrollHeight - vH <= 0) return;

			this.style.height = vH + "px";
			box.style.overflow = "hidden"
			box.style.paddingRight = "32px"
			
			$(this).prepend(
				'<div class="scrollBar">\
					<span class="prev"></span>\
					<span class="dot"></span>\
					<span class="next"></span>\
				</div>');
			
			var dot = $("div.scrollBar span.dot", this).get(0);
			var dotDist = vH - 30;
			var dotShift = 12;
			var step = 10;
			var max = box.scrollHeight - vH;
			
			var moveTo = function (t) {
				if (t < dotShift) t = dotShift;
				if (t > dotDist) t = dotDist;
				dot.style.top = t + "px";
				box.scrollTop = max * t / dotDist
			}
			var moveBy = function (d) {
				moveTo(parseInt(dot.style.top+0) + d);
			}
			$("div.scrollBar", this)
				.each(function() {
					this.style.height = vH + "px"
				})
				.mousedown(function(e){
					sy = parseInt(dot.style.top+0);
					y = e.clientY;
					document.onmousemove = function (ev) {
						var t = sy - y + (ev||event).clientY;
						moveTo(t);
						return false;
					}
					document.onmouseup = function () {
						document.onmousemove = null;
					}
				})

			var intv;
			$("div.scrollBar span.next", this)
				.mouseover(function() {
					intv = window.setInterval(function() { moveBy(step); }, 50)
				})
				.mouseout(function() { clearInterval(intv) });
			$("div.scrollBar span.prev", this)
				.mouseover(function() {
					intv = window.setInterval(function() { moveBy(-step); }, 50)
				})
				.mouseout(function() { clearInterval(intv) });
		});
});