function resizeHandler() {
   show_cb('cb_lo');
}

function multiResizeHandler() {
   show_cb_multi(MULTI_ARRAY);
}

function calendarResizeHandler() {
   init();
   show_cb('cb_lo');
}

window.onresize      = resizeHandler;

var CB_MENUS = new Array();

var MULTI_ARRAY = new Array();


function cb_hi(div) {
   div.className = 'cb-item cb-item-hi';
}

function cb_low(div) {
   div.className = 'cb-item cb-item-lo';
}

function cb_dn(div) {
   div.className = 'cb-item cb-item-dn';
}

function cb_up(div) {
   div.className = 'cb-item cb-item-hi';
}

function show_cb_multi(idList) {

   MULTI_ARRAY = idList;
   window.onresize = multiResizeHandler;

   var i = 0;
   for (i; i < idList.length; i++) {

      var cb   = document.getElementById(idList[i]);
      if (cb) {

         var div = cb.parentNode;
         var pageContents = document.body.childNodes[1];
         if (div && pageContents && pageContents.className == 'page-contents') {

               if (window.frameElement != null) {
                  var thisframe = window.frameElement;

                  if ( (div.offsetHeight + pageContents.offsetHeight + 15) > thisframe.offsetHeight) {
                     cb.style.display = '';
                     cb.className = 'cbfont display-block cb-lo cb';
                  } else {
                     cb.style.display = '';
                     cb.className = 'cbfont display-none cb-lo cb';
                  }

               }
         }
      }
   }
}

function show_cb(id) {

   var cb = document.getElementById(id);

   if (cb) {

      var iHeight = 0;
      if (window.frameElement != null) 
      {
         iHeight = window.frameElement.offsetHeight;
      }
      else
      {
         // handle case where there is no frameElement
         // (i.e. Additional Services page)
         iHeight = document.documentElement.clientHeight;
      }

      if (document.body.scrollHeight > iHeight) {
         cb.style.display = '';
         cb.className = 'cbfont display-block cb-lo cb';
      } else {
         cb.style.display = '';
         cb.className = 'cbfont display-none cb-lo cb';
      }
   }

}

// homepage uses different logic to display the command bar
function show_cb_homepage(id) {

   var cb = document.getElementById(id);

   if (cb) {

	  // bug #345730.bug1
	  // add specified code for IE8
	  var homepageHeight;
	  if(navigator.userAgent.indexOf("MSIE") > 0)
	  {
		homepageHeight = document.body.offsetHeight;
	  }
	  else
	  {
		homepageHeight = document.body.scrollHeight;
	  }
      var column1 = document.getElementById('col1');
      var column2 = document.getElementById('col2');
      if (column1 && column2) {
         // determine which column is taller
         if (column1.scrollHeight > column2.scrollHeight) {
            homepageHeight += column1.scrollHeight;
         }
         else {
            homepageHeight += column2.scrollHeight;
         }

         if (window.frameElement != null) {
            var thisframe = window.frameElement;

            if (homepageHeight > thisframe.offsetHeight) {
               cb.style.display = '';
               cb.className = 'cbfont display-block cb-lo cb';
            }
            else {
               cb.style.display = '';
               cb.className = 'cbfont display-none cb-lo cb';
            }
         }
      }
   }
}

function saveLink(pageURL, pageTitle) {
   if (pageURL != undefined
      && pageTitle != undefined) {
      CAN_SAVE_LINK = true;
   }

   if (pageURL == undefined) {
      pageURL = PAGE_URL;
   }

   if (pageTitle == undefined) {
      pageTitle = PAGE_TITLE;
   }

   if ((typeof(CAN_SAVE_LINK) != 'undefined') && (CAN_SAVE_LINK)) {
      remote = window.open("/links/linkapp.aspx?_command=new&_open=remote&url=" + escape(pageURL) + "&target=in_main&title=" + escape(pageTitle) + "&_appid=34", "_remote_control",
                           "toolbar=yes,location=yes,status=yes,menubar=no,scrollbars=yes,resizable=yes,height=450,width=700,copyhistory=no");
   }

}

function EVENT_cb_arrow_click(menuid, e) {

   stopProp(e);

   // get the command bar menu
   var cbmenu = document.getElementById(menuid);

   toggleMenuVisibility(cbmenu);

}


function EVENT_cb_menu_click(menuid) {

   // get the command bar menu
   var cbmenu = document.getElementById(menuid);

   toggleMenuVisibility(cbmenu);

}


function EVENT_cb_menu_out(menuid, e)
{
   // QuirksMode reference: http://www.quirksmode.org/js/events_mouse.html
   if (!e) var e = window.event;
   var tg = (window.event) ? e.srcElement : e.target;
   if (tg.nodeName != 'DIV') return;
   var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
   while (reltg != tg && reltg.nodeName != 'BODY')
      reltg= reltg.parentNode;
   if (reltg== tg) return;

   // mouseout left layer, toggle menu visiblity
   EVENT_cb_menu_click(menuid);
}




// duplicate of function in navbar.js
function menuhi(div) {
   div.className = 'menuitem menuitem-hi';
   div.getElementsByTagName('div')[0].className = 'menuitem-icon menuitem-icon-hi';
}

// duplicate of function in navbar.js
function menulow(div) {
   div.className = 'menuitem menuitem-lo';
   div.getElementsByTagName('div')[0].className = 'menuitem-icon menuitem-icon-lo';
}


// add a menu
function cb_menu_add(menuid, position, functionArray) {

   var id;
   var objMenu = new Object;

   if (menuid && position) {
      // id:        hi_new
      // menuid:    hi_new_menu
      // position:  hi

      //
      // Could change this to add an array of hide methods and show methods....
      // but not enough time
      //

      id = menuid.replace("_menu", "");
      objMenu = { id: id, menuid: menuid, position: position, move: moveMenu, hide: hideMenu, show: showMenu, methods: functionArray };

      CB_MENUS.push(objMenu);
   }
}

// add a menu
function cb_opt_menu_add(menuid, position, functionArray) {

   var id;
   var objMenu = new Object;

   if (menuid && position) {
      id = menuid.replace("_menu", "");
      objMenu = { id: id, menuid: menuid, position: position, move: moveOptMenu, hide: hideMenu, show: showMenu, methods: functionArray };

      CB_MENUS.push(objMenu);
   }
}

// reposition all dropdown menus
function cb_menu_move() {

   // add event handler here
   document.body.onmouseover   = hideAllMenus;

   if (CB_MENUS && (CB_MENUS.length > 0)) {

      // loop through array of menus
      for (var i=0; i < CB_MENUS.length; i++) {
         CB_MENUS[i].move();
      }

   }
}



/*
   Private functions
*/


// calculates the menu height based on the number and type of child nodes
function menuHeight(element) {

   var iHeight = 0;


if (element)
{
   // loop through children
   for (var i=0; i < element.childNodes.length; i++) {
      // if this child is a div
      if (element.childNodes[i].nodeName == 'DIV') {
         // if this is NOT a separator
         if (element.childNodes[i].className != 'menuitem-separator') {
            // add height of the ".menuitem" style
            iHeight += 20;
         }
         else {
            // add height of the ".menuitem-separator" style
            iHeight += 1;
         }
      }
   }
}
   return iHeight;

}


// finds offsetLeft position of an element
function findPosX(obj)
{
   // QuirksMode reference: http://www.quirksmode.org/js/findpos.html
   var curleft = 0;
   if (obj.offsetParent)
   {
      while (obj.offsetParent)
      {
         curleft += obj.offsetLeft
         obj = obj.offsetParent;
      }
   }
   else if (obj.x)
      curleft += obj.x;
   return curleft;
}

function hideMenu() {

   var dropMenu = document.getElementById(this.menuid);
   if (dropMenu) {
      if (dropMenu.style.visibility == 'visible') {

         if (this.methods) {
            var i = 0;
            for (i; i < this.methods.length; i++) {
               this.methods[i]();
            }
         }

         dropMenu.style.visibility = 'hidden';
      }


   }

}

function showMenu() {

   var dropMenu = document.getElementById(this.menuid);
   if (dropMenu) {
      dropMenu.style.visibility = 'visible';
   }

   if (this.methods) {
      var i = 0;
      for (i; i < this.methods.length; i++) {
         this.methods[i]();
      }
   }

}


// move menu to the correct position
function moveMenu() {

   // command bar
   var cb;
   var cbid;
   var cbNode;
   // dropdown button
   var dropButton;
   // dropdown menu
   var dropMenu;

   // final position of the menu
   var positionLeft = 0;
   var positionTop  = 0;

   // get button and menu elements
   dropButton = document.getElementById(this.id);
   dropMenu   = document.getElementById(this.menuid);

   if (dropButton && dropMenu) {

      // determine the id of the command bar
      cbid = dropButton.parentNode.id;

      // get command bar element
      cb = document.getElementById(cbid);

      /*
         calculate horizontal position
      */

      // NS and IE are 2 pixels different when looking at offset left. use offsetWidth and scrollWidth to synchronize
      // 11 = (10) padding on the command bar menu container + (1) border width
      dropMenu.style.left = (findPosX(dropButton) + (cb.offsetWidth - cb.scrollWidth) - 11 - 10) + 'px';


      /*
         calculate vertical position
      */

      // determine offsetTop of the command bar
      positionTop = cb.offsetTop;

      // if we want to move the menu to the bottom command bar
      if (this.position == 'lo') {
         // top position = commandbar position - borderwidth - top padding - (height of menu options/separators) - (trap padding + 1)
         dropMenu.style.top = (positionTop - 2 - 10 - (menuHeight(dropMenu.firstChild.firstChild)) - 9) + 'px';
      }
      else {
         // top position = 30(command bar height) + 1(borderwidth)
         dropMenu.style.top = '29px';
      }

   }
}

// move menu to the correct position
function moveOptMenu() {

   // command bar
   var cb;
   var cbid;
   var cbNode;
   // dropdown button
   var dropButton;
   // dropdown menu
   var dropMenu;

   // final position of the menu
   var positionLeft = 0;
   var positionTop  = 0;

   // get button and menu elements
   dropButton = document.getElementById(this.id);
   dropMenu   = document.getElementById(this.menuid);

   if (dropButton && dropMenu) {

      // browser detect
      var bIsMSIE7 = (navigator.userAgent.toLowerCase().indexOf('msie 7') > -1);
      var bIsMSIE = (navigator.userAgent.toLowerCase().indexOf('msie') > -1);

      // determine the id of the command bar
      cbid = dropButton.parentNode.id;

      // get command bar element
      cb = document.getElementById(cbid);

      /*
         calculate horizontal position
      */

      if (bIsMSIE7) {
           dropMenu.style.left = (cb.offsetLeft + cb.offsetParent.offsetLeft)+'px';
      } else if (bIsMSIE) {
           dropMenu.style.left = (cb.offsetLeft-80)+'px';
      } else {
           dropMenu.style.left = cb.offsetLeft+'px';
      }
      dropMenu.style.top = '13px';
   }
}

// stop event execution
function stopProp(e) {

   if (e && e.stopPropagation) {
      e.stopPropagation();
   }
   else {
      event.cancelBubble = true;
   }

}

function hideAllMenus() {

   if (CB_MENUS && (CB_MENUS.length > 0)) {

      // loop through array of menus
      for (i=0; i < CB_MENUS.length; i++) {
         CB_MENUS[i].hide();
      }

   }

}


// toggle visibility of a menu
function toggleMenuVisibility(div) {

   var i    = 0;
   var Menu = null;
   for (i; i < CB_MENUS.length; i++) {
      if (CB_MENUS[i].menuid == div.id) {
         Menu = CB_MENUS[i];
         continue;
      }
   }

   if (Menu) {

      // if the menu is not visible
      if (div.style.visibility != 'visible') {
         Menu.show();
      }
      else {
         Menu.hide();
      }

   }
}

// note: can be called many ways
//cb_openhelp('us', '#html/welcome.htm')
//cb_openhelp('us', '#html/welcome.htm','CentersHelp')
//cb_openhelp('us', '#html/welcome.htm','/help/us/CentersHelp')
function cb_openhelp(localeDir, url, siteHelpDir) {

   if (localeDir == null || localeDir.length < 1) {
      localeDir = "us";
   }
   if (siteHelpDir == null || siteHelpDir.length < 1) {
      siteHelpDir= "WebOfcHelp";
   }
   var baseURL;
   if (siteHelpDir.indexOf("/") == -1) {
      baseURL = "/help/" + localeDir + "/" + siteHelpDir + "/webofficehelp.htm";
   } else {
      baseURL = siteHelpDir + "/webofficehelp.htm";
   }

   if (url == null || url.length < 1) {
      url = baseURL;
   }
   else {


      if (url.indexOf('#') == 0) {
          if (navigator.userAgent.indexOf("Safari") != -1) {
            url = baseURL + "#" + url.split("#")[1];
          }
          else {
            url = baseURL + url;
          }
      }
   }
   remote = window.open(url, "_remote","toolbar=yes,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,height=550,width=800,copyhistory=no");
   if ( remote != null ) {
      if (remote.focus) {
         remote.focus();
      }
   }
}

function toggleSharedDropDown() {

   //sharing_picker_control
   var element = document.getElementById('sharing_picker_control');
   if (element) {

      if (element.style.visibility == 'visible') {
         element.style.visibility = 'hidden';
      }
      else {
         element.style.visibility = 'visible';
      }
   }

}
