/*	TEXTBOX CODE
	(c) 2008, Red Beagle Web Development
	Code to mimic a scrolling text box within a fixed set of dimensions on a web page.
	This is assumed to be preferred to an iframe or frame because it is easily searchable, and searches will
	always lead you to the correct page, and the look is browser-independent and customizable.
	
	The default state of the slider bar and arrows is invisible, and are only made visible by a call to
	textbox_init(), and when they'll be needed: if the full text box is larger than the text window.
*/

// Variables based on the client
var textbox_fixed_height=380;	// This is the height of the text area. Could it be document.getElementById("textbox").offsetHeight?
var textbox_step=10;			// The number of pixels to scroll for each time through the routine, if I'm using the arrows
var textbox_arrow_height=12;	// The height of two arrows

// Variables used by the program.
var textbox_offset=0;			// This holds the amount that the textbox is offset from 0
var textbox_roll_active=false;	// Flag to know if I should continue scrolling on a mousedown
var textbox_tracker_active=false;
var textbox_obj;				// The inner text box object: this is what I offset to effect the scrolling of text
var textbox_slider_obj;			// The sliding bar to the right object: this moves along with the text box as a reference to its full height
var textbox_max_scroll;			// The maximum amount I am allowed to scroll. Full text box height minus the window height
var textbox_slider_height=0;	// Slider height is proportional to the ratio of the window height to the full text box height

// Initialize the text box	
function textbox_init() {
	textbox_obj=document.getElementById("textbox_words");				// Get the full text box object
	textbox_slider_obj=document.getElementById("textbox_slider");		// Get the slider bar object
	textbox_max_scroll=textbox_fixed_height-textbox_obj.offsetHeight;	// What's the difference between the full text box height and
																		// the containing window?
	if (textbox_max_scroll<0) {											// If <0, then the buttons and scrolling are active for this page
		document.getElementById("textbox_up").style.display="block";	// Display the up arrow
		document.getElementById("textbox_down").style.display="block";	// Display the down arrow
		// Determine the height of the slider.
		textbox_slider_height=Math.floor((textbox_fixed_height-(textbox_arrow_height*2))*textbox_fixed_height/textbox_obj.offsetHeight);
		// This is the ratio of text box motion to slider bar motion. Works both ways
		slider_ratio=(textbox_fixed_height-(textbox_arrow_height*2)-textbox_slider_height)/textbox_max_scroll;
		textbox_slider_obj.style.height=textbox_slider_height+"px";		// Set the slider bar to the correct height
		document.getElementById("textbox_slider_south").style.top=textbox_slider_height+"px";		// Set the slider bar to the correct height
		document.getElementById("textbox_slider_area").style.display="block";						// Display the slider bar
		textbox_slider_obj.onmousedown=textbox_track;
	}
}

/* TEXTBOX_MOVE(amount)
	Move the text box and slider bar by the amount specified. Set the timeout to repeat this routine.
	If I don't want it to repeat on its own, then make textbox_roll_active=false after calling this routine.
*/
function textbox_move(amount) {
	if (textbox_roll_active) {
		textbox_offset=Math.min(0, Math.max(textbox_max_scroll, textbox_offset+amount)); 
		textbox_obj.style.top=textbox_offset+"px";
		textbox_slider_obj.style.top=(Math.floor(slider_ratio*textbox_offset))+"px";
		setTimeout("textbox_move("+amount+")", 50);
	}
}

// This is called by the arrow buttons, direx=1 or -1 based on which arrow. It initiates rolling the box
function begin_roll(direx) {
	textbox_roll_active=true;
	textbox_move(direx*textbox_step);
}

// Cancel any automatic calls to textbox_move, and prevent future ones
function cancel_roll() {
	textbox_roll_active=false;
}

function textbox_jump(direx) {	// I have clicked either above or below the scrollbar, but not on the arrow.
								// This means, jump up or down by the full height of the text box.
	textbox_track_cancel();
	textbox_roll_active=true;
	textbox_move(direx*textbox_fixed_height);
	cancel_roll();
}

/* TEXTBOX_TRACK
	I have clicked in the slider bar area. Determine if I am "on" the slider bar, then set up to track the mouse appropriately.
	As I offset the mouse, I need to offset the text area by mouse_offset/slider_ratio
*/
function textbox_track(e) {
	if (!e) var e = window.event;
	textbox_start_y=textbox_mouseY(e);
	textbox_tracker_active=true;
}

function textbox_track_move(e) {
	if (textbox_tracker_active) {
		if (!e) e=window.event;
		textbox_new_y=textbox_mouseY(e);
		amount=(Math.floor((textbox_new_y-textbox_start_y)/slider_ratio));
		textbox_start_y=textbox_new_y;
		textbox_offset=Math.min(0, Math.max(textbox_max_scroll, textbox_offset+amount)); 
		textbox_obj.style.top=textbox_offset+"px";
		textbox_slider_obj.style.top=(Math.floor(slider_ratio*textbox_offset))+"px";
	}
}

function textbox_track_cancel() { textbox_tracker_active=false; }

// These settings get us rolling
document.onmousemove=textbox_track_move;
document.onclick=textbox_track_cancel;
document.onmouseup=textbox_track_cancel;

function textbox_mouseY(e) {
	if (!e) var e = window.event;
	if (e.pageY) return e.pageY;
	else if (e.clientY) return e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	return null;
}

function textbox_return_to_top() { //alert("zoober!!!");
	textbox_jump(1000);
}