/**
 * 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 getMouseXY(evt)
 *  @return void setVisibility4DivByPrefix(prefix, visible)
 */

// Objekte
function Coord(x, y){
  this.x = (!x)?0:x;
  this.y = (!y)?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);
  
  this.equalsCoord = this.equals;
  this.equals = equalsCanvas;
}

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(/*Coord*/ c){
  return (this.x == c.x && this.y == c.y);
}
function equalsCanvas(/*Canvas*/ c){
  return ( this.equalsCoord == c.equalsCoord && this.width == c.width && this.height == c.height);
}

// Funktionen

function getLayerRef (id, document) {
  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){

    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.top = coords.y;
      objLayer.style.pixelLeft = coords.x;
    } else if (document.getElementById) {
      objLayer.style.top = coords.y + 'px'; 
      objLayer.style.left = coords.x + 'px';
    }
}

function setPositionById(id, coords){
  var objLayer = getLayerRef(id);
  if(typeof objLayer == undefined) return;
  
  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.top = 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) {

  if(document.layers){
    objLayer.visibility  = 
        (visible == true) ? 'show' : 'hide';
  } else {
    objLayer.style.visibility = 
        (visible == true) ? 'visible' : 'hidden';
  }

}

function setVisibility4DivByPrefix(prefix, visible, d){
  if (!d)
    d = window.document;

  if(document.layers){
    for (var l = 0; l < d.layers.length; l++){
      if(d.layers[l].id.substr(0, prefix.length ) == prefix)
        setVisibility(d.layers[l], visible);
      setVisibility4DivByPrefix(prefix, 
                                visible, 
                                d.layers[l].document);
    }

  } else if(document.all) {

    var layers = document.all.tags("div"); 
    for(i=0; i < layers.length; i++) { 
      if(layers[i].id.substr(0, prefix.length ) == prefix )
        setVisibility(document.all.tags("div")[i], visible);
    }

  } else if(document.getElementsByTagName) {

    var layers = document.getElementsByTagName("div");
    for(i=0; i < layers.length; i++){
      if(layers[i].id.substr(0, prefix.length ) == prefix)
        setVisibility(layers[i], visible);
    }

  }
}

function getMouseXY(evt) {
  e = evt || window.event;
  //if(!e) return null;
  if(!e) return new Coord(100 , 200 );

  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 );
	return new Coord(100 , 200 );
  }
}


function getMouseY(evt) {
  //e = evt || document.event;
  //if(!e) return null;
  //if(!e) return new Coord(200);

  if(document.getElementById) {
    return evt.pageY;
	//return new Coord(200,200);
  }
}


function /*out: Coord*/ getLayerCoords( /*in: HTML-Object*/ objLayer ){
  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 /*out: Coord*/ getLayerCoordsById( /*in: String*/ id ){
  var objLayer = getLayerRef(id);
  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;
}