/* 
 * QJS.qLayer
 *  Provides a DOM-independent way of accessing and modifying layer properties
 *
*/ 
qLayer = function(id) {
    this.id = id;
    this.style = this.getReference();
    
    // Property Names/Values
    if (this.style.backgroundColor) { qLayer.STYLE_BACKGROUND   = "backgroundColor"; }
    else if (this.style.background) { qLayer.STYLE_BACKGROUND   = "background"; }
    else if (this.style.bgColor)    { qLayer.STYLE_BACKGROUND   = "bgColor"; }
    if (typeof(this.style.width) != "undefined") {
                                      qLayer.STYLE_WIDTH        = "width";
                                      qLayer.STYLE_HEIGHT       = "height"; }
    else if (typeof(this.style.pixelWidth) != "undefined") { 
                                      qLayer.STYLE_WIDTH        = "pixelWidth";
                                      qLayer.STYLE_HEIGHT       = "pixelHeight"; }
    
    qLayer.STYLE_PIXEL_SUFFIX = (document.childNodes) ? "px" : 0;
    
    if (this.style.clip.left) {
        this.setClip = function (left, right, top, bottom) {
            this.style.clip.left = left;
            this.style.clip.right = right;
            this.style.clip.top = top;
            this.style.clip.bottom = bottom;
        }
    } else {
        this.setClip = function (left, right, top, bottom) {
            this.style.clip = "rect("+left+"px "+right+"px "+top+"px "+bottom+"px)";
        }
    }
}

// DOM Detection -- critical because different browsers expose styles in different ways
if (document.getElementById) {          // ----- W3C DOM -----
    
    // Get Reference to Layer Object
    qLayer.prototype.getReference = function() { return document.getElementById(this.id).style; }
    
    // Property Names/Values
    qLayer.STYLE_VISIBLE            = "visibility";
    qLayer.STYLE_VISIBLE_SHOW       = "visible";
    qLayer.STYLE_VISIBLE_HIDE       = "hidden";
    
} else if (document.all) {              // ----- Explorer DOM ----- 
    
    // Get Reference to Layer Object
    qLayer.prototype.getReference = function() { return document.all[this.id].style; }
    
    // Property Names/Values
    qLayer.STYLE_VISIBLE            = "visibility";
    qLayer.STYLE_VISIBLE_SHOW       = "visible";
    qLayer.STYLE_VISIBLE_HIDE       = "hidden";
    
} else if (document.layers) {           // ----- Gecko DOM ----- 
    
    // Get Reference to Layer Object
    getReference = function (obj) {
        if(!obj) obj = document;
        var x = obj.layers;
        var foundLayer;
        if (x[this.id]) return x[this.id];
        for (var i=0; i < x.length; i++) {
            foundLayer = getReference(x[i]);
            if (foundLayer) return foundLayer;
        }
    }
    qLayer.prototype.style = getReference(qLayer.prototype.id);
    
    // Property Names/Values
    qLayer.STYLE_VISIBLE            = "visibility";
    qLayer.STYLE_VISIBLE_SHOW       = "show";
    qLayer.STYLE_VISIBLE_HIDE       = "hide";
    
}

// get/set position
qLayer.prototype.getLeft = function () { var v = parseInt(this.style.left); return (isNaN(v)) ? 0 : v; }
qLayer.prototype.getTop = function () { var v = parseInt(this.style.top); return (isNaN(v)) ? 0 : v; }
qLayer.prototype.setLeft = function (value) { this.style.left = value + qLayer.STYLE_PIXEL_SUFFIX; }
qLayer.prototype.setTop = function (value) { this.style.top = value + qLayer.STYLE_PIXEL_SUFFIX; }

// get/set size
qLayer.prototype.getWidth = function () { var v = parseInt(this.style[qLayer.STYLE_WIDTH]); return (isNaN(v)) ? 0 : v; }
qLayer.prototype.getHeight = function () { var v = parseInt(this.style[qLayer.STYLE_HEIGHT]); return (isNaN(v)) ? 0 : v; }
qLayer.prototype.setWidth = function (value) { this.style[qLayer.STYLE_WIDTH] = value + qLayer.STYLE_PIXEL_SUFFIX; }
qLayer.prototype.setHeight = function (value) { this.style[qLayer.STYLE_HEIGHT] = value + qLayer.STYLE_PIXEL_SUFFIX; }

// show/hide this layer
qLayer.prototype.show = function () { this.style[qLayer.STYLE_VISIBLE] = qLayer.STYLE_VISIBLE_SHOW; }
qLayer.prototype.hide = function () { this.style[qLayer.STYLE_VISIBLE] = qLayer.STYLE_VISIBLE_HIDE; }


// Get/Set Arbitrary Style -- NOTE: property names may vary
qLayer.prototype.getStyle = function (propName) { return this.style[propName]; }
qLayer.prototype.setStyle = function (propName, propValue) { this.style[propName] = propValue; }

