// DIVLAYERLIB - Cross Browser Dynamic HTML using DIVs Library
// Status: V0.2 - unfinished, providing the most basic functions
// (c)2000-2002 Freddy Leitner [office@dreamer.de]


// divlayer_checkdom - Checks and returns DOM style: "w3" for standardized, "ie" or "ns" for proprietary DOMs

function divlayer_checkdom() {
  if (document.layers) { return "ns"; } else { if (document.all) { return "ie"; } else { if (document.getElementById) { return "w3"; } } }
}


// divlayer_init -- Pull all layer objects into divlayer[] object array

function divlayer_init() {
  divlayer = new Array();
  if (document.layers) {
    for (i = 0; i < document.layers.length; i++) {
      if (document.layers[i].name != "") {
        divlayer[document.layers[i].name] = new divlayer_init_ns4(document.layers[i]);
      }
    }
  } else {
    if (document.all) {
      var divlayers = document.all.tags("DIV");
      for (i = 0; i < divlayers.length; i++) {
        if (divlayers[i].id != "") {
          divlayer[divlayers[i].id] = new divlayer_init_dom(divlayers[i]);
        }
      }
    } else {
      if (document.getElementById) {
        var divlayers = document.getElementsByTagName("DIV");
        for (i = 0; i < divlayers.length; i++) {
          var divlayerobj = divlayers[i];
          if (divlayerobj.id != "") {
            divlayer[divlayerobj.id] = new divlayer_init_dom(divlayerobj);
          }
        }
      }
    }
  }
}


// W3C DOM object (uses some IEx DOM calls for convenience, can later be switched)

function divlayer_init_dom(obj) {
  this.obj = obj;
  this.name = obj.id;

  this.hide = divlayer_hide_dom;
  this.show = divlayer_show_dom;

  this.getleft = divlayer_getleft_iex;
  this.gettop = divlayer_gettop_iex;
  this.getwidth = divlayer_getwidth_iex;
  this.getheight = divlayer_getheight_iex;

  this.getclipleft = divlayer_getclipleft_dom;
  this.getclipright = divlayer_getclipright_dom;
  this.getcliptop = divlayer_getcliptop_dom;
  this.getclipbottom = divlayer_getclipbottom_dom;

  this.setleft = divlayer_setleft_dom;
  this.settop = divlayer_settop_dom;

  this.setclip = divlayer_setclip_dom;
}


// W3C DOM methods

function divlayer_hide_dom() { this.obj.style.visibility = "hidden"; }
function divlayer_show_dom() { this.obj.style.visibility = "visible"; }

function divlayer_getleft_dom() { return parseInt(this.obj.style.left); }
function divlayer_gettop_dom() { return parseInt(this.obj.style.top); }
function divlayer_getwidth_dom() { return parseInt(this.obj.style.width); }
function divlayer_getheight_dom() { return parseInt(this.obj.style.height); }

function divlayer_getcliptop_dom() { return divlayer_getclipparm(this,0); }
function divlayer_getclipright_dom() { return divlayer_getclipparm(this,1); }
function divlayer_getclipbottom_dom() { return divlayer_getclipparm(this,2); }
function divlayer_getclipleft_dom() { return divlayer_getclipparm(this,3); }
function divlayer_getclipparm(lyr,indx) {
  var clips = lyr.obj.style.clip;
  if (clips.length > 0) {
    clips = clips.slice(5,clips.length-1);
    var clipentries = clips.split(" ");
    return parseInt(clipentries[indx]);
  } else {
    if (indx == 0) { return 0; }
    if (indx == 1) { return lyr.getwidth(); }
    if (indx == 2) { return lyr.getheight(); }
    if (indx == 3) { return 0; }
  }
}

function divlayer_setclip_dom(left,top,right,bottom) {
  if (top == null) { top = this.getcliptop(); }
  if (right == null) { right = this.getclipright(); }
  if (bottom == null) { bottom = this.getclipbottom(); }
  if (left == null) { left = this.getclipleft(); }
  this.obj.style.clip = "rect(" + top + "px, " + right + "px, " + bottom + "px, " + left + "px)";
}

function divlayer_setleft_dom(leftpx) { this.obj.style.left = leftpx + "px"; }
function divlayer_settop_dom(toppx) { this.obj.style.top = toppx + "px"; }

// IEx DOM methods

function divlayer_getleft_iex() { return parseInt(this.obj.offsetLeft); }
function divlayer_gettop_iex() { return parseInt(this.obj.offsetTop); }
function divlayer_getwidth_iex() { return parseInt(this.obj.offsetWidth); }
function divlayer_getheight_iex() { return parseInt(this.obj.offsetHeight); }


// NS4 DOM object

function divlayer_init_ns4(obj) {
  this.obj = obj;
  this.name = obj.name;
  this.hide = divlayer_hide_ns4;
  this.show = divlayer_show_ns4;

  this.gettop = divlayer_gettop_ns4;

  this.getcliptop = divlayer_getcliptop_ns4;
  this.getclipleft = divlayer_getclipleft_ns4;
  this.getclipbottom = divlayer_getclipbottom_ns4;
  this.getclipright = divlayer_getclipright_ns4;

  this.setleft = divlayer_setleft_ns4;
  this.settop = divlayer_settop_ns4;

  this.setclip = divlayer_setclip_ns4;
}


// NS4 DOM methods different from W3C DOM methods (which means all, practically)

function divlayer_hide_ns4() { this.obj.visibility = "hidden"; }
function divlayer_show_ns4() { this.obj.visibility = "visible"; }

function divlayer_gettop_ns4() { return this.obj.top; }

function divlayer_getcliptop_ns4() { return this.obj.clip.top; }
function divlayer_getclipleft_ns4() { return this.obj.clip.left; }
function divlayer_getclipbottom_ns4() { return this.obj.clip.bottom; }
function divlayer_getclipright_ns4() { return this.obj.clip.right; }

function divlayer_setclip_ns4(left,top,right,bottom) {
  if (top != null) { this.obj.clip.top = top; }
  if (left != null) { this.obj.clip.left = left; }
  if (bottom != null) { this.obj.clip.bottom = bottom; }
  if (right != null) { this.obj.clip.right = right; }
}

function divlayer_setleft_ns4(leftpx) { this.obj.left = leftpx; }
function divlayer_settop_ns4(toppx) { this.obj.top = toppx; }


// EOF
