
ComboElement = function () {
    this.elm = arguments[0];
    this.name = this.elm.name;
    this.resize();
}

ComboElement.prototype.resize = function() {
    this.l = this.elm.offsetLeft;
    this.t = this.elm.offsetTop;
    
    var p = this.elm.offsetParent;
    while (p) {
        this.l += p.offsetLeft;
        this.t += p.offsetTop;
        
        p = p.offsetParent;
    }

    this.r = this.l + this.elm.offsetWidth;
    this.b = this.t + this.elm.offsetHeight;
}

ComboHideObject = function () {
    this.cache = [];
    this.s = new Array();
    this.visible = true;
    this.started = false;
    };

ComboHideObject.prototype.loaderStart=function() {
    //var sels = document.all.tags('select');
    //var objs = document.all.tags('object');
    this.started = true;
    var sels = [];
    var objs = [];
    if (is.ie) {
        sels = document.all.tags('select');
        objs = document.all.tags('object');
    } else {
        objs = document.getElementsByTagName('embed');
    }
    var j = 0;

    var num = sels.length;
    for (var i = 0; i < num; i++) {
        this.s[j++] = new ComboElement(sels[i]);
    }

    num = objs.length;
    for (var i = 0; i < num; i++) {
        this.s[j++] = new ComboElement(objs[i]);
    }

    this.num = this.s.length;
};

ComboHideObject.prototype.resize=function() {
    for (var i = 0; i < this.num; i++) {
        this.s[i].resize();
    }
    this.cache = [];
};

ComboHide = null;

if (!is.dom) {
    ComboHideObject.prototype.show = function () {}
    ComboHideObject.prototype.hide = function () {}
}
else {

    ComboHideObject.prototype.show = function () {
        if (this.visible) return;
        for (var i = 0; i<this.num; i++)
            this.s[i].elm.style.visibility = 'inherit';
        this.visible = true;
    }

    ComboHideObject.prototype.hide = function () {
        if (!this.visible) return;
        if (!this.started) {
            this.loaderStart();
        }

        var lay = arguments[0];
        var id = lay.id;

        if (this.cache[id]) {
            for (var i=0; i<this.cache[id].length; i++) {
                this.s[this.cache[id][i]].elm.style.visibility = 'hidden';
            }
        }
        else {
            this.cache[id] = [];
            var clip = lay.getClip();
            var l = lay.getX() + clip[3];
            var t = lay.getY() + clip[0];
            var r = lay.getX() + lay.getWidth() + clip[1];
            var b = lay.getY() + lay.getHeight() + clip[2];
            //alert (l + ':' + t + ':' + r + ':' + b + '\n' + clip);
            //window.status =  l + ':' + t + ':' + r + ':' + b + '\n' + clip;
            for (var i = 0; i<this.num; i++) {
                if (this.collision(this.s[i], l, t, r, b)) {
                    this.s[i].elm.style.visibility = 'hidden';
                    this.cache[id][this.cache[id].length] = i;
                    }
            }
        }
        this.visible = false;
    }        

    ComboHideObject.prototype.collision = function (sel, left2, top2, right2, bottom2) {

        var left1 = sel.l;
        var right1 = sel.r;
        var top1 = sel.t;
        var bottom1 = sel.b;
        //alert(left1 + ', ' + right1 + ', ' + top1 + ', ' + bottom1);
        //alert ('bottom1 < top2:' + bottom1  + '<' + top2);
        if (bottom1 < top2) return false;
        //alert ('top1 < bottom2:' + top1  + '<' + bottom2);
        if (top1 > bottom2) return false;

        //alert ('right1 < left2:' + right1  + '<' + left2);
        if (right1 < left2) return false;
        //alert ('left1 < right2:' + left1  + '<' + right2);
        if (left1 > right2) return false;

        return true;

    }


    DynAPI.addLoadFunction("ComboHide.loaderStart()")
    DynAPI.addResizeFunction("ComboHide.resize()")

}

ComboHide = new ComboHideObject();
