/******************************************************************************
* dhtmllib.js                                                                 *
*                                                                             *
* Copyright 1999 by Mike Hall.
* Visit http://www.dynamicdrive.com                                               *                                       *
* Last update: November 30, 1999.                                             *
*                                                                             *
* Provides basic functions for DHTML positioned elements which will work on   *
* both Netscape Communicator and Internet Explorer browsers (version 4.0 and  *
* up).                                                                        *
******************************************************************************/

// Determine browser.

var isMinNS4 = (document.layers) ? 1 : 0;
var isMaxNS45 = false;
var isMinIE4 = (document.all) ? 1 : 0;
var isMinIE5 = (isMinIE4 && navigator.appVersion.indexOf("5.")) >= 0 ? 1 : 0;
var isDOM = (document.getElementById) ? 1 : 0;

//-----------------------------------------------------------------------------
// Layer visibility.
//-----------------------------------------------------------------------------

function hideLayer(layer) {

  if (HM_NS4)
    layer.visibility = "hide";
  if ((HM_IE)||(HM_DOM))
    layer.style.visibility = "hidden";
}

function showLayer(layer) {

  if (HM_NS4)
    layer.visibility = "show";
  if ((HM_IE)||(HM_DOM))
    layer.style.visibility = "visible";
}

function isVisible(layer) {

  if (HM_NS4 && layer.visibility == "show")
    return(true);
  if (((HM_IE)||(HM_DOM)) && layer.style.visibility == "visible")
    return(true);

  return(false);
}

//-----------------------------------------------------------------------------
// Layer positioning.
//-----------------------------------------------------------------------------

function moveLayerTo(layer, x, y) {

  if (HM_NS4)
    layer.moveTo(x, y);
  if ((HM_IE)||(HM_DOM)) {
    layer.style.left = x;
    layer.style.top  = y;
  }
}

function moveLayerBy(layer, dx, dy) {

  if (HM_NS4)
    layer.moveBy(dx, dy);
  if (HM_IE) 
	{
    layer.style.pixelLeft += dx;
    layer.style.pixelTop  += dy;
	}
  if ((HM_DOM) && !(HM_IE)) 
	{
	layer.style.left = (parseInt(layer.style.left)+dx)+"px";
    layer.style.top  = (parseInt(layer.style.top)+dy)+"px";
	}
}

function getLeft(layer) {

  if (HM_NS4)
    return(layer.left);
  if (HM_IE)
    return(layer.style.pixelLeft);
   if ((HM_DOM) && !(HM_IE))
	{
	if (layer.style.left)
		return (parseInt(layer.style.left));
	else return (0);
    }
  return(-1);
}

function getTop(layer) {

  if (HM_NS4)
    return(layer.top);
  if (HM_IE)
    return(layer.style.pixelTop);
  if ((HM_DOM) && !(HM_IE))
	{
	if (layer.style.top)
		return (parseInt(layer.style.top));
	else return (0);
    }
  return(-1);
}

function getRight(layer) {

  if (HM_NS4)
    return(layer.left + getWidth(layer));
  if (HM_IE)
	return(layer.style.pixelLeft + getWidth(layer));
  if ((HM_DOM) && !(HM_IE))
	{
	if (layer.style.left)
		return (parseInt(layer.style.left) + getWidth(layer));
	else return (getWidth(layer));
	}
 return(-1);
}

function getBottom(layer) {
  if (HM_NS4)
    return(layer.top + getHeight(layer));
  if (HM_IE)
	{
	return(layer.style.pixelTop + getHeight(layer));
	}
  if ((HM_DOM) && !(HM_IE))
	{
	if (layer.style.top)
		return (parseInt(layer.style.top) + getHeight(layer));
	else return (getHeight(layer));
	}
  return(-1);
}

function getPageLeft(layer) {

  if (HM_NS4)
    return(layer.pageX);
  if ((HM_IE)||(HM_DOM))
    return(layer.offsetLeft);
  return(-1);
}

function getPageTop(layer) {

  if (HM_NS4)
    return(layer.pageY);
  if ((HM_IE)||(HM_DOM))
    return(layer.offsetTop);
  return(-1);
}
/*
function getPageTop(layer) {
	var y;
	if (HM_NS4) return layer.pageY;
	if ((HM_IE)||(HM_DOM)) {
		y = 0;
		while (layer.offsetParent != null) {
			y += layer.offsetTop;
			layer = layer.offsetParent;
		}
		y += layer.offsetTop;
		return y;
	}
	return -1;
}*/

function getWidth(layer) {

  if (HM_NS4) {
    if (layer.document.width)
      return(layer.document.width);
    else
      return(layer.clip.right - layer.clip.left);
  }
  if (HM_IE)
	{
    if (layer.style.pixelWidth)
      return(layer.style.pixelWidth);
    else
      return(layer.clientWidth);
	}
  if ((HM_DOM) && !(HM_IE)) 
	{
    if (parseInt(layer.style.width))
      return(parseInt(layer.style.width));
    else
      return(layer.offsetWidth);
	}
  return(-1);
}

function getHeight(layer) {

  if (HM_NS4) {
    if (layer.document.height)
      return(layer.document.height);
    else
      return(layer.clip.bottom - layer.clip.top);
  }
  if (HM_IE)
	{
    if (false && layer.style.pixelHeight)
		{
      return(layer.style.pixelHeight);
		}
    else
      return(layer.clientHeight);
	}
 if ((HM_DOM) && !(HM_IE)) 
	{
    if (false && parseInt(layer.style.height))
      return(parseInt(layer.style.height));
    else
      return(layer.offsetHeight);
	}
  return(-1);
}

function getzIndex(layer) {

  if (HM_NS4)
    return(layer.zIndex);
  if ((HM_IE)||(HM_DOM))
    return(layer.style.zIndex);

  return(-1);
}

function setzIndex(layer, z) {

  if (HM_NS4)
    layer.zIndex = z;
  if ((HM_IE)||(HM_DOM))
    layer.style.zIndex = z;
}

//-----------------------------------------------------------------------------
// Layer clipping.
//-----------------------------------------------------------------------------

function clipLayer(layer, clipleft, cliptop, clipright, clipbottom) {

  if (HM_NS4) {
    layer.clip.left   = clipleft;
    layer.clip.top    = cliptop;
    layer.clip.right  = clipright;
    layer.clip.bottom = clipbottom;
  }
  if ((HM_IE)||(HM_DOM))
    layer.style.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
}

function getClipLeft(layer) {

  if (HM_NS4)
    return(layer.clip.left);
  if ((HM_IE)||(HM_DOM)) {
    var str =  layer.style.clip;
    if (!str)
      return(0);
    var clip = getIEClipValues(layer.style.clip);
    return(clip[3]);
  }
  return(-1);
}

function getClipTop(layer) {

  if (HM_NS4)
    return(layer.clip.top);
  if ((HM_IE)||(HM_DOM)) {
    var str =  layer.style.clip;
    if (!str)
      return(0);
    var clip = getIEClipValues(layer.style.clip);
    return(clip[0]);
  }
  return(-1);
}

function getClipRight(layer) {

  if (HM_NS4)
    return(layer.clip.right);
  if (HM_IE)
	{
    var str =  layer.style.clip;
    if (!str)
      return(layer.style.pixelWidth);
    var clip = getIEClipValues(layer.style.clip);
    return(clip[1]);
	}
  if ((HM_DOM) && !(HM_IE)) 
	{
    var str =  layer.style.clip;
    if (!str)
      return (parseInt(layer.style.width));
    var clip = getIEClipValues(layer.style.clip);
    return(clip[1]);
	}
  return(-1);
}

function getClipBottom(layer) {

  if (HM_NS4)
    return(layer.clip.bottom);
  if (HM_IE)
	{
    var str =  layer.style.clip;
    if (!str)
      return(layer.style.pixelHeight);
    var clip = getIEClipValues(layer.style.clip);
    return(clip[2]);
	}
  if ((HM_DOM) && !(HM_IE)) 
	{
    var str =  layer.style.clip;
    if (!str)
      return (parseInt(layer.style.height));
    var clip = getIEClipValues(layer.style.clip);
    return(clip[2]);
	}
  return(-1);
}

function getClipWidth(layer) {

  if (HM_NS4)
    return(layer.clip.width);
  if (HM_IE)
	{
    var str = layer.style.clip;
    if (!str)
      return(layer.style.pixelWidth);
    var clip = getIEClipValues(layer.style.clip);
    return(clip[1] - clip[3]);
	}
  if ((HM_DOM) && !(HM_IE)) 
	{
    var str = layer.style.clip;
    if (!str)
      return (parseInt(layer.style.width));
    var clip = getIEClipValues(layer.style.clip);
    return(clip[1] - clip[3]);
	}
  return(-1);
}

function getClipHeight(layer) {

  if (HM_NS4)
    return(layer.clip.height);
  if (HM_IE)
	{
    var str =  layer.style.clip;
    if (!str)
      return(layer.style.pixelHeight);
    var clip = getIEClipValues(layer.style.clip);
    return(clip[2] - clip[0]);
	}
  if ((HM_DOM) && !(HM_IE)) 
	{
    var str =  layer.style.clip;
    if (!str)
      return (parseInt(layer.style.height));
    var clip = getIEClipValues(layer.style.clip);
    return(clip[2] - clip[0]);
	}
  return(-1);
}

function getIEClipValues(str) {

  var clip = new Array();
  var i;

  // Parse out the clipping values for IE layers.

  i = str.indexOf("(");
  clip[0] = parseInt(str.substring(i + 1, str.length), 10);
  i = str.indexOf(" ", i + 1);
  clip[1] = parseInt(str.substring(i + 1, str.length), 10);
  i = str.indexOf(" ", i + 1);
  clip[2] = parseInt(str.substring(i + 1, str.length), 10);
  i = str.indexOf(" ", i + 1);
  clip[3] = parseInt(str.substring(i + 1, str.length), 10);
  return(clip);
}

//-----------------------------------------------------------------------------
// Layer scrolling.
//-----------------------------------------------------------------------------

function scrollLayerTo(layer, x, y, bound) {

  var dx = getClipLeft(layer) - x;
  var dy = getClipTop(layer) - y;
	
  scrollLayerBy(layer, -dx, -dy, bound);
}

function scrollLayerBy(layer, dx, dy, bound) {

  var cl = getClipLeft(layer);
  var ct = getClipTop(layer);
  var cr = getClipRight(layer);
  var cb = getClipBottom(layer);

  if (bound) {
    if (cl + dx < 0)

      dx = -cl;

    else if (cr + dx > getWidth(layer))
      dx = getWidth(layer) - cr;
    if (ct + dy < 0)

      dy = -ct;

    else if (cb + dy > getHeight(layer))
      dy = getHeight(layer) - cb;
  }
	
  clipLayer(layer, cl + dx, ct + dy, cr + dx, cb + dy);
  moveLayerBy(layer, -dx, -dy);
}

//-----------------------------------------------------------------------------
// Layer background.
//-----------------------------------------------------------------------------

function setBgColor(layer, color) {

  if (HM_NS4)
    layer.bgColor = color;
  if ((HM_IE)||(HM_DOM))
    layer.style.backgroundColor = color;
}

function setBgImage(layer, src) {

  if (HM_NS4)
    layer.background.src = src;
  if ((HM_IE)||(HM_DOM))
    layer.style.backgroundImage = "url(" + src + ")";
}

//-----------------------------------------------------------------------------
// Layer utilities.
//-----------------------------------------------------------------------------
/*
function getLayer(name) {

  if (HM_NS4)
    return findLayer(name, document);
  if (HM_IE)
    return eval('document.all.' + name);
  if (HM_DOM)
	return (document.getElementById(name))
  
  return null;
}*/
	
function getLayer(name) {

  if (HM_NS4)
    return findLayer(name, document);
  else if (HM_DOM) 
			return (document.getElementById(name))
  		else 
  	  		if (HM_IE)
         		return eval('document.all.' + name);
  return null;
}

function findLayer(name, doc) {

  var i, layer;

  for (i = 0; i < doc.layers.length; i++) {
    layer = doc.layers[i];
    if (layer.name == name)
      return layer;
    if (layer.document.layers.length > 0) {
      layer = findLayer(name, layer.document);
      if (layer != null)
        return layer;
    }
  }

  return null;
}

//-----------------------------------------------------------------------------
// Window and page properties.
//-----------------------------------------------------------------------------

function getWindowWidth() {

  if (HM_NS4)
    return(window.innerWidth);
  if (HM_IE)
    return(document.body.clientWidth);
  if ((HM_DOM) && !(HM_IE))
	return(innerWidth);
  return(-1);
}

function getWindowHeight() {

  if (HM_NS4)
    return(window.innerHeight);
  if (HM_IE)
    return(document.body.clientHeight);
  if ((HM_DOM) && !(HM_IE))
	return(innerHeight);
  return(-1);
}

function getPageWidth() {

  if (HM_NS4)
    return(document.width);
  if ((HM_IE)||(HM_DOM))
    return(document.body.scrollWidth);
  return(-1);
}

function getPageHeight() {

  if (HM_NS4)
    return(document.height);
  if ((HM_IE)||(HM_DOM))
    return(document.body.scrollHeight);
  return(-1);
}

function getPageScrollX() {

  if (HM_NS4)
    return(window.pageXOffset);
  if ((HM_IE)||(HM_DOM))
    return(document.body.scrollLeft);
  return(-1);
}

function getPageScrollY() {

  if (HM_NS4)
    return(window.pageYOffset);
  if ((HM_IE)||(HM_DOM))
    return(document.body.scrollTop);
  return(-1);
}
