<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
?>

<script type="text/javascript">
  en4.core.runonce.add(function() {
    var url = '<?php echo $this->url(array('module' => 'core', 'controller' => 'utility', 'action' => 'advertisement'), 'default', true) ?>';
    var processClick = window.processClick = function(adcampaign_id, ad_id) {
      (scriptJquery.ajax({
        dataType: 'json',
        method : 'post',
        url : url,
        data : {
          format : 'json',
          adcampaign_id : adcampaign_id,
          ad_id : ad_id
        }
      }));
    }
  });
</script>
<div class="core_ad_campaingn core_ad_campaingn_<?php echo $this->identity; ?>">
  <?php foreach($this->ads as $ad): ?>
    <div onclick="javascript:processClick(<?php echo $this->campaign->adcampaign_id.", ".$ad->ad_id;?>)">
      <?php echo $ad->html_code; ?>
    </div>
  <?php endforeach; ?>
</div>
<script type="text/javascript">
en4.core.runonce.add(function() {
  var $carousel = owlJqueryObject(".core_ad_campaingn_<?php echo $this->identity; ?>");
  if (!$carousel.length) return;

  function initCarousel() {
    // prevent multiple initialization (important for AJAX reloads)
    if ($carousel.hasClass('owl-loaded')) return;

    $carousel.owlCarousel({
      nav: true,
      dots: true,
      margin: 0,
      autoHeight: true,
      loop: false,
      items: 1,
      <?php 
      $orientation = ($this->layout()->orientation == 'right-to-left' ? 'rtl' : 'ltr');
      if($orientation == 'rtl') { ?>
        rtl: true,
      <?php } ?>
    });

    owlJqueryObject(".owl-prev").html('<i class="fa fa-angle-left"></i>');
    owlJqueryObject(".owl-next").html('<i class="fa fa-angle-right"></i>');
  }

  var $images = $carousel.find("img");
  var totalImages = $images.length;
  var loadedImages = 0;

  // if no images, init immediately
  if (totalImages === 0) {
    initCarousel();
    return;
  }

  $images.each(function () {
    if (this.complete) {
      loadedImages++;
    } else {
      owlJqueryObject(this).one("load error", function () {
        loadedImages++;
        if (loadedImages === totalImages) {
          initCarousel();
        }
      });
    }
  });

  // fallback if all images already cached
  if (loadedImages === totalImages) {
    initCarousel();
  }

});
</script>

