
var COLUMNLAYOUT = {};

COLUMNLAYOUT.fixedNoOfColumns = 2; // Set to -1 to make it adjustable so large screens contains more columns

COLUMNLAYOUT.Init = function() {
	COLUMNLAYOUT.contentDiv = document.getElementById('maincontent');
	this.ColumnWidth = 345;
	this.ColumnMargin = 20;
	if (COOKIE.Read('TextSize') == 'big') {
		this.LineHeight = 21;
		document.body.className += ' largefont';
	}
	else {
		this.LineHeight = 18;
	}

	this.Autoflow = true;
	this.CurrentColumn = 0;
	this.countColumns = 0;
	this.VisibleColumns = 0;

	this.ScrollDuration = 25;
	this.ScrollThread = null;
	this.ScrollLastPosition = 0;
	this.ScrollAnimation = { time: 0, begin: 0, change: 0.0, duration: 0.0, element: null, timer: null };

	POPUP.InitInternalLinks();
	EVENT.Add(window, 'resize', COLUMNLAYOUT.BeginResetLayout);
};
COLUMNLAYOUT.BeginResetLayout = function() {
	if (COLUMNLAYOUT.isResizing != null) 
		clearTimeout(COLUMNLAYOUT.isResizing);

	COLUMNLAYOUT.isResizing = setTimeout("COLUMNLAYOUT.ResetLayout()", 300);
}
COLUMNLAYOUT.ResetLayout = function() {
	if (COLUMNLAYOUT.fixedNoOfColumns < 0) {
		var availWidth = document.documentElement.clientWidth - COLUMNLAYOUT.scrollPanel.offsetLeft - COLUMNLAYOUT.scrollPanel.offsetParent.offsetLeft;
		if (COLUMNLAYOUT.scrollPanel.offsetWidth > availWidth) { // the columns overflows the width
			document.location.reload();
		}
		else if (COLUMNLAYOUT.scrollPanel.offsetWidth < COLUMNLAYOUT.scrollPanel.scrollWidth && COLUMNLAYOUT.scrollPanel.offsetWidth + COLUMNLAYOUT.ColumnWidth + COLUMNLAYOUT.ColumnMargin < availWidth) { // there is room for another column
			document.location.reload();
		}
		else if (Math.abs(COLUMNLAYOUT.WindowHeight - document.documentElement.clientHeight) > COLUMNLAYOUT.LineHeight) { // the height of the window has change more than a row height
			document.location.reload();
		}
	}
}

COLUMNLAYOUT.CreateTableIcons = function() {
	// replace inline tables with icons
	var filename = location.href;
	filename = filename.substring(filename.lastIndexOf('/') + 1);
	filename = filename.substring(0, filename.indexOf('.'));
	var tables = COLUMNLAYOUT.contentDiv.getElementsByTagName("table");
	for (var i = 0; i < tables.length; i++) {
		var tableicon = document.createElement('DIV');
		tableicon.className = 'illustration tableicon';
		var a = document.createElement('A');
		a.href = "javascript:;";
		a.style.background = 'url(' + filename + i + '.png) 0 24px no-repeat';

		// if the table was preceded with a heading, use that as iconheading
		a.innerHTML = '<h2 class="iconheader">' + tables[i].rows[0].cells[0].innerHTML + '</h2>';

		if (tables[i].previousSibling) {
			if (tables[i].previousSibling.tagName) {
				if (tables[i].previousSibling.tagName.toUpperCase().substring(0, 1) == 'H') {
					a.innerHTML = '<h2 class="iconheader">' + tables[i].previousSibling.innerHTML + '</h2>';
					COLUMNLAYOUT.contentDiv.removeChild(tables[i].previousSibling);
				}
			}
			else if (tables[i].previousSibling.previousSibling) {
				// adjust for firefox text nodes
				if (tables[i].previousSibling.previousSibling.tagName.toUpperCase().substring(0, 1) == 'H') {
					a.innerHTML = '<h2 class="iconheader">' + tables[i].previousSibling.previousSibling.innerHTML + '</h2>';
					COLUMNLAYOUT.contentDiv.removeChild(tables[i].previousSibling.previousSibling);
				}
			}
		}

		tableicon.appendChild(a);
		var tablediv = document.createElement('DIV');
		tablediv.className = 'hiddeninline';
		a.appendChild(tablediv);
		COLUMNLAYOUT.contentDiv.insertBefore(tableicon, tables[i]);
		tablediv.appendChild(tables[i]);
		EVENT.Add(a, 'click', POPUP.Show);
	}
};

COLUMNLAYOUT.RenderColumns = function() {
	// store the window heigth in a variable for later use in the resize function
	COLUMNLAYOUT.WindowHeight = document.documentElement.clientHeight;

	// calculate number of rows and columnheight
	var contentTop = COLUMNLAYOUT.contentDiv.parentNode.offsetTop - 120;
	var rowCount = Math.floor((document.documentElement.clientHeight - contentTop) / COLUMNLAYOUT.LineHeight) - 5;
	var maxHeight = COLUMNLAYOUT.LineHeight * rowCount;
	var contentHeight = COLUMNLAYOUT.contentDiv.scrollHeight;

	// create panels for columns, pagenumbers, and horisontal scroll-feature
	var columns = document.createElement('DIV');
	columns.id = 'columns';

	columns.style.height = maxHeight + 'px';
	var pagenumbers = document.createElement('DIV');
	pagenumbers.className = 'pagenumbers';

	COLUMNLAYOUT.scrollPanel = document.createElement('DIV');
	COLUMNLAYOUT.scrollPanel.id = 'scrollpanel';
	COLUMNLAYOUT.scrollPanel.appendChild(columns);
	COLUMNLAYOUT.scrollPanel.appendChild(pagenumbers);
	document.getElementById('content').insertBefore(COLUMNLAYOUT.scrollPanel, COLUMNLAYOUT.contentDiv);

	// adjust margins for inline graphics and add zoom button for enlargeable objects
	elCount = COLUMNLAYOUT.contentDiv.childNodes.length;
	var childNodes = COLUMNLAYOUT.contentDiv.childNodes;
	for (var i = 0; i < elCount; i++) {
		if (childNodes[i].className == 'illustration') {
			currentHeight = childNodes[i].offsetHeight;
			remainder = currentHeight % COLUMNLAYOUT.LineHeight;
			// add a bottom margin so that the object will align with the text
			childNodes[i].style.marginBottom = (COLUMNLAYOUT.LineHeight - remainder) + 'px';
			if (childNodes[i].firstChild.className == 'enlargeable') {
				var a = document.createElement('A');
				a.href = "javascript:;";
				a.appendChild(childNodes[i].firstChild);
				childNodes[i].appendChild(a);
				EVENT.Add(a, 'click', POPUP.Show);
			}
		}
	}

	// loop each block in the content and move them to new columns when heights are exceeded
	var nextOffset = 0;
	var lastColumnHeight = 0;
	var heightRemainder = 0;
	COLUMNLAYOUT.countColumns = 0;

	if (elCount < 1) return;

	while (COLUMNLAYOUT.contentDiv.childNodes[0]) {
		var column = document.createElement('DIV');
		column.className = 'column';
		column.id = 'col-' + COLUMNLAYOUT.countColumns;
		column.style.marginTop = (-nextOffset) + 'px';
		columns.appendChild(column);

		while (COLUMNLAYOUT.contentDiv.childNodes[0]) {
			if (COLUMNLAYOUT.contentDiv.childNodes[0].className && COLUMNLAYOUT.contentDiv.childNodes[0].className.indexOf('break') > -1) {
				COLUMNLAYOUT.contentDiv.childNodes[0].className = COLUMNLAYOUT.contentDiv.childNodes[0].className.replace('break', '');
				break;
			}
			column.appendChild(COLUMNLAYOUT.contentDiv.childNodes[0]);

			// if the column is full, we need to add another column
			if (COLUMNLAYOUT.Autoflow && column.scrollHeight > (maxHeight + nextOffset)) {
				if (column.lastChild.className.indexOf('illustration') != -1) {
					if (column.childNodes.length > 1) // put it back if this is only one graphical element that is to high
						COLUMNLAYOUT.contentDiv.insertBefore(column.lastChild, COLUMNLAYOUT.contentDiv.firstChild);

					nextOffset = 0;
				}
				else if (false) {
				}
				else {
					var elHeight = (column.offsetHeight - lastColumnHeight);
					if (elHeight > maxHeight) {
						COLUMNLAYOUT.contentDiv.insertBefore(column.lastChild.cloneNode(true), COLUMNLAYOUT.contentDiv.firstChild);
						column.lastChild.className = column.lastChild.className + ' noprint';
						heightRemainder = (column.scrollHeight - (maxHeight + nextOffset));
						nextOffset = (elHeight - heightRemainder);
						lastColumnHeight = 0;
					} else if ((column.scrollHeight - (maxHeight + nextOffset) == COLUMNLAYOUT.LineHeight) && (elHeight != COLUMNLAYOUT.LineHeight)) {
						nextOffset = 0;
					} else {
						COLUMNLAYOUT.contentDiv.insertBefore(column.lastChild.cloneNode(true), COLUMNLAYOUT.contentDiv.firstChild);
						column.lastChild.className = column.lastChild.className + ' noprint';
						heightRemainder = (column.scrollHeight - maxHeight);
						nextOffset = (elHeight) - (heightRemainder) + nextOffset;
						if (nextOffset == 0) {
							obj = column.removeChild(column.lastChild);
							var lastElem = column.lastChild;
							if (lastElem.tagName) {
								if (lastElem.tagName.toUpperCase().substring(0, 1) == 'H') {
									COLUMNLAYOUT.contentDiv.insertBefore(lastElem, COLUMNLAYOUT.contentDiv.firstChild);
								}
							}
						}
					}
				}
				break;
			}
			lastColumnHeight = column.scrollHeight;
		}
		COLUMNLAYOUT.countColumns++;
	}

	var widthOfAllColumns = (COLUMNLAYOUT.countColumns * COLUMNLAYOUT.ColumnWidth) + ((COLUMNLAYOUT.countColumns - 1) * COLUMNLAYOUT.ColumnMargin);
	column.style.marginRight = '0'; // remove the margin for the last column so it's width fit perfectly
	columns.style.width = widthOfAllColumns + 'px';

	// all the content is transfered into the scrollpanel. Hide the content div
	//COLUMNLAYOUT.contentDiv.style.display = 'none';
	COLUMNLAYOUT.contentDiv.appendChild(COLUMNLAYOUT.scrollPanel);

	// add the page numbers at the bottom
	for (var i = 1; i <= COLUMNLAYOUT.countColumns; i++) {
		var pagenumber = document.createElement('DIV');
		pagenumber.innerHTML = i + ' (' + COLUMNLAYOUT.countColumns + ')';
		pagenumbers.appendChild(pagenumber);
	}

	pagenumber.style.marginRight = '0'; // remove the margin for the last column so it's width fit perfectly
	pagenumbers.style.width = widthOfAllColumns + 'px';

	//calculate how many columns are visible and adjust dimensions for the scrollpanel
	var scrollPanelWidth = widthOfAllColumns;
	if (COLUMNLAYOUT.fixedNoOfColumns < 0) {
		if (COLUMNLAYOUT.countColumns < 3) {
			scrollPanelWidth = (3 * COLUMNLAYOUT.ColumnWidth) + (2 * COLUMNLAYOUT.ColumnMargin);
		}
		else {
			var availWidth = document.documentElement.clientWidth - COLUMNLAYOUT.scrollPanel.offsetLeft - COLUMNLAYOUT.scrollPanel.offsetParent.offsetLeft;
			while (scrollPanelWidth > availWidth) {
				scrollPanelWidth -= (COLUMNLAYOUT.ColumnWidth + COLUMNLAYOUT.ColumnMargin);
			}
		}
	}
	else {
		scrollPanelWidth = (COLUMNLAYOUT.fixedNoOfColumns * COLUMNLAYOUT.ColumnWidth) + ((COLUMNLAYOUT.fixedNoOfColumns - 1) * COLUMNLAYOUT.ColumnMargin);
	}

	// decrease the scrollpanelwidth if there is a fixed right column
	var fixedright = document.getElementById('fixedright');
	if (fixedright) {
		while (scrollPanelWidth + fixedright.offsetWidth > availWidth)
			scrollPanelWidth -= (COLUMNLAYOUT.ColumnWidth + COLUMNLAYOUT.ColumnMargin);
		fixedright.style.marginLeft = (scrollPanelWidth + COLUMNLAYOUT.scrollPanel.offsetLeft) + 'px';
		fixedright.style.top = COLUMNLAYOUT.scrollPanel.offsetTop + 'px';
	}

	COLUMNLAYOUT.scrollPanel.style.height = ((rowCount * COLUMNLAYOUT.LineHeight) + 55) + 'px';
	COLUMNLAYOUT.scrollPanel.style.width = scrollPanelWidth + 'px';

	COLUMNLAYOUT.VisibleColumns = Math.floor(scrollPanelWidth / COLUMNLAYOUT.ColumnWidth);
	EVENT.Add(COLUMNLAYOUT.scrollPanel, 'scroll', COLUMNLAYOUT.adjustScroll);

	// add clickable, transparent areas for overriding the ordinary click on a horizontal scroll-arrow
	var a = document.createElement('a');
	a.className = 'scrollarrow';
	a.style.top = (contentTop + COLUMNLAYOUT.scrollPanel.offsetHeight + 140) + 'px';
	if (isIE)
		tmpLeft = COLUMNLAYOUT.contentDiv.offsetLeft + COLUMNLAYOUT.contentDiv.parentNode.offsetLeft;
	else 
		tmpLeft = COLUMNLAYOUT.contentDiv.offsetLeft;
	

	a.style.marginLeft = tmpLeft + 15 + 'px';
	
	document.body.appendChild(a);
	EVENT.Add(a, 'mousedown', COLUMNLAYOUT.ScrollPrevious);
	a = document.createElement('a');
	a.className = 'scrollarrow';
	a.id = 'rightscrollarrow';
	a.style.top = (contentTop + COLUMNLAYOUT.scrollPanel.offsetHeight + 140) + 'px';
	if (isIE)
		tmpLeft = COLUMNLAYOUT.contentDiv.offsetLeft + COLUMNLAYOUT.contentDiv.parentNode.offsetLeft + scrollPanelWidth;
	else 
		tmpLeft = COLUMNLAYOUT.contentDiv.offsetLeft + scrollPanelWidth;
	
	a.style.marginLeft = tmpLeft + 'px';
	document.body.appendChild(a);
	EVENT.Add(a, 'mousedown', COLUMNLAYOUT.ScrollNext);

	// scroll to subheading?
	var pos = location.href.indexOf('#');
	if (pos > 0) {
		var anchorname = 'a' + location.href.substring(pos + 1); // add one letter "a" to avoid build-in scroll-into-view
		var anchor = document.getElementById(anchorname);
		if (anchor) {
			anchor.className = 'selected';
			var col = parseInt(anchor.parentNode.id.substring(4));
			if (col + 1 > COLUMNLAYOUT.VisibleColumns) {
				COLUMNLAYOUT.ScrollTo(col);
			}
		}
	}

	// scroll the entire window downwards
	//document.getElementById('footer').scrollIntoView();
};

COLUMNLAYOUT.ShowAdditionalInfo = function(e) {
	COLUMNLAYOUT.scrollPanel.style.width = '614px';
	document.getElementById('rightscrollarrow').style.marginLeft = '620px';
	COLUMNLAYOUT.VisibleColumns = 2;
}
COLUMNLAYOUT.HideAdditionalInfo = function() {
	COLUMNLAYOUT.scrollPanel.style.width = '928px';
	document.getElementById('rightscrollarrow').style.marginLeft = '939px';
	COLUMNLAYOUT.VisibleColumns = 3;
}

COLUMNLAYOUT.adjustScroll = function(e) {
	if (COLUMNLAYOUT.ScrollThread != null)
		clearTimeout(COLUMNLAYOUT.ScrollThread);
	COLUMNLAYOUT.ScrollThread = setTimeout("COLUMNLAYOUT.doAdjustscroll()", 300);
};
COLUMNLAYOUT.doAdjustscroll = function() {
	var adjustment = COLUMNLAYOUT.scrollPanel.scrollLeft % (COLUMNLAYOUT.ColumnWidth + COLUMNLAYOUT.ColumnMargin);
	if (adjustment != 0) {
		if (adjustment > (COLUMNLAYOUT.ColumnWidth / 2)) { //## ADJUST TO RIGHT
			COLUMNLAYOUT.ScrollTo(parseInt(COLUMNLAYOUT.scrollPanel.scrollLeft / (COLUMNLAYOUT.ColumnWidth + COLUMNLAYOUT.ColumnMargin)) + 1);
		} else { //## ADJUST TO LEFT
			COLUMNLAYOUT.ScrollTo(parseInt(COLUMNLAYOUT.scrollPanel.scrollLeft / (COLUMNLAYOUT.ColumnWidth + COLUMNLAYOUT.ColumnMargin)));
		}
	} else {
		COLUMNLAYOUT.CurrentColumn = (COLUMNLAYOUT.scrollPanel.scrollLeft / (COLUMNLAYOUT.ColumnWidth + COLUMNLAYOUT.ColumnMargin));
	}
	COLUMNLAYOUT.ScrollLastPosition = COLUMNLAYOUT.scrollPanel.scrollLeft;
};
COLUMNLAYOUT.ScrollNext = function() {
	if (COLUMNLAYOUT.CurrentColumn + COLUMNLAYOUT.VisibleColumns >= COLUMNLAYOUT.countColumns) {
		COLUMNLAYOUT.ScrollTo(COLUMNLAYOUT.countColumns - 1)
	} else {
		// if only one column is visible, scroll to the next. Otherwise, scroll to the right, but keep the rightmost column visible
		if (COLUMNLAYOUT.VisibleColumns == 1)
			COLUMNLAYOUT.OffsetScroll(1);
		else
			COLUMNLAYOUT.OffsetScroll(COLUMNLAYOUT.VisibleColumns - 1);
	}
};
COLUMNLAYOUT.ScrollPrevious = function() {
	var targetColumn = (-(COLUMNLAYOUT.VisibleColumns - 1));
	if (COLUMNLAYOUT.VisibleColumns == 1)
		targetColumn--;
	if (COLUMNLAYOUT.CurrentColumn > 1) {
		COLUMNLAYOUT.OffsetScroll(targetColumn);
	} else {
		COLUMNLAYOUT.OffsetScroll(-1);
	}
};
COLUMNLAYOUT.OffsetScroll = function(offset) {
	var target = COLUMNLAYOUT.CurrentColumn + offset;
	if (target > COLUMNLAYOUT.countColumns) return;
	if (target < 0)
		target = 0;
	COLUMNLAYOUT.ScrollTo(target);
};
COLUMNLAYOUT.ScrollTo = function(target) {
	if (target < 0) return;
	COLUMNLAYOUT.doScrollTo(target);
};
COLUMNLAYOUT.doScrollTo = function(target) {
	if (target >= COLUMNLAYOUT.countColumns || COLUMNLAYOUT.ScrollAnimation.timer != null) return;
	if (POPUP.CurrentPopup != null) {
		var metainfo = POPUP.CurrentPopup.name.split(':');
		if (metainfo[4] != 'additionalinfo')
			POPUP.Close();
	}
	if (target >= (COLUMNLAYOUT.countColumns - COLUMNLAYOUT.VisibleColumns)) {
		// scroll all the way to the last column
		target = COLUMNLAYOUT.countColumns - COLUMNLAYOUT.VisibleColumns;
	}
	COLUMNLAYOUT.CurrentColumn = target;
	targetcol = document.getElementById('col-' + target);
	var offsetLeft = target * (COLUMNLAYOUT.ColumnWidth + COLUMNLAYOUT.ColumnMargin);
	if (COLUMNLAYOUT.ScrollAnimation.timer != null) {
		clearInterval(COLUMNLAYOUT.ScrollAnimation.timer);
		COLUMNLAYOUT.ScrollAnimation.timer = null;
	}
	COLUMNLAYOUT.ScrollAnimation.time = 0;
	COLUMNLAYOUT.ScrollAnimation.begin = COLUMNLAYOUT.scrollPanel.scrollLeft;
	COLUMNLAYOUT.ScrollAnimation.change = offsetLeft - COLUMNLAYOUT.scrollPanel.scrollLeft;
	COLUMNLAYOUT.ScrollAnimation.duration = COLUMNLAYOUT.ScrollDuration;
	COLUMNLAYOUT.ScrollAnimation.element = COLUMNLAYOUT.scrollPanel;
	COLUMNLAYOUT.ScrollAnimation.timer = setInterval("COLUMNLAYOUT.scrollHorizAnim();", 15);
};
COLUMNLAYOUT.scrollHorizAnim = function() {
	if (COLUMNLAYOUT.ScrollAnimation.time > COLUMNLAYOUT.ScrollAnimation.duration) {
		clearInterval(COLUMNLAYOUT.ScrollAnimation.timer);
		COLUMNLAYOUT.ScrollAnimation.timer = null;
		COLUMNLAYOUT.ScrollThread = null;
	} else {
		move = -COLUMNLAYOUT.ScrollAnimation.change / 2 * (Math.cos(Math.PI * COLUMNLAYOUT.ScrollAnimation.time / COLUMNLAYOUT.ScrollAnimation.duration) - 1) + COLUMNLAYOUT.ScrollAnimation.begin;
		COLUMNLAYOUT.ScrollAnimation.element.scrollLeft = move;
		COLUMNLAYOUT.ScrollAnimation.time++;
	}
};
