/* 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
*/
WorkflowFigure=function(/*:String*/ url, /*:String*/ text, /*:String*/ title)
{
  this.url = url;
  this.text = text;
  this.title = title;
  Node.call(this);
  this.outputPort1 = null;
  this.outputPort2 = null;
  this.setDimension(100,100);
  this.setColor(null);
  this.setCanDrag(false);
  
  this.highlight = false;
}

WorkflowFigure.prototype = new Node;
WorkflowFigure.prototype.type="WorkflowFigure";

/**
 * Initial call of the framework. This is the right placce to create your HTML elements
 *
 * @private
 **/
WorkflowFigure.prototype.createHTMLElement=function()
{
    var item = Node.prototype.createHTMLElement.call(this);
    if(navigator.appName.toUpperCase() == 'MICROSOFT INTERNET EXPLORER')
    {
    	this.table = document.createElement("table");
//       this.table.border = "1";
//       this.table.style.position = "absolute";
       this.table.cellPadding = "0";
       this.table.cellSpacing = "0";
       this.table.title = this.title;
//       this.table.style.border= "1px solid black";
       
       if(this.url != null){
       	   this.img = document.createElement("img");
//    	   this.img.style.position = "absolute";
	       this.img.src = this.url;
       	   var cell1 = this.table.insertRow(-1).insertCell(-1);
	       cell1.align = "center";
    	   cell1.appendChild(this.img);
       }
       
       if(this.text != null){
  	   		var cell2 = this.table.insertRow(-1).insertCell(-1);
       		cell2.align = "center";
//       		cell2.width = 50;
       		cell2.style.fontSize = "9pt";
//       		cell2.innerHTML = this.text;
       		
       		this.div = document.createElement("div");
//       		this.div.style.fontSize = "9pt";
       		this.div.style.overflow  = "hidden";
//       		this.div.style.width = "20px";
       		this.div.style.height = "100%";
       		this.div.innerHTML = this.text;
       		cell2.appendChild(this.div);
       }
       item.appendChild(this.table);
       // we must creeate an addition div. 
       // REASON: AlphaImageLoader clip all children. In this case no ports are visible.
       //         The ports of a node are DOM children of "this.html". Additional effort. :-(
       //
       this.d = document.createElement("div");
       this.d.style.position = "absolute";
       this.d.style.left     = "0px";
       this.d.style.top      = "0px";
       this.d.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader (src='"+ this.url+"', sizingMethod='scale')";
//       item.appendChild(this.d);
    }
    else
    {
       this.table = document.createElement("table");
//       this.table.border = "1";
//       this.table.style.position = "absolute";
       this.table.cellPadding = "0";
       this.table.cellSpacing = "0";
       this.table.title = this.title;
//       this.table.style.border= "1px solid black";
       
       if(this.url != null){
       	   this.img = document.createElement("img");
//    	   this.img.style.position = "absolute";
	       this.img.src = this.url;
       	   var cell1 = this.table.insertRow(-1).insertCell(-1);
	       cell1.align = "center";
    	   cell1.appendChild(this.img);
       }
       
       if(this.text != null){
  	   		var cell2 = this.table.insertRow(-1).insertCell(-1);
       		cell2.align = "center";
//       		cell2.width = 50;
       		cell2.style.fontSize = "9pt";
//       		cell2.innerHTML = this.text;
       		
       		this.div = document.createElement("div");
//       		this.div.style.fontSize = "9pt";
       		this.div.style.overflow  = "hidden";
//       		this.div.style.width = "20px";
       		this.div.style.height = "100%";
       		this.div.innerHTML = this.text;
       		cell2.appendChild(this.div);
       }

	   item.appendChild(this.table);
//       item.appendChild(this.img);

       // Add an div above the img. Required for a propper drag&drop handling.
       // If you remove this div, the image will crap the event and the internal drag&drop handling
       // is corrupt......bug?
       //
       this.d = document.createElement("div");
       this.d.style.position = "absolute";
       this.d.style.left     = "0px";
       this.d.style.top      = "0px";
//       item.appendChild(this.d);
    }

    item.style.left     = this.x+"px";
    item.style.top      = this.y+"px";

    return item;
}

/**
 * Adjust the additional layer if the user resize the figure
 *
 * @param {int} w The new width of the fiure.
 * @param {int} h The new height of the figure
 **/
WorkflowFigure.prototype.setDimension=function(/*:int*/ w, /*:int*/ h )
{
  Node.prototype.setDimension.call(this,w, h);

  // Adjust the different layer/div/img object of the figure
  //
  if(this.d!=null)
  {
    this.d.style.width  = this.width+"px";
    this.d.style.height = this.height+"px";
  }

  if(this.table!=null)
  {
    this.table.style.width= this.width;
    this.table.style.height=this.height;
    
    this.div.style.width= this.width;
  }

  // Adjust the Output ports to the new dimension
  //
  if(this.outputPort1!=null)
  {
    this.outputPort1.setPosition(this.width+3, this.height/3);
    this.outputPort2.setPosition(this.width+3, this.height/3*2);
  }
}

/**
 * Create the output ports of the figure. This will done if the figure has been assigned to
 * a workflow Canvas.
 *
 * @param {Workflow} workflow The new parent workflow of this figure.
 **/
WorkflowFigure.prototype.setWorkflow=function(/*:Workflow*/ workflow)
{
  Node.prototype.setWorkflow.call(this,workflow);

  if(workflow!=null)
  {
  	//east ports
    this.outputPort1 = new OutputPort();
    this.outputPort1.setMaxFanOut(4); // It is only possible to add "1" Connector to this port
    this.outputPort1.setWorkflow(workflow);
    this.outputPort1.setName("outputPort_east");
    this.outputPort1.setBackgroundColor(new  Color(245,115,115));
    this.addPort(this.outputPort1,this.width+3,this.height/3);
    
    this.inputPort1 = new InputPort();
    this.inputPort1.setWorkflow(workflow);
    this.inputPort1.setName("inputPort1_east");
    this.inputPort1.setBackgroundColor(new  Color(115,115,245));
    this.addPort(this.inputPort1,this.width+3,this.height/3*2);

	//south ports
    this.outputPort2 = new OutputPort();
    this.outputPort2.setMaxFanOut(4); // It is only possible to add "1" Connector to this port
    this.outputPort2.setWorkflow(workflow);
    this.outputPort2.setName("outputPort_south");
    this.outputPort2.setBackgroundColor(new  Color(245,115,115));
    this.addPort(this.outputPort2,this.width/3*2,this.height+3);
    
    this.inputPort2 = new InputPort();
    this.inputPort2.setWorkflow(workflow);
    this.inputPort2.setName("inputPort_south");
    this.inputPort2.setBackgroundColor(new  Color(115,115,245));
    this.addPort(this.inputPort2,this.width/3,this.height+3);
    
    //west ports
    this.outputPort3 = new OutputPort();
    this.outputPort3.setMaxFanOut(4); // It is only possible to add "1" Connector to this port
    this.outputPort3.setWorkflow(workflow);
    this.outputPort3.setName("outputPort_west");
    this.outputPort3.setBackgroundColor(new  Color(245,115,115));
    this.addPort(this.outputPort3,-3,this.height/3*2);
    
    this.inputPort3 = new InputPort();
    this.inputPort3.setWorkflow(workflow);
    this.inputPort3.setName("inputPort_west");
    this.inputPort3.setBackgroundColor(new  Color(115,115,245));
    this.addPort(this.inputPort3,-3,this.height/3);
    
    //north ports
    this.outputPort4 = new OutputPort();
    this.outputPort4.setMaxFanOut(4); // It is only possible to add "1" Connector to this port
    this.outputPort4.setWorkflow(workflow);
    this.outputPort4.setName("outputPort_north");
    this.outputPort4.setBackgroundColor(new  Color(245,115,115));
    this.addPort(this.outputPort4,this.width/3,-3);
    
    this.inputPort4 = new InputPort();
    this.inputPort4.setWorkflow(workflow);
    this.inputPort4.setName("inputPort_north");
    this.inputPort4.setBackgroundColor(new  Color(115,115,245));
    this.addPort(this.inputPort4,this.width/3*2,-3);
  }
}

/**
 * An WorkflowFigure can't be resized. So - this function returns always false, 
 *
 * @return Returns always false.
 * @type boolean
 **/
WorkflowFigure.prototype.isResizeable=function()
{
  return false;
}

WorkflowFigure.prototype.toggle=function()
{
  if(this.highlight)
    this.setBackgroundColor(new Color('#FFFF00'));
  else
    this.setBackgroundColor(new Color('#CFBDE7'));
  this.highlight = !this.highlight;
}

WorkflowFigure.prototype.setTime=function(flag, delay)
{
	if(flag){
		var oThis = this;
		var func = function(){oThis.toggle();};
	    this.timer = window.setInterval(func,delay);
	}
}

WorkflowFigure.prototype.dispose=function()
{
  Node.prototype.dispose.call(this);
  if(this.timer)
	  window.clearInterval(this.timer);
}

