/**********************************
  File: TextObject.js
  Created by Hawkeye
  Created: 9.5.2002

  I make a text object thing.

**********************************/

TextObject.prototype = new Shape();
TextObject.prototype.constructor = TextObject;
TextObject.superclass = Shape.prototype;
function TextObject( x, y, textData, size, color, draw ){
  if (arguments.length > 0) {
    this.init(x,y,textData,size,color,draw);
  }
}
TextObject.prototype.init = function ( x, y, textData, size, color, draw ) {
  //  debug("Create Text Object");
  TextObject.superclass.init.call( this );
  this.handles[0] = new Handle( x, y, this );
  this.rotation = 0;
  this.vertical = false;
  this.x = x;
  this.y = y;
  this.textData = textData;
  this.size = size;
  this.color = color;
  if (draw) {
    this.make( );
  }
}
TextObject.prototype.make = function(){

  this.data = svgDoc.createTextNode( this.textData );
  this.svgNode = svgDoc.createElement("text");
  this.span = svgDoc.createElement("tspan");
  this.svgNode.appendChild(this.span);
  this.span.appendChild(this.data);
  this.span.setAttribute( "dy", this.size );
  this.svgNode.setAttribute( "x", this.x );
  this.svgNode.setAttribute( "y", this.y );
  this.svgNode.setAttribute( "font-size", this.size );
  this.svgNode.setAttribute( "style", "fill:"+this.color+";stroke:"+this.color+";font-family:'MS-Gothic';");


  this.svgNode.setAttribute( "text-anchor", "start" );
  this.svgNode.addEventListener("click", this, false);
  this.svgNode.setAttribute("id", "text");
  drawingGroup.appendChild( this.svgNode );

}
TextObject.prototype.update = function(){
  this.x = this.handles[0].point.x;
  this.y = this.handles[0].point.y;
  this.svgNode.setAttribute("x", this.x);
  this.svgNode.setAttribute("y", this.y);
}
TextObject.prototype.updateData = function(){
  this.x = this.handles[0].point.x;
  this.y = this.handles[0].point.y;
  this.svgNode.setAttribute("x", this.x);
  this.svgNode.setAttribute("y", this.y);

  if ( !this.selected ) {
    this.setShapeStyle("stroke", this.color);
  } else {
    this.returnStroke = this.color;
  }

  this.setShapeStyle("fill", this.color);
  this.setShapeStyle("font-size", this.size);
  this.svgNode.firstChild.firstChild.data = this.textData;
}

TextObject.prototype.rotatePlease = function(from,angle){
  this.centre = new Point2D();
  this.centre.setFromPoint( this.handles[0].point );  
  this.handles[0].rotatePlease( from,angle,this.centre );

  this.rotation += angle;
  if ( Math.abs(this.rotation) >= 90 ){
    this.rotation = 0;
  }
  //  debug("rot:"+this.rotation);
  if ( this.rotation > 45 || this.rotation < -45 ) {
    if (!this.vertical){
      this.vertical = true;
      this.setShapeStyle("glyph-orientation-vertical", "0");
      this.setShapeStyle("writing-mode", "tb");
    }
  } else { 
    if (this.vertical){
      this.vertical = false;
      this.setShapeStyle("glyph-orientation-vertical", "1");
      this.setShapeStyle("writing-mode", "lr");
    }
  }
  this.update();
}
TextObject.prototype.click = function( evt ){
  // edit text by clicking a selected textthing.
  if (this.selected){
    textTool.editText( this );
    return 0;
  }
  if(this.temp_flag != "on"){
    evt = (evt) ? evt : ((window.event) ? window.event : "");
    selectedNode = (evt.target) ? evt.target : evt.srcElement;
    grabObject( evt, this );	
  }
}
TextObject.prototype.destructor = function() { 
  this.svgNode.parentNode.removeChild( this.svgNode );
  delete this.data;
  delete this.span;
  delete this.svgNode;
  delete this.vertical;
  delete this.centre;
  delete this.handles;
  delete this.rotation;
  delete this.size;
  delete this.color;
  delete this.textData;  
  delete this.x;
  delete this.y;
}
