/*!
 * equalSize.js
 *
 * @project   Travelsoft
 * @author    emmanuel.sammut
 * @version   1.0
 * @use       full project
 */

/* ----------------------------------------------------------------
 * 1. FUNCTIONS
 */

  // Relinquish jQuery's control of the $ variable
  jQuery.noConflict();

  /** Protected alias $ of jQuery object */
  (function($){

    /**
     * <p>Get maximum size of all elements</p>
     *
     * @param {String} [val] width or height value
     * @return {Number} maximum width or height size of all elements
     */
    jQuery.fn.maxSize = function(val){
      var that,
          aMax = [];
      // Iterate into elements, get size and set array
      this.each(function(i, elt){
        that = jQuery(this);
        if(val === "height"){
          aMax[i] = that.height();
        }else if(val === "width"){
          aMax[i] = that.width();
        }
      });
      // sort array numerically and descending
      aMax.sort(function(a, b){
        return b - a;
      });
      return aMax[0];
    };

    /**
     * <p>Set equal height or width size for all elements</p>
     * <p>Attention, dependency of maxSize method</p>
     *
     * @param {String} [val] width or height value
     * @return {jQuery} each jQuery object
     */
    jQuery.fn.equalSize = function(val){
      val = val || "height";
      var that,
          max = jQuery(this).maxSize(val);
      return this.each(function(i, elt){
        that = jQuery(this);
        if(val === "height"){
          that.height(max);
        }else if(val === "width"){
          that.width(max);
        }
      });
    };

   })(jQuery);


/* ----------------------------------------------------------------
 * 2. EVENTS & INITS
 */

  /** jQuery onload DOM */
  jQuery(function(){

    /**
     * @init row
     *
     * <p>Emulated align table. Call equalSize method into row* class,
     * <strong>attention : do not remove row container.</strong></p>
     */
    jQuery("#myId").
      find(".row1").equalSize().end().
      find(".row2").equalSize().end().
      find(".row3").equalSize().end().
      find(".row4").equalSize();

  });


