// DivScroller v1.1
// Author: Ian W. Zajdel
// Date: 10/5/2007 (updated 5/31/08)
// Purpose: Script to a wide div inside 'window' div to various sections within the div.

//scroll speed
var scroll_rate = 9;

//scroll distance modifier
var scroll_modifier = 0.33;

//default section
var current_section = "welcome_pane";

function start_scroll(scroller, section, frame)
{	
	if(current_section == section)
	{
		return;
	}
	
	//change highlighted link and update current_section
	document.getElementById(current_section.split("_")[0] + "_tab").className = "inactive";
	document.getElementById(section.split("_")[0] + "_tab").className = "active";
	current_section = section;
	
	var scroller_position = get_position(scroller);
	var section_position = get_position(section);
	var frame_position = get_position(frame);
	var scroller_element = document.all ? eval("document.all."+scroller) : document.getElementById(scroller);
	var frame_element = document.all ? eval("document.all."+frame) : document.getElementById(frame);
	
	/*
	The scroll start should add on the frame's border width, but I haven't been able to read that
	attribute's value with Javascript yet. To get around this, the border width could be passed in,
	but that would be tedious to update. The border could be achieved by wrapping a div around the
	frame div.
	*/
	var scroll_start = scroller_position[0] - frame_position[0];
	var scroll_end = scroller_position[0] - section_position[0];
	if(scroll_start != scroll_end)
	{
		scroll(scroller_element, scroll_start, scroll_end);
	}
}

function scroll(scrolling_element, f, t){

	var sign;
	var new_position;
	var change;
	if(f < t)	//scrolling right
	{
		sign = 1;
	}
	else		//scrolling left
	{
		sign = -1;
	}
	
	//scroll faster when farther away, slow down when close to destiniation
	change = Math.floor( ((t - f) * sign) * scroll_modifier) + 1;
	new_position = f + (sign * change);
	scrolling_element.style.left = new_position + "px";

	if(new_position != t)
	{
		setTimeout(function(){scroll(scrolling_element, new_position, t);}, scroll_rate);
	}
}

function get_position(id)
{
	var e = document.all ? eval("document.all."+id) : document.getElementById(id);
	var l = t = 0;
	if (e.offsetParent) {
		l = e.offsetLeft;
		t = e.offsetTop;
		while (e = e.offsetParent)
		{
			l += e.offsetLeft;
			t += e.offsetTop;
		}
	}
	return Array(l,t);
}
