// JavaScript Document


// gives up and down scroll buttons to images, spans, ... named up_name, down_name, respectively.
// will keep the default scroll_box's style overflow if it encounters errors (so make overflow: auto;)

// usage: put this after the scrollbox div:  var div_scroll1 = new TextScroll('div_scroll1', 'scroll_box');
function TextScroll(scrollname, window_div, content_div, scroller_wrapper, up_name, down_name) {
	
    this.window_div = window_div;
	//this.content_div = content_div;
    this.name = scrollname;
    this.scrollCursor = 0;
    this.speed = 5;
    this.timeoutID = 0;
    this.div_obj = null;
    this.up_name = up_name;
    this.dn_name = down_name;
	
    if (document.getElementById) {
			
		div_obj = document.getElementById(this.window_div);
			
		if (div_obj) {
			this.div_obj = div_obj;
			this.div_obj.style.overflow = 'hidden';
		}
			
		div_up_obj = document.getElementById(this.up_name);
		div_dn_obj = document.getElementById(this.dn_name);
					
		if (div_up_obj && div_dn_obj) {
				
			div_up_obj.onmousedown = function() { eval(scrollname + ".scrollUp();") };
			div_up_obj.onmouseup = function() { eval(scrollname + ".stopScroll();") };
			div_up_obj.onmouseout = function() { eval(scrollname + ".stopScroll();") };
							
			div_dn_obj.onmousedown = function() { eval(scrollname + ".scrollDown();") };
			div_dn_obj.onmouseup = function() { eval(scrollname + ".stopScroll();") };
			div_dn_obj.onmouseout = function() { eval(scrollname + ".stopScroll();") };
						
		}
		
		
		
		// disable the buttons if they're not needed (they're turned on by default)
			
		scroller_obj = document.getElementById(scroller_wrapper);
		
		var window_height = $(window_div).getHeight();
		var content_height = $(content_div).getHeight();
		
		//alert("Window Height = " + window_height + "\nContent Height = " + content_height);

		if (content_height <= window_height) {
			this.scroller_obj = scroller_obj;
			this.scroller_obj.style.display = "none";
		} else {
			this.scroller_obj = scroller_obj;
			this.scroller_obj.style.display = "block";
		}

	
		// disable the scroll up button by default
		$(this.up_name).addClassName('disabled');
		
	}
	


	this.stopScroll = function() {
		clearTimeout(this.timeoutID);
		if (this.scrollCursor == 0) {
			$(this.up_name).addClassName('disabled');
		}
	}
	
	

	this.scrollUp = function() {
		if (this.div_obj) {
			this.scrollCursor = (this.scrollCursor - this.speed) < 0 ? 0 : this.scrollCursor - this.speed;
			this.div_obj.scrollTop = this.scrollCursor;
			this.timeoutID = setTimeout(this.name + ".scrollUp()", 60);
			$(this.dn_name).removeClassName('disabled');
		}
	}
	
	

	this.scrollDown = function() {
		if (this.div_obj) {
			
			$(this.up_name).removeClassName('disabled');

			this.scrollCursor += this.speed;
			this.div_obj.scrollTop = this.scrollCursor;
			//this.timeoutID = setTimeout(this.name + ".scrollDown()", 60);
						
						
			if (this.div_obj.scrollTop == this.scrollCursor) {
				this.timeoutID = setTimeout(this.name + ".scrollDown()", 60);
			} else {
				this.scrollCursor = this.div_obj.scrollTop;
				$(this.dn_name).addClassName('disabled');
			}
					
		}
	}
	
	

	this.resetScroll = function() {
		if (this.div_obj) {
			this.div_obj.scrollTop = 0;
			this.scrollCursor = 0;
		}
	}
	
	
}








/* -----------------------------------------------------  *//*

	Scheduling script that checks to make sure dependencies 
	have been fully loaded before executing a function call
	
	For example, it will make sure the page content is 
	fully loaded before measuring it to see if scrolling 
	is necessary
	
*//* -----------------------------------------------------  */


// schedule("table5", "showTable(\"1\")\;");
// schedule("tableSelector", "document.getElementById(objectID).onchange = selectTable\;");

function schedule(objectID, functionCall) {
	
	if (document.getElementById(objectID)) {
		
		eval(functionCall);
		
	} else {
		
		//alert("It didn't work");
		setTimeout("schedule('" + objectID + "', '" + functionCall + "')", 1);
		
	}
	
	return true;
	
}












	function setupMainContentScrolling() {
		// id_of_scroll_instance, containing_window_element, stretchable_wrapper_div, scroller_buttons_wrapper, up_name, down_name
		content_scroll = new TextScroll('content_scroll', 'page-content', 'content-height', 'scroller', 'scroll_up', 'scroll_down');
	}
	
	
	function setupSideBarContentScrolling() {
		// id_of_scroll_instance, containing_window_element, stretchable_wrapper_div, scroller_buttons_wrapper, up_name, down_name
		content_scroll = new TextScroll('content_scroll', 'side-bar', 'side-bar-content-height', 'scroller', 'scroll_up', 'scroll_down');
	}

	













/* -----------------------------------------------------  *//*

	Finds all anchors with REL attribute of 'external' 
	and adds a target value of '_blank' for valid 
	xhtml 1.1 external links - used for css and xhtml 
	validation links in footer only (tired of having to 
	wait for the w3c server before I can continue 
	tweaking).
	
		Version:					1.0
		Author:						Alex Nichol
		Email:						alex@aelius.net	

		Current Revisions by:*		n/a
		Email:						n/a
	
		* Please initial and date revisions
	
*//* -----------------------------------------------------  */


function externalLinks() {
	
	if (document.getElementsByTagName) {
		
		var anchors = document.getElementsByTagName('a');
		
		for (var i = 0; i < anchors.length; i++) {
			
			if (anchors[i].getAttribute('href') && anchors[i].getAttribute('rel') == 'external') {
				anchors[i].target = '_blank';
				if (anchors[i].getAttribute('title')) {
					anchors[i].title += ' (Opens in a New Window)';
				}
			}
		}
		
	}
}





