/**************************************************
 * setupScroll.js
 * 02.04.2005
 * www.oarbic.com
 **************************************************
 * Based on example from http://www.yougnpup.net/
 **************************************************/

var theThumb, theScroll, theUpButton, theDownButton;
var thumbTravel, ratio, thumbRatio, thumbHeight;

var margin = 0;

theScroll = new ypSimpleScroll("scroll", 0, 0, 365-margin-22, 230-(margin*2));

window.onload = function() {
	theRoot  = document.getElementById("root");
	theThumb  = document.getElementById("thumb");
	theUpButton  = document.getElementById("topButton");
	theDownButton  = document.getElementById("bottomButton");
	theTrack  = document.getElementById("track");
	theContent  = document.getElementById("scrollContent");
	theContainer  = document.getElementById("scrollContainer");

	theScroll.load();

	// Calculate ratio between content and container
	contentRatio = theContent.offsetHeight / theContainer.offsetHeight;

	if (contentRatio <= 1)
	{
		theThumb.style.display = "none";
		theTrack.style.display = "none";
		theUpButton.style.display = "none";
		theDownButton.style.display = "none";
	}
	else
	{
		// Position the elements
		theUpButton.style.left = ((theRoot.offsetWidth-2-margin) - theUpButton.offsetWidth) + "px";
		theUpButton.style.top = margin + "px";

		theTrack.style.left = ((theRoot.offsetWidth-2-margin) - theUpButton.offsetWidth) + "px";
		theTrack.style.top = margin + theUpButton.offsetHeight + 1 + "px";
		theTrack.style.height = (theRoot.offsetHeight - (margin*2) - 26) + "px";

		theThumb.style.left = ((theRoot.offsetWidth-margin) - theUpButton.offsetWidth) + "px";
		theThumb.style.top = margin + theUpButton.offsetHeight + 2 + "px";;

		theDownButton.style.left = ((theRoot.offsetWidth-2-margin) - theUpButton.offsetWidth) + "px";
		theDownButton.style.top = ((theRoot.offsetHeight-margin) - theDownButton.offsetHeight) + "px";

		// Calculate intended height of Thumb (apply content ratio to track height)
		thumbHeight = Math.floor(theTrack.offsetHeight/contentRatio);
		thumbHeight -= 2; // Subtract 2 for the border size
		theThumb.style.height = thumbHeight + "px";

		thumbLeft = theThumb.style.left.substring(0,theThumb.style.left.length-2);
		thumbTop = theThumb.style.top.substring(0,theThumb.style.top.length-2);
		thumbBottom = theTrack.offsetHeight+11 - thumbHeight;

		Drag.init(theThumb, null, eval(thumbLeft), eval(thumbLeft), eval(thumbTop), eval(thumbBottom));

		// the number of pixels the thumb can travel vertically (max - min)
		thumbTravel = theThumb.maxY - theThumb.minY;

		// the ratio between scroller movement and thumbMovement
		ratio = theScroll.scrollH / thumbTravel;

		theThumb.onDrag = function(x, y) {
			theScroll.jumpTo(null, Math.round((y - theThumb.minY) * ratio));
		}

		theUpButton.onclick = function() {
			var top = theThumb.style.top;
			if (top.substring(top.length-2, top.length) == "px")
			{
				top = top.substring(0,top.length-2);
			}


			newY = Math.max(theThumb.minY, Math.round(top - thumbHeight + 2));
			sNewY = Math.round((newY - theThumb.minY) * ratio);

			theScroll.jumpTo(null, sNewY);
			theThumb.style.top=newY + "px";
		}

		theDownButton.onclick = function() {
			var top = theThumb.style.top;
			if (top.substring(top.length-2, top.length) == "px")
			{
				top = top.substring(0,top.length-2);
			}

			newY = Math.min(theThumb.maxY, Math.round(eval(top) + eval(thumbHeight) - 2));
			sNewY = Math.round((newY - theThumb.minY) * ratio);

			theScroll.jumpTo(null, sNewY);
			theThumb.style.top=newY + "px";
		}

		theTrack.onclick = function(e)
		{
			e = Drag.fixE(e);

			var ey	= e.clientY;
			var y = ey - theRoot.offsetTop;

			var top = theThumb.style.top;
			if (top.substring(top.length-2, top.length) == "px")
			{
				top = top.substring(0,top.length-2);
			}

			if (y < top)
			{
				theUpButton.onclick();
			}
			else
			{
				theDownButton.onclick();
			}
		}
	}
}
