/**************************************

  deskDo -- todolist on your desktop

  (c) 2005 Pixel-Apes.

 **************************************/


function deskDoEditable_todo( desk, id ) 
{ 
  this.desk = desk; 
  this.id = id;

  this.preset = "default";
}

deskDoEditable_todo.prototype = new deskDoEditable_simple();
deskDoEditable_todo.prototype.constructor = 
deskDoEditable_todo;

deskDoEditable_todo.prototype._setReadonly = function (value)
{
  var checkbox = document.getElementById( this.id + "_checkbox" );

  if (value) checkbox.disabled = true;
  else       checkbox.disabled = false;
  
}

deskDoEditable_todo.prototype.setData = function (data)
{
  this._data = data;
  this._compiled = false;
  this._wakaTree = [];
  this._wakaData = data;
  this.siblings = [ this ]; // no siblings for now
}

deskDoEditable_todo.prototype.removeIfCompleted = function ()
{
  if (this._data.completed) this.remove();
  this._removeIfCompletedSubtree( this._wakaTree );
}

deskDoEditable_todo.prototype.buildWrapper = function ( wrapperContents )
{
  var html = "<div onclick='document.getElementById(\""+this.id+"_wrapper\")._editable.onWrapperClick(event); return true' "+
                 " id='"+this.id + "_selector' class='"+this._data._css.main+" selector-' >"+
               '<table class="w100 ' +
                    (this._data.completed?this._data._css.completed:this._data._css.open)+ '" '+
                    ' id="' +this.id+ '_table" '+
                    ' cellspacing="0" cellpadding="0" border="0"><tr>'+
               "<td class='checkbox-'><input type='checkbox' " +
                      ' id="' +this.id+ '_checkbox" '+
                      ' onclick="document.getElementById(\''+this.id+'_wrapper\')._editable.onCheckbox(); return true" '+
                      (this._data.completed?" checked='checked' ":"")+ "/></td>"+
               "<td>"+
                    "<div class='contents-' id='"+this.id + "_wrapper'>"+
                    wrapperContents+
                    "</div>"+
               "</td>"+
               '</tr></table>'+
               "</div>";
  return html;
}

deskDoEditable_todo.prototype._buildHtml = function ()
{
  // first div is intentionally left opened. Closing pair could be found in TodoContent waka token
  this._html = '<div onclick="document.getElementById(\''+this.id+'_wrapper\')._editable.onContentClick();return true;" '+
               ' ondblclick="this.onclick()" >'+ 
               "<div id='"+this.id + "_title' class='"+this._data._css.title+"'>"+this._data._inner+"</div>"+
               this._data._glued;
  this._compiled = true;
}


deskDoEditable_todo.prototype._resizeArea = function ()
{
  var addendum = 20;

  // superslim
  var lines = this.t.value.split("\n");
  if (lines.length < 2)
    addendum = 0;

  // normal resize
  var height;
  var text_height = this.inplace.getHeight()  + addendum;
  if (((addendum == 0) && (text_height > 0)) || (text_height > this.textareaHeight))
    height = text_height;
  else                                   
    height = this.textareaHeight;

  if (height < this.title_height) height = this.title_height;

  this.t.style.height = height + "px";
}

deskDoEditable_todo.prototype.rebindWrapper = function()
{
  this.title = document.getElementById(this.id + "_title");
  this.title_height = this.title.offsetHeight;
  if (this._data._tree.length < 2)
    this._textareaHeight = this.title_height;

  // original copypaste
  this.wrapper = document.getElementById(this.id + "_wrapper");
  this.wrapper._editable = this;
  return this.wrapper;
}


deskDoEditable_todo.prototype.buildViewMode = function()
{
  var html = this.getHtml();
  html = 
         "<a href='javascript:;' class='remove-' onclick='return document.getElementById(\""+this.id+"_wrapper\")._editable.onRemove();' title='Remove this task'>remove</a>"+
         html;

  return html;
}

// rip spaces for inplace editing of this item and its subtree
deskDoEditable_todo.prototype.getDataForEdit = function()
{
  if (this.dummy) return "";

  var result = this._data.inner;
  if (this._data._tree) result += this._getDataForEditFromTree( this._data._tree, 0 );
  return result;
}
deskDoEditable_todo.prototype._getDataForEditFromTree = function( tree, depth )
{
  // magic for indenting
  var spaces  = ""; // indenting for todo
  var _spaces = ""; // indenting for notes
  for( var i=0; i<depth; i++) spaces+="  ";
  _spaces = spaces;
  if (depth > 0) _spaces += "    ";
  spaces+="  ";

  var result = "";
  for (var i=0; i<tree.length; i++)
  if (!tree[i]._editable || !tree[i]._editable.dummy)
  {
    var node = tree[i];
    if (node.isTodo) 
      result += node.toText().replace(/(^|\n)\s*/g,  "\n" + spaces);
    else
      result += node.toText().replace(/(^|\n)\s*/g, "\n" + _spaces);

    result = result.replace( /\s+$/, "");

    if (node._tree.length > 0) result += this._getDataForEditFromTree( node._tree, depth+1 );
  }
  return result;
}


// get data from inplace and rebuild all parents. HUH.
// returns data struct, which would be used in setData
// must resemble wakaDoTodo._compile behavior on building this struct.
deskDoEditable_todo.prototype.importDataFromEditMode = function()
{
  if (!this.wrapper) return;

  // 1. prepare for parsing content
  var text = document.getElementById( this.id+"_ta" ).value;
  text = text.replace( /^\s+/g, "" ); 

  // prepare to format
  text = text.replace( /\n([ \r\t]*\n)+/g, "\n" ); // remove all empty lines
  text = text.replace( /\n/g, "\n  " ); // shift everything to the right
  text = "  [" +(this._data.completed?"+":" ")+ "] " + text; // add current state

  // 2. parse tree and get head
  var tree = this.desk.wf.buildTree( text, "todo" );

  for(var i=0; i<tree[0]._tree.length; i++)
    if (!tree[0]._tree[i].isTodo)
      tree[0]._tree[i]._data = tree[0]._tree[i]._data.replace(/(^|\n)/g, "$1    ");

  // bind tree[1+] to tree[0]
  if (tree.length > 0)
  {
    for(var i=1; i<tree.length; i++)
      tree[0].glueChild( tree[i] );
  }
   
  tree = this._shiftWakaTree( tree, this._data.level-1 );
  var head = tree[0];
  var data = head._compileEditableStruct();

  this._token._data = data;
  this._token._tree = head._tree;
  for (var i in this._token._tree)
    this._token._tree[i]._parent = this._token;

  return data;
}

deskDoEditable_todo.prototype._shiftWakaTree = function( tree, depth )
{
  var _spaces = "";
  for( var j=0; j< depth; j++) _spaces+="  ";

  for (var i=0; i< tree.length; i++)
  {
    if (tree[i].isTodo)
      tree[i]._data.level += depth;
    else
      tree[i]._data = _spaces + tree[i]._data;

    if (tree[i]._tree.length)
      tree[i]._tree = this._shiftWakaTree( tree[i]._tree, depth );
  }
  return tree;
}

deskDoEditable_todo.prototype.doAutocomplete = function ()
{
  var state=true;
  for (var i=0; i<this._token._tree.length; i++)
    if (this._token._tree[i].isTodo)
      state = state && this._token._tree[i]._editable._data.completed;

  var _prev = this._data.completed;
  if (state && !this._data.completed)
  {
    this._data.completed=true;
    this._changeCheckboxState();

    if (this._token._parent && this._token._parent.isTodo && this._token._parent._editable)
      this._token._parent._editable.doAutocomplete();
  }
}

deskDoEditable_todo.prototype._changeCheckboxState = function ()
{
  var checkbox = document.getElementById( this.id + "_checkbox" );
  var table    = document.getElementById( this.id + "_table" );
  if (!checkbox) alert( "no checkbox!" );
  checkbox.checked = this._data.completed;
  table.className = "w100 "+ 
                    (this._data.completed?this._data._css.completed:this._data._css.open);
}


deskDoEditable_todo.prototype.onCheckbox = function ()
{
  if (this._readOnly) return;

  var checkbox = document.getElementById( this.id + "_checkbox" );

  // change model
  this._data.completed = checkbox.checked;

  // check parent
  if (this._token._parent && this._token._parent.isTodo && this._token._parent._editable)
    this._token._parent._editable.doAutocomplete();

  this.desk.save();

  // change view
  this._changeCheckboxState();
}

deskDoEditable_todo.prototype._needRebuildHierarchy = function()
{ 
  if (this._data.inner === "")
  {               
    this.dummy = true;
    return true;
  }
  return false; 
}


