///////////////////////////////////////////////////////////////////////////////
// OSU Institute of Technology standard JavaScript functions                 //
///////////////////////////////////////////////////////////////////////////////
//                                                                           //
// (C) 2008-2009 Oklahoma State University Institute of Technology           //
//                                                                           //
// License: CC-by-SA-3.0 <http://creativecommons.org/licenses/by-sa/3.0/>    //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////

///////// UTILITY FUNCTIONS /////////

// returns the top of the box by relying on its parents
// core functionality borrowed from <http://www.aspandjavascript.co.uk/javascript/javascript_api/get_element_top_left.asp>
function get_top(el) {
	elTop = el.offsetTop; // the top is going to be at least the offset of this element
	parentObj = el.offsetParent; // if there's a parent, add its top to our function.
	
	// if there's a parent object, keep adding its offset until it goes away.
	while (parentObj != null) {
		elTop += parentObj.offsetTop;
		parentObj = parentObj.offsetParent;
  	}
	return parseFloat(elTop);
}

///////// SEARCH BOX HELPERS /////////

// Executed when the searchbox is moved out of
function global_searchbox_blur() {
	if ($('globalsearchbox').value == '') {
		$('globalsearchbox').value = 'search...';
		$('globalsearchbox').style.color = '#ff6600';
	}
}

// executed when the searchbox gets focus
function global_searchbox_focus() {
	if($('globalsearchbox').value == 'search...') {
		$('globalsearchbox').value = '';
		$('globalsearchbox').style.color = '#000000';
	}
}


///////// TABLE OF CONTENTS HELPERS /////////

// Scroll handler for Table of Contents
// FIXME: If the ToC box is larger than the viewport, we shouldn't scroll it.
function toc_scroll() {
	if ($$('div.sidetoc').length > 0) {
		var el = $$('div.sidetoc')[0]; // get the first side table of contents
		var content = $$('div.mcontent')[0]; // get the content box

		var el_top = get_top(el);
		var el_height = parseFloat(el.getStyle("height"));
		
		var content_top = get_top(content);
		var content_height = parseFloat(content.getStyle("height"));

		// set up the "window track" for the ToC box
		var min_top = content_top;
		var max_top = (content_top + content_height) - el_height;

		// determine where we want to scroll to
		var new_top = content_top + document.viewport.getScrollOffsets().top;

		// store the bottom of the viewport for determining if a box went below the bottom.
		var viewport_bottom = document.viewport.getScrollOffsets().top + document.viewport.getHeight();

		// now morph to the correct location (either the top or bottom of the track or in the middle)
		if (new_top < min_top)
			el.morph("top: " + min_top + "px;");
		else if (new_top > max_top)
			el.morph("top: " + max_top + "px;");
		else
			el.morph("top: " + new_top + "px;");
	}
}

// When the page loads, set up the scroller, then start the periodic executer to make it slide across the page.
Event.observe(window, 'load', function() { toc_scroll(); new PeriodicalExecuter(toc_scroll, 1); });



