var nupack;

if (!nupack) nupack = {};
if (!nupack.utility) nupack.utility = {}

//looks only in direct descendants.
nupack.utility.delete_child_tags = function(node, tag)
{
    for(var i = 0; i < node.childNodes.length; i++)
    {
        el = node.childNodes[i];
        // only check if it's a tag -- 1 means element, 3 means text node, etc
        if(el.nodeType == 1 && el.tagName.toLowerCase() == tag.toLowerCase())
        {
            node.removeChild(el);
            i--; // we've just shortened the array by one
        }
    }
}

// returns the number of child nodes that are the specified tag (li, div, etc)
nupack.utility.count_child_tags = function(node, tag, recurse_level)
{
    var count = 0;
    for(var i = 0; i < node.childNodes.length; i++)
    {
        el = node.childNodes[i];
        // only check if it's a tag -- 1 means element, 3 means text node, etc
        if(el.nodeType == 1 && el.tagName.toLowerCase() == tag.toLowerCase())
        {
            count += 1;
        }
        else if (recurse_level && recurse_level>0){
          count+=nupack.utility.count_child_tags(el, tag, recurse_level-1);
        }
    }
    return count;
}

// returns the number of child nodes that are the specified tag (li, div, etc)
nupack.utility.get_child_nodes = function(node, tag, recurse_level)
{

    var count = 0;
    var elems= new Array();
    var newElems;
    for(var i = 0; i < node.childNodes.length; i++)
    {
        el = node.childNodes[i];
        // only check if it's a tag -- 1 means element, 3 means text node, etc
        if(el.nodeType == 1 && el.tagName.toLowerCase() == tag.toLowerCase())
        {
            elems[count]=el.id;
            count += 1;
        }
        else if (recurse_level && recurse_level>0){
          newElems=nupack.utility.get_child_nodes(el, tag, recurse_level-1);
          elems=elems.concat(newElems);
        }
    }

    return elems;
}


// Simple helper method. You, uh, specify a tagname and a class. Yup.
nupack.utility.tag_with_class = function(tag,cls)
{
    var node = document.createElement(tag);
    node.className = cls;
    return node;
}

// returns an array of all tags of the appropriate name
nupack.utility.all_tags_with_class = function(root_node, tagname, classname)
{
  tags = $A(root_node.getElementsByTagName(tagname));

  regex = new RegExp(classname);

  tags_with_class = tags.findAll( function(tag)
    {
      return tag.className.match(regex);
    }
  )

  return tags_with_class;
}

function resize_element_from_id(id1, id2){
  var results_2d_cell=document.getElementById(id1);
  if (!results_2d_cell){
    console.log("resize_element_from_id: could not find element id "+id1);
    return;
  }

  var table_width=results_2d_cell.clientWidth;
  var table_height=results_2d_cell.clientHeight;

  var utility_2d_spinner=document.getElementById(id2);
  if (!utility_2d_spinner){
    console.log("resize_element_from_id: could not find element id "+id2);
    return;
  }

  utility_2d_spinner.style.width=table_width+"px";
  utility_2d_spinner.style.height=table_height+"px";
}
