/**
 * QLIB 1.0 Overlay Layer
 * Serge Dolgov (C) 2002
 * studio.quazzle.com
 */

function QLayer_update_ie(op) {
    this.layer.style.filter = (this.filter || "") + (op < 100 ? "alpha(opacity=" + op + ")" : "");
}

function QLayer_update_moz(op) {
    this.layer.style.MozOpacity = op + "%";
}

function QLayer_nop() {
}

function QLayer_show_dom2(show) {
    if (this.visible != show) {
        if (this._TID) {
            clearTimeout(this._TID);
            this._TID = null;
        }
        // !!! all transition effects are disabled so far. Reason: IE6 bug
        var step = 0;//show ? this.effects & 255 : -(this.effects >>> 8);
        if (step) {
            if (!this.visible) {
                this.update(0);
                this.layer.style.visibility = "visible";
                this.visible = true;
                this.fadeEffect(0, step);
            } else {
                this.fadeEffect(this.opacity, step);
            }
        } else {
            this.layer.style.visibility = (this.visible = show) ? "visible" : "hidden";
            if (this.onShow) this.onShow(show, this.tag);
        }
    }
}

function QLayer_fadeEffect(op, step) {
    this._TID = null;
    if (step) {
        op += step;
        if ((op > 0) && (op < this.opacity)) {
            this.update(op);
            this._TID = setTimeout(this.name + ".fadeEffect(" + op + "," + step + ")", 40);
        } else {
            if (op <= 0) {
                this.layer.style.visibility = "hidden";
                this.visible = false;
                this.update(100);
            } else {
                this.update(this.opacity);
            }
            if (this.onShow) this.onShow(this.visible, this.tag);
        }
    }
}

function QLayer_show_ns4(show) {
    if (this.visible != show) {
        this.layer.visibility = (this.visible = show) ? "show" : "hidden";
        if (this.onShow) this.onShow(show, this.tag);
    }
}

function QLayer_focus_dom2() {
    this.layer.style.zIndex = QLayer.TOPINDEX++;
}

function QLayer_focus_ns4() {
    this.layer.zIndex = QLayer.TOPINDEX++;
}

function QLayer_moveTo_dom2(x, y) {
    this.layer.style.left = (this.posx = x) + "px";
    this.layer.style.top = (this.posy = y) + "px";
}

function QLayer_moveTo_ns4(x, y) {
    this.layer.moveTo(this.posx = x, this.posy = y);
}

function QLayer_setOpacity(op) {
    this.update(this.opacity = op - 0);
}

function QLayer_setFilter(codes) { // IE specific
    this.filter = "";
    if (codes & 1) this.filter += "Gray() ";
    if (codes & 2) this.filter += "Xray() ";
    if (codes & 4) this.filter += "Invert() ";
    if (codes & 8) this.filter += "shadow(direction=135) ";
    if (codes & 16) this.filter += "dropShadow(offX=3,offY=3) ";
    this.update(this.opacity);
}

function QLayer(parent, name, posx, posy, width, zindex, visible, content) {
    this.init(parent, name);
    this.posx = posx;
    this.posy = posy;
    this.width = width;
    this.zindex = zindex;
    this.visible = visible;
    this.content = content;
    this.opacity = 100;
    this.setOpacity = QLayer_setOpacity;
    this.setFilter = QLayer_setFilter;
    this.fadeEffect = QLayer_fadeEffect;
    this.update = this.show = this.focus = this.moveTo = QLayer_nop;
    if (document.getElementById || document.all) {
        document.write('<div id="' + this.name + '" style="position:absolute;left:' + posx +
            'px;top:' + posy + 'px;width:' + width + 'px;visibility:' + (visible ? 'visible' : 'hidden') +
            ';overflow:hidden' + (zindex ? ';z-index:' + zindex : '') + ';text-align:left"><div class="layer">');
        if (typeof content == "function") this.content(); else document.write(content);
        document.write('</div></div>');
        this.layer = document.getElementById ? document.getElementById(this.name) : document.all[this.name];
        this.show = QLayer_show_dom2;
        this.focus = QLayer_focus_dom2;
        this.moveTo = QLayer_moveTo_dom2;
        if (this.layer.style.filter != null) {
            this.update = QLayer_update_ie;
        } else if (this.layer.style.MozOpacity != null) {
            this.update = QLayer_update_moz;
        }
    } else if (document.layers) {
        document.write('<layer pagex="' + posx + '" pagey="' + posy + '" width="' + width +
            '" visibility="' + (visible ? 'show' : 'hidden') + '"' + (zindex ? ' z-index="' + zindex + '"' : '') +
            ' onLoad="self.document.layers[\'' + this.name + '\']=this"><div class="layer">');
        if (typeof content == "function") this.content(); else document.write(content);
        document.write('</div></layer>');
        this.show = QLayer_show_ns4;
        this.focus = QLayer_focus_ns4;
        this.moveTo = QLayer_moveTo_ns4;
        this.layer = document.layers[this.name];
        // migrate QLIB images to main document
        with (this.layer.document) {  
            for (var j in images) {
                if (j.charAt && (j.charAt(0) == "$")) document.images[j] = images[j];
            }
        }
    } else {
        document.write("No support for layers");
    }
    this.effects = (this.update == QLayer_nop) ? 0 : 8;
}
QLayer.prototype = new QControl();
QLayer.TOPINDEX  = 1000;

QLayer.NONE    = 0; // layer effects
QLayer.DEFAULT = 8;
QLayer.SLOW    = 514;
QLayer.FAST    = 25;

QLayer.GRAY    = 1; // filters
QLayer.XRAY    = 2;
QLayer.INVERT  = 4;
QLayer.SHADOW  = 8; 
QLayer.CASTING = 16;