<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.15/js/bootstrap-multiselect.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.15/css/bootstrap-multiselect.css" />
<script src="https://cdn.jsdelivr.net/npm/tooltipster@4.2.6/dist/js/tooltipster.bundle.min.js" integrity="sha256-glChvCaC6IJq49dkfulAWd7xlkPwA0FEydjhjPBintA=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/tooltipster@4.2.6/dist/css/tooltipster.bundle.min.css" integrity="sha256-Qc4lCfqZWYaHF5hgEOFrYzSIX9Rrxk0NPHRac+08QeQ=" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/tooltipster@4.2.6/dist/css/plugins/tooltipster/sideTip/themes/tooltipster-sideTip-light.min.css" integrity="sha256-Wa1I4jhSXeWd3N6RhfPlkqr1WlT+zS3Vh2YGCg0129E=" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/css/bootstrap-datepicker.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/js/bootstrap-datepicker.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.4/leaflet.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.4/leaflet.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.4.1/leaflet.markercluster.js"></script>
<!-- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.4.1/MarkerCluster.css" /> -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.4.1/MarkerCluster.Default.css" />

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.full.min.js"></script>
<script type="text/javascript">
            // Setup plugin with default settings
            $(document).ready(function() {
                $.apScrollTop({
                    'onInit': function(evt) {
                        console.log('apScrollTop: init');
                    }
                });
	$(document).on('submit', 'form.form.ajax_form', unbind_ajax_forms);
            });
    </script>
    <script type="text/javascript">
        function getCookie(name) {
            var cookieValue = null;
            if (document.cookie && document.cookie != '') {
                var cookies = document.cookie.split(';');
                for (var i = 0; i < cookies.length; i++) {
                    var cookie = jQuery.trim(cookies[i]);
                    // Does this cookie string begin with the name we want?
                    if (cookie.substring(0, name.length + 1) == (name + '=')) {
                        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                        break;
                    }
                }
            }
            return cookieValue;
        }
        function csrfSafeMethod(method) {
            // these HTTP methods do not require CSRF protection
            return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
        }

    </script>
    <script type="text/javascript">
      function unbind_ajax_forms(event){
	   $(this).find('button').attr('disabled', true);
            event.preventDefault();
            event.stopPropagation();
            var formData = $(this).serialize();
            //var button_text = $(this).find(':button').text();
          $.ajax({
          type: 'POST',
          url: $(this).attr('action'),
          data: formData,
              beforeSend: function(request) {
              var csrftoken = getCookie('csrftoken');
    request.setRequestHeader("X-CSRFToken", csrftoken);
  },
          //data:formData,
            })
          .done(function(response) {
              window[response.call_function](response)}
      )}
</script>
        <script type="text/javascript">
    //script that converts the multi select element of the annotation project menu
    $(document).ready(function() {
        $('select.selectmultiple').multiselect({
            includeSelectAllOption: true,
            enableFiltering: true
        });
        initiate_compare_tooltip();
    })
    </script>
        <script type="application/javascript">
        $('body').on('mouseenter', '.apis-autocomplete-span:not(.tooltipstered)', function() {
                    $(this).tooltipster({
                      theme: 'tooltipster-light',
                      side: 'left',
                      contentAsHTML: true,

                      functionBefore: function(instance, helper) {
                        $('#container2').remove();
                        instance.content('<div class="autocomplete-vis-container" id="container2" style="height: 200px; width: 200px"></div>');
                      },
                      functionReady: function(instance, helper){
                        var func = 'autocomplete_vis_'+$(helper.origin).data('vis-tooltip');
                        var res = window[func]($(helper.origin));
                        console.log(res);
                      },
                    })
                    .tooltipster('reposition')
                    .tooltipster('open');
                });


        function initiate_compare_tooltip() {
          //TODO: correctly detach the tooltips before reinitializing them
        $('.compare_tooltip').tooltipster({
    content: 'Loading...',
            contentAsHTML: true,
    interactive: true,
    theme: 'tooltipster-light',
            updateAnimation: null,
            zIndex: 999,
    functionBefore: function(instance, helper) {
        var $origin = $(helper.origin);
        dataOptions = $origin.attr('data-tooltip');
        if(dataOptions) {

            dataOptions = JSON.parse(dataOptions);
            console.log(dataOptions);
        } else {
            console.log('didnt read out')
        }
        if (typeof $.ApisRev == 'undefined') {
            $.ApisRev = {}; }
        $.ApisRev.url = '/apis/entities/compare/'+dataOptions['app']+'/'+dataOptions['kind']+'/'+dataOptions['pk']
        if ($origin.data('loaded') !== true) {
            $.get($.ApisRev.url, function(data) {
                instance.content(data);
                unbind_compare_forms();
                $origin.data('loaded', true);
            });
        }
    },
    functionReady: function (instance, helper) {
        $.ApisRev.instance = instance;
        unbind_compare_forms();
    }
});};
        </script>
        <script type="text/javascript">
      function unbind_compare_forms(){
      $("#change-history").parents('form').submit(function(event) {
            event.preventDefault();
            event.stopPropagation();
            var formData = $(this).serialize();
          if ($.ApisRev.url == 'undefined'){
              var url = '/'
          } else {
              var url = $.ApisRev.url
          };
          $.ajax({
          type: 'GET',
          url: url,
          data: formData,
          //data:formData,
            })
          .done(function(response) {
              $.ApisRev.instance.content(response);
              unbind_compare_forms();
          }
      )})}
</script>
