// depends on rollover.js

// Defines a primary navigation menu item. Used in header.js
function MenuDescription (imgName, imgSrc, imgHighlightSrc, naviVisibility, layerName) {
        // the name of the image in the primary navigation
        this.imgName = imgName;
        // the source of the image in the primary navigation
        this.imgSrc = imgSrc;
        // the source of the image in the primary navigation if it is highlighted
        this.imgHighlightSrc = imgHighlightSrc;
        // use this to decide whether to show the primary navigation
        // or not (see 'home'); @revisit: rename to isHome?
        this.naviVisibility = naviVisibility;
        // the name of the primary navigation menu layer
        this.layerName = layerName;

        // preload images
        this.image = new Image();
        this.image.src = imgSrc;

        this.imageHi = new Image();
        this.imageHi.src = imgHighlightSrc;
}



// ////////////////////////////////////////////
// Simple container to hold all context dependant properties
function ContextElements(visualURL, bgPixel) {
         this.visualURL = convertToAbsoluteURL(visualURL);
         this.bgPixel = convertToAbsoluteURL(bgPixel);
         this.directory = window.location.pathname;
}

function convertToAbsoluteURL(url) {
         if ((url.indexOf('/') == 0) || (url.indexOf('://') > -1) ) {
                 return url; // already absolute
         }
         currURL = window.location.pathname;
         thisDirIndex = currURL.lastIndexOf('/');
         currDir = currURL.slice(0, thisDirIndex);

         currURLArray = currDir.split('/');
         newURLArray = url.split('/');

         n = newURLArray.length - 1;
         c = currURLArray.length - 2;

         while ((n > -1) && (newURLArray[n] != '..')) {
                 n--;
         }

         firstSubdir = n + 1;

         if (n < 0) {
             // subdir of current dir
             return currDir + '/' + url;

         }

         while ((n > 0) && (c > -1)) {
                 n--;
                 c--;
         }

         result = '';
         for (var k = 0; k <= c; k++) {
              if (currURLArray[k] != '') {
                            result = result + '/' + currURLArray[k];
              }

         }

         for (var d = firstSubdir; d < newURLArray.length; d++) {
              result = result + '/' + newURLArray[d];
         }
         return (result == '') ? '/' : result;


}

// /////////////////////////////////////////////////
// Set the visual and the bgPixel of the header. Highlight the selected
// menu item.
function showContextElementsOnParameterizedHeaderCall(contextElements) {

         // contextElements might be null if passed via top.contextElements
         if (!contextElements) return;


         // //////////////////////////////////
         // set the bgPixel of the header page
         if (client.isNS4) {
                            if (document.images['bgPixel']) {
                                document.images['bgPixel'].src = contextElements.bgPixel;
                            } // if
         } else {
                 if (document.images['bgPixel']) {
                     document.images['bgPixel'].src = contextElements.bgPixel;
                 }
         }

         // Find out which navigation is to be highlighted (by mapping
         // the directory attribute of contextElements to the menuDescriptions keys.
         // Also use this loop to hide all layers of the primary navigation
         var naviSelection = null;
         for (var key in menuDescriptions) {
              if (contextElements.directory.search(key) != -1) {
                  naviSelection = key;
              }
              hideLayers(menuDescriptions[key].layerName);
         } // for

        // get the menu description of the selected menu
         var menuDesc = null;
         if (naviSelection != null) {
             menuDesc = menuDescriptions[naviSelection];
         }

               // @revisit: rename naviVisibility to isHome?
               if ((menuDesc == null) || menuDesc.naviVisibility) {
                   // hide the home banner layer if hp is not loaded in main
                   // and show primary navigation
                   hideLayers('bannerhome');
               } else {
                   // show home banner if home page is in frame 'main'
                   changeImgSrc(contextElements.visualURL, 'bannerhome', 'bannerimg');
                   showLayers('bannerhome');
                   return;
               }

         // load the banner from the source given in contextElements
         var imgBanner = new Image();
         imgBanner.src = contextElements.visualURL;
         if (client.isNS4) {
                 if (document.images['banner']) {
                      document.images['banner'].src = imgBanner.src;
                  }
         } else {
            if (document.images['banner']) {
                document.images['banner'].src = imgBanner.src;
            }
         }


         // //////////////////////////////////////////
         // set the primary navigation highlighted menu item


         // for all browsers loop through the menuDescriptions array,
         // set image source to highlighted if the selected menu item
         // has been found. Otherwise set the none-highlighted source.
             for (var key in menuDescriptions) {
                        if ((menuDesc != null)
                             && (menuDesc.imgName == menuDescriptions[key].imgName)) {
                            if (document.images[menuDescriptions[key].imgName]) {
                                document.images[menuDescriptions[key].imgName].src = menuDescriptions[key].imageHi.src;
                            }
                        } else {
                            if (document.images[menuDescriptions[key].imgName]) {
                                document.images[menuDescriptions[key].imgName].src = menuDescriptions[key].image.src;
                            } // if
                        } // else
             } // for
        // }
}


// /////////////////////////////////////////////////
// Set the visual and the bgPixel of the header. Highlight the selected
// menu item.
function showContextElements(contextElements) {

         // contextElements might be null if passed via top.contextElements
         if (!contextElements) return;

         top.contextElements = contextElements;

         // we cannot do anything because the header file hasn't been
         // loaded yet. Thus return.
         if (!top.frames['header']
              || !top.frames['header'].menuDescriptions) return;


         // //////////////////////////////////
         // set the bgPixel of the header page
         // @revisit
         if (client.isNS4) {
                  with (top.frames['header']) {
                            if (document.images['bgPixel']) {
                                document.images['bgPixel'].src = contextElements.bgPixel;
                            }

                  } // with
         } else {
                 if (top.frames['header'].document.images['bgPixel']) {
                     top.frames['header'].document.images['bgPixel'].src = contextElements.bgPixel;
                 }
         }

         // Find out which navigation is to be highlighted (by mapping
         // the directory attribute of contextElements to the menuDescriptions keys.
         // Also use this loop to hide all layers of the primary navigation
         var naviSelection = null;
         for (var key in top.frames['header'].menuDescriptions) {
              if (contextElements.directory.search(key) != -1) {
                  naviSelection = key;
              }
              with (top.frames['header']) {
                    hideLayers(menuDescriptions[key].layerName);
              }
         } // for

        // get the menu description of the selected menu
         var menuDesc = null;
         if (naviSelection != null) {
             menuDesc = top.frames['header'].menuDescriptions[naviSelection];
         }

         with (top.frames['header']) {
               // @revisit: rename naviVisibility to isHome?
               if ((menuDesc == null) || menuDesc.naviVisibility) {
                   // hide the home banner layer if hp is not loaded in main
                   // and show primary navigation
                   hideLayers('bannerhome');

               } else {
                   // hp
                   changeImgSrc(contextElements.visualURL, 'bannerhome', 'bannerimg');

                    showLayers('bannerhome');
                   return;
               }
         } // if

         // load the banner from the source given in contextElements
         var imgBanner = new Image();
         imgBanner.src = contextElements.visualURL;
         if (client.isNS4) {
            with (top.frames['header']) {
                  if (document.images['banner']) {
                      document.images['banner'].src = imgBanner.src;
                  }
            }
         } else {
            if (top.frames['header'].document.images['banner']) {
                top.frames['header'].document.images['banner'].src = imgBanner.src;
            }
         }

         // //////////////////////////////////////////
         // set the primary navigation highlighted menu item


         // for all browsers loop through the menuDescriptions array,
         // set image source to highlighted if the selected menu item
         // has been found. Otherwise set the none-highlighted source.

             for (var key in top.frames['header'].menuDescriptions) {
                  with (top.frames['header']) {
                        if ((menuDesc != null)
                             && (menuDesc.imgName == menuDescriptions[key].imgName)) {
                            if (document.images[menuDescriptions[key].imgName]) {
                                document.images[menuDescriptions[key].imgName].src =  menuDescriptions[key].imageHi.src; ;//menuDescriptions[key].imgHighlightSrc;
                            }
                        } else {

                            if (document.images[menuDescriptions[key].imgName]) {
                                document.images[menuDescriptions[key].imgName].src =  menuDescriptions[key].image.src;;// menuDescriptions[key].imgSrc;
                            } // if
                        } // else
                  } // with
             } // for
        // }
}