/**
 * Client.js
 * Description: Client javascript code for LernOCP
 * Author: John Hughes
 * Date: 15/11/2007
 */

window.dhtmlHistory.create({
      toJSON: function(o) { return Object.toJSON(o); },
      fromJSON: function(s) { return s.evalJSON(); },
      debugMode: false
    });
    
/**
 * Static class WaitControl hides/shows the busy
 * indicator for ajax functions.
 */
WaitControl = {
  m_wait_stack: new Array(),
  busy: function() {
    // Nobody waiting for anything yet.
    if(!this.m_wait_stack.length) {
      document.body.style.cursor = 'wait';
      $('throbber').show();
    }
    
    this.m_wait_stack.push(1);
  },
  ready: function() {
    this.m_wait_stack.pop();
    
    // Nobody waiting for anything more
    if(!this.m_wait_stack.length) {
      document.body.style.cursor = 'auto';
      $('throbber').hide();
    }
  }
}

MultiColumn = Class.create();
MultiColumn.prototype = {
  initialize: function(classname, editor) {
    this.classname = classname;
    this.columns = $$('input.' + classname);
    
//    tinyMCE.execCommand('mceAddControl',false,'contentarea');

    this.editor = editor;
    this.switchTo(0, true);
    this.editor.onChange.add(this.onEditorChange.bind(this));
    
    // Other classes call triggerSave(), which causes MCE to save the
    // editor back to its underlying store, but in our case, we need
    // to also save to our input field.
    this.editor.onSaveContent.add(this.onEditorChange.bind(this));
  },
  onEditorChange: function(ed, l) {
    //alert('editor changed with value: ' + l.content);
    this.currentColumn.value = l.content;
  },
  switchTo: function(index, first_time) {
    if(!first_time) this.currentColumn.value = this.editor.getContent();

    buttons = $$('div.whitebox > input');
    buttons.invoke('removeClassName', 'selected'); // Remove Selected class from all buttons.
    buttons[index].addClassName('selected');

    this.currentColumn = this.columns[index];
    this.editor.setContent(this.currentColumn.value); //, {format: 'raw', no_events: true});
  }
};

