/*
 *
 *	Author: Ashutosh Raghuwanshi.
 *	E-Mail: ashutosh@pragya.net.in
 *	URL   : http://pragya.net.in
 *  Notice: You may freely use this script as long as you keep this banner as it is.
 *
 */
/*@cc_on @*/
/*@if (@_jscript_version > 3)
   try{window.status = "Using JScript version " + @_jscript_version;}catch(exception){}
   @else @*/
   alert("You need a more recent script engine.");
/*@end @*/

/*
 *       -: INSTALL INSTRUCTION :-
 *
 * 3) At first put this line before </head> :-
 *
 *    <script language="javascript" src="scroller.js" type="text/javascript"></script>
 *
 * 1) Enclose all html inside the <body></body> with a <div></div> or table tag.
 * 2) Set the attribute of <div> as the following one:-
 *
 *    <div id="menus" style="position:absolute;top:17px;left:2px;">
 *     . . . Your page here . . .
 *    </div>
 *
 * 3) After that put this line before </body> :-
 *
 *    <script language="javascript" type="text/javascript">
 *    <!--
 *      attach_scroller();
 *    // -->
 *    </script>
 *
 * 4) Copy "up_scrol.gif" and "dn_scrol.gif" files to the same folder as the HTML page.
 * 5) Add the menu.css to the page between <head> and </head>, like this:-
 *
 *    <link rel="stylesheet" type="text/css" href="./menu.css">
 *
 */

var Scroller_Exists=true;
var Going_Up = false;
var Timer = 0;
var Up_Limit;
var Down_Limit;

function Step(Delta, ULimit, DLimit){
var cur_pos = parseInt(document.all.menus.offsetTop);
var Incr = Delta;
	if(Delta > 0 && cur_pos == DLimit) return;
	if(Delta < 0 && cur_pos == ULimit) return;
	if(Delta > 0 && cur_pos+Incr > DLimit){ cur_pos = DLimit; Incr=0;}
	if(Delta < 0 && cur_pos+Incr < ULimit){ cur_pos = ULimit; Incr=0;}
	document.all.menus.style.top = "" + (cur_pos + Incr) + "px";
	if(document.onscroll != null) document.onscroll();
}

function Scroll(obj){
	obj.style.borderStyle = "inset";
	Going_Up = (obj.title == "Scroll Down");
	obj.all.trow.style.verticalAlign = Going_Up ? "bottom" : "top";
	clearInterval(Timer);
	Up_Limit = document.body.clientHeight - document.all.menus.offsetHeight - document.all.down_scroller.offsetHeight;
	Down_Limit = document.all.down_scroller.offsetHeight;
	Timer = setInterval("Step(" + (Going_Up ? (-4) : 4) + ", "+ Up_Limit +", " + Down_Limit + ")", 25);
}

function Scroll_Fast(){
	clearInterval(Timer);
	Timer=setInterval("Step(" + (Going_Up ? (-20) : 20) + ", " + Up_Limit + ", " + Down_Limit + ")", 25);
}

function Stop_Scroll(obj){
	obj.style.borderStyle = "outset";
	clearInterval(Timer);
	obj.all.trow.style.verticalAlign = "middle";
}

function set_scroller_pos(){
	if((document.all.menus.offsetHeight) > document.body.clientHeight){
		if(!Scroller_Exists){
			document.all.menus.style.top = document.all.down_scroller.offsetHeight;
			document.all.up_scroller.style.visibility = "visible";
			document.all.down_scroller.style.visibility = "visible";
			Scroller_Exists = true;
		}
		document.all.up_scroller.style.top = "0px";
		//alert(document.all.down_scroller.style.top = "" + (document.body.offsetHeight-document.all.down_scroller.offsetHeight) + "px");
	}
	else{
		document.all.menus.style.top = 0;
		document.all.up_scroller.style.visibility = "hidden";
		document.all.down_scroller.style.visibility = "hidden";
		Scroller_Exists = false;
	}

}

function handle_wheel(){
	Up_Limit = document.body.clientHeight - document.all.menus.offsetHeight - document.all.down_scroller.offsetHeight;
	Down_Limit = document.all.down_scroller.offsetHeight;
	Step(event.wheelDelta, Up_Limit, Down_Limit);
}

function attach_scroller(){
	document.write('<table id="up_scroller" class="scroll-button" style="width=100%;position:absolute;top:-100px;left:0px;" border="0" title="Scroll Up" cellspacing="0" cellpadding="2"  onmousedown="Scroll_Fast();" onmouseup="Stop_Scroll(this);Scroll(this);" onmouseover="Scroll(this);" onmouseout="Stop_Scroll(this);">');
	document.write('<tr id="trow" valign="middle">');
	document.write('	<td align="center"><img src="up_scrol.gif" alt="Up" border="0" width="7" height="4"></td>');
	document.write('</tr>');
	document.write('</table>');
	document.write('<table id="down_scroller" class="scroll-button" style="width=100%;position:absolute;top:-100px;left:0px;" border="0" title="Scroll Down" cellspacing="0" cellpadding="3"  onmousedown="Scroll_Fast();" onmouseup="Stop_Scroll(this);Scroll(this);" onmouseover="Scroll(this);" onmouseout="Stop_Scroll(this);">');
	document.write('<tr id="trow" valign="middle">');
	document.write('	<td align="center"><img src="dn_scrol.gif" alt="Down" border="0" width="7" height="4"></td>');
	document.write('</tr>');
	document.write('</table>');

	set_scroller_pos();
	document.body.onresize = set_scroller_pos;
	document.onmousewheel = handle_wheel;
}
