
// Based on `ThreeOhScroll`: custom scroll application. 
// I think the original author is Aaron Boodman,
// who wrote the original script for ThreeOh.com. Check out his site
// at boring.youngpup.net

// Changed August 2005: Functions not needed deleted.

// Junction for browser.
scroll.mo5 = navigator.userAgent.indexOf("Gecko") != -1
scroll.ie4 = navigator.appName  == "Microsoft Internet Explorer" && document.all
scroll.ie5 = navigator.appName  == "Microsoft Internet Explorer" && document.getElementById
scroll.pc  = navigator.platform == "Win32"

// For ie4 pc only 
scroll.ie4pcDescWidth = 100

// This is related to how far the scroller is from the top of the window
// in Mozilla. You will have to play with it to get it just right.
scroll.mozAdjust = scroll.mo5 ? 75 : 0

// This is how long it should take the scroller to animate when the user clicks
// a marker (in milliseconds)
scroll.aniLen = 250


// This function is called in the body-tag of the html-file.
function scroll(id)
{
	if (scroll.mo5 || (scroll.ie4 && scroll.pc) || scroll.ie5) 
	{
		this.id = id		
		this.getMembers(id) // function-call for getting the tag-id's
		
		if (scroll.ie4 && !scroll.ie5 && scroll.pc)
		{
    		this.description.style.width = scroll.ie4pcDescWidth
    }
		
		// Get all the numbers describing the geometry.
		// Width and heights must be desribed in the html-file,
		// stylesheets alone won't do, otherwise NaN's occur.
		this.clipH		  = parseInt(this.container.style.height)		
		this.PTags		  = ypGetDescendantsByTagName("P", this.content)
		
		var lastP		    = this.PTags[this.PTags.length-1]
		var lastPTop	  = lastP.offsetTop - scroll.mozAdjust
		
		this.docH		    = lastPTop + Math.max(lastP.offsetHeight, this.clipH)		
		this.scrollH	  = this.docH - this.clipH	
		this.thumbMax	  = parseInt(this.thumbContainer.style.height) - this.thumbImg.height		
			
		// Eventhandling when using the scrollbar
		this.gRef = "scroll_"+id
		eval(this.gRef+"=this")
		this.thumb.obj	= this
		this.thumb.onmousedown = this.startDrag
		this.thumb.ondragstart = function() { return false }	
  }	
	return this;
}

//
scroll.prototype.getMembers = function(id) 
{
	this.container      = ypGetElementById(id+'Container');
	this.content        = ypGetElementById(id+'Content');
	this.thumb          = ypGetElementById(id+'Thumb');	
	this.thumbImg       = ypGetElementById(id+'ThumbImg');	
	this.thumbContainer = ypGetElementById(id+'ThumbContainer');
}

// Eventhandler when first pushing the scrollbar
scroll.prototype.startDrag = function(e) 
{
	if (!e) e  = window.event
	var ey     = e.pageY ? e.pageY : e.clientY
	this.dragLastY       = ey
	this.dragStartOffset = ey - parseInt(this.style.top)
	scroll.current       = this.obj
	document.onmousemove = this.obj.doDrag
	document.onmouseup   = this.obj.stopDrag
	if (this.obj.aniTimer) window.clearInterval(this.obj.aniTimer)
	return false;
}

// Eventhandler when dragging the scrollbar
scroll.prototype.doDrag = function(e) 
{
	if (!e) e = window.event
	var obj   = scroll.current
	var ey    = (e.pageY ? e.pageY : e.clientY)
	var dy    = ey - obj.thumb.dragLastY
	var ny    = parseInt(obj.thumb.style.top) + dy
	if (ny >= obj.thumbMax) 
	   obj.thumb.dragLastY = obj.thumbMax + obj.thumb.dragStartOffset
	else if (ny < 0) 
	   obj.thumb.dragLastY = obj.thumb.dragStartOffset
	else 
	   obj.thumb.dragLastY = ey;
	ny = Math.min(Math.max(ny, 0), obj.thumbMax)
	obj.jumpTo(ny * obj.scrollH / obj.thumbMax)
	return false;
}

// Eventhandler when stopping the scrollbar
scroll.prototype.stopDrag = function() 
{
	this.onmousemove = null
	this.onmouseup   = null	
}


//
scroll.prototype.jumpTo = function(ny) 
{
	this.thumb.style.top	= Math.round(ny * this.thumbMax / this.scrollH)	
	this.content.style.top	= -ny
}

//
function ypGetChildNodes(objParent) 
{
	return (objParent.childNodes ? objParent.childNodes : objParent.children)
}

//
function ypGetElementById(id) 
{
	return (document.getElementById ? 
	        document.getElementById(id) : 
					document.all ? document.all[id] : false)
}

// 
function ypGetDescendantsByTagName(sTag, objParent)
{
	return (objParent.getElementsByTagName ? 
	        objParent.getElementsByTagName(sTag) : 
					objParent.all && objParent.all.tags ? 
					objParent.all.tags(sTag) : false)
}