// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var TopicForm = {
  editNewTitle: function(txtField) {
    $('new_topic').innerHTML = (txtField.value.length > 5) ? txtField.value : 'New Topic';
  }
}

var EditForm = {
  // show the form
  init: function(commentId) {
    $('edit-comment-' + commentId + '_spinner').show();
    this.clearReplyId();
  },

  // sets the current comment id we're editing
  setReplyId: function(commentId) {
    $('edit').setAttribute('comment_id', commentId.toString());
    $('comments-' + commentId + '-row').addClassName('editing');
    if($('reply')) $('reply').hide();
  },
  
  // clears the current comment id
  clearReplyId: function() {
    var currentId = this.currentReplyId()
    if(!currentId || currentId == '') return;

    var row = $('comments-' + currentId + '-row');
    if(row) row.removeClassName('editing');
    $('edit').setAttribute('comment_id', '');
  },
  
  // gets the current comment id we're editing
  currentReplyId: function() {
    return $('edit').getAttribute('comment_id');
  },
  
  // checks whether we're editing this comment already
  isEditing: function(commentId) {
    if (this.currentReplyId() == commentId.toString())
    {
      $('edit').show();
      $('edit_comment_comment').focus();
      return true;
    }
    return false;
  },

  // close reply, clear current reply id
  cancel: function() {
    this.clearReplyId();
    $('edit').hide()
  }
}

var ReplyForm = {
  // yes, i use setTimeout for a reason
  init: function(parentId, parentType) {
    this.setParent(parentId, parentType);
    $('reply').toggle();
    $('comment_comment').focus();
    // for Safari which is sometime weird
//    setTimeout('$(\"comment_comment\").focus();',50);
  },

  setParent: function(parentId, parentType) {
    $('commentable_id').setAttribute('value', parentId.toString());
    $('commentable_type').setAttribute('value', parentType.toString());
  }

}

function add_another_file() {
  html = $('new_file').innerHTML
  new Insertion.Bottom('files', html);
}

function add_another_edit_file() {
  html = $('new_file_edit').innerHTML
  new Insertion.Bottom('edit_files', html);
}

function set_search_rating(ratingId, valueId, val) {
  $(valueId).value=val;
  $(ratingId).style.width='16px';
  $(ratingId).style.marginLeft=(val - 1) * 16 + 'px'
}

function set_rating(ratingId, valueId, val) {
  $(valueId).value=val;
  $(ratingId).style.width=val * 16 + 'px'
}