// ColorPicker.js
// Javascript Behaviour for the ColorPicker Control
// Copyright (c) by Matthias Hertel, http://www.mathertel.de
// This work is licensed under a BSD style license. See http://www.mathertel.de/License.aspx
// ----- 
// 15.09.2007 created by Matthias Hertel

var ColorPickerBehavior = {
_pickObj: null,
  
onclick: function(evt) {
  var pos = jcl.absolutePosition(this);
  var picker = ColorPickerBehavior._pickObj;
  
  if (! picker)
    picker = this._createPicker();
    
  picker.onmousedown = this.pickClick.bind(this);

  picker.style.top = (pos.top + this.scrollHeight) + "px";
  picker.style.left = pos.left + "px";
  picker.style.display = "";
},


// handle the click onto a colored cell
pickClick: function (evt) {
  evt = evt || window.event;
  var src = evt.srcElement;
  var picker = ColorPickerBehavior._pickObj;
  OpenAjax.hub.publish(jcl.BuildFullEventname(this), src.style.backgroundColor);
  picker.style.display = "none";
  // disable loosing the focus in IE. For FF: search moz-user-select:none;  
  if (jcl.isIE) src.unselectable="on";
},


// create a picker object
_createPicker: function() { 
  var picker = document.createElement("div");
  document.body.appendChild(picker);
  picker.className = "VEColorPicker";
  picker.style.display = "";
  picker.innerHTML = this._colorTableText();
    ColorPickerBehavior._pickObj = picker;
  return(picker);
},


// return the html code of a row of colors
_colorRow: function (ca) {
  var hc = "\n<tr>";
  var c;
  ca = ca.split(',');
  for (var n = 0; n < 9; n++) {
    c = ca[n].replace(/(.)(.)(.)/, "$1$1$2$2$3$3");
    hc += "<td title='"+c+"' style='background-color:#"+c+"'></td>";
  }
  hc += "</tr>";
  return(hc);
}, // _colorRow


// return the complete html code for a the table containing all pickable colors
_colorTableText: function() {
  var hc = "<table><tbody>";
  hc += this._colorRow("000,222,666,888,999,aaa,ccc,eee,fff");
  hc += this._colorRow("003,006,009,00c,00f,33f,66f,99f,ccf");
  hc += this._colorRow("033,066,099,0cc,0ff,3ff,6ff,9ff,cff");
  hc += this._colorRow("030,060,090,0c0,0f0,3f3,6f6,9f9,cfc");
  hc += this._colorRow("030,660,990,cc0,ff0,ff3,ff6,ff9,ffc");
  hc += this._colorRow("330,600,900,c00,f00,f33,f66,f99,fcc");
  hc += this._colorRow("303,606,909,c0c,f0f,f3f,f6f,f9f,fcf");
  hc += "\n</tbody></table>";
  return(hc);
} // _colorTableText


}

// End