(function(){
  function pad(n){ return (n<10?'0':'') + n; }
  function initOne(root){
    if(!root || root.__amzInited) return;
    var end = parseInt(root.getAttribute('data-nikan-amz1-end')||'0',10);
    if(!end) return;
    root.__amzInited = true;

    var hh = root.querySelector('[data-hh]');
    var mm = root.querySelector('[data-mm]');
    var ss = root.querySelector('[data-ss]');

    function tick(){
      var now = Math.floor(Date.now()/1000);
      var diff = end - now;
      if(diff < 0) diff = 0;
      var h = Math.floor(diff/3600);
      var m = Math.floor((diff%3600)/60);
      var s = diff%60;
      if(hh) hh.textContent = pad(h);
      if(mm) mm.textContent = pad(m);
      if(ss) ss.textContent = pad(s);
      if(diff===0){ clearInterval(t); }
    }
    tick();
    var t = setInterval(tick, 1000);
  }

  function scan(){ document.querySelectorAll('.nikan-amz1, .nikan-amz2').forEach(initOne); }

  document.addEventListener('DOMContentLoaded', scan);
  window.addEventListener('load', scan);

  if(window.elementorFrontend && window.elementorFrontend.hooks){
    window.elementorFrontend.hooks.addAction('frontend/element_ready/nikan_sepid_amazing_product_1.default', function($scope){
      var el = $scope[0] || ($scope.get && $scope.get(0));
      if(el) initOne(el);
    });
    window.elementorFrontend.hooks.addAction('frontend/element_ready/nikan_sepid_amazing_product_2.default', function($scope){
      var el = $scope[0] || ($scope.get && $scope.get(0));
      if(el) initOne(el);
    });
  }
})();