﻿//
// JQuery Plugin for handling accordion templates and accordion document tables.
//
(function($) {

  //
  // Convert an accordion section/table so that it...
  // - shows the expand all/collapse all if there is more than one row
  // - adds expand/collapse functionalty to the individual sections
  //
  $.fn.emcAccordion = function() {
                  
    return $(this).each(function(i) {
      var rootNode = $(this);
      var isDocumentTable = (rootNode.attr("class").indexOf("documentTableContainerAccordion") != -1);

      var accordionSectionCount = rootNode.find("div.col1").length;

      //
      // If we have more than 1 accordion section then show the expand all/collapse all section.
      //
      if (accordionSectionCount > 1) {
        rootNode.find("div.col1-control").css("display", "block");
      }

      //
      // Add onclick events for expand all/collapse all section.
      //
      rootNode.find("div.col1-control div.controls a").bind("click", function() {
        return $.fn.emcAccordion.toggleAllSections($(this), isDocumentTable);
      });

      //
      // Close everything but the first section (and make open/close buttons have correct +- sign)...
      //
      var counter = 0;
      var selector = isDocumentTable ? "div.col1 table" : "div.col1 div.Content01, div.col1 div.Content04";
      rootNode.find(selector).each(function() {
        counter++;
                        
        if (!(counter == 1 && i == 0 && $.fn.GetBookmark() == "")){
          $(this).css("display", "none");
        }
      });
      counter = 0;
      rootNode.find("div.col1 div.col1-head div.action a.close").each(function() {
        counter++;
        if (!(counter == 1 && i == 0)){
          $(this).removeClass("close").addClass("open");
        }
      });

      //
      // Add onlick handler for open/close buttons
      //
      rootNode.find("div.col1 div.col1-head div.action a").bind("click", function() {
        return $.fn.emcAccordion.toggleSection($(this), isDocumentTable);
      });

    });
  };

  //
  // Toggle an individual section (pass in the link obj)
  //
  $.fn.emcAccordion.toggleSection = function(obj, isDocumentTable) {
    var sectionContainer;
    if (isDocumentTable) {
      sectionContainer = obj.parent().parent().parent().children("table");
    } else {
      sectionContainer = obj.parent().parent().parent().children("div:eq(1)");
    }

    if (sectionContainer.is(":hidden")) {
      obj.addClass("close").removeClass("open");
      if (isDocumentTable) {
        sectionContainer.css("display", "");  // Special case "" for tables
      } else {
        sectionContainer.css("display", "block");
      }
    } else {
      obj.addClass("open").removeClass("close");
      sectionContainer.css("display", "none");
    }

    return false;
  };

  //
  // Toggles all accordion sections on or off (hideSections = true/false)
  //
  $.fn.emcAccordion.toggleAllSections = function(obj, isDocumentTable) {
    var expandSections = obj.text() == "Expand All";

    var sectionContainer = obj.parent().parent().parent();

    if (expandSections) {
      sectionContainer.find("div.action a.open").addClass("close").removeClass("open");
      if (isDocumentTable) {
        sectionContainer.find("div.col1 table").css("display", "");
      } else {
        sectionContainer.find("div.col1 div.Content01, div.col1 div.Content04").css("display", "block");
      }
    } else {
      sectionContainer.find("div.action a.close").addClass("open").removeClass("close");
      if (isDocumentTable) {
        sectionContainer.find("div.col1 table").css("display", "none");
      } else {
        sectionContainer.find("div.col1 div.Content01, div.col1 div.Content04").css("display", "none");
      }
    }

    return false;
  };
  
  $.fn.GetBookmark = function(){
    var anchorRef = self.document.location.hash;
    return anchorRef.replace("#","");
  }
  
})(jQuery);