var nupack;

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

nupack.event.legend_activate_callback = function(fieldset)
{
  return (function ()
    {
      // set each div to visible
      /*divs_to_display.each( function(div)
        {
          // cannot use Element.show on things with external style def's. Only works on
          // inline style="display: none;"
          if (div.style.display != 'block')
          {
            div.style.display = 'block';
          }
          else
          {
            div.style.display = 'none';
          }
        }
      );*/
      
      var activated_class = 'activated';
      
      if (Element.hasClassName(fieldset, activated_class))
      {
        Element.removeClassName(fieldset, activated_class);
      }
      else
      {
        Element.addClassName(fieldset, activated_class);
      }
      
    }
  );
}


// add onmouseover to all legends in fieldsets with class='pop_open'
// add onmouseover to all divs with class 'fieldset_contents' inside said fieldsets
nupack.event.set_listeners_on_fieldsets = function()
{
    
  // find all fieldsets with the appropriate class
  pop_open_fieldsets = nupack.utility.all_tags_with_class(document, 'fieldset', 'pop_open');
  
  //alert("found " + pop_open_fieldsets.length + " fieldsets");
  
  // for each fieldset.pop_open, find the legends and attach a mouseover handler to each one.
  // Each legend will have a handler that displays all of the div.fieldset_contents in the fieldset.
  // Usually there will only be one legend and one such div,.
  pop_open_fieldsets.each( function(fieldset)
    {
      //var divs_to_display = all_tags_with_class(fieldset, 'div', 'fieldset_contents');
      
      //alert("found divs: " + divs_to_display.length);
      
      // for each legend in the fieldset, add the onmouseover handler to display all
      // .fieldset_contents divs
      $A(fieldset.getElementsByTagName('legend')).each( function(legend)
        {

          //alert("found legend: " + legend.tagName);
          legend.addEventListener('click', nupack.event.legend_activate_callback(fieldset), false);
        }
      );
      
      // add handlers to each div.fieldset_contents to make it disappear on mouseout
      // not doing this for now -- click again on the legend to make it disappear.
      /*
      divs_to_display.each( function(div)
        {
          //div.addEventListener('click', div_deactivate_callback(div), false)
        }
      );
      */
    }
  );
}


nupack.event.set_event_listeners = function()
{
  //alert("loaded");
  if ($("partition_job_num_sequences"))
  {
    $("partition_job_num_sequences").addEventListener('change', nupack.change_sequence_inputs, false);
  }
  
  nupack.event.set_listeners_on_fieldsets();
}
