﻿var divs, txts;
var divsV;
function init() {
    divs = $("divs");
    txts = $("txts");

    if (txts.addEventListener) {
        txts.addEventListener('keydown', kd, false);
        txts.addEventListener('keyup', s, false);
        txts.addEventListener('keypress', kp, false);
    } else if (txts.attachEvent) {
        txts.attachEvent('onkeydown', kd);
        txts.attachEvent('onkeyup', s);
        txts.attachEvent('onkeypress', kp);
    } else {
        txts["onkeydown"] = kd;
        txts["onkeyup"] = s;
        txts["onkeypress"] = kp;
    }
}

function $(id) {
    return document.getElementById(id);
}

function tc(o, i) {
    var n;
    var r = true;

    var oo = o;
    if (o.className != "sub" && (o.parentNode.nextSibling.cells))
        while (oo.parentNode.nextSibling && oo.parentNode.nextSibling.cells && isSub(n = oo.parentNode.nextSibling.cells[0])) {
        if (n.className.substr(n.className.length - 1, 1) == "N") {
            n.className = n.className.substr(0, n.className.length - 2)
            n.parentNode.className = ""
        }
        else {
            n.className += " N";
            n.parentNode.className = "N";
        }

        oo = n;
        r = false;
    }
    if (!r)
        o.className = (o.className == "op" ? "cl" : "op");

    updhdnTree(o.parentNode.parentNode, i)
    return r;
}

function hideall(id, ti) {
    var t = $(id);
    var tds = t.getElementsByTagName("td")
    for (i = 0; i < tds.length; i++) {
        var o = tds[i];
        if (o.className == "sub") {
            o.className = "sub N";
            o.parentNode.className = "N";
        }
        else if (o.className == "op")
            o.className = "cl";
    }

    updhdnTree(t, ti);
}

function updhdnTree(t, ii) {
    var tree = new Array();
    for (var i = 1; i < t.rows.length; i++) {
        var n = t.rows[i].cells[0];
        if (isSub(n)) {
            if (n.className.substr(n.className.length - 1, 1) == "N")
                tree.push(n.className.substr(n.className.length - 1, 1))
            else
                tree.push("");
        }
        else
            tree.push(n.className);

    }
    document.cookie = (ii > 0 ? "tree=" : "treeA=") + tree.join("|");


}

function isSub(cell) {
    return (cell.className.indexOf("sub") == 0);
}

function swapImg(src, zoom, dida) {
    pimg.src = src;
    pimg.zoom = zoom;
    pimg.style.cursor = (zoom ? "pointer" : "default");
    $("dida").innerHTML = dida;
    if (pimg.azoom) {
        pimg.azoom.href = (zoom);
        pimg.azoo1.style.display = (zoom ? "" : "none");
    }
    return false;
}


var _wo;
function wo(a, n, w, h, o) {
    _wo = null;
    _wo = window.open(a, n, (o != null ? o + ',' : '') + 'width=' + w + ',height=' + h);
    if (_wo != null) _wo.focus();
    return (_wo == null);
}

function zoomImg(img) {
    if (img.zoom)
        return wo('zoom.aspx?img=' + img.zoom, "zoom", 600, 600, "menubar=no,location=no,status=no,resizable=yes,toolbar=no,scrollbars=yes,left=0,top=0");
    else
        return false;
}

function fitzoom() {
    var w, h;

    var maxw = Math.round(screen.width * 0.9)
    var maxh = Math.round(screen.height * 0.9)

    with (document.images[0]) {
        w = ((w = width + 50) > maxw ? maxw : w);
        h = ((h = height + 70) > maxh ? maxh : h);
    }
    if (window.outerWidth) {
        window.outerWidth = w;
        window.outerHeight = h;
    }
    else if (window.resizeTo) {
        window.resizeTo(w, h);
    };

    window.focus();
    /*w = document.images[0].width - w;
    h = document.images[0].height - h;
    if (window.resizeBy) 
    window.resizeBy(w,h)
    else if (window.outerWidth) {
    window.outerWidth = w;
    window.outerHeight = h;
    }
    */
}

function showopz(ev, btn, div) {
    hideopz();

    div = $(div);

    var t = btn.offsetTop;
    var p = btn;
    while ((p = (lastp = p).offsetParent) != null)
        t += p.offsetTop;

    if (document.documentElement && document.documentElement.scrollTop) {
        var scrollY = document.documentElement.scrollTop;
    } else if (document.body && document.body.scrollTop) {
        var scrollY = document.body.scrollTop;
    } else if (window.pageYOffset) {
        var scrollY = window.pageYOffset;
    } else if (window.scrollY) {
        var scrollY = window.scrollY;
    }


    if (window.innerHeight) {
        var height = window.innerHeight;
    } else if (document.body && document.documentElement.clientHeight) {
        var height = document.documentElement.clientHeight;
    } else if (document.body && document.body.clientHeight) {
        var height = document.body.clientHeight;
    }

    var bodyh = scrollY + height;
    div.style.display = "";

    if ((t + div.offsetHeight) > bodyh) t -= (t + div.offsetHeight - bodyh);

    div.style.top = t + 'px';

    dontPropagate(ev);
    ev.returnValue = false;
    return false;
}

function hideopz() {
    var divs = document.getElementsByTagName("div");
    for (var i = 0; i < divs.length; i++)
        if (divs[i].className == "artopz")
        divs[i].style.display = "none";

}

function dontPropagate(ev) {
    if (ev.stopPropagation)
        ev.stopPropagation();
    ev.cancelBubble = true;

}


var divih;
function d(e, a, d, p, v, s) {
    with ($(d)) {
        if (divih == null)
            divih = innerHTML;
        innerHTML = divih.replace(/\{0\}/g, p).replace(/\{1\}/g, v).replace(/\{2\}/g, s);

        style.left = (mouseX(e) - style.width.substring(0, style.width.length - 2)) + "px";
        style.top = (mouseY(e) + 10) + "px";
        style.display = "block";

    }

}

function nd(e, d) {
    $(d).style.display = "none";
}

function mouseX(evt) {
    if (evt.pageX) return evt.pageX;
    else if (evt.clientX)
        return evt.clientX + (document.documentElement.scrollLeft ?
                                document.documentElement.scrollLeft :
                                   document.body.scrollLeft);
    else
        return null;
}

function mouseY(evt) {
    if (evt.pageY) return evt.pageY;
    else if (evt.clientY)
        return evt.clientY + (document.documentElement.scrollTop ?
                                document.documentElement.scrollTop :
                                document.body.scrollTop);
    else return null;
}

/*
function setdivSize(source, eventArgs) {
document.getElementById('divs').style.width = '550px';
}
*/

var st = null;

function s(e) {
    e = e || event;

    if ((kc = e.keyCode) == 38 || kc == 40 || kc == 13 || kc == 35 || kc == 36) return true;

    if (st != null) window.clearTimeout(st);
    if (txts.value.length > 2) {
        st = window.setTimeout("s2()", 100)
    }
    else {
        st = null;
        divs.style.visibility = "hidden";
        divsV = false;
        srow = -1;
    };
}

function s2() {
    st = null;
    dominaws.Search(idLingua, txts.value, s3)
}

function s3(res) {
    with (divs) {
        style.visibility = "visible";
        innerHTML = res;
    }
    divsV = true;
    srow = -1;
}

function cs(e) {
    if ((id = (e.target || e.srcElement).id) != "divs" && id != "txts") {
        divs.style.visibility = "hidden";
        divsV = false;
        srow = -1;
    }
    else
        txts.focus();
}

var srow = -1;
function kd(e) {
    e = e || event;

    if (divsV) {
        if ((kc=e.keyCode) == 38 || kc == 40 || kc == 36 || kc == 35) {
            var childs = divs.firstChild.children;
            if (srow >= 0) childs[srow].className = '';
            switch (kc) {
            case 40:
                if (srow < childs.length - 1) srow += 1;
                break;
            case 38:
                if (srow > 0) srow -= 1;
                break;
            case 35:
                srow = childs.length - 1;
                break;
            case 36:
                srow = 0;
                break;
            }
            childs[srow].className = 'hi';
            var as = childs[srow].getElementsByTagName("a");
            with (as[as.length - 1]) {
                if ((o = ((kc==40||kc==36 ? offsetTop + offsetHeight : as[0].offsetTop) - divs.scrollTop)) > divs.offsetHeight || o <0) scrollIntoView(false);
            }
        } else if (kc == 13) {
            gs(e);
            return false;
        }
    } 

}

function kp(e) {
    e = e || event;

    if (divsV) { 
        if (e.keyCode == 13 && srow >= 0) {
            gs(e);
            return false;
        } 
    }

}

function gs(e) {
    var childs = divs.firstChild.children;
    divs.style.visibility = "hidden";
    location.href = childs[srow].getElementsByTagName("a")[0].href;
    be(e);
}

function be(e) {
    e.cancelBubble = true;
    if (e.stopPropagation) {
        e.stopPropagation();
    }
    e.returnValue = false;
    if (e.preventDefault) {
        e.preventDefault();
    }
}

function invp(el) {
    var top = el.offsetTop;
    var left = el.offsetLeft;
    var width = el.offsetWidth;
    var height = el.offsetHeight;

    while (el.offsetParent) {
        el = el.offsetParent;
        top += el.offsetTop;
        left += el.offsetLeft;
    }

    return (
    top >= window.pageYOffset &&
    left >= window.pageXOffset &&
    (top + height) <= (window.pageYOffset + window.innerHeight) &&
    (left + width) <= (window.pageXOffset + window.innerWidth)
  );
}