/* $Id: //release/php/www/js/div_magic.js#3 $ */

/**
 * showDiv
 *
 * show the specified div
 */
function showDiv(divNameToShow)
{
  if (document.getElementById) {
    /**
     * DOM3 = IE5, NS6
     */
    document.getElementById(divNameToShow).style.visibility = 'visible';
    document.getElementById(divNameToShow).style.display    = 'block';
  }
  else {
    if (document.layers) {
      /**
       * NS4
       */
      eval('document.' + divNameToShow + '.visibility="visible"');
      eval('document.' + divNameToShow + '.display="block"');
    }
    else {
      /**
       * IE4
       */
      eval('document.all.' + divNameToShow + '.style.visibility="visible"');
      eval('document.all.' + divNameToShow + '.style.display="block"');
    }
  }
}

/**
 * showDiv
 *
 * show the specified div
 */
function showSpan(spanNameToShow)
{
  if (document.getElementById) {
    /**
     * DOM3 = IE5, NS6
     */
    document.getElementById(spanNameToShow).style.visibility = 'visible';
    document.getElementById(spanNameToShow).style.display    = 'inline';
  }
  else {
    if (document.layers) {
      /**
       * NS4
       */
      eval('document.' + spanNameToShow + '.visibility="visible"');
      eval('document.' + spanNameToShow + '.display="inline"');
    }
    else {
      /**
       * IE4
       */
      eval('document.all.' + spanNameToShow + '.style.visibility="visible"');
      eval('document.all.' + spanNameToShow + '.style.display="inline"');
    }
  }
}


/**
 * hideDiv
 *
 * show the specified div
 */
function hideDiv(divNameToShow,doNotCollapse)
{
  if (doNotCollapse) {
    disp = 'block';
  }
  else {
    disp = 'none';
  }

  if (document.getElementById) {
    /**
     * DOM3 = IE5, NS6
     */
    document.getElementById(divNameToShow).style.visibility = 'hidden';
    document.getElementById(divNameToShow).style.display    = disp;
  }
  else {
    if (document.layers) {
      /**
       * NS4
       */
      eval('document.' + divNameToShow + '.visibility="hidden"');
      eval('document.' + divNameToShow + '.display="' + disp + '"');
    }
    else {
      /**
       * IE4
       */
      eval('document.all.' + divNameToShow + '.style.visibility="hidden"');
      eval('document.all.' + divNameToShow + '.style.display="' + disp + '"');
    }
  }
}


/**
 * unhideDiv
 *
 * Shows the first parameter, and hides the following params
 * Thanks to Jeraimee <asleep@asleep.net>, with some modifications
 */
function unhideDiv(divNameToShow)
{
  if (document.getElementById) {
    /**
     * DOM3 = IE5, NS6
     */

    /**
     * Show the requested div
     */
    document.getElementById(divNameToShow).style.visibility = 'visible';
    document.getElementById(divNameToShow).style.display    = 'block';

    /**
     * Hide the rest of the divs
     * Start at 1 because 0 is for the divNameToShow
     */
    for (id = 1; id < unhideDiv.arguments.length; id++) {
      if (unhideDiv.arguments[id] != divNameToShow) {
        document.getElementById(unhideDiv.arguments[id]).style.visibility = 'hidden';
        document.getElementById(unhideDiv.arguments[id]).style.display    = 'none';
      }
    }
  }
  else {
    if (document.layers) {
      /**
       * NS4
       */
      eval('document.' + divNameToShow + '.visibility="visible"');
      eval('document.' + divNameToShow + '.display="block"');
      /**
       * Hide the rest of the divs
       * Start at 2 because 0 is for the function name and 1 for the divNameToShow
       */
      for (id = 2; id < unhideDiv.arguments.length; id++) {
        if (unhideDiv.arguments[id] != divNameToShow) {
          eval('document.' + unhideDiv.arguments[id] + '.visibility="hidden"');
          eval('document.' + unhideDiv.arguments[id] + '.display="none"');
        }
      }
    }
    else {
      /**
       * IE4
       */
      eval('document.all.' + divNameToShow + '.style.visibility="visible"');
      eval('document.all.' + divNameToShow + '.style.display="block"');
      /**
       * Hide the rest of the divs
       * Start at 1 because 0 is for the divNameToShow
       */
      for (id = 1; id < unhideDiv.arguments.length; id++) {
        if (unhideDiv.arguments[id] != divNameToShow) {
          eval('document.all.' + unhideDiv.arguments[id] + '.style.visibility="hidden"');
          eval('document.all.' + unhideDiv.arguments[id] + '.style.display="none"');
        }
      }
    }
  }
}

/**
 * switchVisibility
 *
 * Swaps the visibility of the specified DIV/LAYER.
 */
function switchVisibility(id)
{
  var state;
  if (document.getElementById) { // DOM3 = IE5, NS6
    state = document.getElementById(id).style.visibility;

    if (state == 'visible') {
      hideDiv(id);
    }
    else {
      showDiv(id);
    }
  }
  else {
    if (document.layers) { // NS4
      eval('state = document.' + id + '.visibility');
    }
    else { // IE4
      eval('state = document.all.' + id + '.style.visibility');
    }
    if (state == "visible") {
      hideDiv(id);
    }
    else {
      showDiv(id);
    }
  }
}


/**
 * moveDiv
 *
 * Move the div to the specified x and y
 */
function moveDiv(divId,x,y)
{
  if (document.getElementById && document.getElementById(divId)) {
    divObj = document.getElementById(divId);

    // x is defined
    if (!isNaN(x)) {

      if (!isNaN(divObj.style.left)) {
        divObj.style.left = x;
      }
      else if (!isNaN(divObj.style.pixelLeft)) {
        divObj.style.pixelLeft = x;
      }
      else if (!isNaN(divObj.left)) {
        divObj.left = x;
      }
    }

    // y is defined
    if (!isNaN(y)) {
      if (!isNaN(divObj.style.top)) {
        divObj.style.top = y;
      }
      else if (!isNaN(divObj.style.pixelTop)) {
        divObj.style.pixelTop = y;
      }
      else if (!isNaN(divObj.top)) {
        divObj.top = y;
      }
    }
  }
  else if (document.layers) {
    eval('document.' + id + '.left = ' + x);
    eval('document.' + id + '.top = ' + y);
  }
  else if (document.all) {
    eval('document.all.' + id + '.style.left = ' + x);
    eval('document.all.' + id + '.style.top  = ' + x);
  }
}


/**
 * moveDivToCenter
 *
 * Moves a div to the center of the screen
 */
function moveDivToCenter(id,itemH,itemW)
{
  height  = getBrowserHeight();
  width   = getBrowserWidth();

  if (height <= 0 || width <= 0) {
    return;
  }

  centerH = Math.floor((height-itemH) / 2);
  centerW = Math.floor((width-itemW)  / 2);

  moveDiv(id,centerW,centerH)
}

function getBrowserHeight()
{
  if (typeof(window.innerHeight) == 'number') {
    return window.innerHeight;
  }

  if (document.documentElement && document.documentElement.clientHeight) {
    //IE 6+ in 'standards compliant mode'
    return document.documentElement.clientHeight;
  }

  if (document.body && document.body.clientHeight) {
    //IE 4 compatible
    return document.body.clientHeight;
  }

  return 0;
}

function getBrowserWidth()
{
  if (typeof( window.innerWidth ) == 'number') {
    //Non-IE
    return window.innerWidth;
  }

  if (document.documentElement && document.documentElement.clientWidth) {
    //IE 6+ in 'standards compliant mode'
    return document.documentElement.clientWidth;
  }

  if (document.body && document.body.clientWidth) {
    //IE 4 compatible
    return document.body.clientWidth;
  }

  return 0;
}
