/*
 * LyrObj - Lightweight DHTML library
 * Version 1.0, 20/11/2001
 * 
 * Copyright (c) 2001 by Netzministerium.de
 * Written by Rene Sander, Ingo Veith & Till Nagel
 * Distributed under the terms of the GNU Lesser General Public. (See licence.txt for details)
 */

// ---------------------------------------------------------------------------

// Simple browser object detection
var ie = document.all ? true : false;
var ns = document.layers ? true : false;
var ns6 = !ie && document.getElementById ? true : false;

// ---------------------------------------------------------------------------

/*
 * LyrObj
 * This class encapsulates a DIV element and provides cross-browser
 * access to its properties such as position, visibility, and clipping.
 * This implementation is the lean core class for working with layers.
 *
 */

/*
 * LyrObj
 * Constructs a new LyrObj wrapping the layer lyrName. If the layer
 * is nested within another, you have to specify the parent layer's name
 * in the nestedRef parameter, otherwise Netscape 4.5x won't be able
 * to find it in the document's object hierarchy.
 *
 * Parameters
 *   String lyrName - the name of the layer as in <div id="(layername)"></div>
 *   String [nestedRef] - path to a nested layer
 *
 * Returns
 *   a new LyrObj object for the layer lyrName
 */
function LyrObj(lyrName, nestedRef) {

	// assign id
  this.lyrname = lyrName;

	// if the nestedRef argument is supplied, this constructs the path for Netscape 4.x
	if (ns && nestedRef) {
		lyrName = nestedRef + ".document." + lyrName;
	}
	
	// get the layer's object reference
	this.ref = ns ? LyrObjGetRef(lyrName) : LyrObjGetRef(lyrName).style;
	
	// switch this on when you're trying to track down layer problems
  // if (!this.ref) { 	alert("Error: Couldn't access " + this.lyrname); }

	// assign object methods
	this.getPos = LyrObjGetPos;
	this.setPos = LyrObjSetPos;
	this.getX = LyrObjGetX;
	this.getY = LyrObjGetY;
	
	this.getClip = LyrObjGetClip;
	this.setClip = LyrObjSetClip;
	
	this.getVisibility = LyrObjGetVisibility;
	this.setVisibility = LyrObjSetVisibility;
	this.show = LyrObjShow;
	this.hide = LyrObjHide;
	this.moveTo = LyrObjMoveTo;
	this.moveBy = LyrObjMoveBy;
	
	this.getzIndex = LyrObjGetzIndex;
	this.setzIndex = LyrObjSetzIndex;
	
	this.toString = LyrObjToString;
	
	return this;
}

/*
 * LyrObj.getRef
 * returns the reference to the actual DIV element
 *
 * Parameters
 *   String layerName - the layer's name
 *
 * Returns
 *   object - layer reference
 */
function LyrObjGetRef(layerName) {
	if (ns) return eval("document." + layerName);
	if (ie) return eval("document.all." + layerName);
	if (ns6) return document.getElementById(layerName);
}

/*
 * LyrObj.getPos
 * Returns the current position of the layer, use the CSS
 * names, "left" for x and "top" for y.
 *
 * Parameters
 *   String which - "left" or "top" to retrieve the respective value
 *
 * Returns
 *   int - the value (without the "px" suffix)
 *
 */
function LyrObjGetPos(which) {
	if (ns) {
		return this.ref[which];
	}
	if (ie || ns6){
		return this.ref[which].split("px")[0];
	}
}	

/*
 * LyrObj.setPos
 * Sets the position of the layer, again, this works with
 * CSS names, "left" for x and "top" for y.
 *
 * Parameters
 *   String which - "left" or "top"
 *   int pos - the new value
 *
 */
function LyrObjSetPos(which, pos) {
	if (this.ref) this.ref[which] = pos;
}

/*
 * LyrObj.getClip
 * Returns the current clip value of the layer.
 * The value has to be set beforehand, otherwise
 * the result is undefined.
 *
 * Parameters
 *   String which - "left", "top", "right", "bottom"
 *
 * Returns
 *   int - the current clip value
 *
 */
function LyrObjGetClip(which) {

	if (ns) { return this.ref.clip[which]; }
	
	if (ie || ns6) {
		// alert("IE: " + this.lyrname + ".getClip\nclip=" + this.ref.clip);
		// strip 
		var clipPos = this.ref.clip.split("rect(")[1].split(")")[0].split("px");
		switch (which) {
			case "top" : return Number(clipPos[0]);
			case "right" : return Number(clipPos[1]);
			case "bottom" : return Number(clipPos[2]);
			case "left" : return Number(clipPos[3]);
		}
	}
}

/*
 * LyrObj.setClip
 * Sets the layer's clip rect property to the
 * rectangle (left, top, right, bottom).
 *
 * Parameters
 *   int left - left clip
 *   int top - top clip
 *   int right - right clip
 *   int bottom - bottom clip
 *
 */
function LyrObjSetClip(left, top, right, bottom){
	if (ns){
		this.ref.clip.top = top;
		this.ref.clip.right = right;
		this.ref.clip.bottom = bottom;
		this.ref.clip.left = left;
	}
	if (ie || ns6)
		this.ref.clip = "rect(" + top +"px " + right +"px " + bottom +"px " + left +"px)";
}

/*
 * LyrObj.getVisibility
 * Returns the layer's current visibility
 *
 * Returns
 *   True if it's visible
 *
 */
function LyrObjGetVisibility() {
	v = this.ref.visibility;
	return v.indexOf("hid") == -1;
}

/*
 * LyrObj.setVisibility
 * Sets the layer's visibility 
 *
 * Parameters
 *   bool visible - true to make it visible, false to hide it
 *
 */
function LyrObjSetVisibility(visible){
	if (ns)	this.ref.visibility = visible ? "show" : "hide";
	if (ie || ns6) this.ref.visibility = visible ? "visible" : "hidden";
}

/*
 * LyrObj.show
 * Shows the layer. Shortcut to setVisibility(true).
 *
 */
function LyrObjShow() {
	this.setVisibility(true);
}

/*
 * LyrObj.hide
 * Hides the layer. Shortcut to setVisibility(false).
 *
 */
function LyrObjHide() {
	this.setVisibility(false);
}

/*
 * LyrObj.getzIndex
 * Returns the layer's current z-Index.
 *
 * Returns
 *   int - the layer's z-Index
 *
 */
function LyrObjGetzIndex() {
	return this.ref.zIndex;
}

/*
 * LyrObj.setzIndex
 * Sets the layer's z-Index.
 *
 * Parameters
 *   int zIndex
 *
 */
function LyrObjSetzIndex(zIndex){
	if (this.ref) this.ref.zIndex = zIndex;
}

/*
 * LyrObj.getX
 * Returns the current x position of the layer.
 * Shortcut to getPos("left").
 *
 * Returns
 *   int x
 *
 */
function LyrObjGetX() {
	return this.getPos("left");
}

/*
 * LyrObj.getY
 * Returns the current y position of the layer.
 * Shortcut to getPos("top").
 *
 * Returns
 *   int y
 *
 */
function LyrObjGetY() {
	return this.getPos("top");
}

/*
 * LyrObj.toString
 * Returns a string representation of the current LyrObj.
 * The html parameter determines the kind of line breaks:
 * true for HTML line breaks,
 * false for normal text.
 *
 * Parameters
 *   bool html - true=HTML, false=TEXT
 *
 * Returns
 *   String - the layer's position, z-Index, clipping and visibility
 *
 */
function LyrObjToString(html) {
	c = (html != null && html) ? "<br>" : "\n" ;
	return this.lyrname + c +
		"pos : (x:" + this.getX() + ", y:" + this.getY() + ", z:" + this.getzIndex() + ")" + c +
		"clip : (left:" + this.getClip("left") + ", top:" +
						this.getClip("top") + ", right:" +
						this.getClip("right") + ", bottom:" +
						this.getClip("bottom") + ")" + c +
		"vis : " + this.getVisibility();
}

/*
 * LyrObj.moveTo
 * Sets the layer to the target position (x, y).
 *
 * Parameters
 *   int x - target x position
 *   int y - target y position
 *
 */
function LyrObjMoveTo(x, y) {
	this.setPos("left", x);
	this.setPos("top", y);
}

/*
 * LyrObj.moveBy
 * Moves the layer by the specified diffences, relative
 * to its current position.
 *
 * Parameters
 *   int dx - x amount
 *   int dy - y amount
 *
 */
function LyrObjMoveBy(dx, dy) {
	this.setPos("left",  1 * this.getPos("left") + dx);
	this.setPos("top",  1 * this.getPos("top") + dy);
}

/*
 * fixNetscape
 *
 * Intention: After starting Netscape 4.x, the first
 * call to the constructor fails, throwing an error.
 * 
 * This is a fix that simply causes a reload of the
 * page in case the test layer couldn't be accessed.
 * To apply, make sure the page contains the line
 * <div id="fixnetscape" style="position:absolute;visibility:hidden"></div>
 * and call this function in your onLoad-handler.
 *
 */
function fixNetscape() {
	if (!LyrObjGetRef("fixnetscape")) document.location.reload();
}



Syntax highlighted by Code2HTML, v. 0.9, modified by Netzministerium, 2001.