function getScrollYCoords() {
  var y;
  if (document.documentElement && document.documentElement.scrollTop) {
    y = document.documentElement.scrollTop;
  }
  else if (document.body && document.body.scrollTop) {
    y = document.body.scrollTop;
  }
  else if (window.pageYOffset) {
    y = window.pageYOffset;
  }
  else if (window.scrollY) {
    y = window.scrollY;
  }
  if (typeof(y) == 'number') {
    return y;
  }
  return 0;
}

function getClientWidth() {
  if (typeof(window.innerWidth) == 'number') {
    return window.innerWidth;
  } 
  else if (document.documentElement &&
          (document.documentElement.clientWidth)) {
    return document.documentElement.clientWidth;
  }
  else if (document.body && (document.body.clientWidth)) {
    return document.body.clientWidth;
  }
}

function getClientHeight() {
  if (typeof(window.innerWidth) == 'number') {
    return window.innerHeight;
  } 
  else if (document.documentElement && 
          (document.documentElement.clientHeight)) {
    return document.documentElement.clientHeight;
  }
  else if (document.body && (document.body.clientHeight)) {
    return document.body.clientHeight;
  }
}

function getTotalDocumentHeight() {
  if (window.innerHeight && typeof(window.scrollMaxY) !== 'undefined') {
    // Firefox
    return window.innerHeight + window.scrollMaxY;
  }
  else if (document.body.scrollHeight > document.body.offsetHeight) {
    // all but Explorer Mac
    return document.body.scrollHeight;
  }
  else {
    // works in Explorer 6 Strict, Mozilla (not FF) and Safari
    return document.body.offsetHeight + document.body.offsetTop;
  }
}

Element.addMethods({
  center: function center(element) {
    if (!(element = $(element))) return;
    var myWidth = getClientWidth();
    var myHeight = getClientHeight();

    var scrollY = getScrollYCoords();

    element.style.position = 'absolute';
    if (typeof KC != 'undefined') {
      element.style.zIndex = KC.getNewZIndex();
    }
    else {
      element.style.zIndex = 99;
    }

    var setX = (myWidth  - element.getWidth()) / 2;
    var setY = (myHeight - element.getHeight()) / 2 + scrollY;

    setX = ( setX < 0 ) ? 0 : setX;
    setY = ( setY < 0 ) ? 0 : setY;

    element.style.left = setX + "px";
    element.style.top = setY + "px";
    element.style.display  = 'block';
    return element;
  }
})

