/**
 * Installment Plan Info Module
 * 
 * @author  Kyo (AKA Yasuhiro Sota)
 * @version 1.1.3
 * @license Commercial License
 * @package admin
 * @subpackage  catalog.view.javascript.installment_plan_info
 */

;(function($) {

  $.extend({
    installmentPlanInfo: {
      settings: {
        autoUpdate: {
          async: false,
          productInfoClass: '.product-info',
          ajaxloaderClass: '.ipi-ajaxloader',
          event: {
            type: 'change',
            selector: 'input[name=\'quantity\'], .options input[type=\'radio\'], .options input[type=\'checkbox\'], .options select'
          },
          productOptions: {
            selector: 'input[type=\'radio\']:checked, input[type=\'checkbox\']:checked, select'
          },
          quantity: {
            selector: 'input[name=\'quantity\']'
          }
        }
      },
  /**
   * init method
   *
   * @param el
   * @param settings
   * @return void
   */
      init: function($el, settings) {
        var _this = this;
     
        settings = settings || {};

        if (settings) {
          $.extend(_this.settings, settings);
        }
        
        $(_this.settings.autoUpdate.productInfoClass)
        .off(_this.settings.autoUpdate.event.type)
        .on(_this.settings.autoUpdate.event.type, _this.settings.autoUpdate.event.selector, function(event) {
          
          _this.updateInstallments($(event.delegateTarget), settings);
          
          return true;
        });        
      },
  /**
   * autoUpdate method
   *
   * @param el
   * @param settings
   * @return void
   */
      updateInstallments: function($el, settings) {
        var _this = this;
        
        settings = settings || {};

        if (settings) {
          $.extend(_this.settings, settings);
        }        
        
        var product_option_value_ids = $el.find(_this.settings.autoUpdate.productOptions.selector).map(function() {
          if ($.isNumeric($(this).val())) {
            return $(this).val();
          }
        });

        var quantity = Number($(_this.settings.autoUpdate.quantity.selector).val());

        if (!($.isNumeric(quantity) && quantity > 0)) {
          return false;
        }

        $('.installment-plan-info').each(function(i, v) {

          $.ajax({
            url: 'index.php?route=module/installment_plan_info/ajax_index',
            async: _this.settings.autoUpdate.async,
            data: {
              content_id: $(v).find('input:hidden[name="content_id"]').val(),
              image_width: $(v).find('input:hidden[name="image_width"]').val(),
              image_height: $(v).find('input:hidden[name="image_height"]').val(),
              layout_id: $(v).find('input:hidden[name="layout_id"]').val(),
              position: $(v).find('input:hidden[name="position"]').val(),
              status: $(v).find('input:hidden[name="status"]').val(),
              sort_order: $(v).find('input:hidden[name="sort_order"]').val(),
              module: $(v).find('input:hidden[name="module"]').val(),
              product_id: $(v).find('input:hidden[name="product_id"]').val(),
              quantity: quantity,
              product_option_value_ids: product_option_value_ids.toArray()
            },
            type: "POST",
            dataType: 'html',
            beforeSend: function(xhr) {
              $(v).find(_this.settings.autoUpdate.ajaxloaderClass).show();
            }
          }).done(function(data){
            if (data) {
              $(v).replaceWith(data);
            }

            $(v).find(_this.settings.autoUpdate.ajaxloaderClass).hide();

          }).fail(function(data){
            console.log('Installment Plan Info: Unexpected error occurred while running auto update!');
          });

        }); 
        
      } // END of autoUpdate()     
    }
  }); 
  
  $.fn.extend({
    installmentPlanInfo: function(settings) {
      return $.installmentPlanInfo.init(this, settings);
    },
    autoUpdate: function(settings) {
      return $.installmentPlanInfo.updateInstallments(this, settings);
    }
  });  

})(jQuery);
