// Reward & Offer Carousel
jQuery(document).ready(function ($) {
  $('ul.carousel').each(function(){
    var _this = this;
    var max_columns = $(_this).data('columns');
    var columns = $(window).width() < 1000 ? Math.ceil(max_columns/2) : max_columns;
    columns = $(window).width() < 600 ? Math.ceil(max_columns/4) : columns;

    var slider = $(_this).lightSlider({
      item: columns,
      loop: false,
      enableDrag: false,
      onSliderLoad: function () {
          $(_this).removeClass('hidden');
      },
      prevHtml: $(_this).data('prev-str'),
      nextHtml: $(_this).data('next-str')
    });
    slider.goToSlide($(_this).data('start'));
  });
});

// Social share button is clicked
jQuery(document).ready(function ($) {
  $('.social-share').click(function(){
    var item_id = $(this).attr('data-item');
    $.post(ajaxurl + '?action=horizon_ajax_api', { 'method': 'social-share', 'item_id':item_id }, function(data){});
  });
});

// Reward grid load pages via ajax
jQuery(document).ready(function ($) {
  $('.widget_horizon_widget_reward_grid').on('click', '.pagination a', function(event){
    event.preventDefault();
    var widget_id = $(this).parents('.widget_horizon_widget_reward_grid').attr('id');
    var container = $(this).parents('.horizon-widget-reward-grid');

    $(container).addClass('loading');
    $.get(ajaxurl + '?action=horizon_widget_reward_grid', { 'widget_id': widget_id, 'start':$(this).attr('data-start') }, function(data){
      var offset = $(container).parent().offset().top;
      if ($(document).scrollTop() > offset){
        $('html, body').animate({ scrollTop: offset }, 300);
      }
      $(container).replaceWith(data);
    });
  });
});

// Offer grid load pages via ajax
jQuery(document).ready(function ($) {
  $('.widget_horizon_widget_offer_grid').on('click', '.pagination a', function(event){
    event.preventDefault();
    var widget_id = $(this).parents('.widget_horizon_widget_offer_grid').attr('id');
    var container = $(this).parents('.horizon-widget-offer-grid');

    $(container).addClass('loading');
    $.get(ajaxurl + '?action=horizon_widget_offer_grid', { 'widget_id': widget_id, 'start':$(this).attr('data-start') }, function(data){
      var offset = $(container).parent().offset().top;
      if ($(document).scrollTop() > offset){
        $('html, body').animate({ scrollTop: offset }, 300);
      }
      $(container).replaceWith(data);
    });
  });
});

// Get Voucher PDF
jQuery(document).ready(function ($) {
    $('.horizon_wallet_widget_get_voucher').on('click', function(event){
        event.preventDefault();
        var offer_id = $(this).data('id');
        var voucher_code = $(this).data('vouchercode');
        window.location = ajaxurl + '?action=horizon_voucher_download&offer_id=' + offer_id + '&voucher_code=' + voucher_code;
    });
});

// Add to Wishlist
jQuery(document).ready(function ($) {
  $('.horizon-add-to-wishlist').on('click', function (event) {
    event.preventDefault();

    var offer_id = $(this).data('id');

    $.get(ajaxurl + '?action=horizon_widget_wishlist_add', { 'offer_id': offer_id, 'start':$(this).attr('data-start') }, function(response){

      if(wishlistCompleteUrl && response == 'SUCCESS') {
        window.location = wishlistCompleteUrl + '?id=' + offer_id;
      }

    });

  });
});

// Remove from Wishlist
jQuery(document).ready(function ($) {
    $('.horizon-widget-wishlist-delete').on('click', function (event) {
        event.preventDefault();

        var wishlistOfferId = $(this).data('id');

        $.get(ajaxurl + '?action=horizon_widget_wishlist_remove', { 'wishlist_offer_id': wishlistOfferId }, function(response){

          if(response == 'SUCCESS') {
            window.location.reload();
          }

        });
    });
});



// Reward grid load pages via ajax
jQuery(document).ready(function ($) {
  $('.widget_horizon_widget_wallet').on('click', '.pagination a', function(event){
    event.preventDefault();
    var widget_id = $(this).parents('.widget_horizon_widget_wallet').attr('id');
    var container = $(this).parents('.horizon-widget-wallet-grid');

    $(container).addClass('loading');
    $.get(ajaxurl + '?action=horizon_widget_wallet_grid', { 'widget_id': widget_id, 'start':$(this).attr('data-start') }, function(data){
      var offset = $(container).parent().offset().top;
      if ($(document).scrollTop() > offset){
        $('html, body').animate({ scrollTop: offset }, 300);
      }
      $(container).replaceWith(data);

      $('.horizon_wallet_widget_get_voucher').on('click', function(event){
        event.preventDefault();
        var offer_id = $(this).data('id');
        var voucher_code = $(this).data('vouchercode');
        window.location = ajaxurl + '?action=horizon_voucher_download&offer_id=' + offer_id + '&voucher_code=' + voucher_code;
      });

    });
  });
});

jQuery(document).ready(function ($) {
  $('.widget_horizon_wishlist_widget').on('click', '.pagination a', function(event){
    event.preventDefault();
    var widget_id = $(this).parents('.widget_horizon_wishlist_widget').attr('id');
    var container = $(this).parents('.horizon-widget-wishlist');

    $(container).addClass('loading');
    $.get(ajaxurl + '?action=horizon_widget_wishlist_grid', { 'widget_id': widget_id, 'start':$(this).attr('data-start') }, function(data){
      var offset = $(container).parent().offset().top;
      if ($(document).scrollTop() > offset){
        $('html, body').animate({ scrollTop: offset }, 300);
      }
      $(container).replaceWith(data);
    });
  });
});

jQuery(document).ready(function ($) {
  $('.widget_horizon_widget_best_loyalty_behaviours').on('click', '.pagination a', function(event){
    event.preventDefault();
    var widget_id = $(this).parents('.widget_horizon_widget_best_loyalty_behaviours').attr('id');
    var container = $(this).parents('.horizon-widget-loyalty-behaviours-grid');

    $(container).addClass('loading');
    $.get(ajaxurl + '?action=horizon_widget_best_loyalty_behaviours', { 'widget_id': widget_id, 'start':$(this).attr('data-start') }, function(data){
      var offset = $(container).parent().offset().top;
      if ($(document).scrollTop() > offset){
        $('html, body').animate({ scrollTop: offset }, 300);
      }
      $(container).replaceWith(data);
    });
  });
});

jQuery(document).ready(function ($) {
  $('.widget_horizon_widget_loyalty_behaviours').on('click', '.pagination a', function(event){
    event.preventDefault();
    var widget_id = $(this).parents('.widget_horizon_widget_loyalty_behaviours').attr('id');
    var container = $(this).parents('.horizon-widget-loyalty-behaviours-grid');

    $(container).addClass('loading');
    $.get(ajaxurl + '?action=horizon_widget_loyalty_behaviours', { 'widget_id': widget_id, 'start':$(this).attr('data-start') }, function(data){
      var offset = $(container).parent().offset().top;
      if ($(document).scrollTop() > offset){
        $('html, body').animate({ scrollTop: offset }, 300);
      }
      $(container).replaceWith(data);
    });
  });
});