/*
* DragCanvas
*/
draw2d.DragCanvas=function(divId, backgroundUrl) {
	draw2d.Workflow.call(this, divId);
	var listener = new draw2d.SelectionListener(this);
	this.addSelectionListener(listener);
	//this.html.style.backgroundImage="";
        this.html.style.backgroundImage="url(" + backgroundUrl + ")";

};

draw2d.DragCanvas.prototype = new draw2d.Workflow();
draw2d.DragCanvas.prototype.type="DragCanvas";
draw2d.DragCanvas.prototype.getContextMenu=function() {

	var menu=new draw2d.Menu();
	var oThis=this;

	menu.appendMenuItem(new draw2d.MenuItem("Add Node",null,function(x,y){

		var text = prompt("Enter label", "");
		if (text == null || text == "")
			return;
			
		var rf = new draw2d.ResourceFigure("images/rtSwitch.gif", 32, 32, text, null);
		oThis.addFigure(rf, x, y);
		
		draw2d.eventOnNewNode();
	}));
	
	menu.appendMenuItem(new draw2d.MenuItem("Add Container",null,function(x,y){

		var rectangle = new draw2d.Rectangle(100, 100);
		rectangle.setZOrder(-999);
		oThis.addFigure(rectangle, x, y);
		rectangle.onRemove=function(wFlow){

			oThis.onRemoveSquare("20");
		};
		
		draw2d.eventOnNewSquare();
	}));

	menu.appendMenuItem(new draw2d.MenuItem("Add Circle",null,function(x,y){
	
		var circle = new draw2d.Circle(75);
		circle.setZOrder(-999);
		oThis.addFigure(circle, x, y);
		circle.onRemove=function(wFlow){

			draw2d.eventOnRemoveCircle();
		};
		
		draw2d.eventOnNewCircle();
	}));

	menu.appendMenuItem(new draw2d.MenuItem("Add Label",null,function(x,y){

  		var text = prompt("Enter text", "");
  		if (text == null || text == "")
  			return;
  		
		var annotation = new draw2d.Annotation(text);
		annotation.html.style.fontFamily = "Arial";
		annotation.setDimension(50, 20);
		annotation.onRemove=function(wFlow) {
		
			draw2d.eventOnRemoveText(this.getText());
		};
		
		annotation.onDoubleClick=function() {
		
			var newText = prompt("Enter text", annotation.getText());
			annotation.setText(newText);
			draw2d.eventOnChangeText(this.getText());
		};
		
		annotation.setBackgroundColor(new draw2d.Color(255, 255, 255));
		oThis.addFigure(annotation, x, y);
		
		draw2d.eventOnNewText(text);
	}));
	
	return menu;
}

draw2d.DragCanvas.prototype.onRemoveSquare=function(id) {
	alert("removed indeed: " + id);
}

draw2d.DragCanvas.prototype.onResourceSelected=function(noid, resourceOid) {
	//alert("resource selected noid: " + noid + ", oid: " + resourceOid);
}

/**
* Link Connection
*/
draw2d.LinkConnection=function(noid, routerType, lineWidth){

	draw2d.Connection.call(this);
	this.setProperty("noid", noid);
	this.sourcePort=null;
	this.targetPort=null;
	this.lineSegments=[];
	this.setColor(new draw2d.Color(0,0,115));
	this.setLineWidth(lineWidth);
	this.routerType = routerType;
	this.setZOrder(1); // lines go in the back
	if (routerType == "smart") {
		this.setRouter(new draw2d.ManhattanConnectionRouter());
	}
	else if (routerType == "curved") {
		this.setRouter(new draw2d.BezierConnectionRouter());
	}
	else if (routerType == "straight") {
		this.setRouter(new draw2d.FanConnectionRouter());
	}
	
	this.myLabel = null;
	this.canDrag = false;
};

draw2d.LinkConnection.prototype=new draw2d.Connection();
draw2d.LinkConnection.prototype.onRemove=function(wFlow) {

	// override callback
	draw2d.eventOnRemoveLink(this.getProperty("noid"));
};
draw2d.LinkConnection.prototype.onDoubleClick = function() {

	var thisNoid = this.getProperty("noid");
	var sourcePort = this.getSource();
	var sourceResourceFigure = sourcePort.parentNode;
	sourceResourceFigure.onConnectionDoubleClick(thisNoid);
};
draw2d.LinkConnection.prototype.updateRouterType = function (routerType) {

	this.routerType = routerType;
	if (routerType == "smart") {
		this.setRouter(new draw2d.ManhattanConnectionRouter());
	}
	else if (routerType == "curved") {
		this.setRouter(new draw2d.BezierConnectionRouter());
	}
	else if (routerType == "straight") {
		this.setRouter(new draw2d.FanConnectionRouter());
	}	
};
draw2d.LinkConnection.prototype.updateLabel = function (newLabel) {

	
	if (this.myLabel == null) {
	
		this.myLabel = new draw2d.Label(newLabel);
	
		this.myLabel.html.style.fontFamily="Arial";
		this.myLabel.setFontSize(8);
		this.myLabel.setBackgroundColor(new draw2d.Color(255, 255, 255));
		//this.myLabel.setBorder(new draw2d.LineBorder(1));
		this.addFigure(this.myLabel,new draw2d.ManhattanMidpointLocator(this));	
	}
	else {
		this.myLabel.setText(newLabel);
	}
};


/**
* Ports
*/
draw2d.MyOutputPort=function(/*:draw2d.Figure*/ uiRepresentation)
{
  draw2d.OutputPort.call(this, uiRepresentation);
  //draw2d.OutputPort.call(this,new draw2d.Rectangle());
  this.setCoronaWidth(3);
};

draw2d.MyOutputPort.prototype = new draw2d.OutputPort();
/** @private **/
draw2d.MyOutputPort.prototype.type="MyOutputPort";
draw2d.MyOutputPort.prototype.setLocation=function(locationStr) {
	this.location = locationStr;
};
draw2d.MyOutputPort.prototype.getLocation=function() {
	return this.location;
};
draw2d.MyOutputPort.prototype.onDragEnter=function(port){
/*
var _574f=new draw2d.EditPolicy(draw2d.EditPolicy.CONNECT);
_574f.canvas=this.parentNode.workflow;
_574f.source=port;
_574f.target=this;
var _5750=this.createCommand(_574f);
if(_5750===null){
return;
}
*/
	this.parentNode.workflow.connectionLine.setColor(new draw2d.Color(117,149, 149));
	this.parentNode.workflow.connectionLine.setLineWidth(2);
	this.showCorona(true);
};

draw2d.MyOutputPort.prototype.onDragLeave=function(port){
	this.parentNode.workflow.connectionLine.setColor(new draw2d.Color(117,149, 149));
	this.parentNode.workflow.connectionLine.setLineWidth(1);
	this.showCorona(false);
};

draw2d.MyOutputPort.prototype.onMouseEnter = function()
{
	var color = new draw2d.Color(87, 87, 87);
	this.setColor(color);
	
};
draw2d.MyOutputPort.prototype.onMouseLeave=function(){
	var color = new draw2d.Color(255, 255, 255);
	//this.setColor(color);
	
	var local = this;
	setTimeout(function() { local.setColor(new draw2d.Color(255, 255, 255)); }, 2000);
};

/**
 * @private
 * @param {draw2d.Port} port The port on which this port has been droped
 **/
draw2d.MyOutputPort.prototype.onDrop = function(/*:draw2d.Port*/ port)
{
  if(this.getMaxFanOut() <= this.getFanOut())
    return;

  if(this.parentNode.id == port.parentNode.id)
  {
    // same parentNode -> do nothing
  }
  else
  {
  
    var sourceResourceFigure = this.parentNode;
    var command = new draw2d.CommandConnect(this.parentNode.workflow,this, port);
    var newLinkNoid = "" + new Date().getTime(); // must be string
    var linkConnection = new draw2d.LinkConnection(newLinkNoid, "smart", 1);
    linkConnection.onRemove = function(wFlow) {
    
    	sourceResourceFigure.onRemoveConnection(this.getProperty("noid"));
    };

    command.setConnection(linkConnection);

    
    var sourceNoid = sourceResourceFigure.getProperty("noid");
    var sourcePortLocation = this.getLocation();
    var destinationNoid = port.parentNode.getProperty("noid");
    var destinationPortLocation = port.getLocation();
    
    this.parentNode.workflow.getCommandStack().execute(command);
    
    sourceResourceFigure.onNewConnection(newLinkNoid, sourceNoid, sourcePortLocation, destinationNoid, destinationPortLocation);
  }
};

/**
* ResourceFigure
*/
draw2d.ResourceFigure=function(imageName, x, y, resourceName, resourceOid)
{
  draw2d.ImageFigure.call(this, imageName);
  this.resourceName = resourceName;
  this.resourceOid = resourceOid;
  this.leftPort = null;
  this.rightPort = null;
  this.topPort = null;
  this.bottomPort = null;
	
  this.setDimension(x, y);
  this.setDeleteable(false);
};

draw2d.ResourceFigure.prototype = new draw2d.ImageFigure();
draw2d.ResourceFigure.prototype.type="ResourceFigure";

draw2d.ResourceFigure.prototype.setWorkflow=function(/*:draw2d.Workflow*/ workflow)
{
  draw2d.ImageFigure.prototype.setWorkflow.call(this,workflow);

  if(workflow!==null && this.rightPort===null)
  {
  	var backgroundColor = new draw2d.Color(255, 255, 255);
  	var color = new draw2d.Color(255, 255, 255);
  	var maxFanOut = 50;
  	var portWidth = 10;
  	var portHeight = 10;
  	
	//right
	this.rightPort = new draw2d.MyOutputPort();
	this.rightPort.setLocation("right");
	this.rightPort.setMaxFanOut(maxFanOut); // It is possible to add "5" Connector to this port
	this.rightPort.setWorkflow(workflow);
	this.rightPort.setBackgroundColor(backgroundColor);
	this.rightPort.setColor(color);
	this.rightPort.setDimension(portWidth, portHeight);
	this.addPort(this.rightPort,this.width,(this.height/2));

	//left
	this.leftPort=new draw2d.MyOutputPort();
	this.leftPort.setLocation("left");
	this.leftPort.setWorkflow(workflow);
	this.leftPort.setMaxFanOut(maxFanOut);
	this.leftPort.setBackgroundColor(backgroundColor);
	this.leftPort.setColor(color);
	this.leftPort.setDimension(portWidth, portHeight);	
	this.addPort(this.leftPort,0,this.height/2);
	
	//top
	this.topPort=new draw2d.MyOutputPort();
	this.topPort.setLocation("top");
	this.topPort.setWorkflow(workflow);
	this.topPort.setMaxFanOut(maxFanOut);
	this.topPort.setBackgroundColor(backgroundColor);
	this.topPort.setColor(color);
	this.topPort.setDimension(portWidth, portHeight);	
	this.addPort(this.topPort,this.width/2,0);

	//bottom
	this.bottomPort=new draw2d.MyOutputPort();
	this.bottomPort.setLocation("bottom");
	this.bottomPort.setWorkflow(workflow);
	this.bottomPort.setMaxFanOut(maxFanOut);
	this.bottomPort.setBackgroundColor(backgroundColor);
	this.bottomPort.setColor(color);
	this.bottomPort.setDimension(portWidth, portHeight);	
	this.addPort(this.bottomPort,this.width/2,this.height);
  }
};

draw2d.ResourceFigure.prototype.onNewConnection=function(linkNoid, sourceNoid, sourcePortLocation, destinationNoid, destinationPortLocation) {

	// override callback
	//alert(linkNoid);
};

draw2d.ResourceFigure.prototype.onRemoveConnection=function(linkNoid) {

	// override callback
	alert(linkNoid);
};

draw2d.ResourceFigure.prototype.getPort=function(portLocation){
	if (portLocation == "left")
		return this.leftPort;
	else if (portLocation == "right")
		return this.rightPort;
	else if (portLocation == "top")
		return this.topPort;
	else if (portLocation == "bottom")
		return this.bottomPort;
	else
		return null;
};

draw2d.ResourceFigure.prototype.setResourceName=function(resourceName){
	this.resourceName = resourceName;
};

draw2d.ResourceFigure.prototype.setFontSize=function(fontSize){
	this.label.style.fontSize = fontSize + "pt";
};

draw2d.ResourceFigure.prototype.setBorderColor=function(color){
	this.label.style.borderTopColor = color; 
};

draw2d.ResourceFigure.prototype.setBorderSize=function(size){
	this.label.style.borderTop = size + "px solid " + this.label.style.borderTopColor;
};

draw2d.ResourceFigure.prototype.getResourceName=function(){
	return this.resourceName;
};

draw2d.ResourceFigure.prototype.getResourceOid=function(){
	return this.resourceOid;
};

draw2d.ResourceFigure.prototype.getContextMenu=function(){

	var menu=new draw2d.Menu();
	var oThis=this;
	menu.appendMenuItem(new draw2d.MenuItem("Add Dependency",null,function(){
		//todo: CALLBACK
		oThis.setBackgroundColor(new draw2d.Color(0,0,255));
	}));
	menu.appendMenuItem(new draw2d.MenuItem("Streaming View",null,function(){
		//todo: CALLBACK
		oThis.setBackgroundColor(new draw2d.Color(0,0,255));
	}));	
	return menu;
};

draw2d.ResourceFigure.prototype.onDoubleClick=function(){
	alert(this.getResourceOid());
};

draw2d.ResourceFigure.prototype.onConnectionDoubleClick=function(noid){
	alert("On Connection Double Click: " + noid);
};

draw2d.ResourceFigure.prototype.onMouseEnter=function(){
	//this.setBackgroundColor(new draw2d.Color(0,0,255));	
};

draw2d.ResourceFigure.prototype.onMouseLeave=function(){
	//this.setBackgroundColor(new draw2d.Color(255,255,255));
};

/**
 * Overrides ImageFigure to add label
 **/
draw2d.ResourceFigure.prototype.createHTMLElement=function()
{
    var item = draw2d.Node.prototype.createHTMLElement.call(this);
    item.style.width=this.width+"px";
    item.style.height=this.height+"px";
    item.style.margin="0px";
    item.style.padding="0px";
    item.style.border="0px";

    if(this.url!==null)
      item.style.backgroundImage="url("+this.url+")";
    else
      item.style.backgroundImage="";
    
    item.style.backgroundRepeat = "no-repeat";
    item.style.backgroundPosition = "center center";
    
    this.label=document.createElement("div");
    

    //this.label.style.width="100%";
    this.label.style.width="40px";

    this.label.style.position="absolute";
    this.label.style.textAlign="center";
    this.label.style.left="-4px";
    this.label.style.fontSize="7pt";
    this.label.style.fontFamily="Arial";
    this.label.style.borderTop = "4px solid";
    this.label.style.borderTopColor="#ffffff";  
    this.disableTextSelection(this.label);

    return item;
};

draw2d.ResourceFigure.prototype.paint=function()
{
  draw2d.ImageFigure.prototype.paint.call(this);

  this.label.style.top= this.getHeight() + 1 + "px"; // space for label
  if (this.resourceName == "") {
	this.label.style.backgroundColor="";
  }
  else {
	this.label.style.backgroundColor="#ffffff";
  	this.label.innerHTML=this.resourceName;
  }
  
  this.html.appendChild(this.label);
};

/*
* Selection listener for the workflow
*/
draw2d.SelectionListener=function(workflow) {
	this.workflow = workflow;
};

draw2d.SelectionListener.prototype.type="SelectionListener";
draw2d.SelectionListener.prototype.onSelectionChanged=function(figure) {

	if (figure instanceof draw2d.ResourceFigure) {
	
		var noid = figure.getProperty("noid");
		var resourceOid = figure.getResourceOid();
		this.workflow.onResourceSelected(noid, resourceOid);
	}
};

draw2d.buildColor=function(hexcolor) {

	hexcolor = hexcolor.replace("#","");
	var rgb = (
         {0:parseInt(hexcolor.substr(0,2),16),
          1:parseInt(hexcolor.substr(2,2),16),
          2:parseInt(hexcolor.substr(4,2),16)}
         );

	var color = new draw2d.Color(rgb[0],rgb[1],rgb[2]);
	return color;
};

/**
* Externalized events
*/

/*
draw2d.eventOnDoubleClick=function(resourceOid) {
	alert("onDoubleClick: " + resourceOid);
};


draw2d.eventOnSelect=function(resourceOid) {
	//alert("onSelect: " + resourceOid);
};

*/


draw2d.eventOnRemoveDependency=function(relationshipId) {
	alert("onRemoveDependency: " + relationshipId);
};

draw2d.eventOnRemoveLink=function(relationshipId) {
	alert("onRemoveLink: " + relationshipId);
};

draw2d.eventOnNewText=function(text) {
	alert("New text: " + text);
};

draw2d.eventOnRemoveText=function(text) {
	alert("Remove text: " + text);
};

draw2d.eventOnChangeText=function(text) {
	alert("Change text: " + text);
};

draw2d.eventOnNewSquare=function() {
	alert("New square");
};

draw2d.eventOnNewCircle=function() {
	alert("New circle");
};

draw2d.eventOnRemoveCircle=function() {
	alert("Remove circle");
};

draw2d.eventOnNewNode=function() {
	
};
