/*!
 * opinion.js
 *
 * @project   Voyages Auchan
 * @author    emmanuel.sammut
 * @version   2.0
 * @use       opinion functionalities into page
 */

/* ----------------------------------------------------------------
 * 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(){
    /**
     * @event hover offer
     *
     * @param {Function} over nothing at all
     * @param {Function} out hide tooltip opinion if always visible
     */
    jQuery(".offer").hover(
      function(){}, // do not removed
      function(){
        var tp = jQuery(this).find(".tpOpinion");
        if(tp.is(":visible")){
          tp.hide();
        }
      }
    );

    /**
     * @event hover opinion
     *
     * @param {Function} over positioning and show tooltip opinion
     * @param {Function} out nothing at all
     */
    jQuery(".opinion").hover(
      function(){
        var that = jQuery(this),
            offer = that.parents(".offer"),
            pos = that.position(),
            btm = offer.height() - pos.top - 25;

        offer.find(".tpOpinion").
          css({
              position:"absolute",
              left: pos.left + "px",
              bottom: btm + "px"
            }).
          show();
      },
      function(){} // do not removed
    );

    /**
     * @event hover tpOpinion
     *
     * @param {Function} over nothing at all
     * @param {Function} out hide tooltip opinion
     */
    jQuery(".tpOpinion").hover(
      function(){},  // do not removed
      function(){
        jQuery(this).hide();
      }
    );

    /**
     * @event hover opinionResult
     *
     * @param {Function} over add "opinionOver" class
     * @param {Function} out remove "opinionOver" class
     */
    jQuery("#opinionList").
      find(".opinionResult").hover(
        function(){
          jQuery(this).addClass("opinionOver");
        },
        function(){
          jQuery(this).removeClass("opinionOver");
        }
    );

    /**
     * @init bargraph
     *
     * Initialized and animate rate of bargraph
     */
    jQuery("#averageCategory").find(".col2 strong").hide().animate({width: "toggle"}, 1500);

    /**
     * @init row
     *
     * Emulated align table. Call equalSize method into row class,
     * attention : do not remove row container.
     */
    jQuery("#opinionConsole").find(".row").equalSize();

    jQuery('#travelerFilterForm input[type=radio]').click(function(){
      var className = jQuery('#travelerFilterForm').attr('class');
      var value = jQuery('input:radio:checked').val();
      if (className == "cont") {
        var urlParams = window.location.href;
        var table = urlParams.split('-');
        var pidTable = table[table.length - 1].split('.');
        var pid = pidTable[0];
        jQuery.ajax({
          url: '/elements/ajaxOpinion.jsp?vty=' + value + '&pid=' + pid,
          type: "GET",
          dataType: "text",
          success: function(msg){
            jQuery('#ajaxOp').html(msg);
          }
        });
      } else {
        var urlParams = window.location.href;
        var pid = "";
        if(jQuery.url.param("pid") != null){
          pid = jQuery.url.param("pid");
        }
        var urlTable = urlParams.split("?");
        if (value != null && value != "") {
          urlParams = urlTable[0] + "?pid=" + pid + "&vty=" + value;
        }else{
          urlParams = urlTable[0] + "?pid=" + pid;
        }
        window.location.href = urlParams;
      }
    });
  });

