{% load compress %}
{% load url from future %}
{% load datepicker_locale from horizon %}

{% datepicker_locale as DATEPICKER_LOCALE %}

{% comment %} Django's JavaScript i18n Implementation {% endcomment %}
<script type="text/javascript" src="{% url 'horizon:jsi18n' 'horizon' %}"></script>

{% comment %} Compress jQuery, Angular, Plugins, Bootstrap, Hogan.js and Horizon-specific JS. {% endcomment %}
{% compress js %}

<script type="text/javascript" src="{{ STATIC_URL }}leonardo/js/lightbox.min.js"></script>

<script type="text/javascript">var STATIC_URL = "{{ STATIC_URL }}";</script>
<script src='{{ STATIC_URL }}horizon/lib/angular/angular-cookies.js'></script>
<script src='{{ STATIC_URL }}horizon/lib/angular/angular-sanitize.js'></script>
<script src="{{ STATIC_URL }}horizon/lib/angular/lrdragndrop.js"></script>
<script src="{{ STATIC_URL }}horizon/lib/angular/smart-table.js"></script>


<script src='{{ STATIC_URL }}horizon/js/angular/controllers/namespace-controller.js'></script>
<script src='{{ STATIC_URL }}horizon/js/angular/controllers/dummy.js'></script>
<script src='{{ STATIC_URL }}horizon/js/angular/controllers/modal-form-update-metadata-ctrl.js'></script>
<script src='{{ STATIC_URL }}horizon/js/angular/directives/forms.js'></script>

<script src='{{ STATIC_URL }}horizon/js/angular/horizon.conf.js'></script>
<script src='{{ STATIC_URL }}horizon/js/angular/services/horizon.utils.js'></script>
<script src='{{ STATIC_URL }}horizon/js/angular/hz.api.module.js'></script>
<script src='{{ STATIC_URL }}horizon/js/angular/services/hz.api.service.js'></script>

<script src='{{ STATIC_URL }}horizon/js/angular/filters/filters.js'></script>
<script src='{{ STATIC_URL }}horizon/js/angular/services/hz.api.security-group.js'></script>

<script src="{{ STATIC_URL }}horizon/lib/d3.js"></script>

<script src='{{ STATIC_URL }}js/angular/ng-FitText.js'></script>
<script src='{{ STATIC_URL }}angular/widget.module.js'></script>
<script src='{{ STATIC_URL }}angular/form/form.js'></script>
<script src='{{ STATIC_URL }}angular/help-panel/help-panel.js'></script>
<script src='{{ STATIC_URL }}angular/wizard/wizard.js'></script>
<script src='{{ STATIC_URL }}angular/action-list/action-list.js'></script>
<script src='{{ STATIC_URL }}angular/action-list/button-tooltip.js'></script>
<script src='{{ STATIC_URL }}angular/table/table.js'></script>
<script src='{{ STATIC_URL }}angular/table/basic-table.js'></script>
<script src="{{ STATIC_URL }}angular/login/login.js"></script>
<script src='{{ STATIC_URL }}angular/modal/modal.js'></script>
<script src='{{ STATIC_URL }}angular/modal/modal-wait-spinner.js'></script>
<script src='{{ STATIC_URL }}angular/bind-scope/bind-scope.js'></script>
<script src='{{ STATIC_URL }}angular/transfer-table/transfer-table.js'></script>
<script src='{{ STATIC_URL }}angular/charts/charts.js'></script>
<script src='{{ STATIC_URL }}angular/charts/chart-tooltip.js'></script>
<script src='{{ STATIC_URL }}angular/charts/pie-chart.js'></script>
<script src='{{ STATIC_URL }}angular/metadata-tree/metadata-tree.js'></script>
<script src='{{ STATIC_URL }}angular/metadata-tree/metadata-tree-service.js'></script>
<script src='{{ STATIC_URL }}angular/metadata-display/metadata-display.js'></script>
<script src='{{ STATIC_URL }}angular/magic-search/magic-search.js'></script>
<script src='{{ STATIC_URL }}angular/validators/validators.js'></script>
<script src='{{ STATIC_URL }}angular/workflow/workflow.js'></script>
<script src="{{ STATIC_URL }}js/lib/select2.js"></script>

<script src='{{ STATIC_URL }}horizon/lib/jquery/jquery.quicksearch.js'></script>
<script src="{{ STATIC_URL }}horizon/lib/jquery/jquery.tablesorter.js"></script>
<script src="{{ STATIC_URL }}horizon/lib/spin.js"></script>
<script src="{{ STATIC_URL }}horizon/lib/spin.jquery.js"></script>
<script src="{{ STATIC_URL }}horizon/lib/jquery-ui/ui/jquery-ui.js"></script>
<script src="{{ STATIC_URL }}horizon/lib/jquery/jquery.bootstrap.wizard.js"></script>

<script src="{{ STATIC_URL }}bootstrap/js/bootstrap.js"></script>
<script src='{{ STATIC_URL }}horizon/lib/bootstrap_datepicker/bootstrap-datepicker.js'></script>

<script src="{{ STATIC_URL }}horizon/lib/magic_search/magic_search.js"></script>

<script src="{{ STATIC_URL }}horizon/lib/hogan.js"></script>

<script src='{{ STATIC_URL }}horizon/js/horizon.accordion_nav.js'></script>
<script src='{{ STATIC_URL }}horizon/js/horizon.communication.js'></script>
<script src='{{ STATIC_URL }}horizon/js/horizon.forms.js'></script>
<script src='{{ STATIC_URL }}horizon/js/horizon_formset_table.js'></script>
<script src='{{ STATIC_URL }}horizon/js/horizon.messages.js'></script>
<script src='{{ STATIC_URL }}horizon/js/horizon.modals.js'></script>

<script type="text/javascript">
  horizon.modals.MODAL_BACKDROP = "{% firstof HORIZON_CONFIG.modal_backdrop 'static' %}";
</script>
<script src='{{ STATIC_URL }}horizon/js/horizon.tables.js'></script>
<script src='{{ STATIC_URL }}horizon/js/horizon.tables_inline_edit.js'></script>
<script src='{{ STATIC_URL }}horizon/js/horizon.tabs.js'></script>
<script src='{{ STATIC_URL }}horizon/js/horizon.templates.js'></script>

{% for file in HORIZON_CONFIG.js_files %}
    <script src='{{ STATIC_URL }}{{ file }}'></script>
{% endfor %}

{% block custom_js_files %}{% endblock %}
{% endcompress %}
<script src="{{ STATIC_URL }}django_select2/django_select2.js"></script>

{% comment %} Client-side Templates (These should *not* be inside the "compress" tag.) {% endcomment %}
{% include "horizon/client_side/templates.html" %}

{% comment %}Locale sensitive js needs to be included outisde the "compress" tag{% endcomment %}
{% if DATEPICKER_LOCALE and DATEPICKER_LOCALE != 'en' %}
<script src='{{ STATIC_URL }}horizon/lib/bootstrap_datepicker/locales/bootstrap-datepicker.{{ DATEPICKER_LOCALE }}.js'></script>
{% endif %}

{% comment %}Go!{% endcomment %}
<script>
  // Call init on DOM ready.
  $(document).ready(horizon.init);
</script>

{% if request.frontend_editing %}

<script type="text/javascript">

/* Setup django csrf-token for ajax calls, necessary since Django 1.2.5 */
/* See http://docs.djangoproject.com/en/1.2/releases/1.2.5/#csrf-exception-for-ajax-requests */
  
$.ajaxSetup({
    beforeSend: function(xhr, settings) {
        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;
        }
        if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
            // Only send the token to relative URLs i.e. locally.
            xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
        }
    }
});

  $(function() {
      // Widgets sort
      $(".col3").sortable({
        revert: true,
        forceHelperSize: true,
        opacity: 0.3,
        tolerance: 'pointer',
        start: function (e,ui) {
          $(ui.helper).addClass('fe_dragging');
        },
        update: function() {
          $(this).sortable('refresh');
          $(this).sortable('refreshPositions');
          widgets = $(this).sortable('toArray');
          sort = true;
          for(var i = 0, len = widgets.length; i < len; i++) {
            if(widgets[i].search("fe_box_new") > -1) {
              sort = false;
            }
            if(widgets[i].length === 0) {
              widgets.pop(i);
            }
          }

          if(sort) {
            sort_options = {
            url: '{% url 'widget_sort' %}',
            type: "POST",
            data: {
                  'page_id': {{ feincms_page.id }},
                  'region': 'col3',
                  'widgets': widgets,
            },
            success: function(){
              horizon.autoDismissAlerts();
            }
            }

            horizon.clearAllMessages();
            $.ajax(sort_options);

          }
        },
        stop: function (e,ui) {
          $(ui.item).css({
            width:''
          }).removeClass('fe_dragging');
        },
        handle: ".leonardo-widget-dnd-handle",
      });

});

</script>
{% endif %}

{% compress js %}
<script type="text/javascript">
$(function() {

  $(document).delegate('*[data-toggle="lightbox"]', 'click', function(event) {
      event.preventDefault();
      $(this).ekkoLightbox();
  }); 

  new WOW().init();
  horizon.modals.addModalInitFunction(function(modal) {
    $('.django-select2').not('django-select2-heavy').select2();
    $('.django-select2.django-select2-heavy').each(function () {
        var field_id = $(this).data('field_id');
        $(this).select2({
            ajax: {
                data: function (params) {
                    return {
                        term: params.term,
                        page: params.page,
                        field_id: field_id
                    };
                },
                processResults: function (data, page) {
                  return {
                    results: data.results
                  };
                }
            }
        });
    });
  });

});
</script>
{% endcompress %}