
/*
* dom_suite.js
*
* Author: Kenan Banks
*      
* getXY() written by Chris Wetherell
*
* A bunch of shorthand and utility function for 
* navigating and accessing the DOM
*
*/


/*
* function ce
*
* Shorthand function for creating DOM Elements
*
* Equivalent to document.createElement(e)
*/
function ce(e){return document.createElement(e)}


/*
* function cex
*
* Shorthand function for creating DOM Elements
* 
* The first parameter is the name of the element to create.
* The second parameter is a Javascript object literal used to
* initialize the elements once created.
*
* Returns the DOM Element created
* 
* Example:
*   var a = cex("DIV", {onmouseover:foo, name:'div1', id:'main'});
*/
function cex(e, x){
    var a = ce(e);
    for (prop in x){
      a[prop] = x[prop];
    }
    return a;
}


/*
* function ge
*
* Shorthand function for document.getElementById(i)
*/
function ge(i){return document.getElementById(i)}



/*
* function ac
*
* Shorthand function for appendChild
* 
* Accepts an arbitrary number of elements as parameters and appends
* the 2nd and later elements to the first element.  Returns the first 
* object in the list after appends.  Useful in chains (see Example)
* 
* Example: ac( house, ac(roof, shingles), ac(floor, ac(tiles, grout)))
*/
function ac(){
    if (ac.arguments.length > 1){
        var a = ac.arguments[0];
        for (i=1; i<ac.arguments.length; i++){
            if (arguments[i])
               a.appendChild(ac.arguments[i]);
        }
        return a;
    } else {
        return null;
    }

}


/**
* function getXY()
*
* Returns the position of any element as an object.
*
* Typical usage:
* var pos = getXY(object);
* alert(pos.x + " " +pos.y);
*/
function getXY(el) {
  var x = el.offsetLeft;
  var y = el.offsetTop;
  if (el.offsetParent != null) {
    var pos = getXY(el.offsetParent);
    x += pos.x;
    y += pos.y;
  }
  return {x: x, y: y}
}


/*
* function showProps
*
* Displays a DIV on the page containing all of the properties
* of the object passed.  Used in debugging
*/
function showProps(obj){
  s = "";
  for (prop in obj){
    s += prop + "<br/>";
  }

  var d = document.createElement("DIV");
  d.style.backgroundColor='red';
  d.style.position='absolute';
  d.style.height='500px';
  d.style.width='500px';
  d.style.top='50px';
  d.style.left='50px';
  d.style.overflow='scroll';
  d.style.zIndex = '30000';
  d.onclick = function() {this.style.display='none';};


  d.innerHTML = s;
  ac(document.body,d);

}


/*
* function x_alert
*
* Displays an alert box as a div that you may copy text from
*/
function x_alert(s){
  var d = cex("DIV",{innerHTML: s, 
                     id:'__x_alert_div'});
  var s = cex("SPAN", {innerHTML:'click this span to kill'});
  s.style.display ='block';
  
  s.onclick = function() {document.getElementById('__x_alert_div').style.display = 'none'}
  
  d.style.position = 'absolute';
  d.style.left = '50px';
  d.style.top = '50px';
  d.style.overflow = 'scroll';
  d.style.backgroundColor = 'white';
  d.style.width = '500px';
  d.style.height = '300px';
  d.style.zIndex = '30000'
  d.style.padding = '1em';
              
  ac(document.body, ac(d,s))
 }


/*
* function addListener 
*
* Attaches listener functions to DOMobject events in a cross browsery fashion
*/
function addListener(obj, eventName, listener){
  // IE Stylee
  if (obj.attachEvent){
    obj.attachEvent("on"+eventName, listener);
  // DOM Stylee
  } else if (obj.addEventListener){
    obj.addEventListener(eventName, listener, false);
  } else {
    return false;
  }
  return true;
}


/*
* function removeListener
* 
* The complement to addListener
*
* Removes handler functions from specified object's event
*/
function removeListener(obj, eventName, listener){
  // IE Stylee
  if (obj.detachEvent){
    obj.detachEvent("on"+eventName, listener);
  } else if (obj.removeEventListener){
    obj.removeEventListener(eventName, listener, false);
  } else {
    return false;
  }
  
  return true;
}
