
function initialize()
{
    if (document.getElementById("articleBody") != null) initArticle();
    document.onmousemove = mousePosition;
    window.onresize = windowResize;
}
function displayFix()
{
}
//window.onload = displayFix;
var mouseX = mouseY =0;
function mousePosition(e)
{
    if (e) event = e;
    mouseX = event.clientX;
    mouseY = event.clientY;
}
function windowResize()
{
    if (document.getElementById("articleBody") != null) setArticleHeight() //adjust article layout
}
if (document.all) classFix = "className"
else classFix = "class"
var pagesTotal = 0;
var originalHeight;
var fontSize = 14;
var lineHeight = fontSize+Math.round(.3*fontSize);
var currentPos = 0;
var tHeight = 0;
var articleGraphic = null;
var snapHeight = 400;
columnMode = 1;
col3Width = 230;
col2Width = 240;
col1Width = 620;
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()
{
    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 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()
{
    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);
    }
}
function layoutArticles()
{
    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"
        }
    }
    articlePages();
}
function setSnap(mod)
{
    if (mod == null) mod =0;
    snap = lineHeight*Math.round((getHeight("window")-mod)/lineHeight)
    if (snap < lineHeight*10)
    {
        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 (document.getElementById("articleBody") != null)
    {
        document.getElementById("articleParent").style.height = setSnap(snapHeight)
        tHeight = getHeight("at0")
        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 getPageNumberingHTML()
{
    resultString = "";
    for(p = 1; pagesTotal > 2 && p < pagesTotal; p++)
    {
    	styleClass = "page-num";
    	if( currentPos == (p - columnMode) ) {
    		styleClass = "page-num-current";
    	}
        resultString += "<a href='javascript:void(0);' onclick='setPage(" + (p - columnMode) + ");' class='" + styleClass + "'>"
        + p + "</a> ";
        //if( p != (pagesTotal - columnMode) ) {
        	if( currentPos == (p - columnMode) ) {
        		resultString += "<img src='" + app_root + "/images/highlighted-div.gif' align=center width=9 height=30> ";
        	} else {
        		resultString += "<img src='" + app_root + "/images/spacer.gif' align=center width=9 height=30> ";
        	}
        //}
    }
    return resultString + "";
}
function articlePages()
{
    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);
    
    document.getElementById("page_numbers").innerHTML = getPageNumberingHTML();
    
    if (pagesCurrent > 9)
    {
        total = pagesCurrent+"";
    }
    else
    {
    }
    if (pagesTotal > 9)
    {
        total = pagesTotal+"";
    }
    else
    {
    }
}
function setPage(num)
{
    parentHeight = getHeight("articleParent")
    if (currentPos < 0) currentPos = 0;
    if ((parentHeight*(num)) < tHeight) //check to make sure page is still visible
    {
        currentPos= parseInt(num);
    }
    layoutArticles();
}
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=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 oneColumn() //switches to two Column mode
{
    currentPos = 0;
    columnMode = 1
    colWidth = col1Width;
    obj = document.getElementById("at0")
    obj.style.width = colWidth;
    obj.style.left = 0;
    obj = document.getElementById("at1")
    obj.style.display = "none"
    obj = document.getElementById("at2")
    obj.style.display = "none"
    document.getElementById("articleNextPage").style.display = "block";
    document.getElementById("articlePrevPage").style.display = "block";
    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));
    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();
    saveFaceSize();
}
function eventFaceLarger()
{
    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 initArticle()
{
    if (document.getElementById("articleBody") != null)
    {
        articleSetup();
        setArticleHeight();
        if (columnMode == 1) oneColumn();
        if (columnMode == 2) twoColumn();
        if (columnMode == 3) threeColumn();
        setFaceSize();
        articlePages();
        layoutArticles()
        obj = document.getElementById("articleParent");
        obj.onkeyup = eventArticleFocus;
        obj.onscroll = testArticle;
    }
}

