// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
// DCS Javascript Classes, requires prototype library

var Console;
try {
    Console = Base.extend({
        initialized: false,
        output: null,

        constructor: function()
        {
            this.init();
        },

        init: function() {
            var dialog = Dialog.getByName("console");
            if( !dialog )
                dialog = new Dialog({title: "Debug Console", in_tab: false, centerH: false, centerV: false, left: 0, top: 0, width: 300, height: 500});
            this.output = dialog.getContentElement();
        },

        print: function(message) {
            this.output.innerHTML += message + "<br/>";
        }
    }, {
        singleton: null,
        print: function(message) {
            if( !Console.singleton )
                Console.singleton = new Console();
            Console.singleton.print(message);
        }
    });
} catch( e )
{
    // ignore
}


//Update the Ajax.Updater class for error handling.
Ajax.Updater.prototype.oldInitialize = Ajax.Updater.prototype.initialize;
Ajax.Updater.prototype.initialize = function(container, url, options) {
    var newContainer = container;
    if(!container.success) newContainer = {success: container};
    var newOptions = { onFailure: Error.handleError };
    Object.extend(newOptions, options);
    this.oldInitialize(newContainer, url, newOptions);
};

Ajax.Updater.prototype.updateContent = function() {
    var receiver = this.container[this.success() ? 'success' : 'failure'];
    var response = this.transport.responseText;

    if (!this.options.evalScripts) response = response.stripScripts();

    if (receiver = $(receiver)) {
        if (this.options.insertion)
            new this.options.insertion(receiver, response);
        else
            receiver.update(response);
    }
    
    tooltipManager.register(receiver);

    if (this.success()) {
        if (this.onComplete)
            setTimeout(this.onComplete.bind(this), 10);
    }
};

//Update the Ajax.Request class for error handling.
Ajax.Request.prototype.oldInitialize = Ajax.Request.prototype.initialize;
Ajax.Request.prototype.initialize = function(url, options) {
    var newOptions = { onFailure: Error.handleError };
    Object.extend(newOptions, options);
    this.oldInitialize(url, newOptions);
};

// 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 LoginForm = {
  setToPassword: function() {
    $('openid_fields').hide();
    $('password_fields').show();
  },
  
  setToOpenID: function() {
    $('password_fields').hide();
    $('openid_fields').show();
  }
}

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

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

    var row = $('post_' + currentId + '-row');
    if(row) row.removeClassName('editing');
    $('edit').setAttribute('post_id', '');
  },
  
  // gets the current post id we're editing
  currentReplyId: function() {
    return $('edit').getAttribute('post_id');
  },
  
  // checks whether we're editing this post already
  isEditing: function(postId) {
    if (this.currentReplyId() == postId.toString())
    {
      $('edit').show();
      $('edit_post_body').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() {
    EditForm.cancel();
    $('reply').toggle();
    $('post_body').focus();
    // for Safari which is sometime weird
//    setTimeout('$(\"post_body\").focus();',50);
  }
}
