/*

Using the object

Please note that you cannot do anything with the object x itself, it
is merely a container for its two properties:

.obj, giving access to the actual HTML element. You have to use this
property to read out or set anything else than styles.

.style, giving access to the styles of the HTML element. You have to
use this property to read out or set the styles of the element.


So to alert the ID of the object, do

alert(x.obj.id)

After all the ID is a property of the object itself, not of the
style. To change the top coordinate, do

x.style.top = '20px';

top is a style, so you should use the style property.

It will pass you the HTML element you asked for, regardless of browser
DOM. Of course the element must have a proper ID and, if your script
must work in Netscape 4, should have a position defined in the style
sheet.


*/

function getObj(name)
{
  if (document.getElementById)
    {
      this.obj = document.getElementById(name);
      this.style = document.getElementById(name).style;
    }
  else if (document.all)
    {
      this.obj = document.all[name];
      this.style = document.all[name].style;
    }
  else if (document.layers)
    {
      this.obj = getObjNN4(document,name) ;
      this.style = this.obj;
    }
}

function getObjNN4(obj,name)
{
var x = obj.layers;
var foundLayer;
for (var i=0;i<x.length;i++)
{
if (x[i].id == name)
 foundLayer = x[i];
else if (x[i].layers.length)
var tmp = getObjNN4(x[i],name);
if (tmp) foundLayer = tmp;
}
return foundLayer;
}


function getByTagName( tagname)
{
  var found = [] ;
  if (document.getElementsByTagName) 
    {
      var els = document.getElementsByTagName( tagname) ;
      for (var i=0; i<els.length; i++) found[ i] = { 
	obj: els[ i] 
      , 
	style: els[ i].style } ;
      
    }
  else if (document.all) 
      {
	var els = document.all.tags( tagname) ;
	for (var i=0; i<els.length; i++) found[ i] = {
	  obj: els[ i] 
	  , 
	  style: els[ i].style } ;
	
      } 
  else if (document.layers) {
    // nothing happens. Can't be bothered with NS4
  } 
  else {
    // WTF?
  }
  return found ;
}


function attach( tagname, clss, hooks)
{
  var ims = getByTagName( tagname) ;
  var rxp = new RegExp( "\\b" + clss + "\\b") ;
  for (var i=0; i<ims.length; i++) {
    var el = ims[ i].obj;
    var cl = ( el.getAttribute) ? el.getAttribute( "class") : el["class"] ;
    if (rxp.test( cl)) {
      addhooks( el, hooks) ;
    }
  }
}

function addhooks( obj, hooks) 
{
  for (var j in hooks) {
    var hook = hooks[ j];
    obj[ hook.evnt] = hook.func ;
  }
}
