/*
This file contains all other user interaction functions:
switching to the 3 column layout, switching to the 1 column
layout, increasing and decreasing the font, setting the 
window resize, etc.
*/

var classFix;
if (document.all){
	classFix = "className";
} else {
	classFix = "class";
}

// set some defaults values
var originalHeight;
var lineHeight = 16;
var currentPos = 0;

// use these values to manipulate the width of the text columns
// col3Width is used in the 3 column layout and col1Width is 
// used in the 1 column layout.
var col1Width = photoColDiv ? 434 : 647;
var colWidth = 0;
var columnMode = numColumns;
var articlePhoto = null;

// returns the height of the current window
function getHeight(obj){
	if (obj == "window"){
		if (window.innerHeight){
			return window.innerHeight;
		} else {
			return document.getElementById("bodyNode").offsetHeight;
		}
	} else {
		obj = document.getElementById(obj);
		if (obj.offsetHeight){
			return obj.offsetHeight;
		}
	}
}
	
// makes the columns and copies the text node into the newly
// created columns ac is the div holding the cloned node - at
function articleSetup(){
	// get the fontSize value from the cookie
	var cv1 = readCookie("ihtuserdata");
	if (cv1){
		cookieValues = parseCookie(cv1);
	} else {
		cookieValues[1] = "12";	
	}
	var fontSize = parseInt(cookieValues[1]);
	lineHeight = fontSize + Math.round(.3 * fontSize);
	var parentDiv = document.getElementById("noticeDisplay");
	var currentPos = 0;
	for (var i = 0; i < numColumns; i++){
		var col = document.createElement("div");
		col.setAttribute("id", "ac" + i);
		col.setAttribute(classFix, "noticeCol");
		parentDiv.appendChild(col);
		var obj = document.getElementById("articleBody");
		var noticeText = obj.cloneNode(true);
		noticeText.setAttribute("id", "at" + i);
		noticeText.style.display = "block";
		noticeText.style.top = "0px";
		noticeText.style.fontSize = fontSize + "px";
		noticeText.style.lineHeight = lineHeight + "px";
		col.appendChild(noticeText);		
	}
}

// moves the columns into position, but adjust the top
function layoutArticles(){	
	var parentHeight = getHeight("noticeDisplay");
	for (var i = 0; i < columnMode; i++){
		var obj = document.getElementById("at" + i);
		//make sure at least 2 rows of article text are available 
		if (parentHeight > (2 * lineHeight)){
			obj.style.top = -1 * (parentHeight * (i + currentPos));
		}
	}
	articlePages();
}

// resets the article to the first "page"
function resetArticle(){
	currentPos = 0;
	layoutArticles();
}

// returns the snap to align article; this adjusts the height of the 
// article window with a number divisible by line height. the minium 
// number of rows is 10
function setSnap(mod)
{
	return lineHeight * Math.round(mod / lineHeight);
}

// clips the parent layer to the defined visible area
// if space allows include bottom, otherwise clip out most of bottom
function setArticleHeight(){
	if (document.getElementById("articleBody") != null)
	{
		document.getElementById("noticeDisplay").style.height = setSnap(heightOfColumns);
		var tHeight = getHeight("at0");
		parentHeight = getHeight("noticeDisplay");

		//check to make sure page is still visible
		while ((parentHeight * (currentPos + (columnMode - 1))) > tHeight && currentPos > 0)
		{
			currentPos = currentPos - 1;
		}
	}
}

// returns the number of screens an article spans, ie the number of pages
function articlePages(){
	var parentHeight = getHeight("noticeDisplay");
	var tHeight = getHeight("at0");
	var totalColumns = tHeight / parentHeight;
	var totalPages = Math.ceil(totalColumns);
	var tPos = (currentPos + columnMode) / columnMode;
	var pagesTotal = Math.ceil(totalPages / columnMode);
	var pagesCurrent = Math.round(tPos);
	var obj = document.getElementById("pagenumbers");
	obj.innerHTML = "PAGE " + pagesCurrent + " OF " + pagesTotal;
}

// adjusts the article to display the next page
function nextPage(){
	tHeight = getHeight("at0")
	var parentHeight = getHeight("noticeDisplay");
	// check to make sure page is still visible 
	if ((parentHeight * (currentPos + columnMode)) < tHeight){
		currentPos = currentPos + columnMode;
	}
	layoutArticles();
	return false;
}

// adjusts the article to display the previous page
function prevPage(){
	currentPos = currentPos - columnMode;
	if (currentPos < 0){
		currentPos = 0;
	}
	layoutArticles();
	return false;
}

// user event to trigger the one column change
function eventOneColumn(){
	currentPos = 0;
	columnMode = 1;
	colWidth = col1Width;
	
	// save columnMode value in the cookie
	var cv = "columnMode:" + columnMode + "&fontSize:" + cookieValues[1] + "&clippings:" + cookieValues[2];
	createCookie("ihtuserdata",cv,7);
	
	obj = document.getElementById("at0");
	obj.style.width = colWidth;
	obj.style.left = 0;
	
	obj = document.getElementById("noticeDisplay");
//	obj.style.height = getHeight("at0");
	
	obj = document.getElementById("at1");
	if (obj) obj.style.display = "none";
	
	obj = document.getElementById("at2");
	if (obj) obj.style.display = "none";

	parentHeight = getHeight("noticeDisplay");

	for (var i = 0; i < columnMode; i++){
		obj = document.getElementById("at" + i);
		obj.style.top = -1 * (parentHeight * (i + currentPos));
	}
	articlePages();

	obj = document.getElementById("next");
//	if (obj) obj.style.display = "none";
	
	obj = document.getElementById("prev");
//	if (obj) obj.style.display = "none";
	
	obj = document.getElementById("pagenumbers");
//	if (obj) obj.style.display = "none";
}

// user event to trigger the three column change
function eventThreeColumn(){
	if (window.scrollTo){
		window.scrollTo(0,0);
	}
	// save columnMode value in the cookie
	var cv = "columnMode:" + numColumns + "&fontSize:" + cookieValues[1] + "&clippings:" + cookieValues[2];
	createCookie("ihtuserdata",cv,7);

	threeColumn();
	setArticleHeight();
	layoutArticles();
	articlePages();
}

// switches to three Column mode
function threeColumn(){

	// get the fontSize value from the cookie
	var cv1 = readCookie("ihtuserdata");
	if (cv1){
		cookieValues = parseCookie(cv1);
	} else {
		cookieValues[1] = "12";
	}
	var fontSize = parseInt(cookieValues[1]);
	
	currentPos = 0;
	columnMode = numColumns;
	colWidth = col3Width;

	if (fontSize > 18){
		fontSize = 18;
	}
	var obj1 = document.getElementById("at0");
	obj1.style.zIndex = 5;
	obj1.style.display = "block";
	obj1.style.width = colWidth;
	obj1.style.left = 0;

	var obj2 = document.getElementById("at1");
	if (obj2)
	{
		obj2.style.zIndex = 5;
		obj2.style.display = "block";
		obj2.style.width = colWidth;
		obj2.style.left = colWidth + 16;
	}

	var obj3 = document.getElementById("at2");
	if (obj3)
	{
		obj3.style.zIndex = 5;
		obj3.style.display = "block";
		obj3.style.width = colWidth;
		obj3.style.left = 2*(colWidth + 16);
	}
}

function initArticle(){
	if (document.getElementById("articleBody") != null){
		articleSetup();
		setArticleHeight();
		if (columnMode == 1){
			eventOneColumn();
		} else if (columnMode == numColumns){
			threeColumn();
		} else {
			alert("columnMode does not exist");
		}
		layoutArticles();
	} else {
		alert("Can not find articleBody node");
	}
}

// increases the font and adjusts the article layout
function increaseFont(){
	// get the current value from the cookie
	var cv1 = readCookie("ihtuserdata");
	if (cv1){
		cookieValues = parseCookie(cv1);
	} else {
		cookieValues[1] = "11";
	}
	var currentFontSize = parseInt(cookieValues[1]);
	var newFontSize = currentFontSize + 2;
	if (newFontSize > 15){
		newFontSize = 15;
	}
	// save new value in the cookie
	var cv = "columnMode:" + numColumns + "&fontSize:" + newFontSize + "&clippings:" + cookieValues[2];
	createCookie("ihtuserdata",cv,7);
	
	// re-draw article
	lineHeight = newFontSize + Math.round(.3 * newFontSize);
	for (var i = 0; i < numColumns; i++){
		obj = document.getElementById("at" + i);
		obj.style.fontSize = newFontSize + "px";
		obj.style.lineHeight = lineHeight + "px";
	}
	setArticleHeight();
	layoutArticles();
}

// increases the font and adjusts the article layout
function decreaseFont(){
	// get the current value from the cookie
	var cv1 = readCookie("ihtuserdata");
	if (cv1){
		cookieValues = parseCookie(cv1);
	} else {
		cookieValues[1] = "11";
	}
	var currentFontSize = parseInt(cookieValues[1]);
	var newFontSize = currentFontSize - 2;
	if (newFontSize < 9){
		newFontSize = 9;
	}
	// save new value in the cookie
	var cv = "columnMode:" + numColumns + "&fontSize:" + newFontSize + "&clippings:" + cookieValues[2];
	createCookie("ihtuserdata",cv,7);
	// re-draw article
	lineHeight = newFontSize + Math.round(.3 * newFontSize);
	for (var i = 0; i < numColumns; i++){
		obj = document.getElementById("at" + i);
		obj.style.fontSize = newFontSize + "px";
		obj.style.lineHeight = lineHeight + "px";
	}			
	setArticleHeight();
	layoutArticles();
}

// adjust article layout when the window is resized
function windowResize(){
	setArticleHeight();
	layoutArticles();
}

window.onresize = windowResize;

// this toggles the format between one and three column layouts
// checks the current value from the cookie and does that opposite
function toggleFormat(){
	var format = columnMode;
	if (format == 1){
		eventThreeColumn();
	} else if (format == numColumns){
		eventOneColumn();
	} else {
		// do nothing
	}
}

// this might be used, should offset the image if there is one
function layoutPhoto(){
	//find if the last column is visible
	if (columnMode != 1){
		var obj = document.getElementById("at" + (columnMode - 1));
		if ((parseInt(obj.style.top) + obj.offsetHeight) < (parentHeight)){
			colHeight = getHeight("at0");
			graphicOffset = (columnMode - 1) * parentHeight;
			articlePhoto.style.marginTop = lineHeight;
			articlePhoto.style.top = colHeight - ((parentHeight * currentPos) + graphicOffset);
			if (parseInt(articlePhoto.style.top) < 0){
				articlePhoto.style.marginTop = 0;
				articlePhoto.style.top = 0;
			}
			if (columnMode == 2){
				articlePhoto.style.left = (1) * colWidth + 20 + "px";
			}
			if (columnMode == numColumns){
				articlePhoto.style.left = (2) * colWidth + 36 + "px";
			}
			articlePhoto.style.visibility = "visible";
		} else {
			articlePhoto.style.visibility = "hidden";
		}
	}
}

