var hideId = 0;
var showId = 0;
var lVisible = null;
var defaultView = null;
var defaultHeight = 400;
var menuObj = null;

function killMenu(menuId) {
        if (!menuId)
          menuId = lVisible;
        lVisible = null;
        var Obj = document.getElementById(menuId);
        if(Obj) {
          Obj.style.display = 'none';
          removeClassName("active", menuObj.parentNode);
          menuObj = null;
        }
}

function showMenu(menuId, pObj) {
        clearTimeout(hideId);
        clearTimeout(showId);
        if (lVisible) {
          if (lVisible != menuId) {
            killMenu(lVisible);
          }
        }
        lVisible = menuId;
        var Obj = document.getElementById(menuId);
        if(Obj) {
          var elxy = getPos(pObj.parentNode);
          Obj.style.left = ( (elxy.x > parseInt(pObj.parentNode.parentNode.parentNode.offsetWidth / 2) ) ? (elxy.x - pObj.parentNode.offsetWidth - 19) : (elxy.x + pObj.parentNode.offsetWidth + 23) ) + 'px';
          var top = elxy.y;
          var elxy_all = getPos(pObj.parentNode.parentNode);
          var height_all = pObj.parentNode.parentNode.offsetHeight;
          if(top + defaultHeight + 43 > elxy_all.y + height_all) {
            top = elxy_all.y + height_all - defaultHeight - 43;
          }
          Obj.style.top = top + 'px'
          Obj.style.display = 'block';
        }
        addClassName("active", pObj.parentNode);
        menuObj = pObj;
        showId = setTimeout('clearAll();',500);
}

function hideMenu(menuId) {
        hideId = setTimeout('killMenu(\''+menuId+'\');',500);
}

function keepMenu(menuId) {
        clearTimeout(hideId);
}

function getPos(elem) {
        var retVal = new Coords(0,0);
        while(elem.offsetParent != null && elem.tagName != 'BODY') {
          retVal.x += elem.offsetLeft;
          retVal.y += elem.offsetTop;
          elem = elem.offsetParent
        }
        if (browserIn && browserIn.isFF) {
          retVal.x -= 0;
          retVal.y -= 0;
        }
        return retVal;
}

function Coords(x,y) {
        this.x=parseInt(x);
        this.y=parseInt(y);
}

function closeAll() {
  if(!showId)
    killMenu(lVisible);
}

function clearAll() {
  clearTimeout(showId);
  showId = 0;
}

function addClassName(className, elem) {
    var currentClass = elem.className;
    if (!new RegExp(("(^|\\s)" + className + "(\\s|$)"), "i").test(currentClass)) {
      elem.className = currentClass + (currentClass ? " " : "") + className;
    }
    return this;
}

function removeClassName(className, elem) {
    var classToRemove = new RegExp(("(^|\\s)" + className + "(\\s|$)"), "i");
    elem.className = (elem.className).replace(classToRemove, function (match) {
      var retVal = "";
      if (new RegExp("^\\s+.*\\s+$").test(match)) {
        retVal = match.replace(/(\s+).+/, "$1");
      }
      return retVal;
    }).replace(/^\s+|\s+$/g, "");
    return this;
}

function showRubrics() {
	document.getElementById('textArea').style.display = 'none';
	document.getElementById('rubricsArea').style.display = 'block';
}

function showText() {
	document.getElementById('textArea').style.display = 'block';
	document.getElementById('rubricsArea').style.display = 'none';
}

if(window.attachEvent) {
  window.attachEvent("onclick", closeAll);
} else {
  window.addEventListener("click", closeAll, false);
}


