//global vars begin
var NAV_STATE_SET = 0;
var CURRENT_FLOAT = null;
var CURRENT_ROLLOVER = "";
var CURRENT_SECTION_ROLLOVER = null;
var CURRENT_EXPAND_NAV  = null;
var BROWSER = getBrowserType();
var FLOAT_TIMEOUT;
//global vars end



//prototypes begin
nodeObject.prototype.add = function(sectionObject) 
{
    this.nodeCollection[this.nodeCounter] = sectionObject;
    this.nodeCounter ++;
}
//prototypes end



/*
------------------------------------------
Function name  -  getBrowserType
Description - queries the user's BROWSER and sets properties in an object 
              for a specific type of BROWSER look at the objects properties
------------------------------------------  
*/
function getBrowserType()
{
    var myAgent = navigator.userAgent;
    var isMac = myAgent.toString().toUpperCase().indexOf("MAC") != -1;
    var isNS = myAgent.toString().toUpperCase().indexOf("MOZILLA") != -1;
    var isNS6 = myAgent.toString().toUpperCase().indexOf("NETSCAPE6/6") != -1;
    var isNS7 = myAgent.toString().toUpperCase().indexOf("NETSCAPE/7") != -1;
    var isMoz = ((parseInt(navigator.appVersion) >= 5) && myAgent.toString().toUpperCase().indexOf("NETSCAPE") == -1);
    
    this.ie = document.all ? 1 : 0;
    this.ns4 = document.layers ? 1 : 0;
    this.dom = document.getElementById ? 1 : 0;
    this.iemac = ((ie) && (isMac)) ? 1 : 0;
    this.ns6 = ((!ie) && (isNS6)) ? 1 : 0;
    this.ns7 = ((!ie) && (isNS7)) ? 1 : 0;
    this.moz = ((!ie) && (isNS) && (isMoz)) ? 1 : 0;
    this.ns = ((!ie) && (isNS) && (!isNS6) && (!isNS7) && (!isMoz) ) ? 1 : 0;
    return this;
}

/*------------------------------------------
Function name  -  getNavRef
Description - looks up a div or layer in the document and returns a reference to it
------------------------------------------*/
function getNavRef(nav)
{
    var navRef;
    if (BROWSER.dom) return document.getElementById(nav);
    if (BROWSER.ns4) {
        navRef = eval(document.layers[nav]);
        return navRef;
    }

}   

/*
------------------------------------------
Function name  - sectionObject
Description - used to create section objects and set its properties 
              this allows a section to live in a directory that is named something different from the section name
------------------------------------------  
*/
function sectionObject(sectionName, sectionDiv)
{
    this.sectionName = sectionName;
    this.sectionDiv = sectionDiv;
    return this;
}

/*
------------------------------------------
Function name  - nodeObject
Description - used to create a collection of nodes
------------------------------------------  
*/
function nodeObject()
{
    this.nodeCollection = new Array();
    this.nodeCounter = 0;
    return this;
}

/*------------------------------------------
Function name  -  currentFloat
Description - used to store properties of the current floater
------------------------------------------*/
function currentFloat(navName) { this.navName = navName; }

/*------------------------------------------
Function name  -  currentExpandNav
Description - used to store properties of the current floater
------------------------------------------*/
function currentExpandNav(sourceNav,navName)
{
    this.sourceNav = sourceNav;
    this.navName = navName;
}

/*------------------------------------------
Function name  -  currentRollOver
Description - used to store properties of the current rollover
------------------------------------------*/
function currentRollOver(navName) { this.navName = navName; }


/*------------------------------------------
Function name  -  currentSectionRollOver
Description - used to store properties of the current rollover
------------------------------------------*/
function currentSectionRollOver(navName) {  this.navName = navName; }

/*------------------------------------------
Function name  -  rollOverObject
Description - used to store the properties of a rollover object
              the properties include the owner of this object, the on state and the off state
------------------------------------------  */
function rollOverObject(onClass,offClass)
{
    this.onClass = onClass;
    this.offClass = offClass;
    return this;
}

/*------------------------------------------
Function name  -  navRollOver
Description - used to handle the on and off state of nav items
              there is no on or off state switch the swtiching is done by querying the rollOver object
------------------------------------------*/
function navRollOver(nav)
{
    if (! BROWSER.dom) return;
    floatShow();
    var navRef = null;
    navRef = getNavRef(nav);
    if (! navRef) return;
    if (CURRENT_EXPAND_NAV) rollOverClear(CURRENT_EXPAND_NAV.sourceNav);
    
    if((CURRENT_SECTION_ROLLOVER) && (CURRENT_SECTION_ROLLOVER.navName == nav)) return;

    var rollObject = new Array();
    rollObject[0] =  new rollOverObject("navItem1On","navItem1Off");
    rollObject[1] =  new rollOverObject("navItem2On","navItem2Off");

    var objChildNodes = navRef.childNodes;
    for (c=0;c<rollObject.length;c++)
    {
        var thisObj = rollObject[c];
        for (thisKey in thisObj)
        {
            if ((BROWSER.dom) && (thisObj["offClass"] == navRef.className)) 
            {
                navRef.className = thisObj["onClass"];
                if ((navRef.parentNode.parentNode) && (navRef.parentNode.parentNode.nodeName.toUpperCase() == "TR")) navRef.parentNode.parentNode.className = thisObj["onClass"];
                for (c=0; c<objChildNodes.length; c++)
                {
                    var tn = objChildNodes[c];
                    if (tn.nodeType == "3") continue;
                    tn.className = thisObj["onClass"];
                }
                
                return;
            }
            if ((BROWSER.dom) && (thisObj["onClass"] == navRef.className)) 
            {
                navRef.className = thisObj["offClass"];
                if ((navRef.parentNode.parentNode) && (navRef.parentNode.parentNode.nodeName.toUpperCase() == "TR")) navRef.parentNode.parentNode.className = thisObj["offClass"];
                for (c=0; c<objChildNodes.length; c++)
                {
                    var tn = objChildNodes[c];
                    if (tn.nodeType == "3") continue;
                    tn.className = thisObj["offClass"];
                }
                return;
            }
        }
    }
}
    
/*------------------------------------------
Function name  -  rollOverClear
Description - used to clear a specific rollover
------------------------------------------*/
function rollOverClear()
{
    if (! BROWSER.dom) return;
    floatShow();
    if (! CURRENT_ROLLOVER.navName) return;
    if ((CURRENT_EXPAND_NAV) && (CURRENT_ROLLOVER.navName ==  CURRENT_EXPAND_NAV.navName)) return;
    var navRef = null;
    navRef = getNavRef(CURRENT_ROLLOVER.navName);
    if (! navRef) return;
    var rollObject = new Array();
    rollObject[0] =  new rollOverObject("navItem1On","navItem1Off");
    rollObject[1] =  new rollOverObject("navItem2On","navItem2Off");
    var objChildNodes = navRef.childNodes;
    for (c=0;c<rollObject.length;c++)
    {
        var thisObj = rollObject[c];
        for (thisKey in thisObj)
        {
            if ((BROWSER.dom) && (thisObj["onClass"] == navRef.className)) 
            {
                navRef.className = thisObj["offClass"];
                if ((navRef.parentNode.parentNode) && (navRef.parentNode.parentNode.nodeName.toUpperCase() == "TR")) navRef.parentNode.parentNode.className = thisObj["offClass"];
                for (c=0; c<objChildNodes.length; c++)
                {
                    var tn = objChildNodes[c];
                    if (tn.nodeType == "3") continue;                   
                    tn.className = thisObj["offClass"];
                }
                return;

            }
        }
    }
}

/*------------------------------------------
Function name  - hideCurrentFloat
Description - used to hide the current nav
------------------------------------------  */
function hideCurrentFloat() 
{ 
    if (! BROWSER.dom) return;
    var navRef = null;
    var sourceNavRef = null;
    if ((CURRENT_EXPAND_NAV) && (CURRENT_FLOAT) && (CURRENT_FLOAT.navName ==  CURRENT_EXPAND_NAV.navName)) return;  
    if (CURRENT_FLOAT) 
    {
        navRef = getNavRef(CURRENT_FLOAT.navName);
        if (! navRef) return;       
        BROWSER.dom ? navRef.style.visibility = "hidden" : navRef.visibility = "hide";
        //floatClear(CURRENT_FLOAT.navName);
    }
}

/*------------------------------------------
Function name  - floatHide
Description - used to hide a float
------------------------------------------  */
function floatHide(waitTime) 
{ 
    if (! BROWSER.dom) return;
    var wt;
    if (arguments.length > 1) return;
    if (! waitTime) {
        wt = 400; 
    } else {
        wt = waitTime;
    }
    if (window.setTimeout) {
        //hideCurrentFloat();
        if (FLOAT_TIMEOUT)  window.clearTimeout(FLOAT_TIMEOUT);
        FLOAT_TIMEOUT = window.setTimeout('rollOverClear();hideCurrentFloat()',wt);
    } else {
        hideCurrentFloat()
    }
}

/*------------------------------------------
Function name  - floatShow
Description - used to keep a float afloat
------------------------------------------  */
function floatShow() 
{ 
    if (! BROWSER.dom) return;
    if (arguments.length > 0) return;   
    if (FLOAT_TIMEOUT) window.clearTimeout(FLOAT_TIMEOUT);
}   

/*------------------------------------------
Function name  - floatClear
Description - used to clear the bgcolor of the float
------------------------------------------  */
function floatClear(destNav)
{
    if (! BROWSER.dom) return;
    if (arguments.length > 1) return;
    var destNavRef = null;
    if (destNav != "") destNavRef = getNavRef(destNav);
    if (! destNavRef) return;
    destNavRef.parentNode.style.backgroundColor = "#FFFFFF";    
    return;
}

/*------------------------------------------
Function name  - navFloat
Description - used to float a div
------------------------------------------  */
function navFloat(sourceNav,destNav) 
{ 
    if (! BROWSER.dom) return;
    if (arguments.length == 0) return;
    var sourceNavRef = null; 
    var destNavRef = null;
    if (sourceNav != "") sourceNavRef = getNavRef(sourceNav);
    if (destNav != "") destNavRef = getNavRef(destNav);
    if (! sourceNavRef) return;
    if (arguments.length == 1)
    {
        if (CURRENT_FLOAT) hideCurrentFloat();
        //if (CURRENT_FLOAT) floatHide(3000);
        rollOverClear();
        navRollOver(sourceNav);
        return;
    } else {
        if(((CURRENT_SECTION_ROLLOVER) && (CURRENT_SECTION_ROLLOVER.navName.toString() != sourceNav)) || (!CURRENT_SECTION_ROLLOVER)) 
        {
        rollOverClear();
        navRollOver(sourceNav);
        CURRENT_ROLLOVER = new currentRollOver(sourceNav);
        }
    }
    if (CURRENT_ROLLOVER.navName) rollOverClear();
    navRollOver(sourceNav);
    if (CURRENT_FLOAT) hideCurrentFloat();
    //if (CURRENT_FLOAT) floatHide(3000);
   

	// Test for the existance of the Top Nav
	// Assumes that dhtml_menus.js is used for the top nav.
	var flyoutAdjust = 22;
	if ((typeof eMenu == 'undefined') || (eMenu == 'undefined')) {
		flyoutAdjust = -3;
	}

    if(destNav != "") CURRENT_FLOAT = new currentFloat(destNav);
    if ((sourceNavRef) && (destNavRef))
    {
        if ((CURRENT_EXPAND_NAV) && (CURRENT_EXPAND_NAV.navName == destNav)) return; 
        destNavRef.style.left = "153px";
        if ((BROWSER.ie || BROWSER.moz) && (!BROWSER.iemac))destNavRef.style.top =  destNavRef.style.top = (sourceNavRef.offsetParent.offsetTop + sourceNavRef.offsetParent.offsetHeight + 28 + flyoutAdjust);
        if (BROWSER.ns7) destNavRef.style.top = (sourceNavRef.offsetParent.offsetTop + sourceNavRef.offsetParent.offsetHeight + 29 + flyoutAdjust);        
        if (BROWSER.ns6) destNavRef.style.top = ((sourceNavRef.parentNode.parentNode.offsetTop) - 2 + flyoutAdjust);
        if (BROWSER.ns) destNavRef.style.top = ((sourceNavRef.parentNode.parentNode.offsetTop) - 22 + flyoutAdjust);
        if (BROWSER.iemac) return;
        destNavRef.style.visibility = "visible";
        destNavRef.parentNode.className = "navLevel2Float";
        destNavRef.className = "navLevel2Float";
        
        /*
        if (CURRENT_FLOAT) 
        {
            destNavRef.parentNode.style.backgroundColor = "#DFEEEE";
        } else  {
            destNavRef.parentNode.style.backgroundColor = "#FFFFFF";
        }
        */
    }
}

/*------------------------------------------
Function name  - navExpand
Description - used to expand a nav item
------------------------------------------  */
function navExpand(sourceNav,destNav,destUrl) 
{ 
    if ((arguments.length > 2) && (destUrl != "")) 
    {
        document.location = destUrl;
        return;
    }

    if (! BROWSER.dom) return;
    var navRef = null;
    navRef = getNavRef(destNav);
    if (! navRef) return;
    
    

    //navRef = document.getElementById(destNav);
    if (CURRENT_EXPAND_NAV) navCollapse(destNav);
    if((sourceNav != "") && (destNav != "")) CURRENT_EXPAND_NAV = new currentExpandNav(sourceNav,destNav);
    if (BROWSER.dom)
    {
        navRef.parentNode.className = "navLevel2Expand";        
        navRef.className = "navLevel2Expand";       
        navRef.style.visibility = "visible";
        navRef.style.position = "static";
        //printObject(navRef.style);
    }   
}

/*------------------------------------------
Function name  - navCollapse
Description - used to collapse a nav item
------------------------------------------  */
function navCollapse(destNav) 
{ 
    if (! BROWSER.dom) return;
    var navRef = null;  
    if ((CURRENT_EXPAND_NAV) && (CURRENT_EXPAND_NAV.navName !=  ""))
    {
        //navRef = document.getElementById(CURRENT_EXPAND_NAV.navName);
        navRef = getNavRef(CURRENT_EXPAND_NAV.navName);
        //if (BROWSER.dom) navRef.className = "navLevel2Collapse";
        if (! navRef) return;       
        BROWSER.dom ? navRef.style.visibility = "hidden" : navRef.visibility = "hide";
        BROWSER.dom ? navRef.style.position = "absolute" : navRef.position = "absolute";
    }
}

/*
------------------------------------------
Function name  - navHighlight
Description - used to highlight a specific nav
------------------------------------------  
*/
function navHighlight(sourceNav)
{
    if (!BROWSER.dom) return;
    if ((! sourceNav) || (sourceNav == "")) return;
    
    var sourceNavRef = null;
    var destNavRef = null;
    var clickedNav = null;
    var destNav;
    
    sourceNavRef= getNavRef(sourceNav);
    if (! sourceNavRef) return; 
    
    //alert("sourceNavRef " + sourceNavRef + "\n" + "sourceNav" + "\n" + sourceNav);
        
    var sn = new String(sourceNav);
    var tm = sn.match(/_/gi);
    if ((tm) && (tm.length == 1))
    {
        destNav = sourceNav + "_0";
    } else if ((tm) && (tm.length > 1)) {
        clickedNav = sourceNav;
        sourceNav = sn.match(/([a-zA-Z]+_\d+)/gi);
        destNav = sn.match(/([a-zA-Z]+_\d+_\d+)/gi);
    }
    

    destNavRef = getNavRef(destNav);
    if (destNavRef)
    {
        CURRENT_EXPAND_NAV = new currentExpandNav(sourceNav,destNav);
        navExpand(sourceNav,destNav);
        if (clickedNav) {
            navRollOver(clickedNav);
            CURRENT_SECTION_ROLLOVER = new currentSectionRollOver(clickedNav);                      
        } else {
            navRollOver(sourceNav);     
            CURRENT_SECTION_ROLLOVER = new currentSectionRollOver(sourceNav);                               
        }
    } else {
        navRollOver(sourceNav);
        CURRENT_SECTION_ROLLOVER = new currentSectionRollOver(sourceNav);                           
    }   
}

/*
------------------------------------------
Function name  - setNavState
Description - used to set the initial nav of the application, this is implemented as a singleton
              the global variable NAV_STATE_SET is used to identify if the function has been called
              This can only be called once per page
------------------------------------------  
*/
function setNavState()
{
    if ((typeof pageMap == 'undefined') || (pageMap == 'undefined')) return;
    if (! BROWSER.dom) return;
    if (NAV_STATE_SET) return;
    NAV_STATE_SET = 1;
    var sn = getSelectedNode();
    if ((sn) && (sn != ""))
    {
        //alert("sn = "+sn);
        navHighlight(sn);
    } else {
        var sectionName = new String(location.href);
        //var nodeRef = new nodeObject();
        //nodeRef.add(new sectionObject("newsletter","home_0"));
        //nodeRef.add(new sectionObject("data_collection","home_1"));
        //nodeRef.add(new sectionObject("study_docs","home_2"));
        //nodeRef.add(new sectionObject("study_management","home_3"));
        //nodeRef.add(new sectionObject("sites","home_4"));
        //nodeRef.add(new sectionObject("resources","home_5"));
        for (c=0;c<nodeRef.nodeCollection.length;c++)
        {
            //alert("sectionName: " + sectionName + " compared to: "+nodeRef.nodeCollection[c].sectionName);
            if (sectionName.indexOf(nodeRef.nodeCollection[c].sectionName) != -1) 
            {
                    //alert("sectionName: "+ sectionName + " ||| " +nodeRef.nodeCollection[c]);
                    //navHighlight(nodeRef.nodeCollection[c].sectionDiv);
                    break;
                    return;
            }
        }
    }   
    
}
/*------------------------------------------
Function name  - getCurrentDir
Description - used to return the current directory
------------------------------------------*/
function getCurrentDir()
{
    if (! BROWSER.dom) return;
    var serverURL = new String(document.location);
    //var serverURL = new String("http://merck-dev/dir1/dir2/dir3/alpha_index.html");
    var pathMatches = serverURL.match(/\/[a-zA-Z0-9_]+/gi);
    var currentDir = "";
    if (pathMatches.length >  3) 
    {
        var offset = (pathMatches.length -2);
        currentDir = new String(pathMatches[offset]);
        currentDir = currentDir.substr(1,currentDir.length);
    } else {
        currentDir = "home";
    }
    //alert("serverURL " + serverURL + "\n currentDir " + currentDir  + "\n pathMatches " + pathMatches + "\n pathMatches.length  " + pathMatches.length );
    return currentDir;
}

/*
------------------------------------------
Function name  - getSelectedNode
Description - used to return the currently selected node from the URL
------------------------------------------  
*/
function getSelectedNode()
{
    if (! BROWSER.dom) return;
    
    var selectedNode = "";
    pageUrl = location.href;
    pageCount = pageMap.length;
    //alert ("page count2 = "+pageCount+"\nPage url = "+pageUrl);
    
    //alert("1: selectedNode = "+selectedNode);
    for (c=0; c < pageCount; c++){
        if (pageUrl.indexOf(pageMap[c]) != -1) 
        {
            //alert("found = "+c+"\nPage url = "+pageUrl+"\nurl = "+pageMap[c]+"\nSN = "+divMap[c]);
            selectedNode = divMap[c];
            navHighlight(divMap[c]);
            break;
        }
    }
    
    return selectedNode;
}


