
HJ.Scribble = Class.create();

HJ.Scribble.prototype = {
   
   
   initialize: function() {
      this.scribbleTable = new Af.HtmlTable("scribbleTable", ",text");
      this.scribbleTable.dndMgr2 = app.dndMgrScribble;
      this.scribbleTable.draggableAttr = "text";
      this.textBox = document.getElementById("scribbleTextBox");
     // this.textBox.onchange = this.changed.bindAsEventListener(this);
      this.scribbleTable.listener = this;
      this.editor = this.textBox.cloneNode(true);
      this.editor.style.width = "98%";
      this.textBox.onchange = this.changed.bindAsEventListener(this);
      this.textBox.onfocus = this.focusOnTextBox.bindAsEventListener(this);
      this.textBox.onblur = this.focusLostTextBox.bindAsEventListener(this);
      this.editor.onchange = this.changed.bindAsEventListener(this);
      this.editor.onblur = this.focusLost.bindAsEventListener(this);
      this.defaultValue = this.textBox.value;
      if (is_ie) {
         this.textChanger = new Af.TextChanger(this.textBox);
      }
      this.editingCell = null;
      this.currentRow = -1;
      
      this.delScribbleLink = document.getElementById("delScribbleLink");
	  this.sendScribble = document.getElementById("sendScribble");

	  Event.observe(this.delScribbleLink, 'keypress', this.keyPressHandler.bindAsEventListener(this));
      this.delScribbleLink.onclick = this.deleteSelectedScribble.bindAsEventListener(this);
	  this.sendScribble.onclick = this.doSendScribble.bindAsEventListener(this);
     
   },
 
  
   cleanup: function() {
      this.textBox.onchange = null;
      this.editor.onchange = null;
      this.editor.onblur = null;
      this.scribbleTable.cleanup();
   },
   
   setDataList: function(dlist) {
      this.scribbleTable.setDataList(dlist);
      this.dataTable = new Af.DataTable(dlist);
      /*
      this.sendScribble = document.getElementsByName("sendScribble");
      if(this.sendScribble!=null){
         for(var index=0;index<this.sendScribble.length;index++){
            this.sendScribble[index].onclick = this.doSendScribble.bindAsEventListener(this);
         }
      }		 */
   },
   
   changed: function(evt) {
      var e = evt.target ? evt.target : evt.srcElement;
      if (e == this.textBox) {
         var v = this.textBox.value;
         if (v == null || v == this.defaultValue) {
            return;
         }
         v = trim(v);
         if (v == "") {
            return;
         }
		 if(v.length > 90){
			 showMessageDialog("Scribble should not be more than 90 characters.", "Excuse us!", 300, 100);
			 return;
		 }
         var obj = this.dataTable.insertObject("text", this.textBox.value, 0, true);
         this.scribbleTable.setDataList(this.dataTable.getDataList());
         this.currentRow = -1;
         this.saveScribble(obj);
         setTimeout(this.setFocusToTextBox.bind(this), 10);
      } else if (e == this.editor && this.editingCell != null) {
         var obj = this.editingObj
         var v = this.editor.value;
         if (v == null || v == "") {
            v = "";
			this.doDeleteScribble();
			return;
         }
         obj.text = v;
         this.scribbleTable.draggableTextChanged(this.currentRow, v);
         this.endEditing();
         this.saveScribble(obj);
      }
      return false;
   },
   
   focusLost: function(evt) {
      var e = evt.target ? evt.target : evt.srcElement;
      if (e == this.editor) {
         this.changed(evt);
      }
   },
   
   setFocusToTextBox: function(evt) {
      this.textBox.value = "";
      this.textBox.focus();
   },
   
   focusOnTextBox: function(evt) {
		var v = this.textBox.value;
		if (v == null) {
		v = "";
		}
		v = trim(v);
		if (v == this.defaultValue) {
			this.textBox.value = "";
			return;
		}
   },
   
   focusLostTextBox: function(evt) {
      var s = this.textBox.value;
      if (s == null) {
        s = "";
      } else {
        s = trim(s);
      }
      if (s == this.defaultValue || s == "") {
         this.textBox.value = this.defaultValue;
         consumeEvent(evt);
      }
   },
   
   endEditing: function() {
      if (this.editingCell != null) {
         removeAll(this.editingCell);
         this.editingCell.appendChild(document.createTextNode(this.editingObj.text));
         this.editingCell = null;
      }
   },
   
   cellSelected: function(row, col, reselected) {
      
      this.currentRow = row;
      if (reselected) {
         var e = this.scribbleTable.getCell(row, 1);
         if (e == this.editingCell) {
            return;
         }
         this.editingCell = e;
         if (e.childNodes.length > 0) {
            this.editor.value = e.childNodes[0].data;
         } else {
            this.editor.value = "";
         }
         this.editingObj = this.dataTable.getDataList()[this.currentRow];
         removeAll(e);
         this.editor.id = e.id;
         e.appendChild(this.editor);
         this.editor.focus();
         setCaretToEnd(this.editor);
      } else {
         this.scribbleTable.selectRow(row);
         this.delScribbleLink.focus();
      }
   },
   
   keyPressHandler: function(e) {
      var r = true;
      var eventTarget = e.target ? e.target : e.srcElement;
      
      var c = e.keyCode ? e.keyCode : e.which;
     
      if (e.ctrlKey) {
         
         
      } else if (this.editingCell == null && this.currentRow != -1) {
         
         if (c == 46 ||
         ((c == 8) && (eventTarget.tagName != "TEXTAREA" && (eventTarget.tagName != "INPUT")))) { // Del
            this.deleteScribbleConfirm();
         }
      }
      r = consumeEvent(e);
      return r;
   },
   
   
   deleteSelectedScribble: function(e) {
      if (this.editingCell == null && this.currentRow != -1) {
         this.deleteScribbleConfirm();
      }
      return false;
   },
  
   deleteScribbleConfirm: function() {
      showConfirmDialog("Are you sure you want to delete selected scribble?", "Delete Scribble", 300, 100, this.doDeleteScribble.bind(this));
      return false;
   },
   
   doDeleteScribble: function() {
      hideDialogWin();
      var obj = this.dataTable.deleteObjectByIndex(this.currentRow);
      this.scribbleTable.setDataList(this.dataTable.getDataList());
      this.deleteScribble(obj);
      this.currentRow = -1;
   },
   
   saveScribble: function(obj) {
      var req = new Af.DataRequest(svcURL,
      this.requestSaveScribbleCompleted.bind(this), this.requestFailedCommon.bind(this), null, this.requestTimedoutCommon.bind(this));
      req.addService("WorkspaceService", "saveScribble");
      this.savedObj = obj;
      if (obj.UUID != null) {
         req.addParameter("UUID", obj.UUID);
      }
      
     // req.addParameter("text", obj.text);
	  var s = "<messages>";
	  var t = obj.text;
	  if (t == null) {
	     t = "";
	  } else {
	     t = xmlEncode(t);
	  }
	  s += "<scribble>" + t + "</scribble>";
	  s += "</messages>";
	  req.xmlDoc = s;
      ajaxEngine.processRequest(req);
   },
   
   deleteScribble: function(obj) {
      var req = new Af.DataRequest(svcURL,
      this.requestDeleteScribbleCompleted.bind(this), this.requestFailedCommon.bind(this), null, this.requestTimedoutCommon.bind(this));
      req.addService("WorkspaceService", "deleteScribble");
      req.addParameter("UUID", obj.UUID);
      ajaxEngine.processRequest(req);
   },
   
   
   requestSaveScribbleCompleted: function(response) {
      if (this.savedObj.UUID == null) {
         this.savedObj.UUID = response.responseText;
      }		 /*
      this.sendScribble = document.getElementsByName("sendScribble");
      if(this.sendScribble!=null){
         for(var index=0;index<this.sendScribble.length;index++){
            this.sendScribble[index].onclick = this.doSendScribble.bindAsEventListener(this);
         }
      }	 */
   },
   
   requestDeleteScribbleCompleted: function(response) {
      
   },
   
   requestFailedCommon: function(dataRequest, msg) {
      hideDialogWin();
      showMessageDialog("We are currently unable to process this scribble request. <br/>Please try again.", "Excuse us!", 300, 100);
   },
   
   requestTimedoutCommon: function(dataRequest) {
      hideDialogWin();
      showMessageDialog("We are currently unable to process this scribble request. <br/>Please try again.", "Excuse us!", 300, 100);
   },
   
   doSendScribble: function(evt) {
   
      var e = evt.target ? evt.target : evt.srcElement;
      
      this.scribbleTable.cellSelected(evt);
     
      if(this.currentRow==-1){
         showMessageDialog("Which Scribble did you want to send?" , "Request", 300, 80);
         return;
      }
      
      if (this.dialog == null) {
         this.dialog = new HJ.SendScribbleEmail("Send A Scribble", this, null);
      }      
      var obj = new Object();
      this.dialog.showHTMLTemplate(basePageURL + "SendScribbleEmail.html", "MainArea", "sendScribbleDiv");
	  this.dialog.centerDialog(500, 350);
      this.dialog.setObj(obj, true);
      this.dialog.initState();
      return false;
   }
   
}

Af.TextChanger = Class.create();

Af.TextChanger.prototype = {

   initialize: function(t) {
      this.textBox = t;
	  Event.observe(this.textBox, 'keypress', this.keyPressHandler.bindAsEventListener(this));
   },

   keyPressHandler: function(e) {
      var eventTarget = e.target ? e.target : e.srcElement;
      
      var c = e.keyCode ? e.keyCode : e.which;
      if (c == 13) {
         setTimeout(this.loseFocus.bind(this), 1);
      }
      return true;
   },
   
   loseFocus: function() {
      try {
         this.textBox.blur();
      } catch(e) {};
   }
}

