/*
===============================================================================
This code was modified from the original supplied at the start of this project
Some functionality was disabled, other was enhanced.

Last update: 10-20-2003

Notes :
    -repeat previous mods. Source control issue.
    -include fix for headline which may span more than one line.
    -remove feature where users can click columns to navigate through story
    -add print and email functions
    -re-activate highlighting current column setting (it is boldened)
===============================================================================
*/

// ============================================================================
// CONFIG
// ============================================================================

if (document.all) classFix = "className"
else classFix = "class";

var fontSize = 12;
var lineHeight = 18;
var currentPos = 0;
var tHeight = 0;
var articleGraphic = null;
var originalHeight;
var currentPageNum;
var totalNumPages;

// default number of columns to display
columnMode = 3;

// define widths of each column for each column mode
col3Width = 230;
col2Width = 345;
col1Width = 400;
colWidth = 0;

// Offset amount, in pixels, for the bottom of the article (provide space for footer)
var pageOffset = 200;

// Define minimum number of rows visible
var MIN_ROWS=10;

// Define range of allowed fonts
FONT_SMALLEST = 8;
FONT_LARGEST = 24;
	
// ==========================================================================
// Do not make modifications below this line.
// ============================================================================

function getHeight(obj)
// ==========================================================================
// returns the height of the supplied ID, or the height of the current window
// ==========================================================================
	{
	if (obj == "window") 
		{
		if (window.innerHeight) return window.innerHeight;
		else return document.getElementById("bodyNode").offsetHeight;
		}
	else
	{
	obj = document.getElementById(obj)
//	    alert (obj.offsetHeight);
	    if (obj.offsetHeight) {
		return obj.offsetHeight;
	    }else{
		//alert ("problem with height");
		return "305";
	    }
	}
	}

function articleSetup()
// ==========================================================================
// creates columns and copies the text node into them
// ==========================================================================
	{
	parentDiv = document.getElementById("articleParent")
	currentPos = 0;
	for (i=0; i < 3; i++)
	{
		col = document.createElement("div")
		col.setAttribute("id", "ac"+i)	     //set id for div
		col.setAttribute(classFix, "artCol") //give the div style

		parentDiv.appendChild(col);
		obj = document.getElementById("articleBody");
		
		artText = obj.cloneNode(true);
		
		artText.setAttribute("id","at"+i);
		artText.style.display = "block" //display the column
		artText.style.top = "0px";
		artText.style.fontSize = fontSize+"px";
		artText.style.lineHeight = lineHeight+"px";

		col.appendChild(artText);		
	}

	if (document.getElementById("articleOwnerGraphic")) 
	{
		obj = document.getElementById("articleOwnerGraphic");
		articleGraphic = obj;
	}
}

function layoutArticles()
// ==========================================================================
// moves the columns into position
// ==========================================================================
	{	
	parentHeight = getHeight("articleParent")
	for (i = 0; i < columnMode; i++)
		{

		obj = document.getElementById("at"+i);
		if (parentHeight > 2*lineHeight) //make sure at least 2 rows of article text are available 
		{
		
		obj.style.top = -1*(parentHeight*(i+currentPos))     //"px"//-1*(parentHeight*(i+currentPos))+"px"

		}
		}
//		if (articleGraphic) layoutGraphic();
		
		articlePages();
	}

function setSnap(mod) 
// ==========================================================================
// calculates vertical height of columns - allowing for headline and footer. 
// Makes sure that the navigation is always visible on the window - 
// (as long as there is space - it will always draw at least MIN_ROWS)
// ==========================================================================
	{
	if (mod == null) mod =0;
	//snap = lineHeight*Math.round((getHeight("window")-mod)/lineHeight)

	var headerHeight = getHeight("articleHeader");

	var heightCorrect = mod + headerHeight;

	var numLines = Math.round((getHeight("window")-heightCorrect)/lineHeight);
	snap = lineHeight*numLines;

	window.scrollTo(0,0);

	// Make sure there are at least "MIN_ROWS" visible;
	if (snap < lineHeight*MIN_ROWS) 
		{
		snap = lineHeight*10;
		if (window.scrollTo) window.scrollTo(0,75);
		}
	else if (window.scrollTo) window.scrollTo(0,0);
	
	return snap;

	}

function setArticleHeight()
// ========================================================================== 
// clips the parent layer to the defined visible area
// ==========================================================================
	{
	//if space allows include bottom, otherwise clip out most of bottom
	//originalHeight = getHeight("articleParent")

	if (columnMode > 1)
	{
	if (document.getElementById("articleBody") != null)
		{

		//document.getElementById("articleParent").style.height = setSnap(295)
		document.getElementById("articleParent").style.height = setSnap(pageOffset)

		tHeight = getHeight("at1");
//		if (articleGraphic) tHeight = tHeight+articleGraphic.offsetHeight;
		
		parentHeight = getHeight("articleParent");

	while ((parentHeight*(currentPos+columnMode-1)) > tHeight && currentPos > 0) //check to make sure page is still visible 
		{
		currentPos= currentPos-1;
		}
		layoutArticles()
		}
	}
	}

function articlePages()
// ==========================================================================
// Caluculate how many pages the aricle will span with the current height setting
// ==========================================================================
	{
	//returns the number of screens an article spans, ie the number of pages 
	parentHeight = getHeight("articleParent")
	
	//tHeight = getHeight("at1")
	
	totalColumns = tHeight/parentHeight;
	totalPages = Math.ceil(totalColumns);
	tPos = (currentPos+columnMode)/columnMode;

	pagesTotal = Math.ceil(totalPages/columnMode)+1;
	pagesCurrent = Math.round(tPos+1);
	
	currentPageNum = pagesCurrent -1;
	totalNumPages = pagesTotal -1;

/*
	if (pagesCurrent > 9) 
		{
		total = pagesCurrent+""; //make it a string;
		document.getElementById("pgCol0").style.top = (parseInt(total.substring(0,1))+1)*-5;
		document.getElementById("pgCol1").style.top = (parseInt(total.substring(1,2))+1)*-5;
		}
		
	else {
		document.getElementById("pgCol0").style.top = 0;
		document.getElementById("pgCol1").style.top = pagesCurrent*-5;
		}
	
	if (pagesTotal > 9) 
		{
		total = pagesTotal+""; //make it a string;
		document.getElementById("pgCol2").style.top = (parseInt(total.substring(0,1))+1)*-5;
		document.getElementById("pgCol3").style.top = (parseInt(total.substring(1,2))+1)*-5;
		}
		
	else {
		document.getElementById("pgCol3").style.top = 0;
		document.getElementById("pgCol2").style.top = pagesTotal*-5;
		}
*/
	
	if (columnMode != 1) {
		document.getElementById("curColText").firstChild.nodeValue=currentPageNum;
		document.getElementById("totalColText").firstChild.nodeValue=totalNumPages;
		//document.getElementById("totalColText").style.visibility="visible";
	}
	else {
		document.getElementById("curColText").firstChild.nodeValue=1;
		document.getElementById("totalColText").firstChild.nodeValue=1;
	}
}
	
function nextPage()
	{
	//tHeight = getHeight("at0")
	parentHeight = getHeight("articleParent");
	
	if ((parentHeight*(currentPos+columnMode)) < tHeight) //check to make sure page is still visible 
		{
		currentPos= currentPos+columnMode;
		//layoutArticles()
		}

	layoutArticles();
	}
	
function prevPage()
	{
	currentPos= currentPos-columnMode;
	if (currentPos < 0) currentPos = 0
	layoutArticles()
	}
	
function eventOneColumn()
	{
	currentPos = 0;
	columnMode = 1 
	colWidth = col1Width;
	
	obj = document.getElementById("at0")
	obj.style.width = colWidth;
	//obj.style.left=70
	obj.style.left=0
	obj = document.getElementById("articleParent");
	obj.style.height = getHeight("at0") //col2Width;
	
	obj = document.getElementById("at1")
	obj.style.display = "none";
	
	obj = document.getElementById("at2")
	obj.style.display = "none"
	
	parentHeight = getHeight("articleParent")
	
	for (i = 0; i < columnMode; i++)
		{
		obj = document.getElementById("at"+i);
		obj.style.top = -1*(parentHeight*(i+currentPos)) //"px"//-1*(parentHeight*(i+currentPos))+"px"
		}
	
	//articleGraphic.style.top = document.getElementById("at0").offsetHeight-articleGraphic.offsetHeight+"px";
	//articleGraphic.style.left = "480px"
	//articleGraphic.style.visibility = "visible";
	
	document.getElementById("oneColumnSelect").style.fontWeight = "bold";
	document.getElementById("oneColumnSelect").style.color = "black";
	document.getElementById("twoColumnSelect").style.color = "#3A527D";
	document.getElementById("twoColumnSelect").style.fontWeight = "normal";
	document.getElementById("threeColumnSelect").style.fontWeight = "normal";
	document.getElementById("threeColumnSelect").style.color = "#3A527D";
	
	saveFaceSize();
	articlePages();
	
	//document.getElementById("articleNextPage").style.display = "none";
	//document.getElementById("articlePrevPage").style.display = "none";
	
	}

function eventTwoColumn() {
	if (window.scrollTo) window.scrollTo(0,0);
	twoColumn();
	setArticleHeight();
	
	saveFaceSize();
	articlePages();
	}

function eventThreeColumn() {
	if (window.scrollTo) window.scrollTo(0,0);
	threeColumn();
	setArticleHeight();

	saveFaceSize();
	articlePages();
	}
	
function twoColumn() //switches to two Column mode
	{
	currentPos = 0;
	columnMode = 2
	
	colWidth = col2Width;
	
	obj = document.getElementById("at0")
	obj.style.width = colWidth;
	obj.style.left = 0;
//	if (document.all) obj.style.cursor = "hand";
//	else  obj.style.cursor = "pointer";
//	obj.onmousemove = prevPageOver;
//	obj.onmouseout = hidePageOver;
//	obj.onmousedown = prevPage;
//	obj.onmouseup = hidePageOver;
	
	obj = document.getElementById("at1")
	obj.style.left = colWidth+20;
	obj.style.width = colWidth;
	obj.style.display = "block"
//	if (document.all) obj.style.cursor = "hand";
//	else  obj.style.cursor = "pointer";
//	obj.onmousemove = nextPageOver;
//	obj.onmouseout = hidePageOver;
//	obj.onclick = nextPage;
//	obj.onmouseup = hidePageOver;
	
	obj = document.getElementById("at2")
	obj.style.display = "none"
	
	document.getElementById("oneColumnSelect").style.color = "#3A527D";	
	document.getElementById("oneColumnSelect").style.fontWeight = "normal";
	document.getElementById("twoColumnSelect").style.fontWeight = "bold";
	document.getElementById("twoColumnSelect").style.color = "black";
	document.getElementById("threeColumnSelect").style.fontWeight = "normal";
	document.getElementById("threeColumnSelect").style.color = "#3A527D";
		
//	document.getElementById("articleNextPage").style.display = "block";
//	document.getElementById("articlePrevPage").style.display = "block";
	
	}

function threeColumn() //switches to four Column mode
	{
	currentPos = 0;
	columnMode = 3
	
	colWidth = col3Width;
	
	if (fontSize > 18) {fontSize = 18}
	obj = document.getElementById("at0")
	obj.style.zIndex = 5;
	obj.style.width = colWidth;
//	if (document.all) obj.style.cursor = "hand";
//	else  obj.style.cursor = "pointer";
//	obj.onmousemove = prevPageOver;
//	obj.onmouseout = hidePageOver;
//	obj.onmousedown = prevPage;
//	obj.onmouseup = hidePageOver;
	obj.style.left = 0;
	
	obj = document.getElementById("at1")
	obj.style.left = colWidth+16;
	obj.style.display = "block"
	obj.style.width = col3Width;
//	obj.style.cursor = "default";
//	obj.onmousemove = null;
//	obj.onmouseout = null;
//	obj.onclick = null;
//	obj.onmouseup = null;

	obj = document.getElementById("at2")
	obj.style.display = "block"
	obj.style.left = 2*(colWidth+16);
	obj.style.width = colWidth;
//	if (document.all) obj.style.cursor = "hand";
//	else  obj.style.cursor = "pointer";
//	obj.onmousemove = nextPageOver;
//	obj.onmouseout = hidePageOver;
//	obj.onclick = nextPage;
//	obj.onmouseup = hidePageOver;
	
	document.getElementById("oneColumnSelect").style.color = "#3A527D";	
	document.getElementById("oneColumnSelect").style.fontWeight = "normal";
	document.getElementById("twoColumnSelect").style.color = "#3A527D";	
	document.getElementById("twoColumnSelect").style.fontWeight = "normal";
	document.getElementById("threeColumnSelect").style.fontWeight = "bold";
	document.getElementById("threeColumnSelect").style.color = "black";
	
//	document.getElementById("articleNextPage").style.display = "block";
//	document.getElementById("articlePrevPage").style.display = "block";

	}

function saveFaceSize()
	{

	var expire = new Date ();
   	expire.setTime (expire.getTime() + (6000 * 24 * 3600000)); //expires in 6 days from users clock
   	expire = expire.toGMTString();
	document.cookie="fontSize="+fontSize+"; columnMode="+columnMode+"; path=/; expires="+expire;
	document.cookie="columnMode="+columnMode+"; path=/; expires="+expire;  	

	}
	
function setFaceSize()
	{
	lineHeight = fontSize+Math.round(.3*fontSize);
	for (i = 0; i < 3; i++)
		{
		obj = document.getElementById("at"+i);
		obj.style.fontSize = fontSize+"px";
		obj.style.lineHeight = lineHeight+"px"
		}
	setArticleHeight();
	if (columnMode == 1) eventOneColumn();
	saveFaceSize();
	}

function eventFaceLarger()
	{
	//alert(document.all.textColumn0.document.style.fontSize)
	fontSize = fontSize+2;

	if (fontSize > 18 && columnMode != 1) {twoColumn();}
	if (fontSize > 26) {fontSize = 26;}
	
	setFaceSize()
	}
	
function eventFaceSmaller()
	{
	fontSize = fontSize-2
	if (fontSize < 18 && columnMode != 1) {threeColumn();}
	if (fontSize < 9) fontSize = 9;
	
	setFaceSize();
	}
	
function eventArticleFocus()
	{
	layoutArticles();
	event.cancelBubble = true;
	}	

function eventArticleBlur()
	{
	window.status = "article blurred"
	}		
	
function testArticle()
	{
	obj = document.getElementById("articleParent")
	obj.doScroll("up");
	window.status = "This interface does not support scrolling.";
	}

function loadFontSize()
	//get font size from a cookie
	{
	tempArray = document.cookie.split(";");		
	for (tA = 0; tA < tempArray.length; tA++)
			{
			if (tempArray[tA].indexOf('fontSize') > -1) //found the font section in cookie
				{
				fontValue = tempArray[tA].split("=")
				fontSize = parseInt(fontValue[1]);
				lineHeight = fontSize+Math.round(.3*fontSize);
				}
			if (tempArray[tA].indexOf('columnMode') > -1) //found the font section in cookie
				{
				colValue = tempArray[tA].split("=")
				columnMode = parseInt(colValue[1]);
				}
			}
	}

function initArticle()
	{
	//loadUserPref()
	if (document.getElementById("articleBody") != null)
		{
		loadFontSize();
		articleSetup();
		
		setArticleHeight();
		if (columnMode == 1) eventOneColumn();
		if (columnMode == 2) twoColumn();
		if (columnMode == 3) threeColumn();
		 
		setFaceSize();
		layoutArticles();
		
		//attempt to get article to NOT scroll when space bar is pressed in IE
		obj = document.getElementById("articleParent");
		obj.onkeyup = eventArticleFocus;
		obj.onscroll = testArticle;

		}
	}
	
function currentPage()
  {
    return currentPageNum;
  }
  
function totalPages() 
  {
    return columnMode;
  }
  

// =============================================================================
// Legacy functions - the following functions are not currently utilised
// =============================================================================

function articlePrint()
	{
	if (window.print) 
		{
		window.print();
		}
	else 
		{
		alert("Your web browser does not support standard web printing.\nThe article will now switch to single column mode.\n\nYou can then print the page using your browsers print command.");
	
		eventOneColumn();
		}
	}

function layoutGraphic()
	{
	/* es
	//find if the last column is visible
	if (columnMode != 1)
	{
	obj = document.getElementById("at"+(columnMode-1));
	
	if ((parseInt(obj.style.top) + obj.offsetHeight) < (parentHeight))
		{
		colHeight = getHeight("at0");
		
		graphicOffset = (columnMode-1)*parentHeight;
		
		articleGraphic.style.marginTop = lineHeight;
		
		articleGraphic.style.top = colHeight-((parentHeight*currentPos)+graphicOffset);
		if (parseInt(articleGraphic.style.top) < 0) 
			{
			articleGraphic.style.marginTop = 0;
			articleGraphic.style.top = 0;
			}
			
		//if (columnMode == 2) articleGraphic.style.left = (1)*colWidth+20+"px"
		//if (columnMode == 3) articleGraphic.style.left = (2)*colWidth+36+"px"
		
		articleGraphic.style.visibility = "visible"
		}
		else articleGraphic.style.visibility = "hidden"
		//put the graphic below the first column which 
		}
	*/ es
	}

function nextPageOver()
	{
	//displays next page icon when mousing over column
/*	
	parentHeight = getHeight("articleParent")
	
	if ((parentHeight*(currentPos+columnMode)) < tHeight)
	{	
		obj = document.getElementById("nextCool")
		obj.style.visibility = "hidden"
		obj = document.getElementById("nextHot")
		obj.style.visibility = "visible"
		}
*/
	}

function prevPageOver()
	{
	//displays next page icon when mousing over column
/*
	if (currentPos > 0)
	{	
		obj = document.getElementById("prevCool")
		obj.style.visibility = "hidden"
		obj = document.getElementById("prevHot")
		obj.style.visibility = "visible"
		}
*/	
	}

function hidePageOver()
	{
	//hides mouse icon when leaving the column
/*
	obj = document.getElementById("prevCool")
	obj.style.visibility = "visible"
	obj = document.getElementById("prevHot")
	obj.style.visibility = "hidden"
	
	obj = document.getElementById("nextCool")
	obj.style.visibility = "visible"
	obj = document.getElementById("nextHot")
	obj.style.visibility = "hidden"
*/
	}

