/*
   These functions collapse and expand menu items

*/
    var STATE = 1;

    function toggle(THISTREE){
       if(STATE == 0){
          expand_all(THISTREE)
          STATE = 1;
       }else{
          collapse_all(THISTREE,0);
          STATE = 0;
       }
  }

  
    // this is needed to deselect all menu items for 
    //all other trees on the page
    //
    function deselect_others(THIS_TREE){
        for(var j=0; j<MY_TREES.length; j++)
           {
           if( TREES[MY_TREES[j]] != TREES[THIS_TREE])
              {
              var thisTree = (TREES[MY_TREES[j]]);
              var thisSelection = thisTree.o_selected;
              if( thisSelection.n_id !== undefined)
                 {
                 thisSelection.select(true);
                 thisTree.ndom_refresh();
                 }
              }
           }
    }

    function expand_all(n_index, n_depth){
         var o_tree = TREES[n_index ? n_index : 0];
         if (!o_tree) alert ("Menu is not initialized!");
         var a_nodes = o_tree.a_nodes;
         for (var i = 0; i<a_nodes.length; i++)
                 if (n_depth == null || a_nodes[i].n_depth <= n_depth)
                         a_nodes[i].open(0,1);
         o_tree.ndom_refresh();
  }

    function collapse_all(n_index, n_depth){
         var o_tree = TREES[n_index ? n_index : 0];
         if (!n_depth) n_depth = 1;
         if (!o_tree) alert ("Menu is not initialized!");
         var a_nodes = o_tree.a_nodes;
         for (var i = a_nodes.length - 1; i >= 0; i--)
                 if (a_nodes[i].n_depth >= n_depth && a_nodes[i].open)
                         a_nodes[i].open(1,1);
         o_tree.ndom_refresh();
  }              

/* 
    replace image
*/

      function replaceImage(imgId){
        img = document.getElementById(imgId)
        imgSrc = img.src;
        imgArray= new Array(10);
        imgArray = imgSrc.split('/');
        newSrc='/images/icons/tree/collapse_b.gif';
        thisImage=imgArray.pop();
        if (thisImage == 'arrow_right_green.gif'){
           newSrc='/images/icons/arrow_down_green.gif';
        }else if (thisImage == 'arrow_down_green.gif'){
           newSrc='/images/icons/arrow_right_green.gif';
        }else if (thisImage == 'arrow_right_white.gif'){
           newSrc='/images/icons/arrow_down_white.gif';
        }else if (thisImage == 'arrow_down_white.gif'){
           newSrc='/images/icons/arrow_right_white.gif';
        }
        img.src=newSrc;
      }

/*
        This function opens first tree item which has the caption specified.
        Closing the existing open items is optional

*/
function openItemByCaption (s_caption, treeName, close) {


        // set to true when debugging the application
        var B_DEBUG = false;
        CURRENT_ITEM = s_caption;

        if (B_DEBUG) 
                 alert("looking for item with caption '" + s_caption+ "'.")

        // exit if required parameter isn't specified
        if (!s_caption)
                return (B_DEBUG
                        ? alert("Required parameter to function openItemByCaption is missing")
                        : false
                );

        // use first tree on the page if tree object isn't explicitly defined
        if (!treeName){
        if (B_DEBUG)
                alert("tree is not defined, assuming it is the first one!")
                o_tree = (TREES[0]);
        } else {
                o_tree = (TREES[treeName]);
        }

        if (!o_tree)
                return (B_DEBUG
                        ? alert("No Tigra Tree Menu PRO instances can be found on this page")
                        : false
                );

    // close all the open items first
    if (close){
        if (B_DEBUG) 
                 alert("tree has " + o_tree.a_nodes.length + " nodes, closing all nodes first!")
        var a_nodes = o_tree.a_nodes;
        var n_depth = 1;
        for (var i = a_nodes.length - 1; i >= 0; i--)
                if (a_nodes[i].n_depth >= n_depth && a_nodes[i].open)
                        a_nodes[i].open(1,1);
        if (B_DEBUG)
                 alert (MY_TREES.length);
        o_tree.ndom_refresh();
    }

    // find item with specified caption
    var a_item = o_tree.find_item(s_caption);
    if (B_DEBUG) 
             alert("found item ("+a_item.length+") long")

    if (B_DEBUG)
             alert(a_item[0].state());

    for(var n=0; n < a_item.length; n++) {
        o_item=a_item[n];
        // collect info about all item's parents
        var n_id = o_item.n_id,
                n_depth = o_item.n_depth,
                a_index = o_item.o_root.a_index,
                a_parents = [o_item];

        while (n_depth) {
                if (a_index[n_id].n_depth < n_depth) {
                        a_parents[a_parents.length] = a_index[n_id];
                        n_depth--;
                }
                n_id--;
        }

        // open all parents starting from root
        for (var i = a_parents.length-1; i >= 0; i--)
           // check if node or root
           if (a_parents[i].n_state & 48){
              a_parents[i].open();
                if (s_caption == a_parents[i].a_config[0]){
                   a_parents[i].select();
		}
           }else{
              a_parents[i].select();
              if (B_DEBUG) 
                 alert("Item with caption '" + a_parents[i].a_config[0]+ 
                    "' is a leaf.\nHierarchy will be opened to its parent node only.")
           }
    }
    deselect_others(treeName);
}
