

function OT() {
}

/*
 * Puts the open string in a cookie on the browser....
 */
OT.pooOpenCookie = function(cookieName) {
    alert(OT.getOpenString());
    setCookie(cookieName, OT.getOpenString());
}




/*
 * Takes the open string from a cookie on the browser and
 * opens the specified nodes.
 */
OT.eatOpenCookie = function(cookieName, action) {
    var s = getCookie(cookieName);
    if (s == null) {
        return;
    }
    var nodes = new Array();
    var n = 0;
    var j = 0;
    var i = 0;
    for (i = 0; i < s.length; i++) {
        if (s[i] == '|') {
            if (j < i) {
                nodes[n] = s.substring(j, i);
                n++;
            }
            j = i + 1;
        }
    }
    if (i > j) {
        nodes[n] = s.substring(j, i);
        n++;
    }

    while (n > 0) {
        var got = false;
        for (var i = 0; i < n; i++) {
            var img = getById('oti_' + nodes[i]);
            if (img) {
                var d = nodes[i].indexOf('_');
                if (d > 0) {
                    OT.setOpen(action, nodes[i].substring(0, d), nodes[i].substring(d + 1, nodes[i].length), true);
                    nodes[i] = nodes[n - 1];
                    i--;
                    n--;
                    //alert(nodes[i]);
                }
            }
        }
        if (!got) {
            return;
        }
    }

}




/*
 * Returns a string telling which type/key nodes are open...
 * kind of lame, but can be stored in cookie to re-open same
 * nodes upon next visit...
 */
OT.getOpenString = function(tree) {
    // get all images and check their stupid id attributes to see if
    // they start with 'oti_'...
    var ss = '';
    var nis = OT.getAllNodeImages(tree);
    var tn = 5;
    if (tree) {
        tn += tree.length;
    }
    if (nis) {
        for (var i = 0; i < nis.length; i++) {
            var img = nis[i];
            var a = 0;
            if (img.src.length > 5) {
                a = img.src.charAt(img.src.length - 6);
            }
            if (a == 'h') {
                var s = nis[i].id.substring(tn, nis[i].id.length);
                ss += '|' + s;
            }
        }
    }
    return ss;
}


/*
 * Returns an array of all the toggle node images.
 */
OT.getAllNodeImages = function(tree) {
    var nodes = new Array();
    var numNodes = 0;
    var tn = 'oti_';
    if (tree) {
        tn += tree;
    }
    var tm = tn.length;
    for (var i = 0; i < document.images.length; i++) {
        if (document.images[i].id && document.images[i].id.length > tm && document.images[i].id.substring(0, tm) == tn) {
            nodes[numNodes] = document.images[i];
            numNodes++;
        }
    }
    return nodes;
}


/*
 * Returns whether the node specified by type and oid (key) is open.
 */
OT.isOpen = function(tree, type, oid) {
    var img = getById('oti_' + tree + '_' + type + '_' + oid);
    if (img) {
        var a = 0;
        var b = 0;
        var prefix = null;
        var ext = 'png';
        if (img.src.length > 5) {
            prefix = img.src.substring(0, img.src.length - 6);
            ext = img.src.substring(img.src.length - 3, img.src.length);
            a = img.src.charAt(img.src.length - 6);
            b = img.src.charAt(img.src.length - 5);
        }
        return a != 'h';
    }
    return false;
}


OT.getLoadingML = function() {
    return '<img src="/img/loading.gif" width="12" height="12" alt=""></img>&nbsp;loading...';
}

/*
 * Sets the node specified by type and oid (key) 
 * open or closed.
 */
OT.setOpen = function(action, tree, type, oid, opento) {
    var img = getById('oti_' + tree + '_' + type + '_' + oid);
    if (img) {
        var a = 0;
        var b = 0;
        var prefix = null;
        var ext = 'png';
        if (img.src.length > 5) {
            prefix = img.src.substring(0, img.src.length - 6);
            ext = img.src.substring(img.src.length - 3, img.src.length);
            a = img.src.charAt(img.src.length - 6);
            b = img.src.charAt(img.src.length - 5);
        }
        if (prefix == null) {
            prefix = '';
        }
        if (a == 'h' && !opento) {
            img.src = prefix + 'sn.' + ext;
            img.alt = 'open';
            var elm = getById(tree + '_' + type + '_' + oid);
            if (elm) setInnerHtml(elm, "");
        } else if (opento) {
            img.src = prefix + 'hn.' + ext;
            img.alt = 'close';

            var elm = getById(tree + '_' + type + '_' + oid);
            if (elm) setInnerHtml(elm, OT.getLoadingML());
            ajaxpage(action + '?tree=' + tree + '&type=' + type + '&key=' + oid, tree + '_' + type + '_' + oid);
            //alert('(' + type + '_' + oid + ') => (' + action + '?type=' + type + '&key=' + oid + ')');
        }
    }
}




/*
 * OrgTree.onClick
 */
OT.c = function(img, action, tree, type, oid) {
    // ab.png
    var a = 0;
    var b = 0;
    var prefix = null;
    var ext = 'png';
    if (img.src.length > 5) {
        prefix = img.src.substring(0, img.src.length - 6);
        ext = img.src.substring(img.src.length - 3, img.src.length);
        a = img.src.charAt(img.src.length - 6);
        b = img.src.charAt(img.src.length - 5);
    }
    if (prefix == null) {
        prefix = '';
    }
    if (a == 'h') {
        img.src = prefix + 'sh.' + ext;
        img.alt = 'open';
        var elm = getById(tree + '_' + type + '_' + oid);
        if (elm) setInnerHtml(elm, "");
    } else {
        img.src = prefix + 'hh.' + ext;
        img.alt = 'close';

        var elm = getById(tree + '_' + type + '_' + oid);
        if (elm) setInnerHtml(elm, OT.getLoadingML());
        ajaxpage(action + '?tree=' + tree + '&type=' + type + '&key=' + oid, tree + '_' + type + '_' + oid);
        //alert('(' + type + '_' + oid + ') => (' + action + '?type=' + type + '&key=' + oid + ')');
    }
}

/*
 * OrgTree.onMouseOver
 */
OT.h = function(img) {
    // ab.png
    var a = 0;
    var prefix = null;
    var ext = 'png';
    if (img.src.length > 5) {
        prefix = img.src.substring(0, img.src.length - 6);
        ext = img.src.substring(img.src.length - 3, img.src.length);
        a = img.src.charAt(img.src.length - 6);
    }
    if (prefix == null) {
        prefix = '';
    }
    if (a == 'h') {
        img.src = prefix + 'hh.' + ext;
        img.alt = 'close';
    } else {
        img.src = prefix + 'sh.' + ext;
        img.alt = 'open';
    }
}


/*
 * OrgTree.onMouseOut
 */
OT.n = function(img) {
    // ab.png
    var a = 0;
    var prefix = null;
    var ext = 'png';
    if (img.src.length > 5) {
        prefix = img.src.substring(0, img.src.length - 6);
        a = img.src.charAt(img.src.length - 6);
    }
    if (prefix == null) {
        prefix = '';
    }
    if (a == 'h') {
        img.src = prefix + 'hn.' + ext;
        img.alt = 'close';
    } else {
        img.src = prefix + 'sn.' + ext;
        img.alt = 'open';
    }
}

