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

/**
 * 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, initialState)
{
  var state;
  if (document.getElementById) { // DOM3 = IE5, NS6
    state = document.getElementById(id).style.visibility;

    switch (state) {
      case 'visible':
        hideDiv(id);
        break;
        
      case 'hidden':
        showDiv(id);
        break;
        
      default:
        switch (initialState) {
          case 'visible':
            hideDiv(id);
            break;
            
          case 'hidden':
            showDiv(id);
            break;
        }
    }
  }
  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;
}

// Functions for IE to get position of an object
function getPageOffsetLeft (el) {
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
}	
function getPageOffsetTop (el) {
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
}

function getCssObjXY(objID)
{
  // This function will return an Object with x and y properties
	var useWindow=false;
	var coordinates=new Object();
	var x=0,y=0;
	// Browser capability sniffing
	var use_gebi=false, use_css=false, use_layers=false;
	if (document.getElementById) { use_gebi=true; }
	else if (document.all) { use_css=true; }
	else if (document.layers) { use_layers=true; }
	// Logic to find position
 	if (use_gebi && document.all) {
		x=getPageOffsetLeft(document.all[objID]);
		y=getPageOffsetTop(document.all[objID]);
		}
	else if (use_gebi) {
		var o=document.getElementById(objID);
		x=getPageOffsetLeft(o);
		y=getPageOffsetTop(o);
		}
 	else if (use_css) {
		x=getPageOffsetLeft(document.all[objID]);
		y=getPageOffsetTop(document.all[objID]);
		}
	else if (use_layers) {
		var found=0;
		for (var i=0; i<document.anchors.length; i++) {
			if (document.anchors[i].name==objID) { found=1; break; }
			}
		if (found==0) {
			coordinates.x=0; coordinates.y=0; return coordinates;
			}
		x=document.anchors[i].x;
		y=document.anchors[i].y;
		}
	else {
		coordinates.x=0; coordinates.y=0; return coordinates;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
}

function moveDivToAnchor(divID,anchorID,offsetX,offsetY)
{
  if (document.getElementById) {
    if (document.getElementById(divID)) {
      var divObj = document.getElementById(divID);
    }
    if (document.getElementById(anchorID)) {
      var anchorObj = document.getElementById(anchorID);
    }
  }
  
  if (isNaN(offsetX)) {
    offsetX = 0;
  }
  
  if (isNaN(offsetY)) {
    offsetY = 0;
  }
  
  coords = getCssObjXY(anchorObj.id);
  anchorObjX = coords.x
  anchorObjY = coords.y

  moveDiv(divID,anchorObjX + offsetX, anchorObjY + offsetY);
}
