/**
 * LayerRef - Referenziert einen Layer und stellt zus?liche Methoden
 * zur Verfgung
 *
 * Browser-Kombatibilit?
 *
 * Nicht funktionsf?g auf NS4.7:
 *
 *  -> read
 *  -> append
 *  -> setOpacity
 *
 * Nicht funktionsf?g auf OperaOpera 7.54u2, 8
 *
 *  -> setOpacity
 *
 * Alles funktioniert auf:
 *
 *  Firefox 1.0.4
 *  IE 6.0
 *  Mozilla 1.0.2
 *  Netscape 6.1 +
 *
 *  ------  ------  ------
 *
 * Methoden der Klasse Layer:
 *
 *  @return Object	LayerReference Layer(String ID, String document)
 *  @return void	moveTo( Coord coords)
 *  @return Coord	getPosition()
 *  @return void	setVisibility( boolean visible)
 *  @return void	setOpacity( float opacity)
 *  @return Coord	getCanvas()
 *	@return boolean	write(String text)
 *  @return boolean	append(String text)
 *	@return boolean	read()
 *
 * <code>
 * testobj = new Layer('testlayer');
 *
 * // Beispiele
 * testobj.setVisibility(0);
 * testobj.setVisibility(1);
 * testobj.write("blub11");
 * alert( "<br>Layer-Inhalt:  " + testobj.read());
 * alert( "<br>Layer-Ma?:    " + testobj.getCanvas());
 * alert( "<br>Layer-Position:" + testobj.getPosition());
 * </code>
 *
 * @param	string	ID des Layers(<div id="Layer-ID">...</div>)
 */
function Layer_Ref(layerId, document, isLayerRef)
{
	/* Layer referenzieren */
	this.layer  = new Object();
	if (typeof(isLayerRef) == "undefined" || isLayerRef == false) {
	 this.layer  = _getLayerRef(layerId, document);
  } else {
    this.layer  = layerId;
  }

	this.layer.moveTo        = Layer_moveTo;
	this.layer.getPosition   = Layer_getPosition;
	this.layer.setVisibility = Layer_setVisibility;
	this.layer.setOpacity    = Layer_setOpacity;
	this.layer.getCanvas     = Layer_getCanvas;
	this.layer.setCanvas     = Layer_setCanvas;
	this.layer.write         = Layer_write;
	this.layer.append        = Layer_append;
	this.layer.read          = Layer_read;

	return this.layer;
}



function _getLayerRef(layerId, document)
{
	/* Layer referenzieren */
	if (!document)
		document = window.document;

	if (document.layers) {
		for (var l = 0; l < document.layers.length; l++)
			if (document.layers[l].id == layerId)  //|| document.layers[l].name == layerId
				return document.layers[l];

		for (var l = 0; l < document.layers.length; l++) {
		  var result = _getLayerRef(layerId, document.layers[l].document);
		  if (result)
		    return result;
		}
		return false;
	}
	/* ie */
	else if (document.all)
		return (document.all[layerId]) ? document.all[layerId] : null;

	/* mozilla */
	else if (document.getElementById)
		return document.getElementById(layerId);

	else
		return false;
}

/**
 * Layer.moveTo - Verschiebt einen Layer zu der bergebenen Position
 *
 * Die Position muss als Objekt(mit den Eigenschaften x und y) bergeben
 * werden. Das Objekt Coord(); kann dafr verwendet werden.
 *
 * Achtung: Im CSS des Layers unbedingt position: absolute oder
 * relative angeben, sonst gibt die Methode keine korrekten Angaben zurck.
 *
 * <code>
 * testobj.moveTo( new Coord(150, 230) );
 * </code>
 *
 * @param	object	Koordinaten an die der Layer verschoben werden soll
 */
function Layer_moveTo(coords){
    if (document.layers) {
      this.top = coords.y;
      this.left = coords.x;
    } else if (window.opera) {
      this.style.top = coords.y;
      this.style.left = coords.x;
    } else if (document.all) {
      this.style.pixelTop = coords.y;
      this.style.pixelLeft = coords.x;
    } else if (document.getElementById) {
      this.style.top = coords.y + 'px';
      this.style.left = coords.x + 'px';
    }
}


/**
 * Layer.getPosition - gibt die Position des Layers zurck
 *
 * Ermittelt die Position des Layers und gibt ein Objekt mit den
 * Eigenschaften x und y zuck. Bentigt das Objekt Coord();
 *
 * Achtung: Im CSS des Layers unbedingt position: absolute oder
 * relative angeben, sonst gibt die Methode keine korrekten Angaben zurck.
 *
 * <code>
 * position = testobj.getPosition();
 * alert( "x: " + position.x + " y:" + position.y );
 * </code>
 */
function Layer_getPosition()
{
	var coords = new Coord();

	if (document.layers) {
		coords.y = this.top;
		coords.x = this.left;
	} else if ( window.opera ) {
		coords.y = this.style.top;
		coords.x = this.style.left;
	} else if (document.all) {
		o = this;
		while(o.offsetParent) {
		  coords.y += parseInt(o.offsetTop);
		  coords.x += parseInt(o.offsetLeft);
		  o = o.offsetParent;
		}
	} else if (document.getElementById) {
		coords.y = parseInt( document.defaultView.getComputedStyle(this, '').getPropertyValue('top') );
		coords.x = parseInt( document.defaultView.getComputedStyle(this, '').getPropertyValue("left") );
	}

	return coords;
}

/**
 * Layer.setVisibility - setzt die Sichtbarkeit eines Layers
 *
 * Setzt den Layer anhand der Uebergabe auf sichtbar oder unsichtbar.
 *
 * <code>
 * // Layer unsichtbar schalten
 * testobj.setVisibility(0);
 * </code>
 *
 * @param	boolean	Sichtbarkeit des Layer(1 = sichtbar, 0 = unsichtbar)
 */
function Layer_setVisibility( visible ){
	if (document.layers){
		this.visibility  = (visible == true) ? 'show' : 'hide';
	} else {
		this.style.visibility = (visible == true) ? 'visible' : 'hidden';
	}
}


/**
 * Layer.setOpacity - setzt die Undurchsichtigkeit eines Layers
 *
 * Setzt den Layer anhand der Uebergabe auf durchsichtig oder undurchsichtig.
 *
 * Achtung: zuerst muss fr den IE im CSS des Layers "filter: alpha(opacity=100)"
 *          (0-100) gesetzt werden.
 *
 * <code>
 * // Layer teildurchsichtig schalten
 * testobj.setOpacity(0.5);
 * </code>
 *
 * @param	float	Durchsichtigkeit des Layer(1 = undurchsichtig, 0 = durchsichtig)
 */
function Layer_setOpacity(val) {
	if(document.all && this.filters) {
		try {
            this.filters['alpha'].opacity = val*100;
        } catch (e) { }
 	} else if (!document.layers) {
		this.style.MozOpacity  = val;
	}
}


/**
	 * Layer.getCanvas - gibt die Ma? des Layer zurck
	 *
	 * Ermittelt die Ma? des Layers und gibt ein Objekt mit den
	 * Eigenschaften x und y zuck. Bentigt das Objekt Canvas();
	 *
	 * Achtung: Im CSS des Layers unbedingt position: absolute oder
	 * relative angeben, sonst gibt die Methode keine korrekten Angaben zurck.
	 *
	 * <code>
	 * canvas = testobj.getCanvas();
	 * alert( "x: " + canvas.x + " y:" + canvas.y );
	 * </code>
	 *
	 */
function Layer_getCanvas(){
	var canvas = new Canvas();

	// NS4x
	if(document.layers){
		canvas.y = this.top;
		canvas.x = this.left;
		canvas.width = this.clip.width;
		canvas.height = this.clip.height;
	}
	// IE - Opera verwendet in neueren Versionen auch document.all
	else
	{
		canvas.width = this.offsetWidth;
		canvas.height = this.offsetHeight;

		var o = this;
		while(o.offsetParent){
			canvas.y += parseInt(o.offsetTop);
			canvas.x += parseInt(o.offsetLeft);
			o = o.offsetParent;
		}
	}

	return canvas;
}

function Layer_setCanvas(/* Object */ lCanvas){
	this.style.width  = lCanvas.width + "px";
	this.style.height = lCanvas.height + "px" ;
}

/**
 * Layer.write - schreibt den bergebenen Text in den Layer
 *
 * <code>
 * testobj.write("neuer Text");
 * </code>
 *
 * @param	string	Text der in den Layer geschrieben werden soll
 */
function Layer_write(text){
	d = this;
	if(document.layers){ // Ns4
		d.document.open();
		d.document.write(text);
		d.document.close();
	}
	else{
	eval(d).innerHTML = text;}
	return d;
}

/**
 * Layer.append - h?t den bergebenen Text an den Layer an
 *
 * <code>
 * testobj.append("neuer Text");
 * </code>
 *
 * @param	string	Text der an den Layer angeh?t werden soll
 */
function Layer_append(text){
	d = this;
	if(!document.layers){
		eval(d).innerHTML = eval(d).innerHTML + text;
	}
	return d;
}

/**
 * Layer.read - gibt den Inhalt des Layers zurck
 *
 * <code>
 * alert( testobj.read() );
 * </code>
 */
function Layer_read(){
	d = this;
	if(!document.layers){
		return eval(d).innerHTML;
	}
	return d;
}

// Ende der Klasse Layer




/**
 * oft genutzte Objekte in DHTML:
 *  Coord
 *  Canvas
*/
function Coord(x, y){
	this.x = (!x) ? 0 : x;
	this.y = (!x) ? 0 : y;

	this.toString = ObjToString;
	//this.equals = EqualsCoord;
}

function Canvas(width, height, x, y ){
	this.width  = (!width)  ? 0 : width;
	this.height = (!height) ? 0 : height;

	this.Coord = Coord;
	this.Coord(x,y);
}



/*
*  @return Coord	getMouseXY(evt)
*/


function GetMouseXY(evt){
	// Funktion liest Mausposition aus
	e = evt || window.event;

	if(document.layers){
		return new Coord(e.pageX, e.pageY);
	}

	else if(window.opera){
		return new Coord(e.clientX, e.clientY);
	}

	else if(document.all){
		return new Coord(e.x + document.body.scrollLeft, e.y + document.body.scrollTop);
	}

	else if(document.getElementById){
		return new Coord(e.pageX, e.pageY);
	}
}

// Objektfunktionen
function ObjToString(){
	var ret = "{";

	for(p in this ){
		if(typeof this[p] == "function" || typeof this[p] == "object")
			continue;

		if(ret.length > 1){
			ret += ",";
		}

		ret += p + ":" + this[p];

	}
	return ret + "}";
}

function EqualsCoord(c){
	return (this.x == c.x && this.y == c.y);
}

function trim(argvalue) {
  var tmpstr = ltrim(argvalue);
  return rtrim(tmpstr);
}

function rtrim(argvalue) {
  while (1) {
    if (argvalue.substring(argvalue.length - 1, argvalue.length) != " ")
      break;
    argvalue = argvalue.substring(0, argvalue.length - 1);
  }
  return argvalue;
}

function ltrim(argvalue) {
  while (1) {
    if (argvalue.substring(0, 1) != " ")
      break;
    argvalue = argvalue.substring(1, argvalue.length);
  }
  return argvalue;
}

