/**
* 2007-2020 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author    PrestaShop SA <contact@prestashop.com>
*  @copyright 2007-2020 PrestaShop SA
*  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*
* Don't forget to prefix your containers with your own identifier
* to avoid any conflicts with others containers.
*/

var mobileViewSize  = 991;
$(window).load(function(){

function gettvcmstabproductsajax(){
  if($('.tvcmstab-title-product').length){
   $.ajax({
        type: 'POST',
        url: gettvcmstabproductslink,
        success: function(data)
        {
           $('.tvcmstab-title-product').html(data);
           tvcmstabproductsajax();
           customImgLazyLoad('.tvcmstab-title-product');
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log(textStatus, errorThrown);
        }
    });
  }
 }

setTimeout(function(){gettvcmstabproductsajax()},500);
//gettvcmstabproductsajax();

function tvcmstabproductsajax(){
     /********************** Start Tab js *****************************/
      $('.tab-index').click(function(e){
        $id = $(this).attr('data-tab-data');
        $paging = $(this).attr('data-tab-paging');
        $('.tab-index').removeClass('active');
        $('.tvcmstab-product').removeClass('active');
        $('.tvtab-pagination').removeClass('active');
        $('.tvcmstab-product').hide();
        $('.tvtab-pagination').hide();

        $(this).addClass('active');
        $('#'+$id).addClass('active').fadeIn('500');
        $('.'+$paging+'-pagination').addClass('active').show();
      });

      $('.tvcmstab-product.active').fadeIn();
      $('.tvtab-pagination.active').fadeIn();

      $('.tvcmstab-title-product .tvtab-pagination-wrapper').insertAfter('.tvcmstab-title-product  .tvcms-main-title');

    /********************** End Tab js *****************************/

    /****************** Start Tab Product Slider Js *******************************************/
    var owlClass = [
      //['slider className','navigation nextClass','navigation prevClass']
      ['.tvtab-featured-product .tvproduct-wrapper-content-box','.tvtab-featured-product-next','.tvtab-featured-product-prev'],
      ['.tvtab-new-product .tvproduct-wrapper-content-box','.tvtab-new-product-next','.tvtab-new-product-prev'],
      ['.tvtab-special-product .tvproduct-wrapper-content-box','.tvtab-special-product-next','.tvtab-special-product-prev'],
      ['.tvtab-best-seller-product .tvproduct-wrapper-content-box','.tvtab-best-seller-product-next','.tvtab-best-seller-product-prev'],
    ];

    for (var i = 0; i < owlClass.length; i++) {
      if ($(owlClass[i][0]).attr('data-has-image') == 'true') {
         var owl = $(owlClass[i][0]).owlCarousel({
          loop: false,
          dots: false,
          nav: false,
          responsive: {
            0: { items: 1},
            320:{ items: 1, slideBy: 1},
            330:{ items: 1, slideBy: 1},
            400:{ items: 1, slideBy: 1},
            480:{ items: 1, slideBy: 1},
            650:{ items: 1, slideBy: 1},
            767:{ items: 1, slideBy: 1},
            768:{ items: 2, slideBy: 1},
            992:{ items: 2, slideBy: 1},
            1023:{ items: 2, slideBy: 1},
            1024:{ items: 3, slideBy: 1},
            1200:{ items: 2, slideBy: 1},
            1350:{ items: 3, slideBy: 1},
            1660:{ items: 3, slideBy: 1},
            1800:{ items: 4, slideBy: 1}
          }
        });
      } else {
        var owl = $(owlClass[i][0]).owlCarousel({
          loop: false,
          dots: false,
          nav: false,
          responsive: {
            0: { items: 1},
            320:{ items: 1, slideBy: 1},
            330:{ items: 1, slideBy: 1},
            400:{ items: 2, slideBy: 1},
            480:{ items: 2, slideBy: 1},
            650:{ items: 2, slideBy: 1},
            767:{ items: 2, slideBy: 1},
            768:{ items: 3, slideBy: 1},
            992:{ items: 3, slideBy: 1},
            1023:{ items: 3, slideBy: 1},
            1024:{ items: 4, slideBy: 1},
            1200:{ items: 3, slideBy: 1},
            1350:{ items: 4, slideBy: 1},
            1660:{ items: 4, slideBy: 1},
            1800:{ items: 5, slideBy: 1}
          }
        });
      }

      $(owlClass[i][1]).on('click', function(e){
        e.preventDefault();
        $('#'+$(this).attr('data-parent')+' .owl-nav .owl-next').trigger('click');
      });
      $(owlClass[i][2]).on('click', function(e){
        e.preventDefault();
        $('#'+$(this).attr('data-parent')+' .owl-nav .owl-prev').trigger('click');
      });
    }
    /****************** End Tab Product Slider Js *******************************************/
    /****************** Start Single Products Slider Js *******************************************/
    var swiperClass = [
      //['slider className','navigation nextClass','navigation prevClass','paging className']
      ['.tvcmsfeatured-product .tvfeatured-product-wrapper','.tvcmsfeatured-next','.tvcmsfeatured-prev','.tvcmsfeatured-product'],
      ['.tvcmsnew-product .tvnew-product-wrapper','.tvcmsnew-next','.tvcmsnew-prev','.tvcmsnew-product'],
      ['.tvcmsspecial-product .tvspecial-product-wrapper','.tvcmsspecial-next','.tvcmsspecial-prev','.tvcmsspecial-product'],
      ['.tvcmsbest-seller-product .tvbest-seller-product-wrapper','.tvcmsbest-seller-next','.tvcmsbest-seller-prev','.tvcmsbest-seller-product'],
    ];

    for (var i = 0; i < swiperClass.length; i++) {
      if ($(swiperClass[i][0]).attr('data-has-image') == 'true') {
        $(swiperClass[i][0]).owlCarousel({
          loop: false,
          dots: false,
          nav: false,
          responsive: {
            0: { items: 1},
            320:{ items: 1, slideBy: 1},
            330:{ items: 1, slideBy: 1},
            400:{ items: 2, slideBy: 1},
            430:{ items: 2, slideBy: 1},
            480:{ items: 2, slideBy: 1},
            650:{ items: 2, slideBy: 1},
            767:{ items: 2, slideBy: 1},
            768:{ items: 2, slideBy: 1},
            992:{ items: 2, slideBy: 1},
            1023:{ items: 2, slideBy: 1},
            1024:{ items: 3, slideBy: 1},
            1200:{ items: 3, slideBy: 1},
            1350:{ items: 3, slideBy: 1},
            1660:{ items: 3, slideBy: 1},
            1800:{ items: 4, slideBy: 1}
          },
        });
      } 
      else {
        $(swiperClass[i][0]).owlCarousel({
          loop: false,
          dots: false,
          nav: false,
          responsive: {
            0: { items: 1},
            320:{ items: 1, slideBy: 1},
            330:{ items: 1, slideBy: 1},
            400:{ items: 2, slideBy: 1},
            480:{ items: 2, slideBy: 1},
            650:{ items: 2, slideBy: 1},
            767:{ items: 2, slideBy: 1},
            768:{ items: 3, slideBy: 1},
            992:{ items: 3, slideBy: 1},
            1023:{ items: 3, slideBy: 1},
            1024:{ items: 4, slideBy: 1},
            1200:{ items: 3, slideBy: 1},
            1350:{ items: 4, slideBy: 1},
            1660:{ items: 4, slideBy: 1},
            1800:{ items: 5, slideBy: 1}
          },
        });
      }
      

      $(swiperClass[i][1]).on('click', function(e){
        e.preventDefault();
        $('.'+$(this).attr('data-parent')+' .owl-nav .owl-next').trigger('click');
      });
      $(swiperClass[i][2]).on('click', function(e){
        e.preventDefault();
        $('.'+$(this).attr('data-parent')+' .owl-nav .owl-prev').trigger('click');
      });
      $(swiperClass[i][3]+' .tv-pagination-wrapper').insertAfter(swiperClass[i][3]+' .tvcmsmain-title-wrapper');
    }
    /****************** End Single Product Sliders Js *******************************************/
      // $('.tvcmstab-title-product .tvproduct-wrapper').balance();
      // $('#index #content-wrapper .tvproduct-wrapper .tvproduct-name').balance();
      // $('.tvcmssame-category-product .tvproduct-wrapper').balance();
      // $('.tvcmslike-product .tvproduct-wrapper').balance();
      // $('.tvcmscross-selling-product .tvproduct-wrapper').balance();
      // $('.tvcmstabcategory-product-slider .tvproduct-wrapper').balance();
      // $('.tvcmsfeatured-product .tvproduct-wrapper').balance();
      // $('.tvcmsbest-seller-product .tvproduct-wrapper').balance();
      // $('.tvcmsnew-product .tvproduct-wrapper').balance();
      // $('.tvcmsspecial-product .tvproduct-wrapper').balance();
      // $('#products.grid .tvgrid-list-view-product .tvproduct-wrapper.grid').balance();

      /*************** Start left Right  Product Toggle in Mobile Size ***************************************************/
      // leftRightProductTitleToggle();
      // $(window).resize(function(){
      //   $('.tvleft-right-penal-all-block .tvleft-right-title-toggle, .tvleft-right-penal-all-block .tvleft-product-wrapper-info').removeClass('open');
      //   leftRightProductTitleToggle();
      // });
      // function leftRightProductTitleToggle()
      // {
      //   $('.tvleft-right-penal-all-block .tvleft-right-title-toggle').on('click', function(e){
      //     e.preventDefault();
      //     if(document.body.clientWidth <= 1199){
      //       if($(this).hasClass('open')) {
      //         $(this).removeClass('open');
      //         $(this).parent().parent().find('.tvleft-product-wrapper-info').removeClass('open').stop(false).slideUp(500, "swing");
      //       } else {
      //         $(this).addClass('open');
      //         $(this).parent().parent().find('.tvleft-product-wrapper-info').addClass('open').stop(false).slideDown(500, "swing");
      //       }
      //     }
      //     e.stopPropagation();
      //   });
      // }
      /*************** End left Right  Product Toggle in Mobile Size ***************************************************/
      changeTabTitles();
}
  $(window).resize(function(){
    changeTabTitles();
  });
  function changeTabTitles()
  {
    if (document.body.clientWidth <= 767 && document.body.clientWidth > 575) {
      $('.tvtab-product .tvtab-title-wrapper').insertAfter('.tvtab-product .tvcmsmain-title-wrapper');
    } else {
      $('.tvtab-product .tvtab-title-wrapper').insertAfter('.tvtab-product .tvcmsmain-title-wrapper .tvcms-main-title');
    }
  }
});