// Filename: globalNav.js
// Date:  7/22/07
// 
// A JavaScript library reponsible for rendering the left global navigation menu
// on third-party vendor hosted content.  This will be content diaplayed in an
// IFRAME on the CHW consumer view portal websites.
// 

// Arrays used to hold the Image objects representing the various states of 
// each primary level menu item.
var navImages = new Array(); //default state
var navImagesRo = new Array(); //rollover state
var navImagesOn = new Array(); //on state
var siteProtocol = "http://";  //default protocol

//this will detect the protocol of the page
if (window.location.protocol.indexOf('https:')==0) {
	siteProtocol = "https://";
}

// Initial function call responsible for rendering the menu.  This function
// will retrieve the menu structure from CHW content servers by using a 
// JSON request (See jsr_class.js for helper functions used to construct a 
// JSON request).
//
// param:  siteId - The unique site identifier for the CHW website.
// param:  siteDomain - The domain name for the CHW website.
// param:  vendorId - The unique vendor identifier for the third-party vendor 
//                   (See globalNav.jsp for assigned vendor IDs.)
function constructCHWMenu(siteId, siteDomain, vendorId) {
    /*************************************************************************
     DEBUG - Sample calls to the displayMenu function where a JSON formatted 
     menu structure is passed as an arguement.  This is used for testing and
     debugging purposes. */
     //displayMenu( { "menu": { "level1": [ { "title": "Medical Services", "image": { "path": "/stellent/fragments/comANavigation/images", "width": "197", "height": "25", "off": "nav_medservices.gif", "ro": "nav_medservices_ro.gif", "on": "nav_medservices_on.gif" }, "link": "http://www.sequoiahospital.org/intradoc-cgi/idc_cgi_isapi.dll?IdcService=SS_GET_PAGE&nodeId=5005685", "selected": false }, { "title": "Find a Physician", "image": { "path": "/stellent/fragments/comANavigation/images", "width": "197", "height": "25", "off": "nav_finddoc.gif", "ro": "nav_finddoc_ro.gif", "on": "nav_finddoc_on.gif" }, "link": "http://www.sequoiahospital.org/intradoc-cgi/idc_cgi_isapi.dll?IdcService=SS_GET_PAGE&nodeId=5005688" }, { "title": "Classes & Events", "image": { "path": "/stellent/fragments/comANavigation/images", "width": "197", "height": "25", "off": "nav_class.gif", "ro": "nav_class_ro.gif", "on": "nav_class_on.gif" }, "link": "http://www.sequoiahospital.org/intradoc-cgi/idc_cgi_isapi.dll?IdcService=SS_GET_PAGE&nodeId=5005696" }, { "title": "Health & Wellness", "image": { "path": "/stellent/fragments/comANavigation/images", "width": "197", "height": "25", "off": "nav_healthref.gif", "ro": "nav_healthref_ro.gif", "on": "nav_healthref_on.gif" }, "link": "http://www.sequoiahospital.org/intradoc-cgi/idc_cgi_isapi.dll?IdcService=SS_GET_PAGE&nodeId=5005689", "selected": true}, { "title": "Careers", "image": { "path": "/stellent/fragments/comANavigation/images", "width": "197", "height": "25", "off": "nav_careers.gif", "ro": "nav_careers_ro.gif", "on": "nav_careers_on.gif" }, "link": "http://www.sequoiahospital.org/intradoc-cgi/idc_cgi_isapi.dll?IdcService=SS_GET_PAGE&nodeId=5005792" }, { "title": "About Us", "image": { "path": "/stellent/fragments/comANavigation/images", "width": "197", "height": "25", "off": "nav_who.gif", "ro": "nav_who_ro.gif", "on": "nav_who_on.gif" }, "link": "http://www.sequoiahospital.org/intradoc-cgi/idc_cgi_isapi.dll?IdcService=SS_GET_PAGE&nodeId=5005697" }, { "title": "Foundation", "image": { "path": "/stellent/fragments/comANavigation/images", "width": "197", "height": "25", "off": "nav_foundation.gif", "ro": "nav_foundation_ro.gif", "on": "nav_foundation_on.gif" }, "link": "http://www.sequoiahospital.org/intradoc-cgi/idc_cgi_isapi.dll?IdcService=SS_GET_PAGE&nodeId=5005690", "selected": false, "delimiter": true } ] } });    
 //   displayMenu( { "menu": { "level1": [ { "title": "Patients & Visitors", "image": { "path": "http://chw-scsdev-001/stellent/groups/jsp/documents/webcontent/", "width": "197", "height": "25", "off": "nav_patients.gif", "ro": "nav_patients_ro.gif", "on": "nav_patients_on.gif" }, "link": "http://chw-scsdev-001/snmh/Patients_And_Visitors" } , { "title": "Find a Doctor", "image": { "path": "http://chw-scsdev-001/stellent/groups/jsp/documents/webcontent/", "width": "197", "height": "25", "off": "nav_finddoc.gif", "ro": "nav_finddoc_ro.gif", "on": "nav_finddoc_on.gif" }, "link": "http://chw-scsdev-001/snmh/Find_a_Doctor" } , { "title": "Medical Services", "image": { "path": "http://chw-scsdev-001/stellent/groups/jsp/documents/webcontent/", "width": "197", "height": "25", "off": "nav_medservices.gif", "ro": "nav_medservices_ro.gif", "on": "nav_medservices_on.gif" }, "link": "http://chw-scsdev-001/snmh/Medical_Services" } , { "title": "Classes & Events", "image": { "path": "http://chw-scsdev-001/stellent/groups/jsp/documents/webcontent/", "width": "197", "height": "25", "off": "nav_class.gif", "ro": "nav_class_ro.gif", "on": "nav_class_on.gif" }, "link": "http://chw-scsdev-001/snmh/Classes_And_Events" } , { "title": "Health Information", "image": { "path": "http://chw-scsdev-001/stellent/groups/jsp/documents/webcontent/", "width": "197", "height": "25", "off": "nav_health.gif", "ro": "nav_health_ro.gif", "on": "nav_health_on.gif" }, "link": "http://chw-scsdev-001/snmh/Health_Information" ,"level2": [ { "title": "Health Information & References", "link": "http://chw-scsdev-001/snmh/Health_Information/Health_Information_And_References/index.htm","selected": true } , { "title": "Personal Health Newsletter", "link": "http://chw-scsdev-001/snmh/Health_Information/Personal_Health_Newsletter/index.htm" }, { "title": "New Information & References", "link": "http://chw-scsdev-001/snmh/Health_Information/Health_Information_And_References/index.htm" } ] ,"selected":true } , { "title": "Careers", "image": { "path": "http://chw-scsdev-001/stellent/groups/jsp/documents/webcontent/", "width": "197", "height": "25", "off": "nav_careers.gif", "ro": "nav_careers_ro.gif", "on": "nav_careers_on.gif" }, "link": "http://chw-scsdev-001/snmh/Careers" } , { "title": "For Physicians & Residents", "image": { "path": "http://chw-scsdev-001/stellent/groups/jsp/documents/webcontent/", "width": "197", "height": "25", "off": "nav_forphyres.gif", "ro": "nav_forphyres_ro.gif", "on": "nav_forphyres_on.gif" }, "link": "http://chw-scsdev-001/snmh/For_Physicians_And_Residents" } , { "title": "Who We Are", "image": { "path": "http://chw-scsdev-001/stellent/groups/jsp/documents/webcontent/", "width": "197", "height": "25", "off": "nav_who.gif", "ro": "nav_who_ro.gif", "on": "nav_who_on.gif" }, "link": "http://chw-scsdev-001/snmh/Who_We_Are" } , { "title": "Foundation", "image": { "path": "http://chw-scsdev-001/stellent/groups/jsp/documents/webcontent/", "width": "197", "height": "25", "off": "nav_foundations.gif", "ro": "nav_foundations_ro.gif", "on": "nav_foundations_on.gif" }, "link": "http://www.google.com", "linkTarget": "Foundation" ,"delimiter": true } ] } });
    /************************************************************************/
    // URL to the JSP responsible for retrieving the menu structure
	//var url = 'http://' + siteDomain + '/stellent/groups/jsp/documents/vendorassets/globalNav.jsp?siteId=' + siteId + '&siteDomain=' + siteDomain + '&vendorId=' + vendorId + '&callback=displayMenu'; // callback is the function in this page to call to process the JSON menu structure
	var url = siteProtocol + siteDomain + '/stellent/groups/jsp/documents/vendorassets/globalNav.jsp?siteId=' + siteId + '&siteDomain=' + siteDomain + '&vendorId=' + vendorId + '&callback=displayMenu'; // Sameer: This is used if classes and events section needs to be over SSL 

   var obj = new JSONscriptRequest(url);
    obj.buildScriptTag(); // Create a new script region in the header of the page
    obj.addScriptTag(); // Add the new script region to the header of the page
}

// The function that will be responsible for processing the JSON formatted
// menu structure and rendering the menu to the page.  This function will be
// executed by the "src" attribute of the new script region added to the page
// from the constructCHWMenu function call.
// 
// param:  data - The structure of the menu formatted in JSON.
//                (The menu is essentially one JavaScript object.)
function displayMenu(data) {
    var menuDivId = 'menu_start';
    var breadcrumbStartDivId = "breadcrumb_start";
    var breadcrumbStartDiv = document.getElementById(breadcrumbStartDivId);
    var siteDomain = data.menu.level0[0].siteDomain;    
    var breadcrumbStart = "<li class=\"home\"><a href=\"http://" + siteDomain + "\" target=\"_top\">Home</a></li>";
    var breadcrumbItemStart = "";
    var breadcrumbItemEnd = "";
    var breadcrumbThirdLevel = "";
    var menu = "";
    var writeMenuStart = false;
    var lvl1Items = data.menu.level1;
    for(i = 0; i < lvl1Items.length; i++) {

        // Iterate through the level 1 menu items (main level items)

        var imgPath = lvl1Items[i].image.path;
        var isLvl1Selected = lvl1Items[i].selected;

        var isLvl2Selected = false;
        var lvl2Menu = '';
        var lvl2MenuSplit = '';
        if(lvl2Items = lvl1Items[i].level2) {
            for(j = 0; j < lvl2Items.length; j++) {

                // Iterate through the level 2 menu items if they exist

                var lvl2Selected = lvl2Items[j].selected;
                var lvl3Selected = false;

                if(lvl2Selected) {
                    // Check to see if a level 3 menu item is selected.  If so, the appropriate style needs to be applied to the
                    // parent level 2 menu item.
                    lvl3Selected = document.getElementById('lvl3Selected') ? true : lvl3Selected;

                    if (!lvl3Selected) {
                      //This is the selected level 2 item, and there is no level 3.  Write out the breadcrumb for this item
                      //without a link
                      breadcrumbItemStart = "<li>" + lvl2Items[j].title + "</li>";           
                    }
                    else {
                      //This is the selected level 2 item, but we're at level 3.  Write out the breadcrumb for this item with a link
                      breadcrumbItemStart = "<li><a href=\"" + lvl2Items[j].link + "\"";
                      if ( lvl2Items[j].linkTarget )
                      {
                        breadcrumbItemStart += " target = \"" + lvl2Items[j].linkTarget + "\"";
                      }
                      breadcrumbItemStart += ">";
                      breadcrumbItemEnd = lvl2Items[j].title + "</a></li>";
                    }
                    breadcrumbThirdLevel += breadcrumbItemStart + breadcrumbItemEnd;
                }                

                // Specify the correct style class for the <p> tag based on the selected state of the item.  Only hyperlink the 
                // secondary menu item if a third level exists and an item on the third level is selected.
                var menuItemStart = '<a href=\"' + lvl2Items[j].link + "\"";
                if ( lvl2Items[j].linkTarget)
                {
                  menuItemStart += " target=\"" + lvl2Items[j].linkTarget + "\"";
                }
                menuItemStart += '>';
                var menuItemEnd = '</a>';
                var pTagAdd = '';
                if (lvl2Selected)
                {
                  pTagAdd = ' id=\"lvl2Selected\"" class=\"open\"';
                  if (!lvl3Selected)
                  {                    
                    menuItemStart = '';
                    menuItemEnd = '';
                  }
                }
                
                
                lvl2Menu += "<div class=\"menu\" style=\"border-bottom-width: 0px; padding-top: 0pt; padding-right: 0pt; padding-bottom: 0pt;\"><p" + pTagAdd + ">" + menuItemStart + lvl2Items[j].title + menuItemEnd + "</p></div>";
                if(lvl2Selected) {
                    // If a selected item is found on the second level save the contents of 'lvl2Menu'.  This is the point where the menu
                    // will be split.  The value of 'lvl2Menu' will ultimately become the first half of the menu which will be placed
                    // in the 'menu_start' span region.

                    lvl2MenuSplit = lvl2Menu;

                    // Reset as any data appended after this point will become part of the last half of the menu which will be placed
                    // in the 'menu_end' span region.
                    lvl2Menu = '';
                    isLvl2Selected = true;                  
                }
            }
        }


        // In some circumstances we may not know if a second level item is selected.  This occurs when the vendor is maintaining
        // the second level menu items.  In this case the vendor will be required to add 'id="lvl2Selected"' to the paragraph
        // tag for the menu item.
        isLvl2Selected = (!isLvl2Selected && document.getElementById('lvl2Selected')) ? true : isLvl2Selected;

        //Logic to write out the breadcrumb, level 1.
        if (isLvl1Selected)
        {
          if (!isLvl2Selected) {
            //This is the selected level 1 item, and there is no level 2.  Write out the breadcrumb for this item without
            //a link
            breadcrumbItemStart = "<li>" + lvl1Items[i].title + "</li>";           
          }
          else {
            //This is the selected level 1 item, but we're at level 2.  Write out the breadcrumb for this item with a link
            breadcrumbItemStart = "<li><a href=\"" + lvl1Items[i].link + "\"";
            if ( lvl1Items[i].linkTarget )
            {
              breadcrumbItemStart += " target = \"" + lvl1Items[i].linkTarget + "\"";
            }
            breadcrumbItemStart += ">";
            breadcrumbItemEnd = lvl1Items[i].title + "</a></li>";
          }          
          breadcrumbStart += breadcrumbItemStart + breadcrumbItemEnd;
        }

        if(lvl1Items[i].delimiter) {
            // Place a delimiter before the menu item if one is specified
		//menu += '<img src="http://' + siteDomain + '/stellent/groups/jsp/documents/webcontent/nav_divider.gif" width="197" height="25" alt="menu divider" /><br/>';
		menu += '<img src="' + siteProtocol + siteDomain + '/stellent/groups/jsp/documents/webcontent/nav_divider.gif" width="197" height="25" alt="menu divider" /><br/>';
        }

        // The primary menu item should not be linked if the user is currently viewing the landing page for that section.  If the user
        // is viewing a page under the primary menu item, then the primary menu item should be linked.
        var menuItemStart = "";
        var menuItemEnd = "";
        var menuImage = lvl1Items[i].image.off;

        if (!isLvl1Selected || (isLvl1Selected && isLvl2Selected))
        {
          menuItemStart = "<a href=\"" + lvl1Items[i].link + "\" onmouseover=\"imgChg('nav" + i + "');\" onmouseout=\"imgChgBack('nav" + i + "', '" + ((isLvl1Selected) ? 'onImgUsed' : '') + "');\"";
          if ( lvl1Items[i].linkTarget)
          {
            menuItemStart += " target=\"" + lvl1Items[i].linkTarget + "\"";
          }
          menuItemStart += ">";
          menuItemEnd = "</a>";
        }
        if (isLvl1Selected)
        {
          menuImage = lvl1Items[i].image.on;          
        }
        menu += menuItemStart + "<img name=\"nav" + i + "\" src=\"" + imgPath + "/" + menuImage + "\" width=\"" + lvl1Items[i].image.width + "\" height=\"" + lvl1Items[i].image.height + "\"" + 
            " alt=\"" + lvl1Items[i].title + "\" />" + menuItemEnd + "<br/>";
        if(lvl2MenuSplit.length > 0) {
            // A split has occured on the second level.  The contents of the second level menu items must be wrapped
            // in a div.  If additional second level menu items continue after the split we must wrap those items in
            // a div as well to prepare for writting them to the 'menu_end' span region later on.  Typically a second level
            // will be wrapped in a single div, however, because the second level is being split into two different span
            // regions we must wrap the items into two separate divs.  A div cannot be started in one span region and ended
            // in a different span region as this is against HTML standards.

            writeMenuStart = menu + "<div class=\"menu\" style=\"border-bottom-width: 0px; padding-top: 0pt; padding-right: 0pt; padding-bottom: 0pt;\">" + lvl2MenuSplit + "</div>";            
            menu = (lvl2Menu.length > 0) ? ("<div class=\"menu\" style=\"padding-top: 0pt; padding-right: 0pt; padding-bottom: 6px;\">" + lvl2Menu + "</div>") : lvl2Menu;            
            menuDivId = 'menu_end';
        }
        else if(isLvl1Selected) {
            // A split has occured on the primary level.

            writeMenuStart = menu + lvl2Menu;            
            menu = "";
            menuDivId = 'menu_end';
        }

        // Construct the image objects for the various states
        // (i.e. rollover, on, and off states)
        navImages['nav' + i] = new Image();
        navImages['nav' + i].src = imgPath + "/" + lvl1Items[i].image.off;
        navImagesRo['nav' + i] = new Image();
        navImagesRo['nav' + i].src = imgPath + "/" + lvl1Items[i].image.ro;
        navImagesOn['nav' + i] = new Image();
        navImagesOn['nav' + i].src = imgPath + "/" + lvl1Items[i].image.on;        
    }
// Write the breadcrumb to the appropriate div regions.
    if (breadcrumbStartDiv)
    {
      var current = breadcrumbStartDiv.innerHTML;
      breadcrumbStartDiv.innerHTML = breadcrumbStart + breadcrumbThirdLevel + current;
    }

    if (writeMenuStart)
    {
      var menuDiv = document.getElementById('menu_start');
      menuDiv.innerHTML = writeMenuStart;
    }

    // Write the menu to the appropriate span region.  If the menu is being split the last half of the menu will be
    // written to the 'menu_end' span region.  If the menu is not being split the menu will be written to the
    // 'menu_start' span region.
    var menuDiv = document.getElementById(menuDivId);
    menuDiv.innerHTML = menu; 

    
    
}

// The function used to change the state of the image for a primary level
// menu item on a onmouseover event (i.e. Change image on rollover).
// 
// param:  imageID - The path to the new image.
// 
function imgChg(imageID) { 
    document.images[imageID].src = navImagesRo[imageID].src;
}

// The function used to change the state of the image for a primary level menu
// upon a onmouseout event.
// 
// param:  imageID - The path to the new image.
// param:  onImgUsed - If the value equals 'onImgUsed' the user is viewing the
//                     landing page for the primary menu level item, and
//                     therefore the image state to revert back to will be
//                     different than the default state.
function imgChgBack(imageID, onImgUsed) {   
    if (onImgUsed=='onImgUsed') {
        document.images[imageID].src = navImagesOn[imageID].src;
    } 
    else {
        document.images[imageID].src = navImages[imageID].src;
    }  
}
