'use strict';

function is_display_type(display_type) {
  return $('.display-type').css('content') == display_type || $('.display-type').css('content') == '"' + display_type + '"';
}
function not_display_type(display_type) {
  return $('.display-type').css('content') != display_type && $('.display-type').css('content') != '"' + display_type + '"';
}

function os_init_sub_menus() {
  var menu_timer;
  $('.menu-activated-on-hover').on('mouseenter', 'ul.main-menu > li.has-sub-menu', function () {
    var $elem = $(this);
    clearTimeout(menu_timer);
    $elem.closest('ul').addClass('has-active').find('> li').removeClass('active');
    $elem.addClass('active');
  });

  $('.menu-activated-on-hover').on('mouseleave', 'ul.main-menu > li.has-sub-menu', function () {
    var $elem = $(this);
    menu_timer = setTimeout(function () {
      $elem.removeClass('active').closest('ul').removeClass('has-active');
    }, 30);
  });

  $('.menu-activated-on-click').on('click', 'li.has-sub-menu > a', function (event) {
    var $elem = $(this).closest('li');
    if ($elem.hasClass('active')) {
      $elem.removeClass('active');
    } else {
      $elem.closest('ul').find('li.active').removeClass('active');
      $elem.addClass('active');
    }
    return false;
  });
}

function extractID(videoURL){
    var videoID = videoURL.split('v=')[1];
    var ampersandPosition = videoID.indexOf('&');
    if(ampersandPosition != -1) {
      videoID = videoID.substring(0, ampersandPosition);
    }
    return videoID;
}

function getIDPWD()
{
    var url = $('#zoomURL').val();    
    var id = url.split('/').pop().split('?').shift();
    $('#zid').val(id);
    var zpw = zoomPWD(url);
    $('#zpw').val(zpw);
}

function zoomPWD(url){
    var zoomID = url.split('pwd=')[1];
    return zoomID;
}

function loadVideo()
{
    var videoURL = $('#url').val();
    if(videoURL.length > 10){
        var ID = extractID(videoURL);
        var embedURL = 'https://www.youtube.com/embed/'+ID+'?enablejsapi=1';
        $('#embed').val('https://www.youtube.com/embed/'+ID);
        $('#player').attr("src", embedURL);
        $('#player').show();
        onYouTubeIframeAPIReady();   
    }else{
        return;
    }
}

function onYouTubeIframeAPIReady() {
    const player = new YT.Player("player",{
        events: {
            "onReady": event => {
                $('#duration').val(player.getDuration());
            }
        }
    });
}
    
$(function () {
    
    $("#exampleModal").on('hidden.bs.modal', function (e) {
        $("#exampleModal iframe").attr("src", $("#exampleModal iframe").attr("src"));
    });

    
    if(uriseg == 'subject_dashboard')
    {
        var attendanceInstance = $("#attendanceChart");
        var liteLineGradientV3 = attendanceInstance[0].getContext('2d').createLinearGradient(0, 0, 0, 70);
        liteLineGradientV3.addColorStop(0, 'rgba(97,93,250,0.8)');
        liteLineGradientV3.addColorStop(1, 'rgba(97,93,250,0)');
        var chartDataV3 = [];
        var chartLabelsV3 = [];
        if(attendanceInstance.data('chart-data')){
            chartDataV3 = attendanceInstance.data('chart-data').split(',');
        }
        if(attendanceInstance.data('chart-labels')){
            chartLabelsV3 = attendanceInstance.data('chart-labels').split(',');
        }

        var attendanceChartData = {
            labels: chartLabelsV3,
            datasets: [{
                label: studentsP,
                fill: true,
                lineTension: 0.45,
                backgroundColor: liteLineGradientV3,
                borderColor: "#615dfa",
                pointBorderColor: "#615dfa",
                pointBackgroundColor: "#fff",
                pointBorderWidth: 2,
                pointHoverRadius: 3,
                pointHoverBackgroundColor: "#615dfa",
                pointHoverBorderColor: "#fff",
                pointHoverBorderWidth: 0,
                pointRadius: 0,
                pointHitRadius: 10,
                data: chartDataV3,
                spanGaps: false
            }]
        };

        var attendanceChart = new Chart(attendanceInstance, {
            type: 'line',
            data: attendanceChartData,
            options: {
                legend: {
                    display: false
                },
                scales: {
                    xAxes: [{
                        ticks: {
                            fontSize: '10',
                            fontColor: '#969da5'
                        },
                        gridLines: {
                            color: 'rgba(0,0,0,0.0)',
                            zeroLineColor: 'rgba(0,0,0,0.0)'
                        }
                    }],
                    yAxes: [{
                        display: true,
                        ticks: {
                            beginAtZero: true,
                        }
                    }]
                }
            }
        });  
    }
    
  if ($('#formValidate').length) {
    $('#formValidate').validator();
  }
  
  if ($('#formValidate').length) {
    $('#formValidate').validator();
  }
  if ($('#dataTable1').length) {
    $('#dataTable1').DataTable({ buttons: ['copy', 'excel', 'pdf'] });
  }

  $('.step-trigger-btn').on('click', function () {
    var btn_href = $(this).attr('href');
    $('.step-trigger[href="' + btn_href + '"]').click();
    return false;
  });

  $('.step-trigger').on('click', function () {
    var prev_trigger = $(this).prev('.step-trigger');
    if (prev_trigger.length && !prev_trigger.hasClass('active') && !prev_trigger.hasClass('complete')) return false;
    var content_id = $(this).attr('href');
    $(this).closest('.step-triggers').find('.step-trigger').removeClass('active');
    $(this).prev('.step-trigger').addClass('complete');
    $(this).addClass('active');
    $('.step-content').removeClass('active');
    $('.step-content' + content_id).addClass('active');
    return false;
  });
  
  if ($('.select2').length) {
    $('.select2').select2();
  }
  
  if ($('#ckeditor1').length) {
    CKEDITOR.replace('ckeditor1');
  }

  $('.mobile-menu-trigger').on('click', function () {
    $('.menu-mobile .menu-and-user').slideToggle(200, 'swing');
    return false;
  });
  os_init_sub_menus();
  $('.content-panel-toggler, .content-panel-close, .content-panel-open').on('click', function () {
    $('.all-wrapper').toggleClass('content-panel-active');
  });

  $('.ae-list').perfectScrollbar({ wheelPropagation: true });

  $('.ae-list .ae-item').on('click', function () {
    $('.ae-item.active').removeClass('active');
    $(this).addClass('active');
    return false;
  });

  $('.ae-side-menu-toggler').on('click', function () {
    $('.app-email-w').toggleClass('compact-side-menu');
  });
  
  $('.ae-item').on('click', function () {
    $('.app-email-w').addClass('forse-show-content');
  });

  if ($('.app-email-w').length) {
    if (is_display_type('phone') || is_display_type('tablet')) {
      $('.app-email-w').addClass('compact-side-menu');
    }
  }

  $('.os-dropdown-trigger').on('mouseenter', function () {
    $(this).addClass('over');
  });
  $('.os-dropdown-trigger').on('mouseleave', function () {
    $(this).removeClass('over');
  });

  $('[data-toggle="tooltip"]').tooltip();

  $('[data-toggle="popover"]').popover();

  $('.fs-selector-trigger').on('click', function () {
    $(this).closest('.fancy-selector-w').toggleClass('opened');
  });

  $('.close-ticket-info').on('click', function () {
    $('.support-ticket-content-w').addClass('folded-info').removeClass('force-show-folded-info');
    return false;
  });

  $('.show-ticket-info').on('click', function () {
    $('.support-ticket-content-w').removeClass('folded-info').addClass('force-show-folded-info');
    return false;
  });

  $('.support-index .support-tickets .support-ticket').on('click', function () {
    $('.support-index').addClass('show-ticket-content');
    return false;
  });

  $('.support-index .back-to-index').on('click', function () {
    $('.support-index').removeClass('show-ticket-content');
    return false;
  });
   
  $('.chat-content-w').perfectScrollbar({ wheelPropagation: true });

  $('.element-action-fold').on('click', function () {
    var $wrapper = $(this).closest('.element-wrapper');
    $wrapper.find('.element-box-tp, .element-box').toggle(0);
    var $icon = $(this).find('i');

    if ($wrapper.hasClass('folded')) {
      $icon.removeClass('os-icon-plus-circle').addClass('os-icon-minus-circle');
      $wrapper.removeClass('folded');
    } else {
      $icon.removeClass('os-icon-minus-circle').addClass('os-icon-plus-circle');
      $wrapper.addClass('folded');
    }
    return false;
  });
});