/**************************************

  deskDo -- todolist on your desktop

  release I, version 002

  (c) 2005 Pixel-Apes.

 **************************************/


function deskDo( idContainer, idEditAll, idEditButton, idCancelButton, text ) 
{ 
  // 
  this.container = document.getElementById( idContainer );
  this.container._desk = this;
  this.id = idContainer;
  this.idEditAll = idEditAll;
  this.containerEdit = document.getElementById( idEditAll );
  //
  this.idEditButton = idEditButton;
  this.containerButton = document.getElementById( idEditButton );
  document.getElementById( idEditButton )._deskDo = this;
  //
  this.idCancelButton = idCancelButton;
  this.containerCancel = document.getElementById( idCancelButton );
  document.getElementById( idCancelButton )._deskDo = this;

  // alter waka31 formatter
  this.wf = new wakaFormatter();
  this.wf.presets["sections"].list  = [ wakaDoSections ];
  this.wf.presets["sections"].empty = wakaDoSectionsContent;
  this.wf.presets["sections"].next  = [ "todo" ];
  this.wf.presets["todo"].list  = [ wakaDoTodo ];
  this.wf.presets["todo"].empty = wakaDoTodoContent;
  this.wf.desk = this;

  if (text == this.undef()) text = "";

  // create all editables
  this._editables = [];
  this.buildFromScratch(text);

}

deskDo.prototype = 
{
    startup : function()
    {
      this.load();
      // init and draw `em
      this.doInitView();
      this.doViewAll();
    },

    currentEditing : false, // which editable is currently in edit mode. false if none

    // what kind editable should be built when building from scratch
    startingEditable : deskDoEditable_sections, 

    buildFromScratch : function( text, non_empty )
    {
      if (non_empty && (text == "")) return;

      // reset numbers of headers & number of "add todo item"
      wakaDoSections.prototype.static_nextIdReset();
      deskDoEditable_todoAdd.prototype.static_nextNoReset();
      
      this._editables = [];
      this.container.innerHTML = "";

      var editable = new this.startingEditable( this, this.nextId() );
      editable.setData( text );
      this.bind( editable );
    },

    // binds an editable (and its siblings) into tree
    bind : function( editable )
    {
      // todo: get hook for replacing here
      var aHook       = this.container;
      var aHookAppend = true;

      for( var i in editable.siblings )
      {
        var e = editable.siblings[i];
        
        var wrapper = e.drawWrapper();
        if (aHookAppend) aHook.appendChild( wrapper );
        // todo: implement replacing of hook and "insertAfter"

        this._editables[ e.id ] = e;
      }
    },


    setReadonly : function( value )
    {
      for( var i in this._editables )
        this._editables[i].setReadonly( value );
    },

    // controller functons go here
    doInitView : function()
    {
      for(var i in this._editables)
        this._editables[i].doView();
    },


    prepareDataForEdit : function()
    {
      var data = "";
      for( var i in this._editables )
        data+= this._editables[i].getDataForEdit();

      data = data.replace( /\s+$/, "" );
      return data;
    },

    editAllMode : false,
    _editAllEditable : false,
    doEditAll : function()
    {
      this.editAllMode = true;

      // 1. get all data
      var data = this.prepareDataForEdit();

      // 3. show/hide
      var targetHeight = this.container.offsetHeight; 
      this.containerEdit.style.display   = "block";
      this.containerButton.value         = "Save all";
      this.containerCancel.style.display = "inline";
      this.container.style.display       = "none";

      // 2. build ONE editable, but not bind it
      var e = new deskDoEditable( this, this.nextId() );
      e.setData( data );
      var wrapper = e.drawWrapper();
      this.containerEdit.replaceChild( wrapper, this.containerEdit.childNodes[0] );
      var _h = e.textareaHeight;
      e.textareaHeight = targetHeight;
      e.doEdit();
      e.textareaHeight = _h;
      this._editAllEditable = e;

      // set current editable
      this.currentEditable = e;
    
    },

    doSaveAll : function()
    {
      if (this._editAllEditable)
        this.buildFromScratch( this._editAllEditable.importDataFromEditMode() );
      this.save();
      this.editAllMode = false;
      this.doInitView();
      this.doViewAll();

      // reset current editable
      this.currentEditable = false;
    },

    doCancelAll : function()
    {
      this.doViewAll();
      // reset current editable
      this.currentEditable = false;
    },
    doViewAll : function()
    {
      this.editAllMode = false;
      this.container.style.display       = "block";
      this.containerEdit.style.display   = "none";
      this.containerButton.value         = "Edit whole";
      this.containerCancel.style.display = "none";
    },

    doRemoveAllCompleted : function()
    {
      for (var i in this._editables)
        this._editables[i].removeIfCompleted();
      this.save();
    },
    doUndo : function()
    {
      if (this.storage.version == 1) alert( "Unable to undo, this is the very first version of the desk!" );
      else this.storage.loadUndo();
    },
    doRedo : function()
    {
      if (this.storage.version < this.storage.maxVersion)
        this.storage.loadUndo( this.storage.version+1 );
      else
        alert( "You are looking on freshest version of the desk!" );
    },

    onRemoveAllCompleted : function()
    {
      if (confirm("Remove all tasks marked as completed?")) 
        this.doRemoveAllCompleted();
    },
    onUndo : function()
    {
      this.doUndo();
    },
    onRedo : function()
    {
      this.doRedo();
    },

    onSurfaceClick : function()
    {
      if (this._skipNextOnClick) return this._skipNextOnClick = false;

      if (this.selectedEditable)
        if (!this.skipClickSequence)
          this.selectedEditable.doDeselect();
      this.skipClickSequence = false;

      if (this.currentEditable)
        if (this.currentEditable == this._editAllEditable) 
          this.doSaveAll();
        else
          this.currentEditable.doSave();
    },

    _skipNextOnClick : false,
    skipNextOnClick : function()
    {
      this._skipNextOnClick = true;
      this.skipClickSequence = false;
    },


    // other stuff
    _nextIdCount : 0,
    nextId : function()
    {
      return this.id + "_" + (this._nextIdCount++);
    },


    // STORAGE, temporary
    save : function()
    {
      var s = [];
      for( var i in this._editables )
        if (!this._editables[i].dummy)
          s[s.length] = this._editables[i].serializeData();
      var glued = s.join( "\n" );
      this.storage.store( glued );
    },

    load : function()
    {
      this.storage.restore();
    },


    // simple function to end the class definition.
    undef : function( param ) { return param; }


}




