$(document).ready(function(){

  // Inline code styles to Bootstrap style.
  $('tt.docutils.literal').not(".xref").each(function (i, e) {
    // ignore references
    if (!$(e).parent().hasClass("reference")) {
      $(e).replaceWith(function () {
        return $("<code />").html($(this).html());
      });
  }});

  /*
   * Scroll the window to avoid the topnav bar
   * https://github.com/twitter/bootstrap/issues/1768
   */
  if ($(".navbar.navbar-fixed-top").length > 0) {
    // var navHeight = $(".navbar").height(),
    var navHeight = 40,
      shiftWindow = function() { scrollBy(0, -navHeight - 10); };
    if (location.hash) {
      setTimeout(shiftWindow, 1);
    }
    window.addEventListener("hashchange", shiftWindow);
  }

  {%- if theme_theme_preview|tobool %}

  var makeHtmlOptions = function(){
      var header_inverse = ($.cookie('header_inverse') == 'inverse') ? "True" : "False";
      var relbar_inverse = ($.cookie('relbar_inverse') == 'inverse') ? "True" : "False";
      var inner_theme = ($.cookie('inner_theme') != '') ? "True" : "False";

      var options_text = "html_theme_options = {\n";
      options_text += "    'header_inverse': " + header_inverse + ",\n";
      options_text += "    'relbar_inverse': " + relbar_inverse + ",\n";
      options_text += "    'inner_theme': " + inner_theme + ",\n";
      if(inner_theme == "True") {
          options_text += "    'inner_theme_name': '" + $.cookie('inner_theme') + "',\n";
      }
      options_text += "}";
      $("#quick-preview-theme-options div.highlight pre").html(options_text);
   }

  /*
   * for QuickPreviewModal
   */
  if($.cookie('inner_theme')) {
      var bootstrap_version = $("#bootstrap-version").html();
      $("#current-theme").attr({href: "_static/css/bootstrap" + bootstrap_version + "/" +$.cookie('inner_theme') + ".css"})
      $("#current-adjust-theme").attr({href: "_static/css/adjust_theme/" + $.cookie('inner_theme') + ".css"})
      $('#select-theme').val($.cookie('inner_theme'));
  }
  if($.cookie('header_inverse')) {
      $("#navbar-top").removeClass("navbar-default");
      $("#navbar-top").removeClass("navbar-inverse");
      $("#navbar-top").addClass("navbar-" + $.cookie('header_inverse'));
      if ($.cookie('header_inverse') == 'inverse') {
        $('#select-header-inverse').attr('checked', true);
      }
  }
  if($.cookie('relbar_inverse')) {
      $("#navbar-related").removeClass("navbar-default");
      $("#navbar-related").removeClass("navbar-inverse");
      $("#navbar-related").addClass("navbar-" + $.cookie('relbar_inverse'));
      if ($.cookie('relbar_inverse') == 'inverse') {
        $('#select-relbar-inverse').attr('checked', true);
      }
  }
  if($.cookie('inner_theme') || $.cookie('header_inverse') || $.cookie('relbar_inverse')) {
      makeHtmlOptions();
  }

  $("#select-theme").change(function(e){
      e.preventDefault();
      var bootstrap_version = $("#bootstrap-version").html();
      var theme;
      $("#select-theme option:selected").each(function () {
          theme = $(this).val();
      });
      if (theme == '') {
          $("#current-theme").attr({href: "_static/css/bootstrap" + bootstrap_version + "/bootstrap.min.css"})
          $("#current-adjust-theme").attr({href: ""})
          $.cookie('inner_theme', theme, {expires: 1, path: '/'});
          makeHtmlOptions();
          return
      }
      $("#current-theme").attr({href: "_static/css/bootstrap" + bootstrap_version + "/" + theme + ".css"})
      $("#current-adjust-theme").attr({href: "_static/css/adjust_theme/" + theme + ".css"})
      $.cookie('inner_theme', theme, {expires: 1, path: '/'});
      makeHtmlOptions();
  });
  $("#select-header-inverse").change(function(e){
      var inverse = $('#select-header-inverse').prop("checked");
      var inverse_type = (inverse) ? 'inverse' : 'default';

      $("#navbar-top").removeClass("navbar-default");
      $("#navbar-top").removeClass("navbar-inverse");
      $("#navbar-top").addClass("navbar-" + inverse_type);
      $.cookie('header_inverse', inverse_type, {expires: 1, path: '/'});
      makeHtmlOptions();
  });
  $("#select-relbar-inverse").change(function(e){
      var inverse = $('#select-relbar-inverse').prop("checked");
      var inverse_type = (inverse) ? 'inverse' : 'default';

      $("#navbar-related").removeClass("navbar-default");
      $("#navbar-related").removeClass("navbar-inverse");
      $("#navbar-related").addClass("navbar-" + inverse_type);
      $.cookie('relbar_inverse', inverse_type, {expires: 1, path: '/'});
      makeHtmlOptions();
  });
  {%- endif %}

});
