
function inArray(searchVal, searchSource) {
    for (nextIndex in searchSource) {
        if (searchSource[nextIndex] == searchVal) {
            return true;
        }
    }
    return false;
}


/* START SITE TOP NAV CUSTOMIZATION */

// Remove top nav items that do not appear but still need to appear in the admin menu (i.e. Admin, Host, etc.).
// The following menu items had to be hidden by javascript since their "a" tags do not readily have IDs
// associated with them as do links like Register.
// Login was added in order to move it to the footer.
var excludeMenuItems = new Array("Admin",
                                 "Host",
                                 "Login",
                                 "Logout"
                                 );

var myTopNav = document.getElementById("tHeaderLinks");

if (myTopNav) {
    var myTopNavList = xGetElementsByClassName("art-menu", myTopNav, "ul");

    if (myTopNavList && myTopNavList.length > 0) {
        var myTopNavListItems = myTopNavList[0].getElementsByTagName("li");

        if (myTopNavListItems && myTopNavListItems.length > 0) {
            // Predetermine how many items will be included in the top nav.
            // This count will be used later to determine whether or not a section's
            // drop-down will be left or right aligned so as to ensure that the width
            // of the drop-down does not appear outside of the content area's boundaries.
            var totalSectionsInTopNav = 0;
            
            for (var i = 0; i < myTopNavListItems.length; i++) {
                var menuItemCurr = myTopNavListItems[i];
                var menuItemLabels = xGetElementsByClassName("t", menuItemCurr, "span");
                
                // Only evaluate the section li's found amongst all li's retrieved above from
                // the top nav menu bar.
                if (menuItemLabels && menuItemLabels.length > 0) {
                    menuItemLabel = menuItemLabels[0];
                    menuItemLabel = menuItemLabel.innerHTML;
                    menuItemLabel = menuItemLabel.replace(/(^\s*)|(\s*$)/g, '');

                    if (!inArray(menuItemLabel, excludeMenuItems)) {
                        // Increment total count for each included section link.
                        totalSectionsInTopNav++;
                    }
                }
            }

            var sectionItemsNowShowingInTopNav = 0;
            for (var i = 0; i < myTopNavListItems.length; i++) {
                var menuItemCurr = myTopNavListItems[i];
                var menuItemLabels = xGetElementsByClassName("t", menuItemCurr, "span");

                // Only evaluate the section li's found amongst all li's retrieved above from
                // the top nav menu bar.
                if (menuItemLabels && menuItemLabels.length > 0) {
                    menuItemLabel = menuItemLabels[0];
                    menuItemLabel = menuItemLabel.innerHTML;
                    menuItemLabel = menuItemLabel.replace(/(^\s*)|(\s*$)/g, '');
                    
                    if (inArray(menuItemLabel, excludeMenuItems)) {
                        if (menuItemLabel == "Login" || menuItemLabel == "Logout") {
                            // Move the login / logout link in the footer.
                            var myLoginLink = myTopNavListItems[i];
                            var myFooterLoginLink = document.getElementById("tLoginLink");

                            if (myLoginLink && myFooterLoginLink) {
                                myFooterLoginLink.innerHTML = myLoginLink.innerHTML;
                            }
                        }
                        else {
                            // Hide top nav items that are listed in the array excludeMenuItems
                            myTopNavListItems[i].style.display = 'none';
                        }
                    }
                    else {
                        sectionItemsNowShowingInTopNav++;

                        // Check all menu items that have not been marked for exclusion to see
                        // if any of its children have been selected. If so then also mark the
                        // parent top nav item as active.
                        var anyChildItemsActive = xGetElementsByClassName("active", menuItemCurr, "a");
                        if (anyChildItemsActive && anyChildItemsActive.length > 0) {
                            myTopNavListItems[i].className = "active";
                        }
                        else {
                            // Add onmouseover to all top nav section links that are not currently active
                            // to enable hover over state.
                            // Reusing the "active" style class since it is required to look the same
                            // as a section marked as currently active.
                            // Had to ensure that any other already assigned style classes remain unchanged.
                            // This is especially important where IE 6 adds "art-menuhover" when hovering over
                            // a nav item.
                            myTopNavListItems[i].onmouseover = function() { var myClassName = this.className; /* trim the class name list */myClassName = myClassName.replace(/(^\s*)|(\s*$)/g, ''); myClassName += ' active '; this.className = myClassName; }
                            myTopNavListItems[i].onmouseout = function() { var myClassName = this.className; /* trim the class name list */myClassName = myClassName.replace(/(^\s*)|(\s*$)/g, ''); /* remove " active " class name */myClassName = myClassName.replace(/\s*active\s*/g, ''); this.className = myClassName; }
                        }
                        
                        // Alter the layout of each section's subpage links from a single column
                        // drop-down to a multiple column. The row count will be limited to 4 until
                        // the max column count of 4 is exceeded. If 4 columns are reached then the
                        // row count limit will no longer be enforced while maintaining 4 columns.
                        var menuItemCurrLink = menuItemCurr.getElementsByTagName("a");
                        var menuItemCurrSubpageLinkList = menuItemCurr.getElementsByTagName("ul");
                        
                        if (menuItemCurrLink && menuItemCurrLink.length > 0
                            && menuItemCurrSubpageLinkList && menuItemCurrSubpageLinkList.length > 0
                            )
                        {
                            // Based on the menu's structure the first "A" tag is expected to be the section's
                            // link and the first UL found is expected to encompass the section's entire
                            // subpage link list.
                            var targetParent = menuItemCurr;
                            var targetSectionLink = '<a href="' + menuItemCurrLink[0] + '">' + menuItemCurrLink[0].innerHTML + '</a>';

                            // Get each subpage link.
                            var menuItemCurrLinkSubpageLinks = menuItemCurrSubpageLinkList[0].getElementsByTagName("li");

                            // To ease the layout alteration process, just wrap the UL in a table
                            // so that columns can be inserted and the original UL divided based on
                            // row and column constraints.
                            var maxRowCount = 4;
                            var maxColumnCount = 4;

                            // Adjust max row and column count if number of subpages exceed default grid dimensions.
                            // Keep max column count fixed and allow row count to adjust to accommodate subpage link count.
                            if (menuItemCurrLinkSubpageLinks.length > (maxRowCount * maxColumnCount)) {
                                maxRowCount = Math.ceil(menuItemCurrLinkSubpageLinks.length / maxColumnCount);
                            }

                            var requiredColumnCount = Math.ceil(menuItemCurrLinkSubpageLinks.length / maxRowCount);
                            var currRowCount = 0;
                            var menuItemCurrTemp = '';
                            var menuDropDownRightAlign = '';

                            // Determine whether or not a section's drop-down will be left or right aligned 
                            // so as to ensure that the width of the drop-down does not appear outside of
                            // the content area's boundaries. A drop-down will be right aligned if its
                            // column count - 1 (subtracting 1 to exclude the column directly beneath the 
                            // current parent section link) is greater than the number of section links to 
                            // the right of its parent section link.
                            if ((requiredColumnCount - 1) > (totalSectionsInTopNav - sectionItemsNowShowingInTopNav))
                            {
                                menuDropDownRightAlign = ' art-menu-multicolumn-right';
                            }
                            
                            menuItemCurrTemp += targetSectionLink;
                            menuItemCurrTemp += '<br /><div class="art-menu-multicolumn' + menuDropDownRightAlign + '"><table class="art-menu-multicolumn" cellpadding="0" cellspacing="0" border="0"><tr><td align="left" valign="top"><ul>';
                            for (var subpageLinkIndex = 0; subpageLinkIndex < menuItemCurrLinkSubpageLinks.length; subpageLinkIndex++) {
                                if (currRowCount >= maxRowCount) {
                                    menuItemCurrTemp += '</ul></td><td align="left" valign="top"><ul>';
                                    currRowCount = 0;
                                }
                                var myClassName = menuItemCurrLinkSubpageLinks[subpageLinkIndex].className;
                                if (myClassName != "") myClassName = ' class="' + myClassName + '"';
                                menuItemCurrTemp += '<li' + myClassName + '>' + menuItemCurrLinkSubpageLinks[subpageLinkIndex].innerHTML + '</li>';

                                currRowCount++;
                            }
                            menuItemCurrTemp += '</ul></td></tr></table></div>';

                            targetParent.innerHTML = menuItemCurrTemp;
                            
                            /*
                            if (window.console) {
                                console.info("script_custom.js:");
                                //console.log("targetParent = " + targetParent);
                                //console.log("targetSectionLink = " + targetSectionLink);
                                //console.log("menuItemCurrLinkSubpageLinks = " + menuItemCurrLinkSubpageLinks);
                                //console.log(menuItemCurrLinkSubpageLinks);
                                //console.log("menuItemCurrTemp = " + menuItemCurrTemp);
                                console.log("maxRowCount = " + maxRowCount);
                            }
                            */
                        }

                        /*
                        if (window.console) {
	                        console.info("script_custom.js:");
	                        console.log("anyChildItemsActive = " + anyChildItemsActive);
	                        console.log("anyChildItemsActive.length = " + anyChildItemsActive.length);
	                        console.log(anyChildItemsActive);
                        }
                        */
                    }
                }
            }
        }
    }
}

/* END SITE TOP NAV CUSTOMIZATION */


/* START CONTENT BANNER CUSTOMIZATION */

// A frame overlay is being used to allow the admin to upload a rectangular banner
// without needing to worry about including and aligning the frame. As a result,
// when a page is in edit mode the edit controls on the actual banner layer are
// not accessible since they are below the frame overlay layer. The following code
// was added to bring the layer that the banner edit controls are on to a level above
// the frame overlay layer.
var myContentBannerLayerHome = document.getElementById("tContentHeaderBannerHome");
if (myContentBannerLayerHome) {
    var isEditMode = xGetElementsByClassName("SubHead", myContentBannerLayerHome, "span");
    if (isEditMode && isEditMode.length > 0) {
        myContentBannerLayerHome.style.zIndex = '3';
    }
}

var myContentBannerLayer = document.getElementById("tContentHeaderBanner");
if (myContentBannerLayer) {
    var isEditMode = xGetElementsByClassName("SubHead", myContentBannerLayer, "span");
    if (isEditMode && isEditMode.length > 0) {
        myContentBannerLayer.style.zIndex = '3';
    }
}

/* END CONTENT BANNER CUSTOMIZATION */


/* START SITE LEFT NAV CUSTOMIZATION */

// Show only the subnav for the currently selected section.
var myLeftNav = document.getElementById("tLeftRailNav");

if (myLeftNav) {
    var myLeftNavList = xGetElementsByClassName("art-menu", myLeftNav, "ul");

    if (myLeftNavList && myLeftNavList.length > 0) {
        var myLeftNavListItems = myLeftNavList[0].getElementsByTagName("li");

        if (myLeftNavListItems && myLeftNavListItems.length > 0) {
            // Left nav is hidden by default for cases where the 
            // current section does not have any subpages.
            document.getElementById("tLeftRailNav").style.display = "none";
            document.getElementById("tLeftRailNavBottomBorder").style.display = "none";

            var noActiveLinksFound = true;
            for (var i = 0; noActiveLinksFound && i < myLeftNavListItems.length; i++) {
                var menuItemCurr = myLeftNavListItems[i];

                // Update banner title with current section's title
                var menuItemCurrTitleSpans = xGetElementsByClassName("t", menuItemCurr, "span");
                if (menuItemCurrTitleSpans && menuItemCurrTitleSpans.length > 0) {
                    menuItemCurrTitle = menuItemCurrTitleSpans[0].innerHTML;
                    document.getElementById("tContentBannerTitleText").innerHTML = menuItemCurrTitle;
                }
                
                // Check each section's menu items to see if any of its children are "active". 
                // If so then make the section's parent container visible.
                var anyChildItemsActive = xGetElementsByClassName("active", menuItemCurr, "a");
                if (anyChildItemsActive && anyChildItemsActive.length > 0) {
                    // No need to keep searching once first active link is found.
                    noActiveLinksFound = false;
                    
                    // Show the left nav. It is hidden by default for cases where the
                    // current section does not have any subpages.
                    var myChildItems = menuItemCurr.getElementsByTagName("li");
                    if (myChildItems.length > 0) {
                        document.getElementById("tLeftRailNav").style.display = "block";
                        document.getElementById("tLeftRailNavBottomBorder").style.display = "block";
                    }
                    
                    // Show the active section's subpages.
                    menuItemCurr.style.display = "block";

                    // Apply the "active" class to the active subpage's link, if a subpage
                    // of the currently active section was selected as opposed to the user
                    // having just selected the section.
                    for (var j = 0; j < myChildItems.length; j++) {
                        var isMyLinkActive = xGetElementsByClassName("active", myChildItems[j], "a");
                        if (isMyLinkActive.length > 0) {
                            myChildItems[j].className = "active";

                            // Add a "blocker" to break the border along the right edge of the left rail.
                            myChildItems[j].innerHTML = '<div class="tLeftRailNavBlocker">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>' + myChildItems[j].innerHTML;
                        }
                    }
                }
            }
        }
    }
}

/* END SITE LEFT NAV CUSTOMIZATION */


/* START BREADCRUMB CUSTOMIZATION */

// Requires that script.js has been included containing...
// function xGetElementsByClassName(clsName, parentEle, tagName)

// Attach a span to the left and right of each item in the breadcrumb trail to contain the rounded edge images.
// Also, alter standard breadcrumb trail so that the farthest right item, being the current page,
// has a unique CSS class name.
var myBreadCrumbs = xGetElementsByClassName("tBreadCrumb", document, "a");
if (myBreadCrumbs.length > 0) {
    /*
    for (var i = 0; i < myBreadCrumbs.length;  i++) {
        var myBreadCrumb = myBreadCrumbs[i];
        myBreadCrumb.outerHTML = '<div class="tBreadCrumbAnchor"><div class="tBreadCrumbLeft">&nbsp;</div></div>' + myBreadCrumb.outerHTML + '<div class="tBreadCrumbAnchor"><div class="tBreadCrumbRight">&nbsp;</div></div>';
    }
    */
    var myBreadCrumb = myBreadCrumbs[(myBreadCrumbs.length - 1)];
    myBreadCrumb.className = "tBreadCrumbCurrentPage";
    myBreadCrumb.href = "javascript:";
}

var myBreadCrumbLeftEdges = xGetElementsByClassName("tBreadCrumbLeft", document, "span");
if (myBreadCrumbLeftEdges.length > 0) {
    var myBreadCrumbLeftEdge = myBreadCrumbLeftEdges[(myBreadCrumbLeftEdges.length - 1)];
    myBreadCrumbLeftEdge.className = "tBreadCrumbLeftActive";
}

/* END BREADCRUMB CUSTOMIZATION */


/* START LOGIN CUSTOMIZATION */

// Check to see if the user login form is being displayed
if (document.getElementById("dnn_ctr_Login_Login_DNN_txtUsername")) {
    // Flag login mode by applying a class name to allow login page to be
    // affected through CSS.
    if (document.getElementById("tHeader")) {
        document.getElementById("tHeader").className = document.getElementById("tHeader").className + ' tLoginModeHeader';
    }
    
    if (document.getElementById("tMainContent")) {
        document.getElementById("tMainContent").className = document.getElementById("tMainContent").className + ' tLoginModeContent';
    }
}

/* END LOGIN CUSTOMIZATION */
