function layerobj(objname, debug) {
   this.name = objname;
   this.bwis = new checkBrowser();
   
   if(!debug) {
	   this.debug = false;
   } else {
	   this.debug = debug;
   }
   
   this._layer = getObjectRef(this.name, this.bwis);
   if (this.debug){alert("Anonymous constructor created.  Returning..");}
   return this;
}

layerobj.prototype.show = _layerobj_show;
layerobj.prototype.hide = _layerobj_unshow;

layerobj.prototype.moveTo = _layerobj_moveTo;
layerobj.prototype.moveBy = _layerobj_moveBy;

layerobj.prototype.slideTo = _layerobj_slideTo;

layerobj.prototype.moveDepth = _layerobj_zmove;
layerobj.prototype.setDepth = _layerobj_zset;

layerobj.prototype.depth = _layerobj_getdepth;
layerobj.prototype.size = _layerobj_getsize;
layerobj.prototype.location = _layerobj_getlocation;
layerobj.prototype.visibility = _layerobj_getvisibility;

layerobj.prototype.getDomObject = _layerobj_getDomObject;

layerobj.prototype.dumpdebug = _layerobj_debuginfo;



// =====================
// visibility controls 
// =====================

function _layerobj_show() {
	if(this.debug){alert("Show Layer function was called on " + this._layer);}
	
	if(this.bwis.dom) {
		this._layer.style.visibility = "visible";
	} else if(this.bwis.ns4) {
		this._layer.visibility = "show";
	} else {
		this._layer.style.visibility = "visible";
	}
	
	return true;
}

function _layerobj_unshow() {
	if(this.debug){alert("Hide Layer function was called on " + this._layer);}
	
	if(this.bwis.dom) {
		this._layer.style.visibility = "hidden";
	} else if(this.bwis.ns4) {
		this._layer.visibility = "hide";
	} else {
		this._layer.style.visibility = "hidden";
	}
	
	return true;
}



// =====================
// movement / positioning controls 
// =====================

function _layerobj_moveTo(xcoord,ycoord) {
	if(this.debug){alert("MoveTo Layer function was called");}
	if (!xcoord) {xcoord = 0;}
	if (!ycoord) {ycoord = 0;}
	
	if(this.debug){alert("moving to (" + xcoord + ", " + ycoord + ").\n");}
	
	if(this.bwis.dom) {
		if(this.debug){alert('using DOM relo commands');}
		this._layer.style.left = xcoord;
		this._layer.style.top = ycoord;
	} else if (this.bwis.ns4) {
		if(this.debug){alert('using ns relo commands');}
		this._layer.moveToAbsolute(xcoord, ycoord);
	} else {
		if(this.debug){alert('using ie relo commands');}
		this._layer.style.left = xcoord;
		this._layer.style.top = ycoord;
	}
	return true;
}

function _layerobj_moveBy(dx,dy) {
	if(this.debug){alert("MoveBy Layer function was called");}
	if (!dx) {dx = 0;}
	if (!dy) {dy = 0;}
	if(this.debug){alert("moving by (" + dx + ", " + dy + ").\n");}
	
	var myloc;
	myloc = this.location();
	return this.moveTo(parseInt(dx) + myloc[0], parseInt(dy) + myloc[1]);
}


function _layerobj_slideTo(xcoord, ycoord, interval, speed) {
	if(this.debug){alert("slideTo Layer function was called");}
	var curLayerPos, totaldx, totaldy, dx, dy;
	
	// read variables and set defaults if not specified
	if (!xcoord) {xcoord = 0;}
	if (!ycoord) {ycoord = 0;}
	if (!interval) {interval = 10;}
	if (!speed) {speed = 200;}
	
	// guarantee our args are numeric
	xcoord = parseInt(xcoord);
	ycoord = parseInt(ycoord);
	interval = parseInt(interval);
	speed = parseInt(speed);
	
	
	if(this.debug){alert("sliding to (" + xcoord + ", " + ycoord + ").\n");}
	
	// find out where we are, and plot the course to the target
	curLayerPos = this.location();
	totaldx = xcoord - curLayerPos[0];
	totaldy = ycoord - curLayerPos[1];
	
	if(totaldx == 0 && totaldy == 0) {
		// no movement needed.
		dx = 0;
		dy = 0;
	} else if(totaldx == 0) {
		if(this.debug){alert("total dX is 0");}
		// only need movement in y direction
		dx = 0;
		if(curLayerPos[1] > ycoord) {
			// we're above the target, move down
			dy = -1 * interval;
		} else {
			// we're below the target, move up
			dy = interval;
		}
	} else if(totaldy == 0) {
		if(this.debug){alert("total dY is 0");}
		// only need movement in x direction
		dy = 0;
		if(curLayerPos[0] > xcoord) {
			// we're to the left of the target, move right
			dx = -1 * interval;
		} else {
			// we're to the right of the target, move left
			dx = interval;
		}
	} else {
		if(this.debug){alert("Calculating vectors:");}
		// movement in both vectors
		var slope;
		slope = totaldy/totaldx;
		if(this.debug){alert("slope is:" + slope);}
		
		// determine new coordinates along the hypotenuse
		dx = parseInt(Math.sqrt(Math.pow(interval,2) / (1 + Math.pow(slope,2))));
		if(curLayerPos[0] > xcoord) {
			dx = -1 * dx;
		}
		dy = parseInt(dx * slope);
		
		
		
		if(this.debug){alert("calculated dx is:" + dx + " and dy is:" + dy);}
	}
	
	
	// overshot protection, if we moved past the target, just move to the target on either axis
	
	// protection for x coordinate
	if(curLayerPos[0] == xcoord) {
		// do nothing, they match.
	} else if(curLayerPos[0] > xcoord) {
		// we started to the right.  did we move to the left?
		if(curLayerPos[0] + dx < xcoord) {
			if(this.debug){alert("detected X overshot from right to left");}
			dx = xcoord - curLayerPos[0];
		}
	} else {
		// we started to the left.  did we move to the right?
		if(curLayerPos[0] + dx > xcoord) {
			if(this.debug){alert("detected X overshot from left to right");}
			dx = xcoord - curLayerPos[0];
		}
	}
	// protection for y coordinate
	if(curLayerPos[1] == ycoord) {
		// do nothing, they match.
	} else if(curLayerPos[1] > ycoord) {
		// we started below.  did we move above?
		if(curLayerPos[1] + dy < ycoord) {
			if(this.debug){alert("detected Y overshot from below to above");}
			dy = ycoord - curLayerPos[1];
		}
	} else {
		// we started above.  did we move below?      
		if(curLayerPos[1] + dy > ycoord) {
			if(this.debug){alert("detected Y overshot from above to below");}
			dy = ycoord - curLayerPos[1];
		}
	}
	
	this.moveBy(dx,dy);
	
	if (dx != 0 || dy != 0) {
		if(this.debug){alert("rescheduling since dx = " + dx + " and dy = " + dy + ".");}
		if(document._layerobj_sliders == undefined) {
			document._layerobj_sliders = new Array();
		}
		document._layerobj_sliders[this.name] = this;
		setTimeout("document._layerobj_sliders['" + this.name + "'].slideTo(" + xcoord + "," + ycoord + "," + interval + "," + speed + ");", speed);
	} else {
		if(this.debug){alert("move complete.");}
		document._layerobj_sliders[this.name] = null;
	}
	
	return true;
}


// =====================
// depth controls
// =====================

function _layerobj_zset(zdepth) {
	if(this.debug){alert("set position on Z axis called");}
	if (!zdepth) {zdepth = 0;}
	if (zdepth < 0) {zdepth = 0;}
	
	if(this.debug){alert("setting zaxis to (" + zdepth + ").\n");}
	
	if(this.bwis.dom) {
		if(this.debug){alert('using DOM z-axis commands');}
		this._layer.style.zIndex = zdepth;
	} else if (this.bwis.ns4) {
		if(this.debug){alert('Netscape does not move layers up and down, sorry.');}
		return false;
	} else {
		if(this.debug){alert('using ie relo commands');}
		this._layer.style.zIndex = zdepth;
	}
	return true;
}

function _layerobj_zmove(dz) {
	if(this.debug){alert("move on Z axis called");}
	if (!dz) {dz = 0;}
	
	if(this.debug){alert("moving up zaxis by (" + dz + ").\n");}
	
	return this.setDepth(parseInt(dz) + parseInt(this.depth()));
}



// =====================
// stats retrieval
// =====================

function _layerobj_getlocation() {
	return getlayerlocation(this._layer);
}

function _layerobj_getsize() {
	return getsize(this._layer);
}

function _layerobj_getdepth() {
	return getdepth(this._layer);
}

function _layerobj_getvisibility() {
	return getvisibility(this._layer);
}

function _layerobj_getDomObject() {
	return this._layer;
}



// =====================
// generic internal utilities
// =====================

function getObjectRef(objname, bwis) {
   if (bwis.ns4) {
      if(this.debug){alert("Detected NS4 and allocating appropriate object");alert(document.eval(objname));}
      return document.eval(objname);
   } else if (bwis.dom) {
      if(this.debug){alert("Detected w3c-compliant browser with getElementByID and allocating appropriate object");}
      return document.getElementById(objname);
   } else if (bwis.bw) {
      if(this.debug){alert("Detected an old IE with document.all and allocating appropriate object");}
      return document.all(objname);
   } else {
      if(this.debug){alert("Detected an non-DHTML browser.  Not allocating any object.");}
      return false;
   }
}

var checkBrowserCache;
function checkBrowser(){
	// borrowed from thomas bratta (bratta.com)
	// in the cool menus script
	if(!checkBrowserCache) {
		checkBrowserCache = new Array();
		checkBrowserCache["ver"] = navigator.appVersion;
		checkBrowserCache["agent"] = navigator.userAgent;
		checkBrowserCache["dom"] = document.getElementById?1:0;
		checkBrowserCache["opera5"] = (navigator.userAgent.indexOf("Opera")>-1 && document.getElementById)?1:0;
		checkBrowserCache["ie5"] = (checkBrowserCache["ver"].indexOf("MSIE 5")>-1 && checkBrowserCache["dom"] && !checkBrowserCache["opera5"])?1:0; 
		checkBrowserCache["ie6"] = (checkBrowserCache["ver"].indexOf("MSIE 6")>-1 && checkBrowserCache["dom"] && !checkBrowserCache["opera5"])?1:0;
		checkBrowserCache["ie4"] = (document.all && !checkBrowserCache["dom"] && !checkBrowserCache["opera5"])?1:0;
		checkBrowserCache["ie"] = checkBrowserCache["ie4"]||checkBrowserCache["ie5"]||checkBrowserCache["ie6"];
		checkBrowserCache["mac"] = checkBrowserCache["agent"].indexOf("Mac")>-1;
		checkBrowserCache["ns6"] = (checkBrowserCache["dom"] && parseInt(checkBrowserCache["ver"]) >= 5) ?1:0; 
		checkBrowserCache["ns4"] = (document.layers && !checkBrowserCache["dom"])?1:0;
		checkBrowserCache["bw"] = (checkBrowserCache["ie6"] || checkBrowserCache["ie5"] || checkBrowserCache["ie4"] || checkBrowserCache["ns4"] || checkBrowserCache["ns6"] || checkBrowserCache["opera5"]);
	}
	
	this.ver=checkBrowserCache["ver"];
	this.agent=checkBrowserCache["agent"];
	this.dom=checkBrowserCache["dom"];
	this.opera5=checkBrowserCache["opera5"];
	this.ie5=checkBrowserCache["ie5"]; 
	this.ie6=checkBrowserCache["ie6"];
	this.ie4=checkBrowserCache["ie4"];
	this.ie=checkBrowserCache["ie"];
	this.mac=checkBrowserCache["mac"];
	this.ns6=checkBrowserCache["ns6"]; 
	this.ns4=checkBrowserCache["ns4"];
	this.bw=checkBrowserCache["bw"];
	
	return this;
}

function _layerobj_debuginfo() {
   var debugtext = "";
   debugtext = debugtext + "Layer name for this object is " + this.name + "\n";
   debugtext = debugtext + "Detected browser is " + this.bwis.ver + " with DOM of " + this.bwis.dom + "\n";   
   debugtext = debugtext + "Type of the child layer object is  " + typeof(this._layer) + "\n";
   debugtext = debugtext + "Children of this object are:\n" ;
   for (i in this) {
      debugtext = debugtext + "   "+ i + ": " + this[i] + "\n";
   }
   
   debugtext = debugtext + "Children of the layer oject are: :\n" ;
   for (i in this._layer) {
      debugtext = debugtext + "   "+ i + ": " + this._layer[i] + "\n";
   }
   return debugtext;
}

