if (document.all) classFix = "className"
else classFix = "class"

var fontSize = 11;
var lineHeight = 16;
var currentPos = 0; //tracks position of columns
var tHeight = 0;
var articleGraphic = null;
var columnMode = 3;
var col1Width = 715;
var col2Width = 345;
var col3Width = 225;
var colWidth = 0;
var parentHeight;
var pagesTotal; 

function AFPfeedback(art_id) {
	var winwidth = 400; 
   var winheight = 430; 
   var winleft = (screen.width / 2) - (winwidth / 2); 
   var wintop = (screen.height / 2) - (winheight / 2); 

	var newWindow;
	var props = 'scrollBars=no,resizable=no,toolbar=no,menubar=no,location=no,directories=no,width=' + winwidth + ',height=' + winheight + ',top=' + wintop + ',left=' + winleft;
	newWindow = window.open('/txt_afpfeedback.asp?id='+art_id, '', props);
}


function feedback(art_id) {
	var winwidth = 400; 
   var winheight = 430; 
   var winleft = (screen.width / 2) - (winwidth / 2); 
   var wintop = (screen.height / 2) - (winheight / 2); 

	var newWindow;
	var props = 'scrollBars=no,resizable=no,toolbar=no,menubar=no,location=no,directories=no,width=' + winwidth + ',height=' + winheight + ',top=' + wintop + ',left=' + winleft;
	newWindow = window.open('/txt_feedback.asp?id='+art_id, '', props);
}

function initialize()
{	
	if (document.getElementById("articleBody") != null) initArticle();	 	
	window.onresize = windowResize;
}

function initArticle()
{
	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 windowResize()
{
	if (document.getElementById("articleBody") != null) setArticleHeight() //adjust article layout
}

function loadFontSize()
{
	tempArray = document.cookie.split(";");		
	for (i = 0; i < tempArray.length; i++)
	{
		if (tempArray[i].indexOf('fontSize') > -1) 
		{
			fontValue = tempArray[i].split("=")
			fontSize = parseInt(fontValue[1]);
			lineHeight = fontSize+Math.round(.3*fontSize);
		}
		if (tempArray[i].indexOf('columnMode2') > -1) 
		{
			colValue = tempArray[i].split("=")
			columnMode = parseInt(colValue[1]);
		}
	}
}

function saveFaceSize()
{
	var expire = new Date ();
   	
   expire.setTime (expire.getTime() + (7000 * 24 * 3600000)); 
   expire = expire.toGMTString();
	document.cookie="fontSize="+fontSize+"; columnMode2="+columnMode+"; path=/; expires="+expire;
	document.cookie="columnMode2="+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 articleSetup()
{
	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 setArticleHeight() //clips the parent layer to the defined visible area
{
	//if space allows include bottom, otherwise clip out most of bottom
	if (columnMode > 1)
	{
		if (document.getElementById("articleBody") != null)
		{
			document.getElementById("articleParent").style.height = setSnap(300)
			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 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 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"
	}
}

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))
		}
	}
	if (articleGraphic) layoutGraphic();
		
	articlePages();
}

function nextPageOver()
{
	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()
{
	if (currentPos > 0)
	{	
		obj = document.getElementById("prevCool")
		obj.style.visibility = "hidden"
		obj = document.getElementById("prevHot")
		obj.style.visibility = "visible"
	}
}

function hidePageOver()
{
	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 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)

	if (snap < lineHeight*10) 
	{
		snap = lineHeight*10; //make the article parent 10 rows tall;
		//if (window.scrollTo) window.scrollTo(0,0);
	}
	//else if (window.scrollTo) window.scrollTo(0,0);
	return snap;	
}

function articlePages()
{
	//returns the number of screens an article spans, ie the number of pages 
	parentHeight = getHeight("articleParent")
	
	totalColumns = tHeight/parentHeight;
	totalPages = Math.ceil(totalColumns);
	tPos = (currentPos+columnMode)/columnMode;

	pagesTotal = Math.ceil(totalPages/columnMode)+1;
	pagesCurrent = Math.round(tPos+1);

	if (pagesTotal == 1) {
		pagesTotal = 2;
	}
	if (columnMode	==1) {
		pagesTotal = 2;
	}

	if (pagesCurrent > 9) 
	{		
		total = pagesCurrent + ""; 
		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 + ""; 
		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()
{
	parentHeight = getHeight("articleParent")
	
	if ((parentHeight*(currentPos+columnMode)) < tHeight) //check to make sure page is still visible 
	{
		currentPos= currentPos+columnMode;
	}

	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=0;
	obj = document.getElementById("articleParent");
	obj.style.height = getHeight("at0");
	
	obj = document.getElementById("at1");
	obj.style.display = 'none';
	
	obj = document.getElementById("at2");
	obj.style.display = "none";
	
	parentHeight = getHeight("articleParent");
	
	obj = document.getElementById("at0");
	obj.style.top = -1*(parentHeight*currentPos);
	
	articleGraphic.style.top = document.getElementById("at0").offsetHeight-articleGraphic.offsetHeight+"px";
	articleGraphic.style.left = "480px";
	articleGraphic.style.visibility = "visible";

	obj = document.getElementById("articleRelated")
	obj.style.visibility = "hidden"
	
	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 eventTwoColumn() 
{
	twoColumn();
	setArticleHeight();

	saveFaceSize();
	articlePages();
}
	
function twoColumn() 
{
	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"

	if (pagesTotal > 2) {
		obj = document.getElementById("articleRelated")
		obj.style.visibility = "visible"
	} else {
		obj = document.getElementById("articleRelated")
		obj.style.visibility = "hidden"
	}
	
	document.getElementById("articleNextPage").style.display = "block";
	document.getElementById("articlePrevPage").style.display = "block";
	
	saveFaceSize();
}

function threeColumn() 
{
	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 = colWidth;
	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;

	if (pagesTotal > 2) {
		obj = document.getElementById("articleRelated")
		obj.style.visibility = "visible"
	} else {
		obj = document.getElementById("articleRelated")
		obj.style.visibility = "hidden"
	}
	
	document.getElementById("articleNextPage").style.display = "block";
	document.getElementById("articlePrevPage").style.display = "block";
	
	saveFaceSize();
}

function eventFaceLarger()
{
	fontSize = fontSize+2;
	if (fontSize > 18 && columnMode != 1) {twoColumn();}
	if (fontSize > 24) {fontSize = 24;}
	
	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 testArticle()
{
	obj = document.getElementById("articleParent")
	obj.doScroll("up");
}

if (!document.all)
{
	document.writeln('<style type="text/css">');
	document.writeln('.c, .click {cursor:pointer}');
	document.writeln('.noborder {border:1px solid}');
	document.writeln('</style>');
}

function displayFix()
{
	document.getElementById("bodyNode").style.display = "block"
}	

window.onload = displayFix;


