<div class="isl-ltd isl-ltd-popup isl-pos-<?php echo $setting['popup']['position'];?> isl-th-<?php echo $theme; ?>" style="background:<?php echo $setting['popup']['background']; ?>">
  <div class="isl-ltd-inner" style="border-width:<?php echo $setting['popup']['border_size']; ?>px; border-color:<?php echo $setting['popup']['border_color']; ?>;">
    <?php echo $message_popup; ?>
    <button type="button" class="close isl-popup-info-modal"><i class="fa fa-info-circle"></i></button>
  </div>
</div>
<div class="modal fade isl-ltd isl-ltd-modal isl-th-<?php echo $theme; ?>" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content modal-body" style="background:<?php echo $setting['modal']['background']; ?>; border-width:<?php echo $setting['modal']['border_size']; ?>px; border-color:<?php echo $setting['modal']['border_color']; ?>;">
      <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      <?php echo $message_modal; ?>
    </div>
  </div>
</div>

<?php if (!empty($setting['modal']['custom_css']) || !empty($setting['popup']['custom_css'])) { ?>
<style>
<?php echo $setting['modal']['custom_css']; ?>
<?php echo $setting['popup']['custom_css']; ?>
</style>
<?php } ?>

<script>
/* LimitedTimeDiscount */
var islLTDName  = '<?php echo $module_name; ?>',
    islLTDPath  = '<?php echo $module_path; ?>',
    islLTDDelay = <?php echo $setting['discount']['delay']; ?>;

$(document).ready(function() {
  islLimitedTimeDiscount(islLtdGetStorage(islLTDName));

  $('.isl-ltd-modal').on('hidden.bs.modal', function (e) {
    $('.isl-ltd-popup').fadeIn();
  });
  $('.isl-popup-info-modal').on('click', function() {
    $('.isl-ltd-popup').fadeOut();
    $('.isl-ltd-modal').modal('show');
  });
});

function islLimitedTimeDiscount($param) {
  if ($param == undefined) {
    $param = [];
  }

  setTimeout(function() {
    $.ajax({
      url      : 'index.php?route=' + islLTDPath + '/ping',
      type     : 'post',
      dataType : 'json',
      data     : $param,
      success: function(data) {
        if (data.discount && data.discount.valid && data.discount.offer_diff) {
          islLtdSetStorage(islLTDName, data.discount);

          // Modal and popup shortcode
          islLtdShortcodes(data.discount.value, data.discount.code, data.discount.offer_diff);

          // Show modal
          if (data.discount.new_coupon) {
            $('.isl-ltd-modal').modal('show');
          }

          // Show popup
          if (!data.discount.new_coupon) {
            islLtdShortcodes(data.discount.value, data.discount.code, data.discount.offer_diff);
            $('.isl-ltd-popup').fadeIn();
          }
        }
      }
    });
  }, (1000 * islLTDDelay));
}
function islLtdShortcodes(discount, code, diff) {
  $('.isl-ltd-discount-code').html(code);
  $('.isl-ltd-discount-value').html(discount);
  $('.isl-ltd-countdown').ltdcountdown({
    until         : +diff,
    compact       : true,
    onExpiry      : function() {
      $('.isl-ltd-countdown').addClass('is-expired').html('Offer is expired!');
      $('.isl-ltd-modal').modal('hide');
      setTimeout(function() {
        $('.isl-ltd-popup').hide();
      }, 2000);
    }
  });
}
function islLtdSetStorage(name, data) {
  if (window.localStorage) {
    window.localStorage[name] = JSON.stringify(data);
  }
}
function islLtdGetStorage(name) {
  if (window.localStorage && window.localStorage[name]) {
    return JSON.parse(window.localStorage[name]);
  }
  return null;
}
</script>
