
/**
 * Layer 1.1
 *
 * by Aloys Maue (aloys.maue@javaxml.org), March 2001
 *
 * Feel free to use this code (see copyright notices below).
 * We would appreciate your modifications will be emailed to us.
 * Latest versions will	be published at
 * http://www.javaxml.org
 *
 * Copyright (c) 2000-2001 javaxml.org
 *
 * We grant you a royalty free license to use or modify this
 * software provided that THIS COPYRIGHT NOTICE APPEARS ON ALL COPIES.
 * This software is provided "AS IS", without a warranty of any kind.
 *
 * This script should be "cross browser compatible"
 * It is testet on Netscape	4.03, 4.05, 4.07, 4.51, 4.7, 6 and InternetExplorer 4.0, 5.0 and 5.5
 * Browser which do not support layers like Netscape versions below 4
 * and InternetExplorer	versions below 4 causes	no errors and they ignore the functions.
 *
 * Layer offers the possibility to access layers and change their properties.
 * This	script is very easy to use. Don't care about browsers and versions anymore!
 *
 * New in version 1.1: - Netscape 6	and	IE 5.5 BugFixes
 *
 */
	function screenWidth() {
		if (window.screen)
			return window.screen.width;
		return 0;
	}
	function screenHeight() {
		if (window.screen)
			return window.screen.height;
		return 0;
	}
	function windowWidth() {
		if (window.innerWidth)
			return window.innerWidth;
		else if (document.all)
			return document.body.clientWidth;
		return 0;
	}
	function windowHeight() {
		if (window.innerHeight)
			return window.innerHeight;
		else if (document.all)
			return document.body.clientHeight;
		return 0;
	}
	function documentLeft(doc) {
		if (doc == null)
			doc = document;
		if (doc.all)
			return document.body.scrollLeft;
		else {
			return pageXOffset;
		}
	}
	function documentTop(doc) {
		if (doc == null)
			doc = document;
		if (doc.all)
			return document.body.scrollTop;
		else {
			return pageYOffset;
		}
	}
	function documentWidth(doc) {
		if (doc == null)
			doc = document;
		if (doc.width)
			return doc.width;
		else if (document.all)
			return doc.body.scrollWidth;
		return 0;
	}
	function documentHeight(doc) {
		if (doc == null)
			doc = document;
		if (doc.height)
			return doc.height;
		else if (document.all)
			return doc.body.scrollHeight;
		return 0;
	}
	/**
	 * The layer access functions
	 * first parameter is always the layer to which the function should be applied.
	 * This could be a name (as String) or the layer object itself.
	 * other parameters can be ommitted.
	 * parameters which	should be ommitted in between can be set to null:
	 * fi.
	 * layerShow(myLayer, null, null, "status text");
	 * This would result in showing the layer and displaying status text
	 * but without moving the layer
	 */
	// depreciated, use layerShow
	function showLayer(aLayer, x, y, text) {
		return layerShow(aLayer, x, y, text);
	}
	// depreciated, use layerHide
	function hideLayer(aLayer) {
		return layerHide(aLayer, null, null, " ");
	}
	function layerShow(aLayer, x, y, text) {
		var lay = getLayer(aLayer);
		if (lay == null) return false;
		layerMoveTo(lay, x, y);
		var obj = lay.style? lay.style : lay;
		obj.visibility = document.layers ? "show" : "visible";
		if (text) self.status = text;
		return true;
	}
	function layerHide(aLayer, x, y, text) {
		var lay = getLayer(aLayer);
		if (lay == null) return false;
		layerMoveTo(lay, x, y);
		var obj = lay.style? lay.style : lay;
		obj.visibility = "hidden";
		if (text) self.status = text;
		return true;
	}
	function layerMoveTo(aLayer, x, y) {
		var lay = getLayer(aLayer);
		if (lay == null) return false;
		var obj = lay.style? lay.style : lay;
		if (x) obj.left = x;
		if (y) obj.top = y;
		return true;
	}
	function layerMoveBy(aLayer, x, y) {
		var lay = getLayer(aLayer);
		if (lay == null) return false;
		if (x) layerMoveTo(lay, layerLeft(lay) + x, null);
		if (y) layerMoveTo(lay, null, layerTop(lay) + y);
		return true;
	}
	function layerLeft(aLayer, x) {
		var lay = getLayer(aLayer);
		if (lay == null) return 0;
		layerMoveTo(lay, x);
		if (document.layers) {
			return lay.left;
		} else if (document.getElementById) {
			return lay.offsetLeft;
		} else if (document.all) {
			return (lay.style.posLeft != 0)? lay.style.posLeft : lay.offsetLeft;
		}
		return 0;
	}
	function layerTop(aLayer, y) {
		var lay = getLayer(aLayer);
		if (lay == null) return 0;
		layerMoveTo(lay, null, y);
		if (document.layers) {
			return lay.top;
		} else if (document.getElementById) {
			return lay.offsetTop;
		} else if (document.all) {
			return (lay.style.posTop != 0)? lay.style.posTop : lay.offsetTop;
		}
		return 0;
	}
	/**
	 * Setting the width smaller than the layer is
	 * would result in different results for different browsers!
	 * Netscape 4.x returns the width of the content of the layer,
	 * not the width specified in a style sheet!
	 */
	function layerWidth(aLayer, w) {
		var lay = getLayer(aLayer);
		if (lay == null) return 0;
		if (lay.style) {
			if (w) lay.style.width = w;
			return lay.style.pixelWidth? lay.style.pixelWidth : lay.offsetWidth;
		} else {
			// This changes previous clipped layers in Netscape 4.x!!! workaround needed
			if (w) lay.clip.width = w;
			return lay.clip.width;
		}
		return 0;
	}
	/**
	 * Setting the height smaller than the layer is
	 * would result in different results for different browsers!
	 */
	function layerHeight(aLayer, h) {
		var lay = getLayer(aLayer);
		if (lay == null) return 0;
		if (lay.style) {
			if (h) lay.style.height = h;
			return lay.style.pixelHeight? lay.style.pixelHeight : lay.offsetHeight;
		} else {
			// This changes previous clipped layers in Netscape 4.x!!! workaround needed
			if (h) lay.clip.height = h;
			return lay.clip.height;
		}
		return 0;
	}
	function layerClipLeft(aLayer, x) {
		var lay = getLayer(aLayer);
		if (lay == null) return 0;
		if (x) layerClip(lay, x, null, null, null);
		if (document.layers) {
			return lay.clip.left;
		} else {
			return clipRect(lay, 0);
		}
		return 0;
	}
	function layerClipTop(aLayer, y) {
		var lay = getLayer(aLayer);
		if (lay == null) return 0;
		if (y) layerClip(lay, null, y, null, null);
		if (document.layers) {
			return lay.clip.top;
		} else {
			return clipRect(lay, 1);
		}
		return 0;
	}
	function layerClipWidth(aLayer, w) {
		var lay = getLayer(aLayer);
		if (lay == null) return 0;
		if (w) layerClip(lay, null, null, w, null);
		if (document.layers) {
			return lay.clip.width;
		} else {
			return clipRect(lay, 2);
		}
		return 0;
	}
	function layerClipHeight(aLayer, h) {
		var lay = getLayer(aLayer);
		if (lay == null) return 0;
		if (h) layerClip(lay, null, null, null, h);
		if (document.layers) {
			return lay.clip.height;
		} else {
			return clipRect(lay, 3);
		}
		return 0;
	}
	/**
	 * parses the layer clip string into usable values:
	 * parameter str: "rect(tpx rpx bpx lpx)" is rect string
	 * with (t = top, r = right, b = bottom, l = left)
	 * returns in case of parameter pos
	 * 0: left, 1: top, 2: width, 3: height;
	 * return values are all relative to the layer position
	 */
	function clipRect(aLayer, pos) {
		var lay = getLayer(aLayer);
		if (lay == null) return 0;
		var obj = lay.style? lay.style : lay;
		var str = obj.clip;
			// minlength = "rect(0px 0px 0px 0px)".length = 21
		if (str == null || str.length < 21 || pos == null) return 0;
		var arr = new Array();
			 // lengths: "rect(" = 5, ")" = 1
		str = str.substring(5, str.length - 1);
		arr = str.split(" ");
		var l = ("auto" == arr[3])? 0 : parseInt(arr[3]);
		var t = ("auto" == arr[0])? 0 : parseInt(arr[0]);
		var w = (("auto" == arr[1])? layerWidth(lay) : parseInt(arr[1])) - l;
		var h = (("auto" == arr[2])? layerHeight(lay) : parseInt(arr[2])) - t;
		arr = null;
		if (pos == 0) return l;
		if (pos == 1) return t;
		if (pos == 2) return w;
		if (pos == 3) return h;
		return 0;
	}
	function layerClip(aLayer, x, y, w, h) {
		var lay = getLayer(aLayer);
		if (lay == null) return false;
		var obj = lay.style? lay.style : lay;
		if (document.layers) {
			if (x != null) obj.clip.left = x;
			if (y != null) obj.clip.top = y;
			if (w != null) obj.clip.width = w;
			if (h != null) obj.clip.height = h;
		} else {
			var l = (x == null)? "auto" : x + "px";
			var t = (y == null)? "auto" : y + "px";
			var r = (w == null)? "auto" : (x + w) + "px";
			var b = (h == null)? "auto" : (y + h) + "px";
			obj.clip = 'rect(' + t + ' ' + r + ' ' + b + ' ' + l + ')';
		}
		return true;
	}

	mouseX = 1;
	mouseY = 1;
	layClickedX = 1;
	layClickedY = 1;
	layZIndex = 0;
	layDragged = null;

	function Dragable(aLayer) {
		var lay = getLayer(aLayer);
		if (lay == null) return null;
		if(document.captureEvents) {
			document.captureEvents(Event.MOUSEMOVE | Event.MOUSEDOWN | Event.MOUSEUP);
		}
		this.lay = lay;
		lay.mouse = mouse;
		//auskommentiert damit im Katalog der Layer immer richtig positioniert werden kann
		//lay.onmouseover = dragableOver;
		//lay.onmouseout = dragableOut;
		lay.onmousedown = dragableDown;
		lay.onmousemove = dragableMove;
		lay.onmouseup = dragableUp;
		lay.isDragged = false;
		return this;
	}
	function dragableOver() {
		if (layDragged == null) {
			document.onmousedown = null;
			if (document.releaseEvents) {
				document.releaseEvents(Event.MOUSEDOWN | Event.MOUSEUP | Event.MOUSEMOVE);
			}
			if (document.captureEvents) {
				this.captureEvents(Event.MOUSEDOWN | Event.MOUSEUP | Event.MOUSEMOVE);
			}
		}
	}
	function dragableOut() {
		if (layDragged == null) {
			document.onmousedown = mouseDown;
			if (document.layers) {
				this.releaseEvents(Event.MOUSEDOWN | Event.MOUSEUP | Event.MOUSEMOVE);
			}
		}
	}
	function dragableMove(e) {
		this.mouse(e);
		if (this == layDragged) {
			layerLeft(this, mouseX - layClickedX);
			layerTop(this, mouseY - layClickedY);
		}
		return false;
	}
	function dragableDown(e) {
		layDragged = this;
		this.mouse(e);
		layClickedX = mouseX - layerLeft(this);
		layClickedY = mouseY - layerTop(this);
		layZIndex = Math.max(layerZIndex(this), layZIndex + 1);
		layerZIndex(this, layZIndex);
		this.x0 = mouseX - layerLeft(this);
		this.y0 = mouseY - layerTop(this);
	}
	function dragableUp() {
		layDragged = null;
	}

	function mouse(e){
		if (document.all) {
			mouseX = event.x;
			mouseY = event.y;
		} else if (document.layers || document.getElementById) {
			mouseX = e.pageX;
			mouseY = e.pageY;
		}
	}

	function layerZIndex(aLayer, z) {
		var lay = getLayer(aLayer);
		if (lay == null) return false;
		if (lay.style) {
			if (z) lay.style.zIndex = z;
			return lay.style.zIndex;
		} else {
			if (z) lay.zIndex = z;
			return lay.zIndex;
		}
		return 0;
	}

	function layerFGColor(aLayer, c) {
		var lay = getLayer(aLayer);
		if (lay == null) return false;
		if (lay.style) {
			if (c) lay.style.color = c;
			return lay.style.color;
		} else {
			if (c) lay.document.fgColor	= c;
			// without effect in current content in Netscape 4.x
			// content has to be rewritten to have an effect
			return lay.document.fgColor;
		}
		return false;
	}
	function layerBGColor(aLayer, c) {
		var lay = getLayer(aLayer);
		if (lay == null) return false;
		if (lay.style) {
			if (c) lay.style.backgroundColor = c;
			return lay.style.backgroundColor;
		} else {
			if (c) lay.bgColor = c;
			return lay.bgColor;
		}
		return false;
	}
	function getLayer(aLayer) {
		if (typeof aLayer != "string")
			return aLayer;
		if (document.layers) {
			return getLayerNetscape(document, aLayer);
		} else if (document.getElementById) {
			return document.getElementById(aLayer);
		} else if (document.all) {
			return eval("document.all." + aLayer);
		}
		return null;
	}
	function getLayerNetscape(doc, layerName) {
		var lay = eval("doc.layers." + layerName);
		if (lay != null) return lay;
		for (var i=0; i<doc.layers.length; i++) {
			lay = getLayerNetscape(doc.layers[i].document, layerName);
			if (lay != null) return lay;
		}
		return null;
	}
	function props(obj)	{
		s = "";
		for (all in obj) {
			value = "" + obj[all.toString()];
			if (value.length > 30)
				value = value.substring(0, 30) + "...";
			s += all.toString() + ":" + value + "|\t";
		}
		return s;
	}
