/**
 * oft genutzte Objekte in DHTML:
 *  Coord
 *  Canvas
 * oft genutzt Funktionen in DHTML
 *  @return LayerReference getLayerRef(String ID)
 *  @return void setPosition(LayerReference objLayer, Coord coords)
 *  @return void setVisibility(LayerReference objLayer, boolean visible)
 *  @return Coord getLayerCoors(LayerReference objLayer)
 *  @return Coord getLayerCanvas(String ID)
 *  @return boolean writeLayer(String ID, String text, String frame)
 *  @return Coord getMouseXY(evt)
 */

// Objekte

function Coord(x, y){
	this.x = (!x) ? 0 : x;
	this.y = (!x) ? 0 : y;

	this.toString = ObjToString;
	//this.equals = EqualsCoord;
}

function Canvas(x, y, width, height){
	this.width = (!width)?0:width;
	this.height = (!height)?0:height;

	this.Coord = Coord;
	this.Coord(x,y);
}

// FUNKTIONEN

function getLayerRef (id, document) {
	// Funktion referenziert einen Layer als Objekt
	// Nutzung:
	// object = getLayerRef("layer");
	// object.style.color = "blue"; // nur ein Beispiel
	if (!document)
	document = window.document;
	if (document.layers) {
	for (var l = 0; l < document.layers.length; l++)
	  if (document.layers[l].id == id)
	    return document.layers[l];
	for (var l = 0; l < document.layers.length; l++) {
	  var result = getLayerRef(id, document.layers[l].document);
	  if (result)
	    return result;
	}
	return null;
	}
	else if (document.all) {
	return document.all[id];
	}
	else if (document.getElementById) {
	return document.getElementById(id);
	} else {
	return null;
	}
}

function setPosition(objLayer, coords){
	// Funktion setzt die Position eines Layers
	// Nutzung:
	// var coords = new Coord();
	// coords.x = 123;
	// coords.y = 456;
	// setPosition("layer", coords);
    if (document.layers) {
      objLayer.top = coords.y;
      objLayer.left = coords.x;
    } else if (window.opera) {
      objLayer.style.top = coords.y;
      objLayer.style.left = coords.x;
    } else if (document.all) {
      objLayer.style.pixelTop = coords.y;
      objLayer.style.pixelLeft = coords.x;
    } else if (document.getElementById) {
      objLayer.style.top = coords.y + 'px';
      objLayer.style.left = coords.x + 'px';
    }
}

function setVisibility(objLayer, visible) {
	// Funktion setzt die Sichtbarkeit eines Layers per CSS
	// Nutzung:
	// setVisibility(objlayer, 1); // setzt Objekt auf sichtbar
	if(document.layers){
	objLayer.visibility  =
	    (visible == true) ? 'show' : 'hide';
	} else {
	objLayer.style.visibility =
	    (visible == true) ? 'visible' : 'hidden';
	}
}

function getLayerCoords( objLayer ){
	// Funktion liest die Koordinaten eines Layers aus
	// Nutzung:
	// coords = getLayerCoords("layer");
	// alert(coords.x); // bzw y
	// Benötigt:
	// Coord()
	var coords = new Coord();

	if (document.layers) {
	coords.y = objLayer.top;
	coords.x = objLayer.left;
	} else if ( window.opera ) {
	coords.y = objLayer.style.top;
	coords.x = objLayer.style.left;
	} else if (document.all) {
	o = objLayer;
	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(objLayer, '').getPropertyValue('top') );
	coords.x = parseInt( document.defaultView.getComputedStyle(objLayer, '').getPropertyValue("left") );
	}

	return coords;
}


function getLayerCanvas(id){
	// Funktion liest die Höhe und die Breite eines Layers aus
	// Nutzung:
	// coords = getLayerCanvas("layer");
	// alert(coords.height); // bzw width
	// Benötigt:
	// getLayerRef()
	// Canvas()
	var objLayer = getLayerRef(id);
	var canvas = new Canvas();

	// NS4x
	if(document.layers){
		canvas.y = objLayer.top;
		canvas.x = objLayer.left;
		canvas.width = objLayer.clip.width;
		canvas.height = objLayer.clip.height;
	}
	// IE - Opera verwendet in neueren Versionen auch document.all
	else if(document.all){
		o = objLayer;
		canvas.width = objLayer.offsetWidth;
		canvas.height = objLayer.offsetHeight;
		while(o.offsetParent){
			canvas.y += parseInt(o.offsetTop);
			canvas.x += parseInt(o.offsetLeft);
			o = o.offsetParent;
		}
	}
}


function writeLayer(id, text, frame){
	// schreibt... 'text'
	// in den Layer... 'id'
	// in dem Frame... 'frame'(optional)
	var d = (frame) ?  getLayerRef(id, top.frames[frame].document) : getLayerRef(id);
	if(document.layers){ // Ns4: *schreibe in Layer - Special*
		d.document.open();
		d.document.write(text);
		d.document.close();
	}
	else{
	eval(d).innerHTML = text;}
	setVisibility(d, 1)
	return d;
}


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);
}
