// App JS

$(function() {

  var currentTab = "summary";

  function init() {

    activateTabbedPanels();

    activateSelectableRows();

    toggleProblemsCheckboxes();

    bindRequiredPasswordMarks();

    // On page apps/:app_id/edit
    $('a.copy_config').on("click", function() {
      $('select.choose_other_app').show().focus();
    });

    $('select.choose_other_app').on("change", function() {
      var loc = window.location;
      window.location.href = loc.protocol + "//" + loc.host + loc.pathname +
                             "?copy_attributes_from=" + $(this).val();
    });

    $('input[type=submit][data-action]').live('click', function() {
      $(this).closest('form').attr('action', $(this).attr('data-action'));
    });
  }

  function activateTabbedPanels() {
    $('.tab-bar a').each(function(){
      var tab = $(this);
      var panel = $('#'+tab.attr('rel'));
      panel.addClass('panel');
      panel.find('h3').hide();
    });

    $('.tab-bar a').click(function(){
      activateTab($(this));
      return(false);
    });
    activateTab($('.tab-bar ul li a.button[rel=' + currentTab + ']'));
  }

  function activateTab(tab) {
    tab = $(tab);
    var panel = $('#'+tab.attr('rel'));

    tab.closest('.tab-bar').find('a.active').removeClass('active');
    tab.addClass('active');

    // If clicking into 'backtrace' tab, hide external backtrace
    if (tab.attr('rel') == "backtrace") { hide_external_backtrace(); }

    $('.panel').hide();
    panel.show();
  }

  window.toggleProblemsCheckboxes = function() {
    var checkboxToggler = $('#toggle_problems_checkboxes');

    checkboxToggler.on("click", function() {
      $('input[name^="problems"]').each(function() {
        this.checked = checkboxToggler.get(0).checked;
      });
    });
  }

  function activateSelectableRows() {
    $('.selectable tr').click(function(event) {
      if(!_.include(['A', 'INPUT', 'BUTTON', 'TEXTAREA'], event.target.nodeName)) {
        var checkbox = $(this).find('input[name="problems[]"]');
        checkbox.attr('checked', !checkbox.is(':checked'));
      }
    });
  }

  function bindRequiredPasswordMarks() {
    $('#user_github_login').keyup(function(event) {
      toggleRequiredPasswordMarks(this)
    });
  }

  function toggleRequiredPasswordMarks(input) {
      if($(input).val() == "") {
        $('#user_password').parent().attr('class', 'required')
        $('#user_password_confirmation').parent().attr('class', 'required')
      } else {
        $('#user_password').parent().attr('class', '')
        $('#user_password_confirmation').parent().attr('class', '')
      }
  }

  toggleRequiredPasswordMarks();

  function hide_external_backtrace() {
    $('tr.toggle_external_backtrace').hide();
    $('td.backtrace_separator').show();
  }
  function show_external_backtrace() {
    $('tr.toggle_external_backtrace').show();
    $('td.backtrace_separator').hide();
  }
  // Show external backtrace lines when clicking separator
  $('td.backtrace_separator span').on('click', show_external_backtrace);
  // Hide external backtrace on page load
  hide_external_backtrace();

  $('.head a.show_tail').click(function(e) {
    $(this).hide().closest('.head_and_tail').find('.tail').show();
    e.preventDefault();
  });

  init();
});

@@ -23,40 +23,8 @@
 ) %7B%0A
-%0A  var currentTab = %22summary%22;%0A%0A
   fu
@@ -705,32 +705,120 @@
 );%0A    %7D);%0A  %7D%0A%0A
+  function currentTabName()%7B%0A    return window.location.hash.slice(1) %7C%7C 'summary'%0A  %7D%0A%0A
   function activ
@@ -1103,24 +1103,63 @@
 e);%0A    %7D);%0A
+    var currentTab = currentTabName();%0A
     activate
@@ -1248,24 +1248,101 @@
 eTab(tab) %7B%0A
+    var rel = tab.attr('rel');%0A    history.pushState(null, null, '#' + rel);%0A
     tab = $(
@@ -1369,31 +1369,19 @@
 = $('#'+
-tab.attr('
 rel
-')
 );%0A%0A    
@@ -1546,31 +1546,19 @@
     if (
-tab.attr('
 rel
-')
  == %22bac
@@ -1606,43 +1606,349 @@
     
-$('.panel').hide();%0A    panel.show(
+setAnchorForPaginationLinks();%0A%0A    $('.panel').hide();%0A    panel.show();%0A  %7D%0A%0A  function setAnchorForPaginationLinks()%7B%0A    var links = $('.notice-pagination a');%0A    links.each(function()%7B%0A      var link = $(this);%0A      var new_href = link.attr('href').replace(/#.*/, '') + '#' + currentTabName();%0A      link.attr('href', new_href);%0A    %7D
 );%0A 

save current tab