$(function(){
$('audio').on("canplay", function(){
  $(this).parents('.part-html').find('.podcast-time').html(toHHMMSS(this.currentTime)+" / "+toHHMMSS(this.duration))
});
$('.podcast-playpause').on('click', function() {
  let that = this;
  let audio = $(this).parents('.part-html').find('audio').get(0);
  if($(this).hasClass('playing')) {
    $(this).removeClass('playing');
    audio.pause();
    $(audio).off('timeupdate');
  } else {
    $(this).addClass('playing');
    audio.play();
    $(audio).on('timeupdate', function() { 
      $(that).parents('.part-html').find('.podcast-progress').attr("value", this.currentTime / this.duration);
      $(that).parents('.part-html').find('.podcast-time').html(toHHMMSS(this.currentTime)+" / "+toHHMMSS(this.duration))
    });
  }
});
$('.podcast-speed a').on('click', function() {
  $(this).siblings().removeClass('active');
  $(this).addClass('active');
  let audio = $(this).parents('.part-html').find('audio').get(0);
  if($(this).hasClass('podcast-speed-10')) {
    audio.playbackRate=1;
  }
  else if($(this).hasClass('podcast-speed-15')) {
    audio.playbackRate=1.5;
  }
  else if($(this).hasClass('podcast-speed-20')) {
    audio.playbackRate=2;
  }
});
$('.podcast-progress').on('click', function(e) { console.log(this.offsetWidth)
  let audio = $(this).parents('.part-html').find('audio').get(0);
  var percent = e.offsetX / this.offsetWidth;
  audio.currentTime = percent * audio.duration;
  $(this).val(percent);
  $(this).parents('.part-html').find('.podcast-time').html(toHHMMSS(audio.currentTime)+" / "+toHHMMSS(audio.duration))
});
});

function toHHMMSS(sec) {
    var sec_num = parseInt(sec, 10); 
    var hours   = Math.floor(sec_num / 3600);
    var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
    var seconds = sec_num - (hours * 3600) - (minutes * 60);

    if (hours   < 10) {hours   = "0"+hours;}
    if (minutes < 10) {minutes = "0"+minutes;}
    if (seconds < 10) {seconds = "0"+seconds;}
    return hours == "00" ? minutes+':'+seconds : hours+':'+minutes+':'+seconds;
}
