#ifndef ____workflow_js_h__
#define ____workflow_js_h__

#include "HTTPPage.h"

namespace http {

   class __workflow_js : public HTTPPage {

      int processCommand(std::string request, core::TCPSession *session, HTTPSession *httpSession, HTTPRequest &httpRequest, std::stringstream &data) {	   
         data << "var mouseDownX;" << std::endl;
         data << "var mouseDownY;" << std::endl;
         data << "var isMouseDown = false;" << std::endl;
         data << "var dragObject;" << std::endl;
         data << "var dragHint = \"move\";" << std::endl;
         data << "var selected;" << std::endl;
	 data << "listSource();" << std::endl;
         data << "" << std::endl;
         data << "function listSource() {" << std::endl;
         data << " var connectors = document.getElementsByTagName(\"path\");" << std::endl;
         data << " for(var i = 0; connectors[i]; ++i) {" << std::endl;
         data << "  var source = connectors[i].getAttribute(\"data-source\");" << std::endl;
         data << "  var sourceElem = source.split(\":\");" << std::endl;
         data << "  var sourceId = document.getElementById(sourceElem[0]);" << std::endl;
         data << "  var offset = 0;" << std::endl;
         data << "  if(sourceElem.length > 1) {" << std::endl;
         data << "   var options = sourceId.getElementsByTagName(\"div\");" << std::endl;
         data << "   for(var o = 0; options[o]; ++o) {" << std::endl;
         data << "	 var option = options[o];" << std::endl;
         data << "	 if(sourceElem[1] == options[o].getAttribute(\"data-name\")) {" << std::endl;
         data << "	    offset = option.offsetTop + (parseInt(option.style.height) / 2);" << std::endl;
         data << "	 }" << std::endl;
         data << "   }" << std::endl;
         data << "  }" << std::endl;
         data << "" << std::endl;
         data << "  var dest = connectors[i].getAttribute(\"data-dest\");" << std::endl;
         data << "  var destId = document.getElementById(dest);" << std::endl;
         data << "  var d = \"M\" + getPos(sourceId, sourceId.offsetWidth, offset) +" << std::endl;
         data << "          \" L\" + getPos(sourceId, sourceId.offsetWidth + 30, offset) + " << std::endl;
         data << "          \" L\" + getPos(destId, destId.offsetWidth / 2, -30) + " << std::endl;
         data << "          \" L\" + getPos(destId, destId.offsetWidth / 2);" << std::endl;
         data << "  connectors[i].setAttribute(\"d\", d);" << std::endl;
         data << "//  console.log(d);" << std::endl;
         data << " } " << std::endl;
         data << "}" << std::endl;
         data << "" << std::endl;
         data << "function getMouseX(e) {" << std::endl;
         data << " return e.clientX;" << std::endl;
         data << "}" << std::endl;
         data << "" << std::endl;
         data << "function getMouseY(e) {" << std::endl;
         data << " return e.clientY;" << std::endl;
         data << "}" << std::endl;
         data << "" << std::endl;
         data << "function getPos(item, offsetX = 0, offsetY = 0) {" << std::endl;
         data << " var pos = \" \" + (parseInt(item.style.left) + offsetX);" << std::endl;
         data << " pos += \" \" + (parseInt(item.style.top) + offsetY);" << std::endl;
         data << " return pos;	" << std::endl;
         data << "}" << std::endl;
         data << "" << std::endl;
         data << "function mousedown(obj, e) {" << std::endl;
         data << " var mouseX = getMouseX(e);" << std::endl;
         data << " var mouseY = getMouseY(e);" << std::endl;
         data << " mouseDownX = mouseX - obj.offsetLeft;" << std::endl;
         data << " mouseDownY = mouseY - obj.offsetTop;" << std::endl;
         data << " dragObject = obj;" << std::endl;
         data << " selected = obj;" << std::endl;
         data << " isMouseDown = true;" << std::endl;
         data << "}" << std::endl;
         data << "" << std::endl;
         data << "function mouseup(e) {" << std::endl;
         data << " isMouseDown = false;" << std::endl;
         data << " dragObject = null;" << std::endl;
         data << "}" << std::endl;
         data << "" << std::endl;
         data << "function mousemove(e) {" << std::endl;
         data << " var mouseX = getMouseX(e);" << std::endl;
         data << " var mouseY = getMouseY(e);" << std::endl;
         data << " if(isMouseDown) {" << std::endl;
         data << "  if(dragHint == \"move\") {" << std::endl;
         data << "   dragObject.style.left = (mouseX - mouseDownX) + \"px\";" << std::endl;
         data << "   dragObject.style.top = (mouseY - mouseDownY) + \"px\";  " << std::endl;
         data << "   listSource();" << std::endl;
         data << "  } " << std::endl;
         data << " }" << std::endl;
         data << "}" << std::endl;

	 httpRequest.response.addHeader("Content-Type", "script/javascript");
         return 0;

      }

   };

}

#endif
