// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery2
//= require jquery_ujs
//= require jquery-ui/autocomplete
//= require markdown/bootstrap-markdown.js
//= require markdown/bootstrap-markdown.de.js
//= require init.js
//= require_tree .

/* global cforum, MathJax */

function hasLocalstorage() {
  try {
    return 'localStorage' in window && window['localStorage'] !== null;
  }
  catch (e) {
    return false;
  }
}

function t(key, deflt) {
  var pieces = key.split('.');
  var loc = cforum.l10n;

  for(var i = 0; i < pieces.length; ++i) {
    if(loc[pieces[i]]) {
      loc = loc[pieces[i]];
    }
    else {
      if(deflt) {
        return deflt;
      }

      return "translation missing: " + key;
    }
  }

  return loc;
}

function setDismissHandlers() {
  $("[data-dismiss]").click(function() {
    var $this = $(this);
    var clss = $this.attr('data-dismiss');

    var elem = $this.closest("." + clss);
    elem.fadeOut('fast', function() { $(this).remove(); });
  });

  $("[data-dismiss]").each(function() {
    var $this = $(this);
    var clss = $this.attr('data-dismiss');

    $this.closest("." + clss).on('click', function() {
      $(this).fadeOut('fast', function() { $(this).remove(); });
    });
  });
}

function autohideAlerts() {
  window.setTimeout(function() {
    $(".cf-success").fadeOut('fast', function() { $(this).remove(); });
  }, 3000);
}

$(function() {
  setDismissHandlers();
  $(".select2").select2({minimumResultsForSearch: -1});
  $("form").dependentQuestions();

  $("#forum-list select").on('change', function() {
    if($(this).val() != "") {
      $("#forum-list").submit();
    }
  });

  autohideAlerts();
  $("textarea").tabEnable();
});

cforum.alert = {
  alert: function(text, type) {
    var alrt = $("<div class=\"" + type + " cf-alert\"><button type=\"button\" class=\"close\" data-dismiss=\"cf-alert\" aria-label=\"Close\"><span aria-hidden=\"true\">&times;</span></button></div>");
    alrt.text(text);
    $("#alerts-container").append(alrt);
    setDismissHandlers();

    window.setTimeout(function () { alrt.fadeOut('fast', function() { $(this).remove(); }); }, 3000);
  },

  error: function(text) {
    cforum.alert.alert(text, 'cf-error');
  },

  success: function(text) {
    cforum.alert.alert(text, 'cf-success');
  }
};

cforum.updateFavicon = function() {
  var favicon = $('link[rel="shortcut icon"]');
  if(favicon.attr('href') != cforum.faviconUrl) {
    favicon.remove();
    $('head').append('<link rel="shortcut icon" type="image/x-icon" href="' + cforum.faviconUrl + '">');
  }
};

// eof

@@ -2229,16 +2229,87 @@
 nable();
+%0A%0A  $(window).on('focus', function() %7B%0A    cforum.resetFavicon();%0A  %7D);
 %0A%7D);%0A%0Acf
@@ -3177,15 +3177,315 @@
   %7D%0A%7D;%0A%0A
+cforum.resetFavicon = function() %7B%0A  var favicon = $('link%5Brel=%22shortcut icon%22%5D');%0A  if(favicon.attr('href') != 'http://src.selfhtml.org/favicon2.ico') %7B%0A    favicon.remove();%0A    $('head').append('%3Clink rel=%22shortcut icon%22 type=%22image/x-icon%22 href=%22http://src.selfhtml.org/favicon2.ico%22%3E');%0A  %7D%0A%7D;%0A%0A
 // eof%0A

reset favicon on focus