Ajax.Responders.register({
  onCreate: function() {
    if (Ajax.activeRequestCount > 0)
      Element.show('spinner');
  },
  onComplete: function() {
    if (Ajax.activeRequestCount == 0)
      Element.hide('spinner');
  }
});

var Opinion = {
  addElement: function(element) {
    new Effect.BlindDown('add-'+element, {duration:0.2});
    new Effect.ScrollTo(element+'-title-input');
    Field.focus(element+'-title-input');
  },
  
  cancelAddElement: function(element) {
    new Effect.BlindUp('add-'+element, {duration:0.1});
  },
  
  renameArea: function(id) {
    if($('rename-area-form-'+id)) {
      Element.hide('area-inner-'+id);
      Element.show('rename-area-form-'+id);
    }
    else {
      new Ajax.Request('/admin/rename_area_form/'+id, {evalScripts:true});
    }
  },
  
  cancelRenameElement: function(id, element) {
    new Effect.BlindUp('rename-'+element+'-form-'+id, {duration:0.1});
    Element.show(element+'-inner-'+id);
  },
  
  renameCategory: function(id) {
    if($('edit-category-form-'+id)) {
      Element.hide('category-inner-'+id);
      Element.show('edit-category-form-'+id);
    }
    else {
      new Ajax.Request('/'+current_area+'/categories/rename_category_form/'+ id, {evalScripts:true});
    }
  },
  
  removeCategory: function(id) {
    new Ajax.Request('/'+current_area+'/categories/remove_category/'+ id, {evalScripts:true});
  },
  
  cancelRenameCategory: function(id) {
    new Effect.BlindUp('edit-category-form-'+id, {duration:0.1});
    Element.show('category-inner-'+id);
  },
  
  addTopic: function() {
    new Effect.BlindDown('add-topic', {duration:0.2});
    new Effect.ScrollTo('add-topic');
    Field.focus('topic-title-input');
  },
  
  cancelAddTopic: function() {
    new Effect.BlindUp('add-topic', {duration:0.1});
  },
  
  postComment: function() {
    new Effect.BlindDown('post-comment', {duration:0.2});
    new Effect.ScrollTo('post-comment');
    Field.focus('comment-body');
  },
  
  cancelPostComment: function() {
    new Effect.BlindUp('post-comment', {duration:0.1});
  },
  
  addRemoteSite: function() {
    Element.hide('add-remote-link');
    new Effect.BlindDown('add-remote', {duration:0.2});
    Field.focus('remote-url');
  },
  
  cancelAddRemoteSite: function() {
    new Effect.BlindUp('add-remote', {duration:0.1});
    Element.show('add-remote-link');
  },
  
  removeRemote: function(id) {
    new Ajax.Request('/admin/remove_remote/'+ id, {evalScripts:true});
  },
  
  createUser: function() {
    new Effect.BlindDown('create-user', {duration:0.2});
    new Effect.ScrollTo('create-user');
    if($('user_email')) { Field.focus('user_email'); }
  },
  
  cancelCreateUser: function() {
    new Effect.BlindUp('create-user', {duration:0.1});
  },
  
  toggleTextarea: function(bigger) {
    if(bigger) {
      Element.addClassName('topic_body',"more");
      Element.removeClassName('topic_body',"less");
      Element.hide("more-space");
      Element.show("less-space");
    } else {
      Element.addClassName('topic_body',"less");
      Element.removeClassName('topic_body',"more");
      Element.hide("less-space");
      Element.show("more-space");
    }
  },
  
  showTextileHelp: function() {
    if($('textile-help')) {
      if(Element.visible('textile-help')) {
        new Effect.BlindUp('textile-help', {duration:0.2});
      } else {
        new Effect.BlindDown('textile-help', {duration:0.2});
      }
    } else {
      new Ajax.Request("/textile_help");
    }
  },
  
  togglePair: function(element1, element2) {
    Element.toggle(element1);
    Element.toggle(element2);
  }

}