/**
* ScrollbarReplacement v0.2
*	By: me@daantje.nl
*	
*	Description:
*		This script will replace the normal scrollbars of the browser...
*		Works in MSIE 5 or higher and Mozilla 1.3 or higher and Netscape 6 or higher.
*
*	Documentation:
*		The IFRAME needs a name AND id parameter!
*		The content html needs an object with the id 'content' I use it to messure 
*		the correct height of the content that needs to be scrolled.
*
*	License:
*		This script is free to use under LGPL license.
*/

/*
	EXAMPLE HTML scrollbar.html

<html>
<script language="JavaScript" src="scrollbar.js"></script>
<style>
#scroller{
	cursor:hand;
}
</style>
<body>

<table border="1" height=500>
<tr>
	<td><iframe src=content.html scrolling="no" height=100% id=frame1 name=frame1></iframe></td>
	<td>
		<div style="height:300px;width:20px;background-color:#ff9900;" ID="container1">
		<div height=5 ID="bar1">
		<table border='0' cellspacing='0' cellpadding='0'>
		<tr><td ID=drag style="width:20px;height:1px;background-color:#ff0000;"><spacer width=2></td></tr>
		<tr><td ID=scroller style="width:20px;height:10px;background-color:#000000;"><spacer width=2></td></tr>
		</table>
		</div>
		</div>
	</td>
</tr>
</table>

</body>
</html>


	EXAMPLE HTML content.html
<html>
<head>
	<title></title>
</head>
<body>

<div id=content> <!-- IMPORTANT OBJECT!!! -->
Here some stuff to scroll
</div>

</body>
</html>
	END OF EXAMPLE HTML
*/

var ie = document.all;
var ns6 = document.getElementById && !document.all;

function drag_drop(e){
	if(scrollerapproved){
		ee = ie ? event.clientY : e.clientY;
		
		//when mouse pos is within the scroll bar, resize the scrollbar...
		if(ee > parseInt(crossobjContainer.offsetTop) + (parseInt(crossobjRSR.style.height) * 2) && ee < parseInt(crossobjContainer.style.height) + parseInt(crossobjContainer.offsetTop) + parseInt(crossobjRSR.style.height)){
			//contents
			crossobj.style.height = crossobjDrag.style.height = ee - tempy - (parseInt(crossobjRSR.style.height) * 2);
			//scroll block (mousepointer pos)
			crossobjRSR.style.top = parseInt(crossobjContainer.offsetTop) - parseInt(crossobjRSR.style.height);
			//scroll the frame...
			scrollFrame('frame1',crossobj.style.height);
		}
	}
	return false;
}

function initializedrag(e){
	resetdrag();
	if (ie || ns6){
		/* container */ crossobjContainer = document.getElementById ? document.getElementById('container1') : document.all.container1;
		/* bar   	 */ crossobj = document.getElementById ? document.getElementById('bar1') : document.all.bar1;
		/* drag  	 */ crossobjDrag = document.getElementById ? document.getElementById('drag') : document.all.drag;
		/* rszR  	 */ crossobjRSR = document.getElementById ? document.getElementById('scroller') : document.all.scroller;
	}
	offsety = ie ? event.clientY : e.clientY;
	//get top pos of scrollbar...
	tempy = parseInt(crossobjContainer.offsetTop);
	//check if we are in the scrollbar block...
	if((ie && event.srcElement.id=='scroller') || (ns6 && e.target.id=='scroller'))
		scrollerapproved = true;
	document.onmousemove = drag_drop;
}

function resetdrag(){
	scrollerapproved=false;
}

document.onmousedown = initializedrag;
document.onmouseup = resetdrag;

function scrollFrame(frameID,scrollPos){
	heightCorrection = 30; //my height seems not correctly calculated...
	crossobjFrame = document.getElementById ? document.getElementById(frameID) : document.all.frameID;
	
	//get content height within the iframe
	if(window.frames[frameID]){
		if(!window.frames[frameID].document.getElementById('content'))
			contentHeight = window.frames[frameID].document.body.offsetHeight + heightCorrection;
		else
			contentHeight = window.frames[frameID].document.getElementById('content').offsetHeight + heightCorrection;
	}else{
		contentHeight = document.getElementById(frameID).contentDocument.getElementById('content').offsetHeight + heightCorrection;
	}
				
	//calc where to scroll to
	framePos = Math.round(((contentHeight - crossobjFrame.offsetHeight) / 100) * (parseInt(scrollPos) / (crossobjContainer.offsetHeight / 100)));
	//scroll...
	if(window.frames[frameID])
		window.frames[frameID].scrollTo(0, framePos);
	else
		document.getElementById(frameID).contentWindow.scrollTo(0, framePos);		
}