/**
 * The purpose of this Javascript is to extend the functionality of modules using 
 * phones/_list_of_phone_numbers.html.erb to be able to handle grouped functionality.
 */
$(document).ready(function() {
  $(".module .more, .module .less").click(function(event) {
    /* Grouping is determined by classes on the encapsulating DIV */
    var groups = $(this).parents("div.module").attr("class").match(/group_\w+/g) || [];
    if (groups.length > 0) /* If we have groups, find every using module.
                              We are here dynamically building a jQuery selector. */
      groups = $($.map(groups, function(x,i) { return "div."+x; }).join(", "))
    else
      groups = $(this).parents("ul:first");
    groups.find("li.hidden").toggle();
    groups.find("a.more").parent().toggle();
    event.preventDefault();
  });
});
