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) {

      if (window.frameElement != null) {
         var thisframe = window.frameElement;
         if (document.body.scrollHeight > 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';
         }

      }
   }

}

// homepage uses different logic to display the command bar
function show_cb_homepage(id) {

   var cb = document.getElementById(id);

   if (cb) {

      var 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() {

   if ((typeof(CAN_SAVE_LINK) != 'undefined') && (CAN_SAVE_LINK)) {
      remote = window.open("/links/linkapp.aspx?_command=new&_open=remote&url=" + escape(PAGE_URL) + "&target=in_main&title=" + escape(PAGE_TITLE) + "&_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);
   }
}

// 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
*/

// counts the number of div tags in an element
function countDivs(element) {

   var numChildren = 0;

   // loop through children
   for (var i=0; i < element.childNodes.length; i++) {
      // if this child is a div
      if (element.childNodes[i].nodeName == 'DIV') {
         numChildren += 1;
      }
   }

   return numChildren;

}


// 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 - (number of menu options * height of a menu option) - (trap padding + 1)
         dropMenu.style.top = (positionTop - 2 - 10 - (countDivs(dropMenu.firstChild.firstChild) * 20) - 9) + 'px';
      }
      else {
         // top position = 30(command bar height) + 1(borderwidth)
         dropMenu.style.top = '29px';
      }

   }
}


// 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();
      }

   }
}

function cb_openhelp(localeDir, url) {

   if (localeDir == null || localeDir.length < 1) {
      localeDir = "us";
   }

   var baseURL = "/help/" + localeDir + "/WebHelp/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';
      }
   }

}
