// DAR
var lowopifzeroprd_ur115_20X = 'index.php?route=module/lowopifzeroprd/';

var lowopifzeroprd_url23X_30X = 'index.php?route=extension/module/lowopifzeroprd/';

var lowopifzeroprd_url401X = 'index.php?route=extension/lowopifzeroprd/module/lowopifzeroprd|';

var lowopifzeroprd_url402X = 'index.php?route=extension/lowopifzeroprd/module/lowopifzeroprd.';


var lowopifzeroprd = {

  'setprc': function () {

    postdata = $('#product input[type=\'text\'], #product input[type=\'number\'], #product input[type=\'hidden\'], #product input[type=\'radio\']:checked, #product input[type=\'checkbox\']:checked, #product select, #product textarea');

    if ($('.product-info .cart').length) {
      /*for OC 1.5.X*/

      postdata = $('.product-info input[type=\'text\'], .product-info input[type=\'number\'], .product-info input[type=\'hidden\'], .product-info input[type=\'radio\']:checked, .product-info input[type=\'checkbox\']:checked, .product-info select, .product-info textarea');

    }


    $.ajax({

      url: lowopifzeroprd_url23X_30X + 'setprc',

      type: 'post',

      data: postdata,

      dataType: 'json',

      beforeSend: function () {

        $('.ulprc').after('<i class="fa fa-circle-o-notch fa-spin"></i>');

      },

      complete: function () {

        $('.fa-spin').remove();

      },

      success: function (json) {

        if (json['success'] == 1) {

          if (json['special'] && $('.oldprc').length) {

            $('.newprc').html(json['special']);

            $('.oldprc').html(json['price']);

          } else {

            $('.newprc').html(json['price']);

          }

          if (json['tax']) {
            $('.extaxprc').html(json['tax']);
          }

          // $('html, body').animate({ scrollTop: 200 }, 'slow'); 

        }

      }

    });

  },

  'prcupdate': function () {

    $('#product').parent().find('ul.list-unstyled').find('h2').parent().parent().addClass('ulprc');

    $('ul.ulprc li span').addClass('oldprc');

    $('ul.ulprc li h2').addClass('newprc');

    if ($('.oldprc').length) {

      $('ul.ulprc').find('li').eq(2).addClass('extaxprc');

    } else {

      $('ul.ulprc').find('li').eq(1).addClass('extaxprc');

    }


    $('input[name="quantity"]').keyup(function () {

      lowopifzeroprd.setprc();

    });

    $('input[name="quantity"]').change(function () {

      lowopifzeroprd.setprc();

    });

    if ($('input[name="quantity"]').val() > 1) {

      lowopifzeroprd.setprc();

    }

    $('#product select, #product input[type=\'checkbox\'], #product input[type=\'radio\']').change(function () {

      lowopifzeroprd.setprc();

    });

  },

  'atgrid': function () {

    var product_ids = [];

    $("[onclick*='cart.add'], [onclick*='addToCart']").each(function () {

      var target = $(this).closest('.product-thumb, .product-wrapper');

      var product_id = $(this).attr('onclick').match(/[0-9]+/).toString();

      if (target.length && target.hasClass('lowopifzeroprd_atgridcol') == false) {

        target.addClass('lowopifzeroprd_atgridcol').addClass('lowopifzeroprd_atgridcol' + product_id);

        product_ids.push(product_id);

      }

    });

    if (product_ids.length) {

      //console.log(product_ids.length);

      $.ajax({

        url: lowopifzeroprd_url23X_30X + 'getcache',

        type: 'post',

        data: {
          product_ids: product_ids
        },

        cache: true,

        complete: function () {

        },

        success: function (json) {

          if (json['prod']) {

            $("[onclick*='cart.add'], [onclick*='addToCart']").each(function () {

              var product_id = $(this).attr('onclick').match(/[0-9]+/).toString();

              var target = $(this).closest('.product-thumb, .product-wrapper');

              if (json['prod'][product_id] && target.length && json['pfxtxt'].length && target.hasClass('pfxtxt') == false) {

                target.addClass('pfxtxt').find('.price').prepend(json['pfxtxt']);

              }

            });

          }

        }

      });

    }

  },


  'prodpage': function () {

    var product_ids = [];

    var product_id = false;


    if ($(".button-group-page input[name='product_id']").length) {

      product_id = $(".button-group-page input[name='product_id']").val();

      product_ids.push(product_id);

    } else if ($('#input-product-id').length) {

      product_id = $('#input-product-id').val();

      product_ids.push(product_id);

    } else if ($(".product-info input[name='product_id']").length) {

      product_id = $(".product-info input[name='product_id']").val();

      product_ids.push(product_id);

    } else if ($("#product input[name='product_id']").length) {

      product_id = $("#product input[name='product_id']").val();

      product_ids.push(product_id);

    }


    if (product_ids.length) {

      if ($("#product ul.price .product-price").length || $('#product .price-wrapper .product-price').length) {

        /*do nothing for J2 and J3*/

      } else {

        lowopifzeroprd.prcupdate();

      }


      $.ajax({

        url: lowopifzeroprd_url23X_30X + 'getcache',

        type: 'post',

        data: {
          product_ids: product_ids
        },

        cache: true,

        complete: function () {

        },

        success: function (json) {

          if (json['prod']) {

            var target = $('#product');

            var targetj2 = $("#product ul.price .product-price");

            var targetj3 = $('#product .price-wrapper .product-price');


            if (json['prod'][product_id] && target.length && json['pfxtxt'].length && target.hasClass('pfxtxt') == false) {

              target.addClass('pfxtxt').parent().find('ul.list-unstyled').find('h2').prepend(json['pfxtxt']);

              targetj2.addClass('pfxtxt').prepend(json['pfxtxt']);

              targetj3.addClass('pfxtxt').prepend(json['pfxtxt']);

            }


            if (json['povid'][product_id] && target.length) {

              $('#product input[type=\'radio\'], #product input[type=\'checkbox\']').each(function () {

                if (parseInt($(this).val()) == parseInt(json['povid'][product_id])) {

                  //$(this).prop('checked', true).attr("checked", "checked").trigger('change');

                }

              });

              $('#product option').each(function () {

                if (parseInt($(this).val()) == parseInt(json['povid'][product_id])) {

                  $(this).prop("selected", true).attr("selected", "selected").trigger('change');

                }

              });

            }

          }

        }

      });

    }

  },

  'initjson': function () {

    lowopifzeroprd.atgrid();

    $(document).ajaxStop(function () {
      lowopifzeroprd.atgrid();
    });

    lowopifzeroprd.prodpage();

  }

}

$(document).ready(function () {

  lowopifzeroprd.initjson();

});
