/* sizer.js : 

Used to resize content containers on audio headphones family page so that the tops of the content containers align in a gridlike fashion.

The code makes these assumptions regarding the html:
1. Products within the same group have identically ordered content elements
2. Elements that do not contain content are present and empty
3. Element heights are resized to the tallest element in the group to create horizontal alignment
4. Groups have a css class of .grp, class='grp'
5. products have a css class of class="product_thirds"
6. the last product in a group has a css class of class="product_thirds last"

See the project wiki page for details: http://ocgwiki/display/OCG/AE2i+Product+Launch+and+Audio+Headphones+Family+Page
*/

var ReSizer =
{
  groups:'',             // Wrapped jQuery set of all the group divs (number of group nodes)
  num_groups:'0',        // The number of groups in the set
  products:'',           // Wrapped jQuery set of the product nodes in the group
  num_products:'0',      // The number of product divs in the current group
  num_children:'0',      // The number of child nodes in the product - should always be the same order and number within a group
  g_cntr:0,              // Counter used to determine recursion condition in check_for_groups();
  temp_height:[],        // Max heights for elements in the group are store in temp_height[] array

  check_for_groups:function() // Verify that we have at least one group. Process groups sequentially
  {
    if ((ReSizer.num_groups > 0) && (ReSizer.g_cntr < ReSizer.num_groups))
    {
      ReSizer.get_heights();                // Determine and store max heights used to align product child nodes
      ReSizer.g_cntr = ReSizer.g_cntr + 1;  // Increment the group counter
      ReSizer.check_for_groups();           // Self-invoke until all groups have been processed
    }
    ReSizer.products = '';                // Clear out the product nodes variable
    ReSizer.num_products = 0;             // Zero out the number of product nodes variable
    ReSizer.temp_height = [];             // Zero out the temp_height array
  },

  get_heights:function()  // This function runs once for each group to examine each product in the group to get element heights
  {
    var i,j,child_elm;
    ReSizer.products = jQuery(jQuery(ReSizer.groups).get(ReSizer.g_cntr)).children(); // The product nodes in the group
    ReSizer.num_products = jQuery(ReSizer.products).size();                      // The number of products in the group
    if (ReSizer.num_products > 1)                                 // Skips the group if there is only one product in it
    {
      for (j=0; j < ReSizer.num_products; j++) // Loop through all the child nodes in the product node
      {
        ReSizer.num_children = jQuery(jQuery(jQuery(ReSizer.products).get(j)).children()).size(); // Must be the same for every product node in the group
        for (i = 0; i < ReSizer.num_children; i++)
        {
          child_elm = jQuery(jQuery(jQuery(ReSizer.products)[j]).children()[i]);
          if (j == 0) // If this is the first product from the group 
          {
            // ReSizer.temp_height[i] = jQuery(child_elm).height(); // initialize temp_height[] with element heights
ReSizer.temp_height[i] = jQuery(child_elm).innerHeight(); // initialize temp_height[] with element heights            
          }
          else // If 2nd or 3rd (or more) product in group 
          {
            // if (jQuery(child_elm).height() > ReSizer.temp_height[i]) // compare current element height to value stored in temp_height[]
            if (jQuery(child_elm).innerHeight() > ReSizer.temp_height[i]) // compare current element height to value stored in temp_height[]            
            {
              // ReSizer.temp_height[i] = jQuery(child_elm).height();   // If the current element height is greater, replace the value in temp_height[]
              ReSizer.temp_height[i] = jQuery(child_elm).innerHeight();   // If the current element height is greater, replace the value in temp_height[]              
            }
          }
        }
      }
      ReSizer.change_heights();   // Change heights of the product elements for the current group         
    }
  },

  change_heights:function()  // Loop through and resize the child nodes in a groups product nodes
  {
  var i,j,child_elm;
    for (j = 0; j < ReSizer.num_products; j++)
    {
      for (i = 0; i < ReSizer.num_children; i++)
      {
        child_elm = jQuery(jQuery(jQuery(ReSizer.products)[j]).children()[i]); // select the element from the wrapped set
        jQuery(child_elm).height(ReSizer.temp_height[i]);                      // resize the element
      }
    }
  }
};

// jQuery(document).ready(function(){
  jQuery(window).load(function(){
  ReSizer.groups = jQuery(".grp");                     // Need a jQuery wrapped set (groups) and the number
  ReSizer.num_groups = jQuery(ReSizer.groups).size();  // of groups (num_groups) to run check_for_groups()
  ReSizer.check_for_groups();
 });
 

