/* This notice must be untouched at all times.

Open-jACOB Draw2D
The latest version is available at
http://www.openjacob.org

Copyright (c) 2006 Andreas Herz. All rights reserved.
Created 5. 11. 2006 by Andreas Herz (Web: http://www.freegroup.de )

LICENSE: LGPL

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License (LGPL) as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA,
or see http://www.gnu.org/copyleft/lesser.html
*/

/**
 * A factory for creating new EditParts. EditPartViewers can be configured with an EditPartFactory.
 * Whenever an EditPart in that viewer needs to create another EditPart, it can use the Viewer's factory.
 * The factory is also used by the viewer whenever EditPartViewer.setContents(Object)  is called.
 * 
 * @version @VERSION@
 * @author Andreas Herz
 * @constructor
 */
var map = new Object();

var createCanvas = function(/*:HTMLElement*/ parent) 
{
  var canvas = document.createElement("canvas");
  parent.appendChild(canvas);
  if ("G_vmlCanvasManager" in window) 
  {
    canvas = G_vmlCanvasManager.initElement(canvas);
  }
  return canvas;
}

function disableSelection(element) {
    element.onselectstart = function() {
        return false;
    };
    element.unselectable = "on";
    element.style.MozUserSelect = "none";
    element.style.cursor = "default";
}

map.Mindmap=function(/*:String*/ id, /*:int*/width, /*:int*/ height)
{
   this.width  = width;
   this.height = height;
   this.leftChildren = new map.ArrayList();
   this.rightChildren = new map.ArrayList();
   this.html = null;

   $(id).appendChild(this.getHTMLElement());

   var oThis = this;
   Event.observe(this.html.parentNode, 'mousedown', function(event)
   {
     oThis.clientX = event.clientX;
     oThis.clientY = event.clientY;
     oThis.mousedown=true;
   });
   Event.observe(this.html.parentNode, 'mouseup', function(event)
   {
     oThis.mousedown=false;
   });
   Event.observe(this.html.parentNode, 'mousemove', function(event)
   {
     if(oThis.mousedown==true)
     {
        var diffX = event.clientX - oThis.clientX;
        var diffY = event.clientY - oThis.clientY;
        oThis.scrollTo(this.scrollLeft=this.scrollLeft-diffX,this.scrollTop-diffY);
        oThis.clientX = event.clientX;
        oThis.clientY = event.clientY;
     }     
     //alert("move");
   });
   disableSelection(this.html);
}

/**
 * @type HTMLElement
 **/
map.Mindmap.prototype.addNode=function(/*:map.Node*/ node)
{
   if(node instanceof map.LeftNode)
   {
      this.leftChildrenHTML.appendChild(node.getHTMLElement());
      this.leftChildren.add(node);
      node.parent=this;
      node.drawLines();
   }
   else
   {
   }
}

map.Mindmap.prototype.getHeight=function()
{
  return this.height;
}

map.Mindmap.prototype.getWidth=function()
{
  return this.width;
}

map.Mindmap.prototype.scrollTo=function(/*:int*/ x, /*:int*/y)
{
  this.html.parentNode.scrollLeft=x;
  this.html.parentNode.scrollTop=y;
}

/**
 * @type HTMLElement
 **/
map.Mindmap.prototype.getHTMLElement=function()
{
   if(this.html==null)
   {
      this.html = new Element("table");
      this.html.className ="root";
      this.html.style.width  =this.width+"px";
      this.html.style.height =this.height+"px";

      var row=this.html.insertRow(0);

      this.leftChildrenHTML=row.insertCell(0);
      this.leftChildrenHTML.className = "left_canvas";
      this.leftChildrenHTML.width= ""+parseInt(this.width/2);

      this.leftLines=row.insertCell(1);
      this.leftLines.style.width= "50px";
      this.leftLines.style.height =this.height+"px";
          this.leftCanvas = createCanvas(this.leftLines);
          this.leftCanvas.style.width = "50px";  //IE
          this.leftCanvas.style.height = this.height+"px";
          this.leftCanvas.setAttribute('width', "50" ); //FF
          this.leftCanvas.setAttribute('height', this.height);//FF

      this.centerCanvas=row.insertCell(2);
      this.centerCanvas.className = "center_canvas";
      this.centerCanvas.width= "80";
      this.centerCanvas.innerHTML= "CENTER";

      this.rightLines=row.insertCell(3);
      this.rightLines.style.width= "50px";
      this.rightLines.style.height =this.height+"px";
          this.rightCanvas = createCanvas(this.rightLines);
          this.rightCanvas.style.width = "50px";  //IE
          this.rightCanvas.style.height = this.height+"px";
          this.rightCanvas.setAttribute('width', "50" ); //FF
          this.rightCanvas.setAttribute('height', this.height);//FF

      this.rightChildrenHTML=row.insertCell(4);
      this.rightChildrenHTML.className = "right_canvas";
      this.rightChildrenHTML.width= ""+parseInt(this.width/2);
   }
   return this.html;
}


map.Mindmap.prototype.drawLines=function()
{
     var ctx = this.leftCanvas.getContext('2d');
     // save and clear
//     ctx.save();
     var thisAnchor = Element.cumulativeOffset($(this.leftCanvas));
     ctx.clearRect(0, 0, 50, this.height);

     ctx.strokeStyle = '#999999';
     ctx.lineWidth = 0.3;
     for(var i=0;i<this.leftChildren.getSize();i++)
     {
       var child = this.leftChildren.get(i);
       var anchor = child.getAbsoluteAnchor();
       var top = (anchor.top-thisAnchor.top)+ child.getAnchorHeight()/2;
  //     var top = anchor.top+ child.getAnchorHeight()/2;

       ctx.moveTo(0,top);
       ctx.bezierCurveTo(20,top, 15, this.height/2,30,this.height/2);
//       ctx.lineTo(50,this.height/2);
       ctx.stroke();
     }
}

