﻿// JScript File



//can't guarantee where this reference will be placed in the .NET code.  Since there may be styling
//of text occuring after this call, wait 100ms to call our specific styling.
function fixNavigation() {    setTimeout("doTasks()",100); }
function doTasks() {
    fixSeparators('active');
    fixSeparators('mypei');
    fixNotBreakingSpaces();
    fixBottomSpan();
    fixDecorationOfTopSubnavItem();
    //fixMyPEI();
    //fixSubnavLocation();
    fixTopSpan(); 
}



//this fixes the nonbreaking space characters found in the subnav menu with regular spaces so they will wrap
function fixNotBreakingSpaces() {
    var myObj = document.getElementById('sidenav');
    if (myObj!=null) {
      myObj.innerHTML = myObj.innerHTML.replace(/&nbsp;/g,' ');
    }
}



//this makes the left and right separator's active around an active menu item which does not occur normally
//  when secondary items are active
//it also takes care of turning the "MyPEI tab black (when setType==mypei)
function fixSeparators(setType) {
    var sepImageSuffix;
    if (setType=='mypei') {
        sepImageSuffix = '_spec_';
    } else {
        sepImageSuffix = '_sel_';
    }

    //define the primary container we're going to work with
    var objContainer = getElementsByClassName('dnn_dnnsolpartmenu_ctldnnsolpartmenu_spmbctr','table');

    try {
        //2009.02.25 DSL moved from above
        var objTable = objContainer[0];
        var objTBody = objTable.getElementsByTagName("tbody")[0];
        var objTR = objTBody.getElementsByTagName("tr")[0];
        var objTD = objTR.getElementsByTagName("td");

        //we'll find the active item, then look at the tables to the left and to the right to find the separator
        //images and replace the _off_ state with the sepImageSuffix state
        var indexLeftcell = 0;
        var indexRightcell = 0;
        for (var i=0; i<objTD.length-1; i++) {
            if (setType=='mypei') {
                var objNavItem =  getElementsByClassName('MainMenu_RootMenuItem','tr',objTD[i]);
                for (var j=0; j<objNavItem.length; j++) {
                    if (objNavItem[j].innerHTML.indexOf("Wiki PEI")>0) {
                        objNavItem[j].style.cssText="background-color:#000000;"
                        objNavItem[j].setAttribute('onmouseover','');
                        indexLeftcell = i;
                        indexRightcell = i;
                        break;
                    }
                }
            } else {
                var objActiveNav =  getElementsByClassName('MainMenu_RootMenuItemActive','tr',objTD[i]);
                if (objActiveNav.length > 0) {
                    indexLeftcell = i;
                    indexRightcell = i;
                    break;
                }
            }
        }

        //the tables containing the separator cells aren't directly to the right and left of the selected item, so
        // we'll iterate through the objects until we find the closest ones.
        var objImg = null;
        do {
            indexLeftcell--;
            objImg = tableDrilldown(objTD[indexLeftcell],0,0,0,1,0);
            if (indexLeftcell<0) break;
            
        } while (objImg==null);
        objImg.setAttribute("src",objImg.src.replace(/_off_/,sepImageSuffix).replace(/_sel_/,sepImageSuffix));
        
        do {
            indexRightcell++;
            objImg = tableDrilldown(objTD[indexRightcell],0,0,0,0,0);
            if (indexRightcell>objTD.length) break;
        } while (objImg==null);
        objImg.setAttribute("src",objImg.src.replace(/_off_/,sepImageSuffix).replace(/_sel_/,sepImageSuffix));

    } catch(err) {
    }
}


//by default, the menu is flush left.  
//This procedure causes it to full-justify, adding padding within the tabs to lengthen the menu as necessary
function fixTopSpan() {
    try {

        //now look at the width of all the break characters
        var totWidth = 0;
        var thisWidth = 0;
        var navCount = 0;
        var i = 0;
        var objBreaks = getElementsByClassName('dnn_dnnsolpartmenu_ctldnnsolpartmenu_spmbrk','td');
        for (i=0; i<objBreaks.length; i++) {
            thisWidth = objBreaks[i].offsetWidth;
            totWidth+=thisWidth;
        }
        //now add the width of actual nav elements, and count the number of elements
        var objNav = getElementsByClassName('dnn_dnnsolpartmenu_ctldnnsolpartmenu_spmbar', 'tr');
        for (i = 0; i < objNav.length; i++) {
            thisWidth = objNav[i].offsetWidth;
            totWidth+=thisWidth;
            navCount++;
        }
        
        
        //remove trailing spacer images in the menu
        var objContainer = getElementsByClassName('dnn_dnnsolpartmenu_ctldnnsolpartmenu_spmbctr','table');
        var objTable = objContainer[0];
        var objTBody = objTable.getElementsByTagName("tbody")[0];
        var objTR = objTBody.getElementsByTagName("tr")[0];
        var objTD = objTR.getElementsByTagName("td");
        
        objTR.removeChild(objTD[objTD.length-1]);       //remove last space that dnn inserts to the right
        
        var objLastBreak = objTD[objTD.length-1];
        var objLastBreakImages = objLastBreak.getElementsByTagName("img");
        var lastBreakImageWidth = objLastBreakImages[objLastBreakImages.length-1].getAttribute("width");
        objLastBreak.removeChild(objLastBreakImages[objLastBreakImages.length-1]);  //remove last space that we define as part of the tab break.
        
        

        //calculate the padding to add to each element
        var navWidth = document.getElementById('nav').offsetWidth;
        totWidth-=lastBreakImageWidth;
        var widthDiff = navWidth-totWidth;
        
        var extraPaddingPerItem = widthDiff/navCount;
        var extraPaddingPerItem_remainder = widthDiff%navCount;
        extraPaddingPerItem = getInteger(extraPaddingPerItem,extraPaddingPerItem_remainder,navCount);
        
        var padLeft = 0; 
        var padRight = 0;
        var padSide = extraPaddingPerItem/2
        var padSide_remainder = extraPaddingPerItem%2
        if (padSide_remainder>0) {
            padSide = getInteger(padSide,padSide_remainder, 2)
            padLeft = padSide;
            padRight = padSide+1;
        } else {
            padLeft = padSide;
            padRight = padSide;
        }
        
        //add the padding to each element
        var stylePadding;
        for (i = 0; i < objNav.length; i++) {
            //finally, we have to compensate for any remaining pixels:
            if (extraPaddingPerItem_remainder>0) {
                stylePadding = "padding-left:" + padLeft + "px; padding-right:" + (padRight + 1) + "px;"
                extraPaddingPerItem_remainder--;
            } else {
                stylePadding = "padding-left:" + padLeft + "px; padding-right:" + padRight + "px;"
            }
            objNav[i].getElementsByTagName("td")[0].style.cssText=stylePadding;
        }
        
        
    } catch (err) {
    }    

}



//by default, the menu is flush left.  In this case, we want to center justify.  We also want to add some space between each element
function fixBottomSpan() {

    //create references to containers
    var objContainer = document.getElementById('bottomNav');
    var objSpan = objContainer.getElementsByTagName("span")[0];
    var objTable = objSpan.getElementsByTagName("table")[0];

    //2009.02.25 DSL added if
    if (objTable) {
        var objTBody = objTable.getElementsByTagName("tbody")[0];
        var objTR = objTBody.getElementsByTagName("tr")[0];
        var objTD = objTR.getElementsByTagName("td");
    
        //this inner table is set to 100% width, we don't want a full span, so get rid of that.
        objTable.removeAttribute("width");
    
        //go through each TD item to get to the inner table cells, and add 5 space characters
        for (var i=0; i<objTD.length-1; i++) {
            var objSubTD = tableDrilldown(objTD[i],0,0,0,0,null); 
            if (objSubTD!=null) {           
                objSubTD.innerHTML="<span style=\"cursor:pointer;\">" + objSubTD.innerHTML + "</span>&nbsp; &nbsp; &nbsp;"  
            }
        }

        //delete the last td item which contains an image that pushes everything left
        objTR.removeChild(objTD[objTD.length-1]);
    }
}


//get rid of dotted line above the first subnav item
function fixDecorationOfTopSubnavItem() {
        var objContainer = getElementsByClassName('MainMenu_SubMenu','div');
        for (var i=0; i<objContainer.length; i++) {
            var objTopItemRow = getElementsByClassName('MainMenu_MenuItem','tr',objContainer[i])[0];
            var objTD = objTopItemRow.getElementsByTagName("td");
            for (j=0; j<objTD.length; j++) {
                objTD[j].style.cssText="background-image:none";
            }
        }
}

//subnav needs to be moved a bit to the left so it drops below the separators not the main area
function fixSubnavLocation() {
    var objContainer = getElementsByClassName('MainMenu_SubMenu','div');
        //alert(objContainer[0].style.cssText + objContainer[0].id);
    for (var i=0; i<objContainer.length; i++) {
    //dnn_dnnsolpartmenu_ctldnnsolpartmenu_spmsub 
    }
}








//helper functions
function getInteger(num, rem, div) {
    return(Math.round(num-(rem/div)));
}

function tableDrilldown(objContainer, iTbl, iTB, iTR, iTD, iObj) {
    try {
        var oT = objContainer.getElementsByTagName("table")[iTbl];
        var oTB = oT.getElementsByTagName("tbody")[iTB];
        var oTR = oTB.getElementsByTagName("tr")[iTR];
        if (iTD==null) {
            return oTR;
        } else {
            var oTD = oTR.getElementsByTagName("td")[iTD];
            if (iObj==null) {
                return oTD;
            } else {            
                return(oTD.getElementsByTagName("img")[iObj]);
            }
        }
    } catch (err) {
        return (null);
    }
}


//scroller function -- fix FAQ module bug by scrolling window without using anchor
function scrollpage(target) {
    try {
        var destination = document.getElementById(target);
        var curtop = 0;
        
        if (destination.offsetParent) {
            do {
                curtop += destination.offsetTop;
            } while (destination = destination.offsetParent);
            window.scroll(0,curtop);
            return true;
        } else {
            return false;
        }
    } catch (err) {
        return false;
    }
}
