var currentColumn = 0;
var countColumns = 0;
var scrollpanel;
var scrollduration = 25;
var colStartsAt = 0;
var columnWidth = 425;
var columnmargin = 20;
var lineHeight = 21;
var rowcount = 21;
var maxHeight = lineHeight * rowcount; // i.e. each page fits 19 rows of text


function mousewheel(e) {
	//scrollpanel.scrollLeft -= (e.wheelDelta / 3);
}

var scrollthread = null;
var lastscrollpos = 0;

function adjustscroll(e) {
	if (scrollthread != null) {
		clearTimeout(scrollthread);
	}
	
	scrollthread = setTimeout("doAdjustscroll()", 300);
}

function doAdjustscroll() {
	adjustment = scrollpanel.scrollLeft % (columnWidth + columnmargin);	
	if (adjustment != 0) {
		if (Math.abs(adjustment) <= 10) // the user clicked the right arrow button: adjust the div to the right
			ScrollNext();
		else if (Math.abs(adjustment - (columnWidth + columnmargin)) <= 10) // the user clicked the left arrow button: adjust the div to the left
			ScrollPrevious();
		else { // the user used the bar or the area between the bar and an arrow button: adjust the div to the nearest
			if (adjustment > (columnWidth/2)) // adjust to right
				ScrollTo(parseInt(scrollpanel.scrollLeft / (columnWidth + columnmargin)) + 1);
			else // adjust to left
				ScrollTo(parseInt(scrollpanel.scrollLeft / (columnWidth + columnmargin)));
		}
	}
	else {
		currentColumn = (scrollpanel.scrollLeft / (columnWidth + columnmargin));
	}
	lastscrollpos = scrollpanel.scrollLeft;
}

function ScrollNext() {
	OffsetScroll(1);
}

function ScrollPrevious() {
	OffsetScroll(-1);
}

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

function OffsetScroll(offset) {
	
	target = currentColumn + offset;		
	if (target < 0 || target > countColumns) 
		return;
	
	ScrollTo(target);
}

function ScrollTo(target)
{
	if (target < 0) return;
	doScrollTo(target);
}

function doScrollTo(target) {
	if (target >= countColumns)
		return;
	
	currentColumn = target;
	targetcol = document.getElementById('col-' + target);
	


	if (target == (countColumns - 1)) {		
		offsetLeft = scrollpanel.scrollWidth - scrollpanel.offsetWidth;
	}
	else {
		offsetLeft = 0;
		//alert ("targetcol: col-" + target + ' offsetleft=' + targetcol.offsetLeft + ' classname=' + targetcol.className);
		do {
			offsetLeft += targetcol.offsetLeft;
		} while ( targetcol = targetcol.offsetParent )
		
		
		targetcol = document.getElementById('col-0');
		colStartsAt = 0;
		do {
			colStartsAt += targetcol.offsetLeft;
		} while ( targetcol = targetcol.offsetParent )
		offsetLeft -= colStartsAt;
	}
	
	
	if (scrollanim.timer != null) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	}	
	
	scrollanim.time = 0;
	scrollanim.begin = scrollpanel.scrollLeft;
	scrollanim.change = offsetLeft - scrollpanel.scrollLeft;	
	scrollanim.duration = scrollduration;
	scrollanim.element = scrollpanel;
	scrollanim.timer = setInterval("scrollHorizAnim();", 15);
}

var scrollanim = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};

function scrollHorizAnim()
{
	if (scrollanim.time > scrollanim.duration) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	}
	else {
		move = -scrollanim.change/2 * (Math.cos(Math.PI*scrollanim.time/scrollanim.duration) - 1) + scrollanim.begin;
		scrollanim.element.scrollLeft = move;
		scrollanim.time++;
	}
}

//-- 

function initColumnLayout() {
	contentsDiv = document.getElementById('contents');
	contentHeight = contentsDiv.scrollHeight;
	if (contentHeight < maxHeight)
		return;
		
		
	
//	contentsDiv.style.padding = '0';
	contentsDiv.style.overflow = 'hidden';
	columns = document.createElement('div');
	columns.id = 'columns';
	columns.style.height = maxHeight + 'px';
	columns.style.overflow = 'hidden';
	
	
	pagenumbers = document.createElement('div');
	pagenumbers.id = 'pagenumbers';
	
	// setup divs for the horizontal scrolling feature
	scrollpanel = document.createElement('div');
	scrollpanel.id = 'scrollpanel';
	scrollpanel.appendChild(columns);
	scrollpanel.appendChild(pagenumbers);
	contentsDiv.appendChild(scrollpanel);
	
		
		
		
	var nextOffset = 0;
	var lastColumnHeight = 0;
	var i = 0;
	var maxLoop = 200;
	
	while (contentsDiv.childNodes.length > 1) {
		i++;
		if (i>maxLoop) //TO PREVENT INFINITE LOOPS
			return;
		
		column = document.createElement('div');
		column.className = 'column';
		column.id = 'col-' + countColumns;		
		column.style.marginTop = (-nextOffset) + 'px';
		columns.appendChild(column);
		
		while (contentsDiv.childNodes.length > 1) {
			column.appendChild(contentsDiv.childNodes[0]);
			if (column.scrollHeight > (maxHeight + nextOffset)) { //if col is full
			
			    //is the overflowing element an additional
				if (column.lastChild.className.indexOf('additional') > -1) {
					contentsDiv.insertBefore(column.lastChild, contentsDiv.firstChild);
					
					//added by Ted 2008-03-17 to circumvent bug in Firefox which causes an infinite loop
					if (navigator.userAgent.indexOf("Firefox")!=-1)
					{
					    nextOffset = 1;
					}
					else
					{
					    nextOffset = 0;
					}

				}
				else 
				{
					elHeight = (column.scrollHeight - lastColumnHeight); //column height now - col height before add
					if (elHeight > maxHeight) { //element is larger than col
						contentsDiv.insertBefore(column.lastChild.cloneNode(true), contentsDiv.firstChild);
						remainder = (column.scrollHeight - (maxHeight + nextOffset)); 
						nextOffset = elHeight - remainder;
						lastColumnHeight = 0; 	
					} //if last child isn't visible, perhaps check if last element is header
					else if (column.scrollHeight - (maxHeight + nextOffset) == lineHeight && elHeight != lineHeight) {
						nextOffset = 0;
					}
					else {
						contentsDiv.insertBefore(column.lastChild.cloneNode(true), contentsDiv.firstChild);
						column.lastChild.className = column.lastChild.className + ' noprint';
						remainder = (column.scrollHeight - maxHeight);
						nextOffset = (elHeight) - (remainder) + nextOffset;
						if(nextOffset == 0)
						{
						    //remove the last element because it's not visible
						    column.removeChild(column.lastChild);
						    var lastElem = column.childNodes[column.childNodes.length-1];
						    
						    //if last element is a header, move it to top of next page
						    if(lastElem.tagName.substring(0, 1) == 'H')
						    {
						        contentsDiv.insertBefore(lastElem, contentsDiv.firstChild);
						    }
						}
					}
				}
				break;
			}
			lastColumnHeight = column.scrollHeight;
		}
		a = column.getElementsByTagName('a');
		for (var k = 0; k < a.length; k++) {
			if (a[k].id == '')
				initInternalLink(a[k]);
		}
		countColumns++;
	}
	
	
	//columns added, now add the pagenumbers
	for (var i = 0; i < columns.childNodes.length; i++) {
		pagenumber = document.createElement('div');
		pagenumber.innerHTML = (i + 1) + ' (' + countColumns + ')';
		pagenumbers.appendChild(pagenumber);
	}
		
	columns.style.width = (columnWidth * countColumns + columnmargin * (countColumns - 1) + 10) + 'px';
	column.style.marginRight = '0';
	pagenumber.style.marginRight = '0';
	pagenumbers.style.width = columns.style.width;
	
	contentsDiv.style.width = '925px';
	contentsDiv.appendChild(scrollpanel);
	
	// setup divs for hiding scrollfeature
	div = document.createElement('div');
	div.id = 'leftpane';
	scrollpanel.appendChild(div);
	div = document.createElement('div');
	div.id = 'rightpane';
	scrollpanel.appendChild(div);
	
	if (scrollpanel.addEventListener) {
		scrollpanel.addEventListener('scroll', adjustscroll, false); 
	} 
	else if (scrollpanel.attachEvent) {
		scrollpanel.attachEvent('onscroll', adjustscroll);
		scrollpanel.attachEvent('onmousewheel', mousewheel);
	}
	
	// everything is set-up: check to see if there is an anchorlink requested, and scroll there if so. 
	pos = location.href.indexOf('#');
	if (pos > 0) {
		if (location.href.substring(pos, pos + 6) != '#user=' && location.href.indexOf('#hjalmar') == -1) {
			pos2 = location.href.indexOf('&', pos)
			if (pos2 < 0) {
				anc = 'id' + location.href.substring(pos + 1);
			}
			else {
				anc = 'id' + location.href.substring(pos + 1, pos2);
				sword = location.href.substring(pos2 + 3);
				if (sword.length > 2) {
					// allow "cross-tag" phrases
					sword = unescape(sword).replace(/ +/g, "(\\s+|\\s*<[^>]*>\\s*)");
					// exclude all hits within tags (tag-attributes)
					var re = new RegExp(">([^<]*)(" + sword + ")", "igm");
					scrollpanel.innerHTML = scrollpanel.innerHTML.replace(re, ">$1<span class=\"highlight\">$2</span>");
				}
			}
			
			if (pos2 - pos - 1 != 0 && location.href.length - pos - 1 != 0) {
				h = document.getElementById(anc);
				span = document.createElement('span');
				span.className = 'highlight';
				span.innerHTML = h.innerHTML;
				h.innerHTML = '';
				h.appendChild(span);
				col = parseInt(h.parentNode.id.substring(4));
				if (col > 0)
					ScrollTo(col);
			}
		}
	}
	
	// look for the financial menu, and hide it
}

