
/*jslint bitwise: true, eqeqeq: true, immed: true, newcap: true, nomen: false,
 onevar: false, plusplus: false, regexp: true, undef: true, white: true, indent: 2
 browser: true */

/*global jQuery: true Drupal: true window: true ThemeBuilder: true */

/**
* ScrollTo
* - event.data.scrollTarget
*   The element to scroll to, passed in with the event object
* - Acquia object is created if it doesn't exist and a theme object built or referenced
*   if it exists to store the functions for this theme.
*/
var Acquia = Acquia || {};
Acquia.theme = Acquia.theme || {}; 

Acquia.theme.scrollTo = function (event) {
  event.preventDefault(); // Keep the page from jumping to the hash target
  
  var targetOffset = event.data.scrollTarget.offset().top;
  jQuery('html,body')
  .animate({scrollTop: targetOffset}, 1000);
};

Acquia.theme.equalizeColumns = function (stacks) { //Loops through all regions with sidebars sends each set to be leveled
  for (var i = 0; i < stacks.size(); i++) {
    
    var _this = stacks.eq(i); // Get the current stack reference
    var boxHeight = _this.height(); // Store the stack's height
    var cols = _this.find('.col, .sidebar'); // Get the columns in the stack
    
    for (var j = 0; j < cols.size(); j++) {
      if (cols.eq(j).height() > boxHeight) {
        boxHeight = cols.eq(j).height(); // If the height of the col is more than previous height, use it
      }
    }
    
    cols.css('min-height', boxHeight);
  }
};

/**
* Document ready handler.
*
*/
jQuery(document).ready(function () {
  var addCommentTrigger = jQuery('.comment_add');
  var addCommentForm = jQuery('#comment-new');
  
  if (addCommentForm.size() > 0) { // Add only if the comment form exists on the page.
    addCommentTrigger.bind('click', {scrollTarget: addCommentForm}, Acquia.theme.scrollTo); // Bind click event handlers to all .comment_add links
  }
  
  Acquia.theme.equalizeColumns(jQuery('.box', '.stack')); // Set all sidebars to the height of their parent container.
  
});
;

