/*

$RCSfile: ihtArticle.js,v $ 
$Author: john $  
$Date: 2001/02/27 04:43:11 $  
$Locker:  $ 
$Revision: 1.5 $ 


 Bug in Mozzilla when the end of an article is reached
the first column will not take mouse events unless the user clicks on a menu item
*/


if (document.all) classFix = "className"
else classFix = "class"

var originalHeight;
var fontSize = 12;
var lineHeight = 16;
var currentPos = 0; //tracks position of columns

var tHeight = 0;

var articleGraphic = null;
columnMode = 3;

col3Width = 230;
col2Width = 345;
col1Width = 400;

colWidth = 0;

FONT_SMALLEST = 8;
FONT_LARGEST = 24;

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 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"
	}
	
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;
	}
	}

function articleSetup()
	//makes the columns and copies the text node into the newly created columns 
	//ac is the div holding the cloned node - at
	{
	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 layoutGraphic()
	{
	
	//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 
		}
	}

function layoutArticles()
	//moves the columns into position, but adjust the top
	{	
	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) 
	//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
	{
	if (mod == null) mod =0;
	//snap = lineHeight*Math.round((getHeight("window")-mod)/lineHeight)
	snap = lineHeight*Math.round((getHeight("window")-mod)/lineHeight)

	if (snap < lineHeight*10) 
		{
		snap = lineHeight*10; //make the article parent 10 rows tall;
		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)


		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()
	{
	//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);
	
	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;
		}
	
	
	}
	
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 = 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";
	
	saveFaceSize();
	articlePages();
	
	document.getElementById("articleNextPage").style.display = "none";
	document.getElementById("articlePrevPage").style.display = "none";
	}

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("articleNextPage").style.display = "block";
	document.getElementById("articlePrevPage").style.display = "block";
	
	saveFaceSize();
	}


function threeColumn() //switches to four Column mode
	{
	currentPos = 0;
	columnMode = 3
	
	colWidth = col3Width;
	
	if (fontSize > 18) {fontSize = 18}
	obj = document.getElementById("at0")
	if (document.all) obj.style.cursor = "hand";
	else  obj.style.cursor = "pointer";
	obj.style.zIndex = 5;
	obj.style.width = colWidth;
	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")
	if (document.all) obj.style.cursor = "hand";
	else  obj.style.cursor = "pointer";
	obj.style.display = "block"
	obj.style.left = 2*(colWidth+16);
	obj.style.width = colWidth;
	obj.onmousemove = nextPageOver;
	obj.onmouseout = hidePageOver;
	obj.onclick = nextPage;
	obj.onmouseup = hidePageOver;
	

	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;

		}
		
	}
	
