/****************************************************************
 * dhtml-lib.js                                                 *
 * ver 2.0                                                      *
 * last updated: 3/25/2002                                      *
 *                                                              *
 * Provides basic functionality for creating cross browser      *
 * DHTML pages                                                  *
 *                                                              *
 * Created By: R Jeremy Margaritondo                            *
 ****************************************************************/
 

//-----------------------------------------------------------------------------
// Determine Browser
//-----------------------------------------------------------------------------

//var document.all = (document.all)
//var document.layers = (document.layers)

//-----------------------------------------------------------------------------
// Layer Visibility
//-----------------------------------------------------------------------------

//  Netscape 6
function hideLayer(layer) {
  if (document.layers)
    document.layers[layer].visibility = 'hide'
  if (document.all)
    document.all[layer].style.visibility = 'hidden'
  if (document.getElementById)
    document.getElementById(layer).style.visibility = 'hidden'
}
//  Netscape 6
function showLayer(layer) {
  if (document.layers)
    document.layers[layer].visibility = 'show'
  if (document.all)
    document.all[layer].style.visibility = 'visible'
  if (document.getElementById)
    document.getElementById(layer).style.visibility = 'visible'
}

function getVisibility(layer) {
  if (document.layers) {
    if (document.layers[layer].visibility == 'show')
      return 'visible'
    if (document.layers[layer].visibility == 'hide')
      return 'hidden'
    return document.layers[layer].visibility
  }  
  if (document.all)
    return document.all[layer].style.visibility
  return ""
}

//-----------------------------------------------------------------------------
// Layer Positioning
//-----------------------------------------------------------------------------

function getLayer(layer) {
	if (document.layers){
		if (document.layers[layer])		  		
		return true
		else
		return false
	}
	if (document.all) {
		if (document.all[layer])
		return true
		else
		return false
	}
	if (document.getElementById) {
		if (document.getElementById(layer))
		return true
		else
		return false
	}
}

function getLayerLeft(layer) {
  if (document.layers)
    return document.layers[layer].left
  if (document.all)
    return document.all[layer].style.pixelLeft
}

function getLayerRight(layer) {
  return getLayerWidth(layer) + getLayerLeft(layer)
}

function getLayerTop(layer) {
  if (document.layers)
    return document.layers[layer].top
  if (document.all)
    return document.all[layer].style.pixelTop
}

function getLayerBottom(layer) {
  if (document.layers)
    return document.layers[layer].top + getLayerHeight(layer)
  if (document.all)
    return document.all[layer].style.pixelTop + getLayerHeight(layer)
}

function getLayerWidth(layer) {
  if (document.layers)
    return document.layers[layer].document.width
  if (document.all) {
    return document.all[layer].clientWidth
  }
}

function getLayerHeight(layer) {
  if (document.layers) {
    return document.layers[layer].clip.bottom - document.layers[layer].clip.top
  }
  if (document.all) {
    return document.all[layer].clientHeight
  }
}

function moveLayerTo(layer,x,y) {
  if (document.layers)
    document.layers[layer].moveTo(x,y)
  if (document.all) {
    document.all[layer].style.left = x
    document.all[layer].style.top = y
  }
}

function moveLayerBy(layer,amountX,amountY) {
  if (document.layers)
    document.layers[layer].moveBy(amountX,amountY)
  if (document.all) {
    document.all[layer].style.pixelLeft += amountX
    document.all[layer].style.pixelTop  += amountY
  }
  else if (document.getElementById){
    document.getElementById(layer).style.left = document.getElementById(layer).offsetLeft + amountX
	document.getElementById(layer).style.top  = document.getElementById(layer).offsetTop + amountY
  }
}

function getzIndex(layer) {
  if (document.layers)
    return document.layers[layer].zIndex
  if (document.all)
    return document.all[layer].style.zIndex
}

function setzIndex(layer,z) {
  if (document.layers)
    document.layers[layer].zIndex = z
  if (document.all)
    document.all[layer].style.zIndex = z
}

//-----------------------------------------------------------------------------
// Layer Clipping
//-----------------------------------------------------------------------------

//These clipping functions are not yet 100% stable in Internet Explorer.

function clipLayer(layer, clipleft, cliptop, clipright, clipbottom) {
  if (document.layers) {
				document.layers[layer].clip.left = clipleft
    document.layers[layer].clip.top = cliptop
    document.layers[layer].clip.right = clipright
    document.layers[layer].clip.bottom = clipbottom
  }
  if (document.all)
    document.all[layer].style.clip = 'rect(' + cliptop + ', ' +  clipright + ', ' + clipbottom + ', ' + clipleft +')';
}

function getClipLeft(layer) {
  if (document.layers)
    return document.layers[layer].clip.left
  if (document.all) {
    var str =  document.all[layer].style.clip
    if (!str)
						return 0
    var clip = getIEClipValues(document.all[layer].style.clip)
    return(clip[3])
  }
}

function getClipTop(layer) {
  if (document.layers)
    return document.layers[layer].clip.top
  if (document.all) {
    var str =  document.all[layer].style.clip
    if (!str)
      return 0
    var clip = getIEClipValues(document.all[layer].style.clip)
    return clip[0]
  }
}

function getClipRight(layer) {
  if (document.layers)
    return document.layers[layer].clip.right
  if (document.all) {
    var str =  document.all[layer].style.clip
    if (!str)
      return document.all[layer].style.pixelWidth
    var clip = getIEClipValues(document.all[layer].style.clip)
    return clip[1]
  }
}

function getClipBottom(layer) {
  if (document.layers)
    return document.layers[layer].clip.bottom
  if (document.all) {
    var str =  document.all[layer].style.clip
				if (!str)
      return document.all[layer].clientHeight
    var clip = getIEClipValues(document.all[layer].style.clip)
				return clip[2]
  }
}

function getClipWidth(layer) {
  if (document.layers)
    return document.layers[layer].clip.width
  if (document.all) {
    var str = document.all[layer].style.clip
    if (!str)
      return document.all[layer].style.pixelWidth
    var clip = getIEClipValues(document.all[layer].style.clip)
    return clip[1] - clip[3]
  }
}

function getClipHeight(layer) {
  if (document.layers)
    return document.layers[layer].clip.height
  if (document.all) {
  //  var str =  document.all[layer].style.clip
   // if (!str){
      return document.all[layer].clientHeight
	//  }
  //  var clip = getIEClipValues(document.all[layer].style.clip)
   // return clip[2] - clip[0]
  }
}

function getIEClipValues(str) {

  var clip = new Array()
  var i

  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 Utilities
//-----------------------------------------------------------------------------

function setLayerBgColor(layer,color) {
  if (document.layers)
    document.layers[layer].bgColor = color
  if (document.all)
    document.all[layer].style.backgroundColor = color
}
//  Netscape 6
function alignToImg(img,layer,offsetX,offsetY) {
  if (!offsetX)
    offsetX = 0
  if (!offsetY)
    offsetY = 0
  if (document.layers) {
	document.layers[layer].left = getImgPosX(img) + offsetX
	document.layers[layer].top = getImgPosY(img)  + offsetY
  }
  if (document.all) {
	document.all[layer].style.pixelLeft = getImgPosX(img) + offsetX
	document.all[layer].style.pixelTop = getImgPosY(img) + offsetY
  }
  else {
	  if (document.getElementById) {
		document.getElementById(layer).style.left = getImgPosX(img) + offsetX
		document.getElementById(layer).style.top = getImgPosY(img) + offsetY
	  }
  }
}

//  Netscape 6
function writeToLayer(layer,content) {
  if (document.layers) {
    document.layers[layer].document.open()
	document.layers[layer].document.write(content)
	document.layers[layer].document.close()
  }
  if (document.all)
    document.all[layer].innerHTML = content		
  if (document.getElementById)
    document.getElementById(layer).innerHTML = content		
}

//-----------------------------------------------------------------------------
// Image Utilities
//-----------------------------------------------------------------------------

//  Netscape 6
function getImgPosX(img) {
  var x
  if (document.layers)
    x = document.images[img].x
  if (document.all)
    x = document.images[img].offsetLeft
  if (document.getElementById)
    x = document.images[img].offsetLeft
  return x
}
//  Netscape 6
function getImgPosY(img) {
  var y
  
  if (document.layers)
    y = document.images[img].y
  if (document.all)
    y = document.images[img].offsetTop
  if (document.getElementById)
    y = document.images[img].offsetTop
  return y
}

//-----------------------------------------------------------------------------
// Window Utilities
//-----------------------------------------------------------------------------

function getWindowHeight() {
  if (document.layers) 
		  return window.innerHeight
		if (document.all)
		  return document.body.clientHeight
}

function getWindowWidth() {
  if (document.layers)
		  return window.innerWidth
		if (document.all)
		  return document.body.clientWidth
}

function getPageHeight() {
  if (document.layers)
		  return document.height
		if (document.all)
		  return document.body.scrollHeight
}

function getPageWidth() {
  if (document.layers)
		  return document.width
		if (document.all)
		  return document.body.scrollWidth
}

function getPageScrollX() {
  if (document.layers)
    return window.pageXOffset
  if (document.all)
    return document.body.scrollLeft
}

function getPageScrollY() {
  if (document.layers)
    return window.pageYOffset
  if (document.all)
    return document.body.scrollTop
}

//-----------------------------------------------------------------------------
// Resize Bug Fixes
//-----------------------------------------------------------------------------

var browser

function initResizeFix(pBrowser) {  
  if (pBrowser)
    browser = pBrowser
  if (!window.saveInnerWidth) {
    window.onresize = resizeFix
    window.saveInnerWidth = window.innerWidth
    window.saveInnerHeight = window.innerHeight
  }
}

function resizeFix() {
  if (saveInnerWidth < window.innerWidth || 
      saveInnerWidth > window.innerWidth || 
      saveInnerHeight > window.innerHeight || 
      saveInnerHeight < window.innerHeight)
  {
    window.history.go(0);
  }
  if (document.all && browser == 'all')
    window.history.go(0)
}
