//util functions =====================================================================================

function TotalCols(){ //returns the total number of colums needed to display all text
    obj = document.getElementById('textcol0');
    myRest = (obj.offsetHeight / ScreenX.pxEmRatio) % columnHeight;
    if (myRest>0){
        myTotal = parseInt((obj.offsetHeight / ScreenX.pxEmRatio) / columnHeight) + 1;
    }else{
        myTotal = parseInt((obj.offsetHeight / ScreenX.pxEmRatio) / columnHeight);
    }
    return myTotal;
}

function LeftCol(){ //returns column number of the column showed on the left
    return myStartPos / myColHeight + 1;
}

//end util functions ---------------------------------------------------------------------------------

//article setup functions ============================================================================

function RemoveCols(){
    var myFrame=document.getElementById('articleframe');
    i = 0;
    while (myFrame && (i < myFrame.childNodes.length)) {
        if ((myFrame.childNodes.item(i).nodeName == 'DIV') && myFrame.childNodes.item(i).id && myFrame.childNodes.item(i).id.match(/^col[0-9]+$/)){
            while (myFrame.childNodes.item(i).hasChildren) {
                myFrame.childNodes.item(i).removeChild(myFrame.childNodes.item(i).lastChild);
            }
            myFrame.removeChild(myFrame.childNodes.item(i));
        } else {
            i++;
        }
    }
}


function SetupCols(){
    //calculate column dimensions
    GetColWidth();
    GetArticleHeight();
    // add style rule for lineheight
    mySheet = document.styleSheets[document.styleSheets.length - 1];
    if (mySheet.cssRules) { // DOM2
        mySheet.insertRule('#articleframe * { line-height:' + myLineHeight + 'em; }', mySheet.cssRules.length);
        mySheet.insertRule('#articleframe p { margin-bottom:' + myLineHeight + 'em; }', mySheet.cssRules.length);
    } else if (mySheet.rules) {// IE 4+
        mySheet.addRule('#articleframe *', '{ line-height:' + myLineHeight + 'em; }');
        mySheet.addRule('#articleframe p', '{ margin-bottom:' + myLineHeight + 'em; }');
    }
    myStartPos=0;
    //get the frame where everything occurs
    var myFrame=document.getElementById('articleframe');
    myFrame.style.height=(articleHeight)+'em';
    myFrame.style.lineHeight=myLineHeight+'em';
    for (i=0;i<myColCount;i++){
        //create column
        var myCol=document.createElement("DIV");
        s=myCol.style;
        myCol.setAttribute('id','col'+i);
        s.position='absolute';
        s.top='0px';
        s.left=(myColWidth*i+myColDist*i)+'em';
        s.width=myColWidth+'em';
        s.height=articleHeight+'em';
        s.overflow='hidden';
        s.paddingTop='0px';
        s.marginTop='0px';
        myFrame.appendChild(myCol);
        //get text in column
        var myText=document.getElementById('articletext');
        myTextCol=myText.cloneNode(true);
        s=myTextCol.style;
        myTextCol.setAttribute('id','textcol'+i);
        s.position='relative';
        s.display='block';
        s.lineHeight=myLineHeight+'em';
        newTop = '-' + (articleHeight *i);
        s.top=newTop+'em';
        if (i==0) {
            s.paddingRight = '0.5em';
        } else if (i+1 == myColCount) {
            s.paddingLeft = '0.5em';
        } else {
            s.paddingRight = '0.25em';
            s.paddingLeft = '0.25em';
        }
        myCol.appendChild(myTextCol);
    }
}

function SetupNavigation() {
    var obj=document.getElementById('textcol0');//left column
    try {
        if (columnHeight > 0) {
            columnHeight = obj.parentNode.offsetHeight / pxEmRatio;
        }
    } catch (Exception){
        columnHeight = obj.parentNode.offsetHeight / pxEmRatio;
    }
    obj.parentNode.style.backgroundImage='none';
    obj.parentNode.style.backgroundRepeat='no-repeat';
    if (Math.round(obj.style.top.replace(/[^0-9\.-]{1,2}/,'')) < 0) {
        obj.onmouseover=PrevColOver;
        obj.onmouseout=PrevColOut;
        obj.onclick=PrevCol;
    } else {
        obj.onmouseover=null;
        obj.onmouseout=PrevColOut;
        obj.onclick=null;
        PrevColOut();
    }
    var obj=document.getElementById('textcol'+(myColCount-1));//right column
    obj.parentNode.style.backgroundImage='none';
    obj.parentNode.style.backgroundRepeat='no-repeat';
    if (!obj.style.top.match(/em/)) {
        obj.style.top = (obj.style.top.replace(/[^0-9\.-]{1,2}/, '') / pxEmRatio) + 'em';
    } 
    if (-(obj.style.top.replace(/[^0-9\.-]{1,2}/,'') - columnHeight) < (obj.offsetHeight / ScreenX.pxEmRatio)) {
        obj.onmouseover=NextColOver;
        obj.onmouseout=NextColOut;
        obj.onclick=NextCol;
    } else {
        obj.onmouseover=null;
        obj.onmouseout=NextColOut;
        obj.onclick=null;
        NextColOut();
    }
    /* set col numbers */
    myTotalCols = TotalCols();
    obj=document.getElementById('textcol0');
    topPos = obj.style.top.replace(/[^0-9\.-]{1,2}/,'');
    colNumber = Math.round(-1 * topPos / columnHeight) + 1;
    myTotalColObj = document.getElementById('totalcols');
    myColText = 'column ' + colNumber;
    for (i = 2; i < myColCount; i++) {
        myColText += ', ' + (colNumber + i - 1);
    }
    myColText += ' and ' + (colNumber + myColCount - 1) + ' of ' + myTotalCols;
    myTextNode = document.createTextNode(myColText);
    while (myTotalColObj.hasChildNodes()) {
        myTotalColObj.removeChild(myTotalColObj.lastChild);
    }
    myTotalColObj.appendChild(myTextNode);
}

function GetColWidth() {
    //ScreenX.pxEmRatio = 12;
    obj = document.getElementById('articletext');
    obj.style.display='block';
    myWidth = obj.offsetWidth / ScreenX.pxEmRatio;
    obj.style.display='none';
    myColDist = 1;
    myColWidth=myWidth-(myColCount-1)* myColDist;
    myColWidth-=myColWidth%myColCount;
    myColWidth=myColWidth/myColCount;
}

function GetArticleHeight() {
    obj = document.getElementById('articleframe_padding');
    obj.style.marginTop='0px';
    heightAbove = obj.offsetTop;
    while (obj = obj.offsetParent) {
        heightAbove += obj.offsetTop;
    }
    totalHeight = WindowX.innerSize.y;
    objButtonBar = document.getElementById('articlebuttonbar');
    heightAboveBar = objButtonBar.offsetTop;
    while (objButtonBar = objButtonBar.offsetParent) {
        heightAboveBar += objButtonBar.offsetTop;
    }
    firstBodyChild = document.getElementById('site_frame_div_id')
    heightBelow = firstBodyChild.offsetHeight - heightAboveBar;
    articleHeight = (totalHeight - heightAbove - heightBelow) / ScreenX.pxEmRatio;
    myLineHeight= Math.round(1.4 * ScreenX.pxEmRatio) / ScreenX.pxEmRatio;
    articleHeight -= articleHeight%myLineHeight + myLineHeight;
    if ((articleHeight / myLineHeight) < 10) {
        articleHeight = (myLineHeight *10);
    }
    return articleHeight; 
}

//end article setup functions ------------------------------------------------------------------------

//start articleextra functions -----------------------------------------------------------------------


extraColScrolling=0;

extraActiveItem=0;

function DrawExtraCol(){
    obj=document.getElementById('article_header');
    headerHeight = obj.offsetHeight / ScreenX.pxEmRatio;
    obj=document.getElementById('articleframe_padding');
    articlePaddingHeight = obj.offsetHeight / ScreenX.pxEmRatio;
    obj=document.getElementById('article_right_div_id');
    obj.style.height = (articlePaddingHeight + headerHeight) + 'em';
    obj=document.getElementById('articleextraframe');
    obj.style.height = (articlePaddingHeight + headerHeight - 2) + 'em';
}

function MouseOverExtraColUp(){
    obj=document.getElementById('articleextraframe');
    col=document.getElementById('articleextracol');
    colTop=col.style.top;
    colTop=colTop.replace(/[^0-9\.-]+$/,'')*1;
    if (colTop<0) {
        col.style.top=(colTop+1)+'px';
        extraColScrolling=setTimeout('MouseOverExtraColUp()',extraColScrollSpeed);
    }
}

function MouseOutExtraColUp(){
    if (extraColScrolling!=0){
        clearTimeout(extraColScrolling);
        extraColScrolling=0;
    }
}

function MouseOverExtraColDown(){
    obj=document.getElementById('articleextraframe');
    col=document.getElementById('articleextracol');
    colTop=col.style.top;
    colTop=colTop.replace(/[^0-9\.-]+$/,'')*1;
    if ((col.offsetHeight+colTop+12)>obj.offsetHeight) {
        col.style.top=(colTop-1)+'px';
        extraColScrolling=setTimeout('MouseOverExtraColDown()',extraColScrollSpeed);
    }
}

function MouseOutExtraColDown(){
    if (extraColScrolling!=0){
        clearTimeout(extraColScrolling);
        extraColScrolling=0;
    }
}

function ExtraItemClick(linkObj){
    obj = linkObj.parentNode.nextSibling;
    if (obj.style.display == 'none') {
        obj.style.display='block';
        obj.setAttribute('class','active');
    } else {
        obj.style.display='none';
        obj.removeAttribute('class');
    }
}

function ExtraItemShow(){
    obj=document.getElementById('articleextramenugroup');
    //get first item and simulate a click
    for (i=0;i<obj.childNodes.length;i++){
        if (obj.childNodes.item(i).nodeName=='A'){
            ExtraItemClick(obj.childNodes.item(i).id.replace(/(.*)_link$/i,'$1'));
            break;
        }
    }
}

//end articleextra functions -------------------------------------------------------------------------

//event functions ====================================================================================

function FirstCol() {
    try {
        if (columnHeight > 0) {
            columnHeight = columnHeight;
        }
    } catch (Exception){
        obj = document.getElementById('col0');
        columnHeight = obj.offsetHeight / ScreenX.pxEmRatio;
    }
    for (i = 0; i < myColCount; i++) {
        obj = document.getElementById('textcol' + i);
        obj.style.top = (-1 * i * columnHeight) + 'em';
    }
    SetupNavigation();
}

function PrevCol(e) {
    // detect columnHeight
    try {
        if (columnHeight > 0) {
            columnHeight = columnHeight;
        }
    } catch (Exception){
        obj = document.getElementById('col' + (myColCount -1));
        columnHeight = obj.offsetHeight / ScreenX.pxEmRatio;
    }
    // set top position left column
    colObj = document.getElementById('textcol0');
    topPos = colObj.style.top;
    if (!topPos.match(/em/)) {
        topPos = topPos.replace(/[^0-9\.-]*/, '') / ScreenX.pxEmRatio;
    }
    topPos = parseFloat(topPos.replace(/[^0-9\.-]*/, ''));
    if (topPos + columnHeight >= 0) {
        FirstCol();
        return;
    }
    // right columns
    for (i = 0; i < myColCount; i++) {
        colObj = document.getElementById('textcol' + i);
        colObj.style.top = parseFloat(topPos + ((1 - i) * columnHeight)) + 'em';
    }
    SetupNavigation();
}

function PrevArrowOver(){
    arrowobj=document.getElementById('articleprevcol');
    arrowobj.style.color='#cc3333';
    arrowobj=document.getElementById('articleprevcolarrow');
    arrowobj.src= websiteConfig.SiteRoot+websiteConfig.Layout+'/arrowpreviousover.gif';
}

function PrevArrowOut(){
    arrowobj=document.getElementById('articleprevcol');
    arrowobj.style.color='#333366';
    arrowobj=document.getElementById('articleprevcolarrow');
    arrowobj.src=websiteConfig.SiteRoot+websiteConfig.Layout+'/arrowprevious.gif';
}

function PrevColOver(){
    obj=document.getElementById('col0');
    if (obj.style.backgroundImage!=myColLeftBack){
        obj.style.backgroundImage=myColLeftBack;
        obj.style.backgroundPosition='0% '+ ((columnHeight-columnHeight%2)/2-50/ScreenX.pxEmRatio)+'em';
        obj.firstChild.style.cursor = myColChangeCursor;
        obj.setAttribute('title','previous column');
        PrevArrowOver();
    }
}

function PrevColOut(){
    obj=document.getElementById('col0');
    obj.style.backgroundImage=myColBack;
    obj.firstChild.style.cursor = 'auto';
    obj.removeAttribute('title');
    PrevArrowOut();
}

function NextCol(e){
    obj = document.getElementById('col0');
    colObj = document.getElementById('textcol0');
    // detect columnHeight
    try {
        if (columnHeight > 0) {
            columnHeight = columnHeight;
        }
    } catch (Exception){
        columnHeight = obj.offsetHeight / ScreenX.pxEmRatio;
    }
    // set top position right column
    topPos = colObj.style.top;
    if (!topPos.match(/em/)) {
        topPos = (topPos.replace(/[^0-9\.-]{1,2}/, '') / ScreenX.pxEmRatio) + 'em';
    } 
    if (-(colObj.style.top.replace(/[^0-9\.-]{1,2}/,'') - columnHeight) > (colObj.offsetHeight / ScreenX.pxEmRatio)) {
        LastCol();
        return false;
    }
    topPos = parseFloat(topPos.replace(/[^0-9\.-]*/, ''));
    // left columns
    for (i = 0; i < myColCount; i++) {
        colObj = document.getElementById('textcol' + i);
        topPos -= columnHeight;
        colObj.style.top = topPos + 'em';
    }
    SetupNavigation();
}

function NextArrowOver(){
    arrowobj=document.getElementById('articlenextcol');
    arrowobj.style.color='#cc3333';
    arrowobj=document.getElementById('articlenextcolarrow');
    arrowobj.src=websiteConfig.SiteRoot+websiteConfig.Layout+'/arrownextover.gif';
}

function NextArrowOut(){
    arrowobj=document.getElementById('articlenextcol');
    arrowobj.style.color='#333366';
    arrowobj=document.getElementById('articlenextcolarrow');
    arrowobj.src=websiteConfig.SiteRoot+websiteConfig.Layout+'/arrownext.gif';
}

function NextColOver(){
    obj=document.getElementById('col'+(myColCount-1));
    if (obj.style.backgroundImage!=myColRightBack){
        obj.style.backgroundImage=myColRightBack;
        obj.style.backgroundPosition='100% '+ ((columnHeight-columnHeight%2)/2-50/ScreenX.pxEmRatio)+'em';
        obj.firstChild.style.cursor = myColChangeCursor;
        obj.setAttribute('title','next column');
        NextArrowOver();
    }
}

function NextColOut(){
    obj=document.getElementById('col'+(myColCount-1));
    obj.style.backgroundImage=myColBack;
    obj.firstChild.style.cursor = 'auto';
    obj.removeAttribute('title');
    NextArrowOut();
}

function LastCol(){
    try {
        if (columnHeight > 0) {
            columnHeight = columnHeight;
        }
    } catch (Exception){
        obj = document.getElementById('col0');
        columnHeight = obj.offsetHeight / ScreenX.pxEmRatio;
    }
    for (i = 0; i < myColCount; i++) {
        obj = document.getElementById('textcol' + i);
        obj.style.top = -((parseInt(obj.offsetHeight / ScreenX.pxEmRatio / columnHeight) - myColCount + i + 1) * columnHeight) + 'em';
    }
    SetupNavigation();
}

function SendImageOver(){
    obj=document.getElementById('articlesendimage');
    obj.src=websiteConfig.SiteRoot+websiteConfig.Layout+'/sendover.gif';
}

function SendImageOut(){
    obj=document.getElementById('articlesendimage');
    obj.src=websiteConfig.SiteRoot+websiteConfig.Layout+'/send.gif';
}

function PrintImageOver(){
    obj=document.getElementById('articleprintimage');
    obj.src=websiteConfig.SiteRoot+websiteConfig.Layout+'/printover.gif';
}

function PrintImageOut(){
    obj=document.getElementById('articleprintimage');
    obj.src=websiteConfig.SiteRoot+websiteConfig.Layout+'/print.gif';
}

function SingleCol(){
    topPos = 0;
    RemoveCols();
    myColCount=1;
    GetArticleHeight();
    obj = document.getElementById('articletext');
    obj.style.height=articleHeight+'em';
    obj.style.overflow='auto';
    obj.style.display='block';
    DrawExtraCol();
    myColSaveExpire=new Date();
    myColSaveExpire.setMonth(myColSaveExpire.getMonth()+1);
    myColSaveExpire=myColSaveExpire.toGMTString();
    document.cookie='articleColCount=1;expires='+myColSaveExpire+';path=/;';
}

function MultiCols(){
    topPos = 0;
    RemoveCols();
    if (MultiCols.arguments && (MultiCols.arguments.length > 0) && (parseInt(MultiCols.arguments[0]) <= myMaxColCount) && (parseInt(MultiCols.arguments[0]) > 0)) {
        myColCount = MultiCols.arguments[0];
    } else if ((myColCount < 2) || (myColCount == myMaxColCount)) {
        myColCount = 2;
    } else if (myColCount < myMaxColCount) {
        myColCount++;
    }
    GetArticleHeight();
    obj = document.getElementById('articletext');
    obj.style.height='';
    obj.style.overflow='';
    obj.style.display='block';
    SetupCols();
    SetupNavigation();
    DrawExtraCol();
    myColSaveExpire=new Date();
    myColSaveExpire.setMonth(myColSaveExpire.getMonth()+1);
    myColSaveExpire=myColSaveExpire.toGMTString();
    document.cookie='articleColCount=' + myColCount + ';expires='+myColSaveExpire+';path=/;';
}

function ResizeHandler(){
    /* call previous resize handler */
    if (_previousOnResize) {
        _previousOnResize();
    }
    if (myColCount == 1) {
        SingleCol();
    } else {
        MultiCols(myColCount);
    }
}

// functions for clippings

function addToClippings(id) {
    myColSaveExpire=new Date();
    myColSaveExpire.setMonth(myColSaveExpire.getMonth()+12);
    myColSaveExpire=myColSaveExpire.toGMTString();
    clippings = cookieGet('knipsels');
    if (clippings) {
        eval('clippingsArray = ' + clippings);
        for (i = 0; i < clippingsArray.length; i++) {
            if (clippingsArray[i] == id)
                return;
        }
        clippingsArray[i] = id;
        document.cookie = 'knipsels=['+clippingsArray.toString()+'];expires='+myColSaveExpire+';path=/;';
    } else {
        document.cookie='knipsels=['+id+'];expires='+myColSaveExpire+';path=/;';
    }
}