﻿function btnIsPrimary(btnID) {
   return document.getElementById(btnID).className.indexOf("primary") == 0;
}
function btnHover(btnID) {
   setClass(btnID + "_a", btnIsPrimary(btnID + "_a") ? "primary-hover" : "secondary-hover");
   setClass(btnID + "_span", btnIsPrimary(btnID + "_a") ? "primary-hover" : "secondary-hover");
}
function btnHoverWithMenu(btnID) {
   var menuID = btnID + "_menu";
   var menu = document.getElementById(menuID);
   if (menu.style.display == "none")
      btnHover(btnID);
}
function btnHoverEnd(btnID) {
   setClass(btnID + "_a", btnIsPrimary(btnID + "_a") ? "primary" : "secondary");
   setClass(btnID + "_span", btnIsPrimary(btnID + "_a") ? "primary" : "secondary");
}
function btnHoverEndWithMenu(btnID) {
   var menuID = btnID + "_menu";
   var menu = document.getElementById(menuID);
   if (menu.style.display == "none")
      btnHoverEnd(btnID);
}
function btnDown(btnID) {
   setClass(btnID + "_a", btnIsPrimary(btnID + "_a") ? "primary-down" : "secondary-down");
   setClass(btnID + "_span", btnIsPrimary(btnID + "_a") ? "primary-down" : "secondary-down");
}
function btnDownWithMenu(btnID) {
   setClass(btnID + "_a", btnIsPrimary(btnID + "_a") ? "primary-down-menu" : "secondary-down-menu");
   setClass(btnID + "_span", btnIsPrimary(btnID + "_a") ? "primary-down-menu" : "secondary-down-menu");
}
function btnUp(btnID) {
   btnHover(btnID);
}
var skipHideBtnDropDown = "";
function toggleBtnDropDown(ev, btnID, callerID) {
   var menuID = btnID + "_menu";
   var menu = document.getElementById(menuID);
   if (menu.style.display == "") {
      menu.style.display = "none";
      btnUp(btnID);
   }
   else {
      // ------------------------------------------------------------------------------
      // -- getBounds() returns the absolute position in the page, so a btnDropDown  --
      // -- inside a relatively positioned div might have rect.x=200 but with        --
      // -- offsetLeft=80. The correct left value is then left=80px, not left=200px. --
      // ------------------------------------------------------------------------------
      //rect = Sys.UI.DomElement.getBounds(document.getElementById(callerID));
      //menu.style.top = (rect.y + rect.height) + "px";
      //menu.style.left = rect.x + "px";
      // ------------------------------------------------------------------------------
      var caller = document.getElementById(callerID);
      menu.style.top = (caller.offsetTop + caller.offsetHeight) + "px";
      menu.style.left = caller.offsetLeft + "px";
      menu.style.display = "";
      skipHideBtnDropDown = menuID;
      
      var shadowID = btnID + "_shadow";
      var shadow = document.getElementById(shadowID);

      // ------------------------------------------------------------------------------
      // -- getBounds() returns the absolute position in the page, so a btnDropDown  --
      // -- inside a relatively positioned div might have rect.x=200 but with        --
      // -- offsetLeft=80. The correct left value is then left=80px, not left=200px. --
      // ------------------------------------------------------------------------------
      //menuRect = Sys.UI.DomElement.getBounds(menu);
      //shadow.style.top = (menuRect.y + 5) + "px";
      //shadow.style.left = (menuRect.x + 5) + "px";
      //shadow.style.height = menuRect.height + "px";
      //shadow.style.width = menuRect.width + "px";
      // ------------------------------------------------------------------------------
      shadow.style.top = (menu.offsetTop + 5) + "px";
      shadow.style.left = (menu.offsetLeft + 5) + "px";
      shadow.style.height = menu.offsetHeight + "px";
      shadow.style.width = menu.offsetWidth + "px";
      shadow.style.display = "";
   }
   
   document.getElementById(callerID).blur();
}
function hideBtnDropDown(btnID) {
   var menuID = btnID + "_menu";
   if (skipHideBtnDropDown == menuID)
      skipHideBtnDropDown = "";
   else {
      var menu = document.getElementById(menuID);
      menu.style.display = "none";
      var shadow = document.getElementById(btnID + "_shadow");
      shadow.style.display = "none";
      btnHoverEnd(btnID);
   }
}
