/*
 DynEl.js
*/

/* 
  Constructor for DynEl class
*/
  function DynEl(window, id, body, left, top, width, height)
  {
    this.window = window;
    this.id = id;
    this.body = body;
    this.left = left;
    this.top = top;
    this.width = width;
    this.height = height;
    if(id != null)
    {
      DynEl.els[DynEl.els.length] = this;
    }
  }
  new DynEl(null, null, null, null, null, null);
/*
 Class vars
*/
  DynEl.els = new Array();
  DynEl.ok = false;
  
/*
 Only DOM3 methods are supported
*/
if (document.getElementById)
{
  /*
   Print the style definitions - must be called in 
   head
  */
  DynEl.ok = true;
  DynEl.printStyle = function()
  {
    var i;
    var d = window.document;
    d.writeln('<' + 'style type="text/css">');
    for(i = 0; i < DynEl.els.length; i++)
    {
      DynEl.els[i].printStyle(d);
    }
    d.writeln("</" + "style>");
  }
  /*
   Output the style definition
  */
  DynEl.prototype.printStyle = function(d)
  {
    d.writeln("#" + this.id + " {position: absolute;");
    if (this.left)
    {
      d.write("left:" + this.left + "px;");
    }
    if (this.top)
    {
      d.write("top:" + this.top + "px;");
    }
    if (this.width)
    {
      d.write("width:" + this.width + "px;");
    }
    if (this.height)
    {
      d.write("height:" + this.height + "px;");
    }
    d.writeln("}");
  }

  /*
    Output the element itself.  Must be called first before 
    other methods
  */
  DynEl.output = function()
  {
    var i;
    for(i = 0; i < DynEl.els.length; i++)
    {
      DynEl.els[i].output();
    }
  }

  DynEl.prototype.output = function()
  {
    var d = this.window.document;

    //Output the element
    d.writeln('<div id="' + this.id + '">');
    d.writeln(this.body);
    d.writeln("</div>");
    
    //Save a reference to the layer object
    this.element = d.getElementById(this.id);
    this.style = this.element.style;
    this.style.left = this.left + "px";
    this.style.top = this.top + "px";
    if (this.width)
    {
      this.style.width = this.width + "px";
    }
    if (this.height)
    {
      this.style.height = this.height + "px";
    }
    this.style.position = "absolute";
    this.style.visibility = "visible";
    this.style.zIndex = 0;
  }
  
  //Modify the element
  DynEl.prototype.moveTo = function(x, y)
  {
    this.left = x;
    this.top = y;
    this.style.left = (this.left + "px");
    this.style.top = (this.top + "px");
  }
  DynEl.prototype.moveBy = function(x, y)
  {
    this.left += x;
    this.top += y;
    this.style.left = (this.left + "px");
    this.style.top = (this.top + "px");
  }
  DynEl.prototype.show = function()
  {
    this.style.visibility = "visible";
  }
  DynEl.prototype.hide = function()
  {
    this.style.visibility = "hidden";
  }
  DynEl.prototype.setStackingOrder = function(z)
  {
    this.style.zIndex = z;
  }
  DynEl.prototype.setBgColor = function(color)
  {
    this.style.backgroundColor = color;
  }
  DynEl.prototype.setBgImage = function(image)
  {
    this.style.backgroundImage = image;
  }

  //Query the element
  DynEl.prototype.getX = function()
  {
    return this.left;
  }
  DynEl.prototype.getY = function()
  {
    return this.top;
  }
  DynEl.prototype.getWidth = function()
  {
    return this.style.width;
  }
  DynEl.prototype.getHeight = function()
  {
    return this.style.height;
  }
  DynEl.prototype.getStackingOrder = function()
  {
    return this.style.zIndex;
  }
  DynEl.prototype.isVisible = function()
  {
    return this.style.visibility == "visible";
  }
  DynEl.prototype.getDOMElement = function()
  {
    return this.element;
  }
  
  
  /*
    Dynamically change the contents of the element
    Then arg(s) should be HTML strings which become the new body
  */
  DynEl.prototype.setBody = function(body)
  {
    this.element.innerHTML = body;
  }
}
/*
  Place holders for incompatible browsers
*/
else
{
  function DynEl_proto_0arg(){ return null; }
  function DynEl_proto_1arg(a){ return null; }
  function DynEl_proto_2arg(a, b){ return null; }
  
  DynEl.printStyle = DynEl_proto_0arg;
  DynEl.prototype.printStyle = DynEl_proto_1arg;
  DynEl.output = DynEl_proto_0arg;
  DynEl.prototype.output = DynEl_proto_0arg;
  DynEl.prototype.moveTo = DynEl_proto_2arg;
  DynEl.prototype.moveBy = DynEl_proto_2arg;
  DynEl.prototype.show = DynEl_proto_0arg;
  DynEl.prototype.hide = DynEl_proto_0arg;
  DynEl.prototype.setStackingOrder = DynEl_proto_1arg;
  DynEl.prototype.setBgColor = DynEl_proto_1arg;
  DynEl.prototype.setBgImage = DynEl_proto_1arg;
  DynEl.prototype.getX = DynEl_proto_0arg;
  DynEl.prototype.getY = DynEl_proto_0arg;
  DynEl.prototype.getWidth = DynEl_proto_0arg;
  DynEl.prototype.getHeight = DynEl_proto_0arg;
  DynEl.prototype.getStackingOrder = DynEl_proto_0arg;
  DynEl.prototype.isVisible = DynEl_proto_0arg;
  DynEl.prototype.getDOMElement = DynEl_proto_0arg;
  DynEl.prototype.setBody = DynEl_proto_0arg;
}
