/* Element Methods */
register("pedro.element");

pedro.element.util = {}

pedro.element.util.display = {
    BLOCK  : "block",
    INLINE : "inline",
    NONE   : "none"
}

pedro.element.util.visbility = {
    HIDDEN   : "hidden",
    COLLAPSE : "collapse",
    INHERIT  : "inherit",
    VISIBLE  : "visible"
}

function addClass(el,klass){
    if(isUndef(el.style)) throw Error(el + " is not an HTMLElement. [addClass]");
    if(el.className.indexOf(klass) > -1) return;
    el.className += (el.className.length > 0) ? (" " + klass) : klass;
}

function removeClass(el,klass){
    if(isUndef(el.style)) throw Error(el + " is not an HTMLElement. [removeClass]");
    if(el.className.indexOf(klass) == -1) return;
    el.className = el.className.replace(new RegExp("\s?"+klass+"\s?"),"");
}

function hasClass(el,klass){
    if(isUndef(el.style)) throw Error(el + " is not an HTMLElement. [hasClass]");
    return (el.className.indexOf(klass) > -1)
}

function setDisplay(el,display){
    if(isUndef(el.style)) throw Error(el + " is not an HTMLElement. [setDisplay]");
    el.style.display = display;
}

function setVisibility(el,visibility){
    if(isUndef(el.style)) throw Error(el + " is not an HTMLElement. [setVisibility]");
    el.style.visibility = visibility;
}

function getWidth(el) {
    if(isUndef(el.style)) throw Error(el + " is not an HTMLElement. [getWidth]");
    return el.offsetWidth;
}

function getHeight(el) {
    if(isUndef(el.style)) throw Error(el + " is not an HTMLElement. [getHeight]");
    return el.offsetHeight;
}

function getRelPos(el) {
    return new Coord(getRelPosX(el),getRelPosY(el));
}

function getRelPosX(el) {
    if(isUndef(el.style)) throw Error(el + " is not an HTMLElement. [getRelPosX]");
    if (el.offsetParent) { return el.offsetLeft;}
      if (el.x) { return el.x; }
    return 0;
}

function getRelPosY(el) {
    if(isUndef(el.style)) throw Error(el + " is not an HTMLElement. [getRelPosY]");
    if (el.offsetParent) { return el.offsetTop; }
      if (el.y) { return  el.y; }
    return 0;
}

function getAbsoPos(el) {
    return new Coord(getAbsoPosX(el),getAbsoPosY(el));
}

function getAbsoPosX(el) {
    if(isUndef(el.style)) throw Error(el + " is not an HTMLElement. [getAbsoPosX]");
    var cl = 0;
    if (el.offsetParent) {
        while (el.offsetParent) { cl += el.offsetLeft; el = el.offsetParent; }
      }else if (el.x) { cl += el.x; }
    return cl;
}

function getAbsoPosY(el) {
    if(isUndef(el.style)) throw Error(el + " is not an HTMLElement. [getAbsoPosY]");
    var ct = 0;
    if (el.offsetParent) {
        while (el.offsetParent) { ct += el.offsetTop; el = el.offsetParent; }
      } else if (el.y) { ct += el.y; }
    return ct;
}

function setOpacity(el,opacity){
    if(isUndef(el.style)) throw Error(el + " is not an HTMLElement. [setOpacity]");
    opacity = (opacity == 1) ? 0.999 : opacity;
    el.style.opacity = opacity;
    el.style.filter = 'alpha(opacity=' + opacity*100 + ')';
}

function setHeight(el,height) {
    if(isUndef(el.style)) throw Error(el + " is not an HTMLElement. [setHeight]");
    var unit = (typeof el.style.top == 'string') ? 'px' : 0;
    el.style.height = height + unit;
}

function setWidth(el,width) {
    if(isUndef(el.style)) throw Error(el + " is not an HTMLElement. [setWidth]");
    var unit = (typeof el.style.top == 'string') ? 'px' : 0;
    el.style.width = width + unit;
}

function shift(el,point) {
    if(isUndef(el.style)) throw Error(el + " is not an HTMLElement. [shift]");
    var unit = (typeof el.style.top == 'string') ? 'px' : 0;
    el.style.left = point.x + unit;
    el.style.top = point.y + unit;
}

function getBounds(el){
    if(isUndef(el.style)) throw Error(el + " is not an HTMLElement. [getBounds]");
    var pos = getAbsoPos(el);
    var w = getWidth(el);
    var h = getHeight(el);
    return new BoundingBox(pos,new Coord(pos.x+w,pos.y+h));
}

function inBounds(bounds,pos){
    if(isNull(bounds) || isNull(pos)) return false;
    return ((pos.x > bounds.tl.x) &&
            (pos.x < bounds.br.x) &&
            (pos.y > bounds.tl.y) &&
            (pos.y < bounds.br.y));
}

var Coord = function(x,y){
    this.x = x;
    this.y = y;
}

Coord.prototype.toString = function() { return this.x + "," + this.y; }

var BoundingBox = function(tl,br){
    this.tl = tl;
    this.br = br;
}

BoundingBox.prototype.toString = function() { return this.tl + ":" + this.br; }


/* Coord math */

function coordSubtract(a,b){ return new Coord(a.x - b.x, a.y - b.y); }
function coordAdd(a,b){ return new Coord(a.x + b.x, a.y + b.y); }
