function initBusinessArea (businessAreaRelativeLocation){
    var bodyId=null;
    //prompt("businessAreaRelativeLocation", businessAreaRelativeLocation);
    jQuery("#nhd_gnav_one a[href$='" + businessAreaRelativeLocation + "']").each(function(anchorIndex, anchorDOMElement){
        bodyId=this.className;
    });
    if (bodyId){
        document.body.id=bodyId;
    }
}

function initNavigation(){

    var relativeLocation_to_use=null;
    try {
        // check the relativeLocation
        if (relativeLocation){
            // if we have a variable and it is defined and not null,
            // then that's what we're using
            relativeLocation_to_use=relativeLocation;
        }
    } catch(e){
        // In preview, the variable relativeLocation is not even declared, let alone defined or null
        // so we must use other variables to build one up
        try {
            relativeLocation_to_use="/" + iw_ls_site_name + "/" + iw_ls_page_name + ".shtml";
        } catch(ex){
            return(null);
        }
    }
    // hide the  navigation block first
    //jQuery("#nhd_lnav > ul").css("display", "none");

    // we need to go through each link anchor tag and
    // find the one that has an href attribute that matches the current request
    jQuery("#nhd_lnav a[href$='" + relativeLocation_to_use + "']").each(function(anchorIndex, anchorDOMElement){
        // for the links we find, we update their style to be what they need
        // for this, we get the current class, which gives an an indication of the level they're at
        // and we choose the class we need based on that
        var liNode=null;
        if (this.className.match("nhd_2nd")!=null){
            // i.e. CSR
            rebuildBreadcrumbs(this,2);
            liNode = this.parentNode.parentNode;
            regExp = new RegExp(/nhd_lnav2/);
            liNode.className = liNode.className.replace(regExp, "nhd_lnav2 current");
            // onlyb do this if we need to
            if (discardTopLevelSiblings){
                collapseNavigationSiblings(liNode, 2);
            }
            collapseNavigationChildren(liNode, 2);
        }

        if (this.className.match("nhd_3nd")!=null){
            // i.e. CSR > Environment
            rebuildBreadcrumbs(this,3);
            liNode = this.parentNode.parentNode;
            var regExp = new RegExp(/nhd_lnav3/);
            // i.e. CSR > Environment
            liNode.className = liNode.className.replace(regExp, "nhd_lnav3 current");
            jQuery(liNode).children("span").children("a").get(0).className="nhd_3nd_include4";
            collapseNavigationSiblings(liNode, 3);
        }

        if (this.className.match("nhd_4nd")!=null){
            // i.e. CSR > Environment > Activity
            rebuildBreadcrumbs(this,4);
            liNode = this.parentNode.parentNode;        // the li containing the span containing the anchor
            liNode.className="current";                 // we make this node a current one
            var anchor = jQuery(liNode.parentNode.parentNode).children("span").children("a").get(0);
            anchor.className="nhd_3nd_include4";
            collapseNavigationSiblings(liNode, 4);
        }

        if (this.className.match("nhd_5nd")!=null){
            // i.e. CSR > Environment > Activity > Homes
            rebuildBreadcrumbs(this,5);
            liNode = this.parentNode.parentNode;        // the li containing the span containing the anchor
            liNode.className="current";                 // we make this node a current one
            var anchor = jQuery(liNode.parentNode.parentNode).children("span").children("a").get(0);
            anchor.className="nhd_4nd_include5";
            anchor = jQuery(liNode.parentNode.parentNode.parentNode.parentNode).children("span").children("a").get(0);
            anchor.className="nhd_3nd_include4";
            collapseNavigationSiblings(liNode, 5);
        }
    });
    jQuery("#nhd_lnav > ul").css("display", "block");
}

function rebuildBreadcrumbs(anchor, level){
    clearBreadcrumbs();
    jQuery("#nhd_breadcrumb ul").append(buildBreadcrumbs(anchor, level, level));
}

function clearBreadcrumbs(){
    jQuery("#nhd_breadcrumb ul li:first").nextAll("li").remove();
}

function buildBreadcrumbs(anchor, level, displayLevel){
        

    var currentCrumb="";
    try {
        if (includeTopSectionPageBreadCrumb==false && displayLevel==3 && level==2){
            currentCrumb=""; // we leave the current crumb empty
        } else {
            if (displayLevel==level){
                currentCrumb = "<li><img class=\"dlm-arrow1\" height=\"9\" alt=\" \> \" src=\"/resource/image/common/dlm_arrow1.gif\" width=\"5\" /><strong>" + (anchor.innerText || anchor.textContent) + "</strong>\n</li>";
            } else {
                currentCrumb = "<li><img class=\"dlm-arrow1\" height=\"9\" alt=\" \> \" src=\"/resource/image/common/dlm_arrow1.gif\" width=\"5\" /><a href=\"" + anchor.getAttribute("href") + "\">" + (anchor.innerText || anchor.textContent) + "</a>\n</li>";
            }
        }
    } catch(exception){
    // do nothing - silent catch
    }
    //prompt("built crumb for level: ",currentCrumb);

    if (level>2){
        var newAnchor;
        if (level==5){
            newAnchor = jQuery(anchor.parentNode.parentNode.parentNode.parentNode).children("span").children("a").get(0);
        }
        if (level==4){
            newAnchor = jQuery(anchor.parentNode.parentNode.parentNode.parentNode).children("span").children("a").get(0);
        }
        if (level==3){
            newAnchor = jQuery(anchor.parentNode.parentNode).prevAll("li.nhd_lnav2:first").children("span").children("a").get(0);
        }
        return(buildBreadcrumbs(newAnchor, level-1 , displayLevel ) + currentCrumb);
    } else {
        return(currentCrumb);
    }
}

function collapseNavigationChildren(liNode, level){
    //return(null);
    jQuery(liNode).siblings("li.nhd_lnav3").children("ul.nhd_lnav4").css("display","none");
}


function collapseNavigationSiblings(liNode, level){
    //return(null);
    // we also want to "collapse" the siblings

    // debug // jQuery(liNode).css("border","1px dotted blue");
    // debug // alert("collapseNavigationSiblings level " + level);
    var jQueryLiNode=jQuery(liNode);
    if (level==5){
        // debug // jQuery(liNode).css("border","1px solid gold");
        jQueryLiNode.parent().parent().siblings("li.nhd_lnav4").children("ul").css("display","none");
        jQueryLiNode.parent().parent().each(function(index, domElement){
            collapseNavigationSiblings(this, 4);
        });
    } else if (level==4){
        // debug // jQuery(liNode).css("border","1px solid green");
        jQueryLiNode.siblings("li.nhd_lnav4").children("ul").css("display","none");
        jQueryLiNode.parent().parent().siblings("li.nhd_lnav3").children("ul").css("display","none");
        jQueryLiNode.parent().parent().prevAll("li.nhd_lnav2:first").each(function(index, domElement){
            collapseNavigationSiblings(this, 2);
        });
    } else if (level==3){
        // debug // jQuery(liNode).css("border","1px solid pink");
        jQueryLiNode.children("ul").children("li").children("ul").css("display","none");
        jQueryLiNode.siblings("li.nhd_lnav3").children("ul").css("display","none");
        jQueryLiNode.prevAll("li.nhd_lnav2:first").each(function(index, domElement){
            collapseNavigationSiblings(this, 2);
        });
    } else if (level==2){
        // debug // jQuery(liNode).css("border","1px solid orange");
        jQueryLiNode.prevAll("li").css("display","none");
        jQueryLiNode.nextAll("li.nhd_lnav2").css("display","none");
        jQueryLiNode.nextAll("li.nhd_lnav2").nextAll("li.nhd_lnav3").css("display","none");
        //jQuery(liNode).children("span").children("a").css("border","1px solid green");
        //jQuery(liNode).html("laurent was here");
        // we're collapsing the siblings of a top business area (/europe/csr/), so we can also set the business area for the business area component
        //alert(jQuery(liNode).children("span").children("a").get(0).getAttribute("href"));
        initBusinessArea(jQueryLiNode.children("span").children("a").attr("href"));
    }
}

jQuery(document).ready(initNavigation);
