<!-- check to see if this controller/action allow tinymce before we load the gigantor js; see application_helper -->
<% if allow_tinymce?(controller) %>
  <%= yield :tinymce %>
<% end %>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type="text/javascript"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/jquery-ui.min.js" type="text/javascript"></script>
<!-- if user has googleapis blocked for some reason we need a fallback -->
<script type="text/javascript">
  if (typeof jQuery == 'undefined') {
    document.write(unescape("%3Cscript src='/javascripts/jquery.min.js' type='text/javascript'%3E%3C/script%3E"));
    document.write(unescape("%3Cscript src='/javascripts/jquery-ui.min.js' type='text/javascript'%3E%3C/script%3E"));
  }
</script>

<% if Rails.env.test? %>
  <!--
    The autocomplete dropdown for a field (generated by jquery.tokeninput) will only appear
    if the field has focus. The jQuery :focus pseudo-selector returns the focused element
    on the page, but only if the browser itself has focus, and we're running tests in a
    headless browser which is always out of focus.

    We get around this by forcing jQuery to use its own selector engine instead of
    the browser's.

    By Matthew O'Riordan (http://mattheworiordan.com).
    License: IDGAF.

    https://github.com/mattheworiordan/jquery-focus-selenium-webkit-fix
  -->
  <script type="text/javascript">
    jQuery.find.selectors.filters.focus = function(elem) {
      var doc = elem.ownerDocument;
      return elem === doc.activeElement && !!(elem.type || elem.href);
    }
  </script>
  <!--
    Disable jQuery animations, because the autocomplete dropdown cannot be
    clicked while it's in the slideDown animation. This causes intermittent
    failures in JavaScript-based autocomplete tests.
  -->
  <script type="text/javascript">
    jQuery.fx.off = true;
  </script>
<% end %>

<script type="text/javascript">$j = jQuery.noConflict();</script>
<%= javascript_include_tag "jquery.scrollTo.min", "jquery.livequery.min", "rails", "application", "bootstrap/bootstrap-dropdown.min", "jquery-shuffle", "jquery.tokeninput.min", "jquery.trap.min", "ao3modal.min", "js.cookie.min", skip_pipeline: true %>

<%= javascript_include_tag "filters.min", skip_pipeline: true %>

<% if allow_tinymce?(controller) %>
  <%= yield :tinymce_init %>
<% end %>

<% unless Rails.env.test? || current_user.try(:accepted_tos_version) == @current_tos_version || tos_exempt_page? %>
  <%= javascript_tag do %>
    // We can't rely on !window.localStorage to test localStorage support in
    // browsers like Safari 9, which technically support it, but which have a
    // storage length of 0 in private mode.
    // Credit: https://github.com/getgrav/grav-plugin-admin/commit/cfe2188f10c4ca604e03c96f3e21537fda1cdf9a
    function isSupported() {
        var item = "localStoragePolyfill";
        try {
            localStorage.setItem(item, item);
            localStorage.removeItem(item);
            return true;
        } catch (e) {
            return false;
        }
    }

    function acceptTOS() {
      if (isSupported()) {
        localStorage.setItem("accepted_tos", "<%= @current_tos_version %>");
      } else {
        Cookies.set("accepted_tos", "<%= @current_tos_version %>", { expires: 365 });
      }
    }

    $j(document).ready(function() {
      <% if current_user %>
        <%# Users who haven't accepted this TOS need the popup. %>
        $j("body").prepend("<%= escape_javascript(render "layouts/tos_prompt") %>");
      <% elsif params[:tos] == "yes" %>
        <%# Guests can bypass the popup using ?tos=yes in the URLs. %>
        acceptTOS();
      <% else %>
        <%# Otherwise, guests who don't have the localStorage item or the
            cookie need the popup.
            We have to use JavaScript to remember their choice or risk issues
            when full page caching is enabled. %>
        if (localStorage.getItem("accepted_tos") !== "<%= @current_tos_version %>" && Cookies.get("accepted_tos") !== "<%= @current_tos_version %>") {
          $j("body").prepend("<%= escape_javascript(render "layouts/tos_prompt") %>");
        }
      <% end %>
    });
  <% end %>
<% end %>

<% # Renders layouts/proxy_notice below skip links in layouts/application. %>
<% if %w(staging production).include?(Rails.env) %>
  <%= javascript_tag do %>
    $j(document).ready(function() {
      var permitted_hosts = <%= ArchiveConfig.PERMITTED_HOSTS.to_json.html_safe %>;
      var current_host = window.location.hostname;

      if (!permitted_hosts.includes(current_host) && Cookies.get("proxy_notice") !== "0" && window.location.protocol !== "file:") {
        $j("#skiplinks").after("<%= escape_javascript(render "layouts/proxy_notice") %>");
      }
    });
  <% end %>
<% end %>

<%= yield :footer_js %>
