// © rod morelos | rodmorelos@msn.com
// keep these two lines and you're free to use this code

// object constructor
function createObjectById(id)
{
        this.el = document.getElementById ? document.getElementById(id) : null;
        if (!this.el) throw new Error('Element with "' + id + '" id not found.');
        this.css = this.el.style;
        this.i = createObjectById.registry.length;
        createObjectById.registry[this.i] = this;
        this.w = this.el.offsetWidth ? this.el.offsetWidth : 0;
        this.h = this.el.offsetHeight ? this.el.offsetHeight : 0;
        this.x = this.el.offsetLeft ? this.el.offsetLeft : 0;
        this.y = this.el.offsetTop ? this.el.offsetTop : 0;
        this.o = 100;
}

// global reference to the dhtml object
createObjectById.registry = [];

// update object values
createObjectById.prototype.update = function()
{
        this.w = this.el.offsetWidth ? this.el.offsetWidth : 0;
        this.h = this.el.offsetHeight ? this.el.offsetHeight : 0;
        this.x = this.el.offsetLeft ? this.el.offsetLeft : 0;
        this.y = this.el.offsetTop ? this.el.offsetTop : 0;
}

// visibility
createObjectById.prototype.show = function() { this.css.visibility = 'visible'; }
createObjectById.prototype.hide = function() { this.css.visibility = 'hidden'; }

// display
createObjectById.prototype.display = function(d) { this.css.display = d; }

// colors
createObjectById.prototype.fg = function(f) { this.css.color = f; }
createObjectById.prototype.bg = function(b) { this.css.backgroundColor = b; }

// set layer opacity (in percent, between 0 and 100)
createObjectById.prototype.setOpacity = function(o)
{
        if (typeof this.css.MozOpacity != 'undefined') { this.css.MozOpacity = (o / 100); this.o = o; }
        else if (this.el.filters) { this.css.filter = 'alpha(opacity=' + o + ')'; this.o = o; }
}

// moves a layer to (x, y) pixels
createObjectById.prototype.moveTo = function(x, y)
{
        if (x != null && typeof x == 'number')
        {
                x = Math.round(x);
                this.x = x;
                this.css.left = x + 'px';
        }
        if (y != null && typeof y == 'number')
        {
                y = Math.round(y);
                this.y = y;
                this.css.top = y + 'px';
        }
}

// resize layer to (width, height) pixels
createObjectById.prototype.setSize = function(w, h)
{
        if (w != null && typeof w == 'number')
        {
                if (w < 0) w = 0;
                w = Math.round(w);
                this.css.width = w + 'px';
        }
        if (h != null && typeof h == 'number')
        {
                if (h < 0) h = 0;
                h = Math.round(h);
                this.css.height = h + 'px';
        }
        this.update();
}

// clips layer to specific dimensions; (top, right, bottom, left) in pixels
createObjectById.prototype.clipTo = function(t, r, b, l)
{
        if (t < 0) t = 0; if (r < 0) r = 0; if (b < 0) b = 0; if (l < 0) l = 0;
        this.css.clip = 'rect(' + t + 'px, ' + r + 'px, ' + b + 'px, ' + l + 'px)';
}

// write content to layer
createObjectById.prototype.write = function(text)
{
        if (typeof this.el.innerHTML != 'undefined')
        {
                this.el.innerHTML = text;
                this.update();
        }
}