/*jslint browser: true, sloppy: true, newcap: true, maxlen: 80, indent: 2 */

/*globals getElement, getBrowserIsIE, recursiveGetOffsetTop,
  recursiveGetOffsetLeft */

function getEventElement(event) {
  if (event.target) {
    return event.target;
  } else {
    return event.srcElement;
  }
}
function getElementsByClassName(Element, TagName, ClassNames) {
  var Elements = (TagName === "*" && document.all) ?
    document.all :
    Element.getElementsByTagName(TagName),
    ReturnElements = [],
    RegExpClassNames = [],
    x,
    CurrentElement,
    MatchesAll,
    y;
  if (typeof (ClassNames) === "object") {
    for (x = 0; x < ClassNames.length; x += 1) {
      RegExpClassNames.push(
        new RegExp(
          "(^|\\s)" +
            ClassNames[x].replace(/\-/g, "\\-") +
            "(\\s|$)"
        )
      );
    }
  } else {
    RegExpClassNames.push(
      new RegExp(
        "(^|\\s)" +
          ClassNames.replace(/\-/g, "\\-") +
          "(\\s|$)"
      )
    );
  }
  CurrentElement = false;
  MatchesAll = false;
  for (x = 0; x < Elements.length; x += 1) {
    CurrentElement = Elements[x];
    MatchesAll = true;
    for (y = 0; y < RegExpClassNames.length; y += 1) {
      if (!RegExpClassNames[y].test(CurrentElement.className)) {
        MatchesAll = false;
        break;
      }
    }
    if (MatchesAll) {
      ReturnElements.push(CurrentElement);
    }
  }
  return ReturnElements;
}
function getGreatestWidth(NODE) {
  var greatestWidth = 0,
    x,
    nodeWidth,
    subWidth;
  if (NODE) {
    for (x = 0; x < NODE.childNodes.length; x += 1) {
      if (NODE.childNodes[x].nodeType === 1) {
        nodeWidth = NODE.childNodes[x].offsetWidth;
        if (nodeWidth > greatestWidth) {
          greatestWidth = nodeWidth;
        }
        if (NODE.childNodes[x].childNodes.length > 0) {
          subWidth = getGreatestWidth(NODE.childNodes[x]);
          if (subWidth > greatestWidth) {
            greatestWidth = subWidth;
          }
        }
      }
    }
  }
  return greatestWidth;
}
function getGreatestHeight(NODE) {
  var greatestHeight = 0,
    x,
    nodeHeight,
    subHeight;
  if (NODE) {
    for (x = 0; x < NODE.childNodes.length; x += 1) {
      if (NODE.childNodes[x].nodeType === 1) {
        nodeHeight = NODE.childNodes[x].offsetHeight;
        if (nodeHeight > greatestHeight) {
          greatestHeight = nodeHeight;
        }
        if (NODE.childNodes[x].childNodes.length > 0) {
          subHeight = getGreatestHeight(NODE.childNodes[x]);
          if (subHeight > greatestHeight) {
            greatestHeight = subHeight;
          }
        }
      }
    }
  }
  return greatestHeight;
}
function setGreatestWidth(NODE, WIDTH) {
  var x;
  if (NODE) {
    for (x = 0; x < NODE.childNodes.length; x += 1) {
      if (NODE.childNodes[x].nodeType === 1 &&
          !(NODE.childNodes[x].style && NODE.childNodes[x].style.width)) {
        NODE.childNodes[x].style.width = WIDTH + "px";
        if (NODE.childNodes[x].childNodes.length > 0) {
          setGreatestWidth(NODE.childNodes[x], WIDTH);
        }
      }
    }
  }
}
function setGreatestHeight(NODE, HEIGHT) {
  var x;
  if (NODE) {
    for (x = 0; x < NODE.childNodes.length; x += 1) {
      if (NODE.childNodes[x].nodeType === 1 &&
          !(NODE.childNodes[x].style && NODE.childNodes[x].style.height)) {
        NODE.childNodes[x].style.height = HEIGHT + "px";
        if (NODE.childNodes[x].childNodes.length > 0) {
          setGreatestWidth(NODE.childNodes[x], HEIGHT);
        }
      }
    }
  }
}
function createFloatingDiv(EVENT, FOLLOW_MOUSE, CENTERED, OPENFUNCTION,
                           CLOSEFUNCTION) {
  var FloatingDivEventElement, FloatingDivMouseFollow, FloatingDivCentered,
    FloatingDivXOffset, FloatingDivYOffset, OLDDIV, IEIFRAME, NewFloatingDiv,
    ID, hoverNodes, NewFloatingHTML, NEWDIVHeight, NEWDIVWidth,
    EVENTELEMENTHeight, EVENTELEMENTWidth, EVENTELEMENTTop, EVENTELEMENTLeft,
    greatestWidth, greatestHeight, x, IEBUG, SRC, FRAMEBORDER, SCROLLING;
  EVENT = EVENT || window.event;
  FloatingDivEventElement = new getEventElement(EVENT);
  FloatingDivMouseFollow = (FOLLOW_MOUSE) ? true : false;
  FloatingDivCentered = (CENTERED) ? true : false;
  FloatingDivXOffset = 10;
  FloatingDivYOffset = 10;
  // If you hover over the div we need to get the td node so we can get the
  // div that contains the hover contents.
  while (FloatingDivEventElement.nodeName !== "TD") {
    FloatingDivEventElement = FloatingDivEventElement.parentNode;
  }
  OLDDIV = new getElement("FloatingDiv");
  if (OLDDIV.obj) {
    if (OLDDIV.obj.onClose) {
      OLDDIV.obj.onClose();
    }
    OLDDIV.obj.style.display = "none";
    document.body.removeChild(OLDDIV.obj);
    document.body.onmousemove = "return false";
    if (getBrowserIsIE()) {
      IEIFRAME = new getElement("IEIFRAME");
      IEIFRAME.style.display = "none";
    }
  }
  NewFloatingDiv = document.createElement('DIV');
  ID = document.createAttribute('id');
  ID.value = "FloatingDiv";
  NewFloatingDiv.setAttributeNode(ID);
  if (CLOSEFUNCTION) {
    NewFloatingDiv.onClose = CLOSEFUNCTION;
  }
  NewFloatingDiv.style.position = "absolute";
  hoverNodes = getElementsByClassName(
    FloatingDivEventElement,
    "DIV",
    "hoverContent"
  );
  NewFloatingHTML = "";
  if (FloatingDivCentered) {
    NewFloatingHTML += '<div class="centered"><div><div>';
  }
  NewFloatingHTML += hoverNodes[0].innerHTML;
  if (FloatingDivCentered) {
    NewFloatingHTML += "</div></div></div>";
  }
  NewFloatingDiv.innerHTML = NewFloatingHTML;
  document.body.appendChild(NewFloatingDiv);
  NEWDIVHeight = NewFloatingDiv.offsetHeight / 2;
  NEWDIVWidth = NewFloatingDiv.offsetWidth / 2;
  EVENTELEMENTHeight = FloatingDivEventElement.offsetHeight / 2;
  EVENTELEMENTWidth = FloatingDivEventElement.offsetWidth / 2;
  EVENTELEMENTTop = recursiveGetOffsetTop(FloatingDivEventElement);
  EVENTELEMENTLeft = recursiveGetOffsetLeft(FloatingDivEventElement);
  greatestWidth = getGreatestWidth(NewFloatingDiv);
  greatestHeight = getGreatestHeight(NewFloatingDiv);
  if (NEWDIVHeight < greatestHeight / 2) {
    NEWDIVHeight = greatestHeight / 2;
  }
  if (NEWDIVHeight < EVENTELEMENTHeight) {
    NEWDIVHeight = EVENTELEMENTHeight;
    NewFloatingDiv.style.height = (FloatingDivEventElement.offsetHeight) + "px";
    NewFloatingDiv.style.width = greatestWidth + "px";
    for (x = 0; x < NewFloatingDiv.childNodes.length; x += 1) {
      if (NewFloatingDiv.childNodes[x].nodeType === 1) {
        NewFloatingDiv.childNodes[x].style.height =
          (EVENTELEMENTHeight * 2) +
          "px";
      }
    }
  }
  if (NEWDIVWidth < greatestWidth / 2) {
    NEWDIVWidth = greatestWidth / 2;
  }
  setGreatestWidth(NewFloatingDiv, greatestWidth);
  NewFloatingDiv.style.width = (greatestWidth + 20) + "px";
  setGreatestHeight(NewFloatingDiv, greatestHeight);
  // If not passed in the hover is centered
/*
  if (FloatingDivMouseFollow) {
    NewFloatingDiv.style.top = (getYOffset(EVENT) + FloatingDivYOffset) + "px";
  } else {
    NewFloatingDiv.style.top =
      (EVENTELEMENTTop + EVENTELEMENTHeight - NEWDIVHeight) +
      "px";
  }
  if (!getBrowserIsIE()) {
    if (FloatingDivMouseFollow) {
      NewFloatingDiv.style.left =
        (getXOffset(EVENT) + FloatingDivXOffset) +
        "px";
    } else {
      NewFloatingDiv.style.left =
        (EVENTELEMENTLeft + EVENTELEMENTWidth - NEWDIVWidth) +
        "px";
    }
  } else {
    if (FloatingDivMouseFollow) {
      NewFloatingDiv.style.left =
        (getXOffset(EVENT) + FloatingDivXOffset) +
        "px";
    } else {
      NewFloatingDiv.style.left =
        (EVENTELEMENTLeft + EVENTELEMENTWidth - NEWDIVWidth) +
        "px";
    }

  overridden with the two lines below
*/
  NewFloatingDiv.style.top =
    ((document.body.offsetHeight / 2) + 120) - (greatestHeight / 2) +
    "px";
  NewFloatingDiv.style.left =
    ((document.body.offsetWidth / 2) - 64) - (greatestWidth / 2) +
    "px";

  if (getBrowserIsIE()) {
    NewFloatingDiv.style.zIndex = "1000";
    IEBUG = new getElement("IEIFRAME");
    if (IEBUG.obj === undefined) {
      IEBUG = document.createElement('IFRAME');
      SRC = document.createAttribute('src');
      SRC.value = "";
      IEBUG.setAttributeNode(SRC);
      FRAMEBORDER = document.createAttribute('frameBorder');
      FRAMEBORDER.value = "0";
      IEBUG.setAttributeNode(FRAMEBORDER);
      SCROLLING = document.createAttribute('scrolling');
      SCROLLING.value = "no";
      IEBUG.setAttributeNode(SCROLLING);
      ID = document.createAttribute('id');
      ID.value = "IEIFRAME";
      IEBUG.setAttributeNode(ID);
    }
    IEBUG.style.display = "block";
    IEBUG.style.position = "absolute";
    IEBUG.style.top = recursiveGetOffsetTop(NewFloatingDiv);
    IEBUG.style.left = recursiveGetOffsetLeft(NewFloatingDiv);
    IEBUG.style.height = NewFloatingDiv.offsetHeight + "px";
    IEBUG.style.width = NewFloatingDiv.offsetWidth + "px";
    IEBUG.style.zIndex = "999";
    if (IEBUG.obj === undefined) {
      document.body.appendChild(IEBUG);
    }
  }
  if (OPENFUNCTION) {
    OPENFUNCTION();
  }
/*
  MOUSEMOVEFUNC = function (e) {
    var scrollLeft, scrollTop, offsetLeft, offsetTop;
    e = e || window.event;
    scrollLeft = document.documentElement.scrollLeft;
    scrollTop = document.documentElement.scrollTop;
    offsetLeft = recursiveGetOffsetLeft(FloatingDivEventElement);
    offsetTop = recursiveGetOffsetTop(FloatingDivEventElement);
    if (e.clientX < offsetLeft - scrollLeft ||
        e.clientX >
          offsetLeft - scrollLeft + FloatingDivEventElement.offsetWidth ||
        e.clientY < offsetTop - scrollTop ||
        e.clientY >
          offsetTop - scrollTop + FloatingDivEventElement.offsetHeight) {
      if (NewFloatingDiv.onClose) {
        NewFloatingDiv.onClose();
      }
      NewFloatingDiv.style.display = "none";
      document.body.removeChild(NewFloatingDiv);
      document.body.onmousemove = "return false";
      if (getBrowserIsIE()) {
        var IEIFRAME = new getElement("IEIFRAME");
        IEIFRAME.style.display = "none";
      }
    } else {
      if (FloatingDivMouseFollow) {
        var NewFloatingDivLeft = (getXOffset(e) + 10) + "px";
        var NewFloatingDivTop = (getYOffset(e) + 10) + "px";
        if (getBrowserIsIE()) {
          var IEIFRAME = new getElement("IEIFRAME");
          IEIFRAME.style.left = NewFloatingDivLeft;
          IEIFRAME.style.top = NewFloatingDivTop;
        }
        NewFloatingDiv.style.left = NewFloatingDivLeft;
        NewFloatingDiv.style.top = NewFloatingDivTop;
      }
    }
  }
  document.body.onmousemove = MOUSEMOVEFUNC;

  overridden with the function below
*/
}
function closeNewFloatingDiv() {
  var OLDDIV = new getElement("FloatingDiv"),
    IEIFRAME;
  if (OLDDIV.obj) {
    OLDDIV.obj.style.display = "none";
    document.body.removeChild(OLDDIV.obj);
    if (getBrowserIsIE()) {
      IEIFRAME = new getElement("IEIFRAME");
      IEIFRAME.style.display = "none";
    }
  }
}


var mmonths = [
  "", "January", "February", "March", "April", "May", "June", "July", "August",
  "September", "October", "November", "December"
];
var February = 2;
var firstWeekday = 0;
function isLeap(year) {
  return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
}
function highlightColor(e) {
  e = e || window.event;
  var TEXT = new getEventElement(e);
  TEXT.style.color = "#FF0000";
}
function unhighlightColor(e) {
  e = e || window.event;
  var TEXT = new getEventElement(e);
  TEXT.style.color = "#000000";
}

function loadMonthYearSelector(e) {
  var DIV, NEWDIV2, NEWROW, contents, currentYear, currentMonth, x,
    NEWDIV2Height, NEWDIV2Width, IEBUG, SRC, FRAMEBORDER, SCROLLING, ID,
    MOUSEMOVEFUNC;
  e = e || window.event;
  DIV = new getEventElement(e);
  NEWDIV2 = document.createElement('DIV');
  NEWDIV2.style.position = "absolute";
  contents =
    "<table style='border: 1px solid #000000; background-color: #FFFFFF; " +
    "margin: 0px; padding: 0px; font-size: 10pt;'><tbody>";
  currentYear = new Date().getFullYear();
  currentMonth = new Date().getMonth() + 1;
  for (x = currentMonth; x < mmonths.length; x += 1) {
    contents +=
      "<tr><th><a href='/events?year=" +
      currentYear +
      "&month=" +
      x +
      "'>" +
      mmonths[x] +
      " " +
      currentYear +
      "</a></th></tr>";
  }
  for (x = 1; x < currentMonth; x += 1) {
    contents +=
      "<tr><th><a href='/events?year=" +
      (currentYear + 1) +
      "&month=" +
      x +
      "'>" +
      mmonths[x] +
      " " +
      (currentYear + 1) +
      "</span></th></tr>";
  }
  contents += "</tbody>";
  NEWDIV2.innerHTML = contents;
  document.body.appendChild(NEWDIV2);
  NEWROW = NEWDIV2.getElementsByTagName('TR');
  NEWDIV2Height = NEWDIV2.offsetHeight;
  NEWDIV2Width = NEWDIV2.offsetWidth;
  NEWDIV2.style.position = "absolute";
  NEWDIV2.style.top =
    (recursiveGetOffsetTop(DIV) - (NEWDIV2.offsetHeight / 2)) +
    "px";
  NEWDIV2.style.left =
    (recursiveGetOffsetLeft(DIV) + (DIV.offsetWidth / 2) -
    (NEWDIV2.offsetWidth / 2)) +
    "px";
  if (getBrowserIsIE()) {
    NEWDIV2.style.zIndex = "1000";
    IEBUG = new getElement("IEIFRAME2");
    if (IEBUG.obj === undefined) {
      IEBUG = document.createElement('IFRAME');
      SRC = document.createAttribute('src');
      SRC.value = "javascript:false;";
      IEBUG.setAttributeNode(SRC);
      FRAMEBORDER = document.createAttribute('frameBorder');
      FRAMEBORDER.value = "0";
      IEBUG.setAttributeNode(FRAMEBORDER);
      SCROLLING = document.createAttribute('scrolling');
      SCROLLING.value = "no";
      IEBUG.setAttributeNode(SCROLLING);
      ID = document.createAttribute('id');
      ID.value = "IEIFRAME2";
      IEBUG.setAttributeNode(ID);
    }
    IEBUG.style.display = "block";
    IEBUG.style.position = "absolute";
    IEBUG.style.top = recursiveGetOffsetTop(NEWDIV2);
    IEBUG.style.left = recursiveGetOffsetLeft(NEWDIV2);
    IEBUG.style.height = NEWDIV2.offsetHeight;
    IEBUG.style.width = NEWDIV2.offsetWidth;
    IEBUG.style.zIndex = "999";
    if (IEBUG.obj === undefined) {
      document.body.appendChild(IEBUG);
    }
  }
  MOUSEMOVEFUNC = function (e) {
    var scrollLeft, scrollTop, IEIFRAME;
    e = e || window.event;
    scrollLeft = document.documentElement.scrollLeft;
    scrollTop = document.documentElement.scrollTop;
    if (e.clientX < recursiveGetOffsetLeft(NEWDIV2) - scrollLeft ||
        e.clientX >
          recursiveGetOffsetLeft(NEWDIV2) - scrollLeft + NEWDIV2.offsetWidth ||
        e.clientY < recursiveGetOffsetTop(NEWDIV2) - scrollTop ||
        e.clientY >
          recursiveGetOffsetTop(NEWDIV2) - scrollTop + NEWDIV2.offsetHeight) {
      NEWDIV2.style.display = "none";
      document.body.removeChild(NEWDIV2);
      document.body.onmousemove = "return false";
      if (getBrowserIsIE()) {
        IEIFRAME = new getElement("IEIFRAME2");
        IEIFRAME.style.display = "none";
      }
    }
  };
  document.body.onmousemove = MOUSEMOVEFUNC;
}

