/*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 */

/*
 * 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 || {};

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

/**
 * Equalizes all .col elements in a stack to the height of the tallest .col
 *
 * @param {DomElement} stacks
 *   Set of .stack DomElements
 */
Acquia.theme.equalizeColumns = function (stacks) {
  for (var i = 0; i < stacks.length; i++) {
    // Get the current stack reference
    var _this = stacks.eq(i);
    // Instantiate the boxHeight variable
    var boxHeight = 0;
    // Get the columns in the stack
    var cols = _this.find('.tb-height-balance');
    // We don't need to balance the columns if only one column is present
    if (cols.length > 1) {
      for (var j = 0; j < cols.size(); j++) {
        var col = cols.eq(j);
        // Only balance the height if the col is visible
        if (col.css('display') !== 'none' && col.css('visibility') !== 'hidden') {
          var colHeight = col.outerHeight(false);
          if ( colHeight > boxHeight) {
            // If the height of the col is more than previous height, use it
            boxHeight = colHeight;
          }
        }
      }
    }
    // Only set the min-heights if boxHeight was assigned a real value
    if (boxHeight > 0) {
      cols.css('min-height', boxHeight);
    }
  }
};

/**
 * Adds a click handler to the layout wrapper of the rotating banner.  Otherwise this element
 * would obsure the link wrapped around the image element
 */
Acquia.theme.enhanceRotatingBanner = function () {
  var $ = jQuery;
  var layouts = $('.layout-wrapper', '.rotating-banner');
  var len = layouts.length;
  if (len > 0) {
    while (len--) {
      Acquia.theme.pseudoLink(layouts.eq(len));
    }
  }
};

/**
 * Navigates to the designated element's data-link attribute value
 * 
 * @param {DomElement} element
 *   Element that the pseudo link will be applied to
 *
 * @attribute 
 *   data-link is an element attribute that uses the HTML5 data- pattern to store information in the DOM
 */
Acquia.theme.pseudoLink = function (element) {
  var $ = jQuery;
  var link = element.attr('data-link');
  if (link.length > 0) {
    //Bind a click event to the slide layout wrapper if it has a link, stored in data-link attribute
    element.click(function (event) {
      window.location = $(event.currentTarget).attr('data-link');
    });
  }
};

/**
 * Determines if the ThemeBuilder is open
 *
 * @return {Boolean}
 *   returns a boolean that represents the open state of the ThemeBuilder. True equals open.
 */
Acquia.theme.checkThemeBuilderStatus = function () {
  var $ = jQuery;
  return $('body').hasClass('themebuilder') ? true : false;
};

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

