/* typewatch() borrowed from http://stackoverflow.com/questions/2219924/idiomatic-jquery-delayed-event-only-after-a-short-pause-in-typing-e-g-timew  */
var typewatch = (function(){
  var timer = 0;
  return function(callback, ms){
    clearTimeout (timer);
    timer = setTimeout(callback, ms);
  }
})();

function autosave_request(e) {
  $('#article_form').keyup(function() {
    typewatch(function() {
      $.ajax({
        type: "POST",
        url: '/admin/content/autosave',
        data: $("#article_form").serialize()});
    }, 5000)
  });
}

function set_widerea(element) {
  if ($("#article_id").val() == "") {
    wideArea().clearData(element);
  }

  wideArea();
}

function tag_manager() {
  var tagUrl = "/admin/content/auto_complete_for_article_keywords";

  $.getJSON(tagUrl, function (tags) {
    var tagApi = $("#article_keywords").tagsManager({
      prefilled: $('#article_keywords').val(),
      onlyTagList: true,
      tagList: tags
    });

    $("#article_keywords").typeahead({
      limit: 15,
      prefetch: tagUrl
    }).on('typeahead:selected', function (e, d) {
      tagApi.tagsManager("pushTag", d.value);
    });
  });
}

function save_article_tags() {
  $('#article_keywords').val($('#article_form').find('input[name="hidden-article[keywords]"]').val());
}

function doneTyping () {
  $( "#save-bar").fadeIn(2000, function() {

  });
}

function set_savebar() {
  var typingTimer;
  var doneTypingInterval = 3000;

  $( "#article_body_and_extended" ).keydown(function() {
    $( "#save-bar").fadeOut(2000, function() {

    });
    clearTimeout(typingTimer);
  });

  $('#article_body_and_extended').keyup(function(){
      typingTimer = setTimeout(doneTyping, doneTypingInterval);
  });
}

$(document).ready(function() {
  $('#article_form').each(function(e){autosave_request(e)});
  $('#article_form').submit(function(e){save_article_tags()});
  $('#article_form').each(function(e){tag_manager()});
  $('#article_form').each(function(e){set_widerea($('#article_body_and_extended'))});
  $('#article_body_and_extended').each(function(e){set_savebar()});
  $('#page_form').each(function(e){set_widerea($('#page_body'))});
});

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

@@ -303,253 +303,8 @@
 );%0A%0A
-function autosave_request(e) %7B%0A  $('#article_form').keyup(function() %7B%0A    typewatch(function() %7B%0A      $.ajax(%7B%0A        type: %22POST%22,%0A        url: '/admin/content/autosave',%0A        data: $(%22#article_form%22).serialize()%7D);%0A    %7D, 5000)%0A  %7D);%0A%7D%0A%0A
 func
@@ -331,16 +331,16 @@
 ment) %7B%0A
+
   if ($(
@@ -554,24 +554,106 @@
 on (tags) %7B%0A
+%0A    $('#article_keywords').val($('#article_keywords').val().replace(/%5C%22/g,%22%22));%0A%0A
     var tagA
@@ -1024,35 +1024,36 @@
 %7B%0A  
-$('#a
+var hiddenA
 rticle
-_k
+K
 eywords
-').val(
+ = 
 $('#
@@ -1119,16 +1119,296 @@
 ').val()
+,%0A      keywords = hiddenArticleKeywords.split(',');%0A%0A  if (hiddenArticleKeywords == %22%22) %7B%0A    $('#article_keywords').val(%22%22);%0A    return;%0A  %7D%0A%0A  for (keyword in keywords) %7B%0A    keywords%5Bkeyword%5D = '%22' + keywords%5Bkeyword%5D + '%22'%0A  %7D%0A%0A  $('#article_keywords').val(keywords.join(',')
 );%0A%7D%0A%0Afu
@@ -1838,16 +1838,16 @@
  %7D);%0A%7D%0A%0A
+
 $(docume
@@ -1873,69 +1873,8 @@
 ) %7B%0A
-  $('#article_form').each(function(e)%7Bautosave_request(e)%7D);%0A
   $(

remove auto save as it was causing new tags to be saved