function findPosX(obj) {
  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 findPosY(obj) {
  var curtop = 0;

  if (obj.offsetParent) {
    while (obj.offsetParent) {
      curtop += obj.offsetTop;
      obj = obj.offsetParent;
    }
  } else if (obj.y)
    curtop += obj.y;

  return curtop;
}



var calendar;  /* global object so we can  use it when an event occurs */
var DEFAULT_TOOLTIP = "Select Date";
/**
 * initCalendar() - Initializes each cell of the calendar and applies
 *                  the individual event handlers.
 */  
function initCalendar() {
  calendar = document.getElementById('calendar');

  /* Loop through all of the cells, except for cells in row 2 (week n
   * and also the last row (tooltip) and apply the event handlers */
  for (i = 0; i < calendar.rows.length - 1; i++) {
    if (i == 2) continue;

    for (j = 0; j < calendar.rows[i].cells.length; j++) {
      cell = calendar.rows[i].cells[j];
      id   = cell.getAttribute('id');

      /* if it is a week arrow
       * else if it is a navigation button (without menus)
       * else if it is a navigation button (with menus)
       * else its a normal calendar cell */
      if (i >= 3 && i < calendar.rows.length-1 && j == 0) {
        cell.className   = 'arrow week';
        cell.onmouseover = function (e) { showToolTip(this.getAttribute('id')); this.className = 'button_up highlight_on arrow week'; }
        cell.onmouseout  = function (e) { showToolTip(DEFAULT_TOOLTIP); this.className = 'highlight_off arrow week'; }
        cell.onmousedown = function (e) { this.className = 'button_down highlight_on arrow week'; }


      } else if ((i == 1 && j == 2) || (i === 0 && j === 0)) {
        cell.className   = 'button_up name';
        cell.onmouseover = function (e) { showToolTip(this.getAttribute('id')); this.className = 'button_up highlight_on name'; }
        cell.onmouseout  = function (e) { showToolTip(DEFAULT_TOOLTIP); this.className = 'button_up name'; }
        cell.onmousedown = function (e) { this.className = 'button_down name'; }
        cell.onmouseup   = function (e) { this.className = 'button_up highlight_on name'; }


      } else if (i == 1 && (j < 2 || j > 2)) { 
        cell.className   = 'button_up arrow';
        cell.setAttribute("pressed", 0); 
        cell.onmouseover = function (e) { 
          showToolTip(this.getAttribute('id'));
          if (this.getAttribute("pressed") == 0)
            this.className = 'button_up highlight_on arrow'; 
        }
        cell.onmouseout  = function (e) { 
          showToolTip(DEFAULT_TOOLTIP);
          if (this.getAttribute("pressed") == 0)
            this.className = 'button_up arrow'; 
        }
        cell.onmousedown = function (e) { 
          /* hide all of the other menus except the one we want to see */
          for (x = 0; x < calendar.rows[1].cells.length; x++) {
            if (x == 2) continue;  /* ignore 'Today' */

            cell = calendar.rows[1].cells[x];

            if (this.getAttribute('id') != cell.getAttribute('id')) {
              cell.setAttribute("pressed", 0);
              cell.className = 'button_up arrow';
              showMenu(this,x,0)

            } else {
              if (this.getAttribute("pressed") == 1) {
                this.setAttribute("pressed", 0);
                showMenu(this,x,0); this.className = 'button_up arrow';
              } else {
                this.setAttribute("pressed", 1);
                showMenu(this,x,1); this.className = 'button_down arrow';
              }
            }
          }
        }


      } else {
        cell.onmouseover = function (e) { showToolTip(this.getAttribute('id')); this.className = 'button_up highlight_on'; }
        cell.onmouseout  = function (e) { showToolTip(DEFAULT_TOOLTIP); this.className = 'highlight_off'; }
        cell.onmousedown = function (e) { this.className = 'button_down highlight_on'; }
      }

      cell.setAttribute("unselectable", true); /* IE only */
    }
  }
}


function showMenu(obj,id,show) {
  var menuObj = document.getElementById('menu_'+id);

  if (menuObj == null) return;

  var mcw = menuObj.offsetWidth;

  if (typeof mcw == "undefined")
    mcw = 50;
  menuObj.style.left = obj.offsetLeft + "px";
  menuObj.style.top = (obj.offsetTop + obj.offsetHeight) + "px";

  menuObj.style.display = (show) ? 'block' : 'none'; 
}


function showToolTip(text) {
  var obj = document.getElementById('tooltip');

  obj.innerHTML = text;
}
