/**********************************************************************
	BioWare Javascript File
	
	File: layer_functions.js
	
	Description: Layer Functions - references browser detection variables from HMBio_Loader.js
	
	Created: Robin Mayne, April 11, 2002 Copyright 2002 BioWare Corp.
	Based on "Layer functions" code by Mike Hall http://www.dynamicdrive.com 
***********************************************************************	
	Version: 0.0.2
	
	Version History:
		0.0.1 / 11.04.02 / Created / RM
						PC browsers tested:
						IE 6.0, IE 5.5, IE 4.72, Opera 5.11, Opera 6.0, NS 4.08, NS 4.78, NS 6.21, Mozilla 0.98
						MAC browsers tested:
						IE 5.0, IE 4.5, Opera 5.0, NS 4.78, NS 6.1
		0.0.2 / 16.04.02 / Updated / RM
						Added setWidth() function for IE and NS6

***********************************************************************/

//-----------------------------------------------------------------------------
// Layer utilities.
//-----------------------------------------------------------------------------

function getLayer(name) {
	  if (HM_NS4) 
	    return findLayer(name, document);
	  if (HMBio_NS6) 
		return document.getElementById(name); 
	  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;
}

//-----------------------------------------------------------------------------
// Layer visibility.
//-----------------------------------------------------------------------------

function hideLayer(layer) {
	  if (HM_NS4) 
	    layer.visibility = "hide";
	  if (HM_IE || HMBio_NS6) 
	    layer.style.visibility = "hidden";
}

function showLayer(layer) {
	  if (HM_NS4) 
	    layer.visibility = "show";
	  if (HM_IE || HMBio_NS6) 
	  	layer.style.visibility = "visible";
}

//-----------------------------------------------------------------------------
// Layer positioning.
//-----------------------------------------------------------------------------

function moveLayerTo(layer, x, y) {
		if (HM_NS4) 
		    layer.moveTo(x, y);
		if (HM_IE || HMBio_NS6) { 
		    layer.style.left = x;
		    layer.style.top = y;
	   }
}

function moveToShow(name, x, y) { // NOTE - function uses getLayer() to retreive layer object
	moveLayerTo(getLayer(name), x, y);
	showLayer(getLayer(name));
	eval(name + "vis = 1"); // sets a visibility flag to 1
}

function moveToHide(name, x, y) { // NOTE - function uses getLayer() to retreive layer object
	moveLayerTo(getLayer(name), x, y);
	hideLayer(getLayer(name));
	eval(name + "vis = 0"); // sets a visibility flag to 0
}


function moveLayerBy(layer, dx, dy) {

  if (HM_NS4)
    layer.moveBy(dx, dy);
  if (HMBio_NS6) {
  	var xlocation = parseInt(layer.style.left);
	var ylocation = parseInt(layer.style.top);
	xlocation += dx;
	ylocation += dy;
  	layer.style.left = xlocation;
	layer.style.top = ylocation;
  }    
  if (HM_IE) {
    layer.style.pixelLeft += dx;
    layer.style.pixelTop  += dy;
  }
}

function getLeft(layer) {

  if (HM_NS4)
    return(layer.left);
  if (HMBio_NS6)
  	return(parseInt(layer.style.left));
  if (HM_IE)
    return(layer.style.pixelLeft);
  return(-1);
}

function getTop(layer) {

  if (HM_NS4)
    return(layer.top);
  if (HMBio_NS6)
  	return(parseInt(layer.style.top));
  if (HM_IE)
    return(layer.style.pixelTop);
  return(-1);
}

function setWidth(layer, w) {
	if (!HM_Opera && (HM_IE || HMBio_NS6)) {
		layer.style.width = w;
	}
}

/******************************************************************************
* End Layer functions                                                         *
******************************************************************************/

//////////////////////////
// Element Positioning and Offsets
/////////////////////////

// Obtain X coordinate of imageName
function findX(imageName) {
	var objImg
	objImg = document.images[imageName]
	if (HM_NS4){		
		if (document.images[imageName]) { // Test for image in document object
			return eval(objImg).x
		} else { // Fix for Netscape 4 div bug - find image in document.layers object instead (will NOT work for nested divs)			
			for (a = 0; a < document.layers.length; a++) {
				if (document.layers[a].document.images[imageName]) {
					xtotal = document.layers[a].left + document.layers[a].document.images[imageName].x;
					return xtotal;
				}
			}
		}
	} else {
		return getXPosition(objImg);
	}
}

// Called by findX()
function getXPosition(imgElem) {
	xPos = eval(imgElem).offsetLeft;
	tempEl = eval(imgElem).offsetParent;
  	while (tempEl != null) {
  		xPos += tempEl.offsetLeft;
  		tempEl = tempEl.offsetParent;
  	}
	return xPos;
}		

// Obtain Y coordinate of imageName
function findY(imageName) {
	var objImg
	objImg = document.images[imageName]
	if (HM_NS4){		
		if (document.images[imageName]) { // Test for image in document object
			return eval(objImg).y
		} else { // Fix for Netscape 4 div bug - find image in document.layers object instead (will NOT work for nested divs)			
			for (a = 0; a < document.layers.length; a++) {
				if (document.layers[a].document.images[imageName]) {
					ytotal = document.layers[a].top + document.layers[a].document.images[imageName].y;
					return ytotal;
				}
			}			
		}
		
	} else {
		return getYPosition(objImg);
	}
}

// Called by findY()
function getYPosition(imgElem) {
	yPos = eval(imgElem).offsetTop;
	tempEl = eval(imgElem).offsetParent;
  	while (tempEl != null) {
  		yPos += tempEl.offsetTop;
  		tempEl = tempEl.offsetParent;
  	}
	return yPos;
}

//////////////////////////
// End Element Positioning and Offsets
/////////////////////////

// Functions needed to be updated to new detection and browser support:
function getWindowWidth() {

  if (HM_NS4 || HMBio_NS6 || HM_Opera)
	return(window.innerWidth);
  if (HM_IE)
	return(document.body.clientWidth);
  return(-1);
}