/****************************************************************************
  DRSC_rowtools.js - Routines for collapsing families of rows in a table.

  Modifications:
    ITF 20100624 Move rowtools logic from DRSC_gene_lookup.pl to here.
****************************************************************************/

//preload images for OpenClose
if (document.images) {
    btnPlus = new Image;
    btnMinus = new Image;
    btnPlus.src = "/images/DRSC-btn-plus-w6.gif";
    btnMinus.src = "/images/DRSC-btn-minus-w6.gif";
} else {
    btnPlus = "";
    brnMinus = "";
}

//the global array of display groups:
// Each display group is a set of rows with the same id.  We want to turn
// display on or off for all of them at once and track their last
// setting, since they may get turned off by a grandparent group,
// in which case we will want to restore their original state when
// the parent group is turned on.
// The array gets filled in after all the rows are created.
GroupInfo = new Object; // Object == associative array
//GroupList = new Array; // array of group names



function openClose(ThisID, ImageName)
{
    if (document.getElementById(ThisID).style.display == 'none')
    {
        document.getElementById(ThisID).style.display = '';
        document[ImageName].src = btnMinus.src;
   } else {
        document.getElementById(ThisID).style.display = 'none';
        document[ImageName].src = btnPlus.src;
    }
}



// get all the rows with a specific base name (not children)
function getRowsByName(basename)
{
    myrlist = new Array();
    if (isNav) {
        myrlist = document.getElementsByName("n" + basename);
    } else {
        // IE has getElementsByName, but it is perma-broken, because
        // being Microsoft means not having to care.
        allrows = document.getElementsByTagName('tr');
        //alert("found " + allrows.length + " TR tags");
        c = 0;
        for (i = 0; i < allrows.length; i++) {
            tr = allrows[i];
            if (tr.name && tr.name == "n" + basename) {
                myrlist[myrlist.length] = tr;
            }
        }
        //alert("myrlist.length = " + myrlist.length);
    }
    return myrlist;        
}



//openCloseRows('DRSC05220_Genl')
function openCloseRows(basename) // single group only - no children
{
    //alert("openCloseRows(" + basename + ") - isNav = " + isNav);

    rlist = getRowsByName(basename)
    //rlist = document.getElementsById("d" + basename);
    if (rlist.length) {
        dstate = rlist[0].style.display;
        ImageName = "i" + basename;
        if (dstate == 'none') {
            document[ImageName].src = btnMinus.src;
            GroupInfo[basename].state = '';
        } else {
            document[ImageName].src = btnPlus.src;
            GroupInfo[basename].state = 'none';
        }
        for (i = 0; i < rlist.length; i++) {
            if (dstate == 'none') {
                rlist[i].style.display = '';
            } else {
                rlist[i].style.display = 'none';
            }
        }
    } else {
        alert("No rows named n" + basename + ".");
    }
}



// turn a group on or off and adjust its children accordingly
function toggleGroup(groupname)
{
    // first we handle the named group and its image
    openCloseRows(groupname);
    newstate = GroupInfo[groupname].state;

    // now go through the group names, looking for children of
    // this group, and set their states to newstate (without
    // changing their stored state)
    for (var thisname in GroupInfo) //i = 0; i < GroupInfo.length; i++)
    {
        //thisname = GroupList[i];
        if (groupname == thisname.substring(0,groupname.length) && 
            groupname != thisname)
        {
            rlist = getRowsByName(thisname);
            if (newstate == 'none')
            {
                for (j = 0; j < rlist.length; j++)
                {
                    rlist[j].style.display = newstate;
                }
            }
            else if (GroupInfo[thisname].state != 'none')
            {
                // this child group is set to on, but we dont want to
                // display it unless its parent group is actually on
                parentgroup = GroupInfo[thisname].parent
                if (parentgroup) {
                    parentlist =
                        getRowsByName(parentgroup);
                    if (parentlist[0].style.display != 'none') {
                        for (j = 0; j < rlist.length; j++)
                        {
                            rlist[j].style.display = '';
                        }
                    }
                    //else { alert("parentlist[0].style.display == " + 
                    //             parentlist[0].style.display); }
                }
                //else { alert("No parentgroup = " + parentgroup); }
            }
        }
    }
}



// some groups get openned along with their children the first time
function firstOpenGroup(groupname)
{
    thisanchor = document.getElementById("a" + groupname);
    document["i" + groupname].src = btnMinus.src;
    //alert("firstOpenGroup(" + thisanchor + ", " + groupname + ")");
    // open this group and any children, set state for all to "not none"
    for (var thisname in GroupInfo) //i = 0; i < GroupList.length; i++)
    {
        //thisname = GroupList[i];
        if (groupname == thisname.substring(0,groupname.length))
        {
            rlist = getRowsByName(thisname);
            for (j = 0; j < rlist.length; j++)
            {
                rlist[j].style.display = '';
            }
            GroupInfo[thisname].state = '';
        }
    }

    // from now on, this button should call toggleGroup() instead
    thisanchor.href = "javascript: toggleGroup('" + groupname + "')";
}




// initialize the global data structure GroupInfo
//rows = getElementsByTagName('tr');
//for (i = 0; i < rows.length; i++)
function initGroupInfo()
{
    //alert(GroupInfo);

    rows = document.getElementsByTagName('tr');
    //alert("rows.length = " + rows.length);

    //for(i = 0; i < document.all.length; i++)
    for (i = 0; i < rows.length; i++)
    {
        //if (document.all(i).tagName != "TR")
        //{
        //    continue;
        //}
        //row = document.all(i);
        row = rows[i];
        thisid = row.id;
        if (!thisid || thisid.substring(0,1) != 'd')
        {
            continue;
        }
        thisgroup = thisid.substring(1);
        if (!(thisgroup in GroupInfo))
        {
            GroupInfo[thisgroup] = { state:row.style.display,
                                     children: new Array() };
            //alert("GroupInfo[" + thisgroup + "] initialized.");
            for (j = thisgroup.length-1; j > 0; j--) {
                //if (thisgroup == "Reagents_Amp") {
                //    alert("thisgroup.substring(j,j+1) = " +
                //          thisgroup.substring(j,j+1));
                //}
                if ("_" == thisgroup.substring(j,j+1)) {
                    parentgroup = thisgroup.substring(0,j);
                    if (!(parentgroup in GroupInfo)) {
                        alert("Parent " + parentgroup + " for group " +
                              thisgroup + " not found.")
                    } else {
                        GroupInfo[parentgroup].children[
                            GroupInfo[parentgroup].children.length] =
                            thisgroup;
                    }
                    GroupInfo[thisgroup].parent = parentgroup;
                    //alert("GroupInfo[" + thisgroup + "].parent = " +
                    //      parentgroup);
                    break;
                }
            }
        }
        //for (j = 0; j < GroupList.length; j++)
        //{
        //    if (GroupList[j] == thisgroup)
        //    {
        //        thisgroup = null;
        //        break;
        //    }
        //}
        // if thisgroup still a string, it is new and needs to be added
        //GroupList[GroupList.length]
    }
    //names = "";
    //for (var name in GroupInfo) {
    //    names = names + name + "\\n";
    //}
    //alert("Names = " + names);
}



