/*
 * Author: Vertex Logic, Inc
 * Copyright 2005, 2006, 2007 - All rights reserved
 *
 * Vertex Logic, Inc., California, USA 
 *
 * Vertex Logic grants you ("Licensee") a non-exclusive license to use
 * and modify this source and recompile it in
 * accordance with the terms of the Agreement under which this software is bought
 * and provided that (i) this copyright notice appear on all copies of the Software; 
 * (ii) Licensee does not sale the software as is or with modification without
 * the prior consent of Vertex Logic. (iii) Licensee agrees that it does not have any 
 * title or ownership of the Software.
 *
 * The program is provided "as is" without any warranty express or
 * implied, including the warranty of non-infringement and the implied
 * warranties of merchantibility and fitness for a particular purpose.
 * Vertex Logic will not be liable for any damages suffered 
 * by you as a result of using the Program. 
 * In no event will Vertex Logic be liable for any
 * special, indirect or consequential damages or lost profits even if
 * Vertex Logic have been advised of the possibility of their occurrence. 
 * Vertex Logic will not be liable for any third party claims against you.
 */
 var COLOR_PICKER_WIDTH = 162;
var COLOR_PICKER_HEIGHT = 108;



Af.ColorPicker = Class.create();

Af.ColorPicker.prototype = {
   initialize: function(handler) {
      this._initializeDialog(handler);
   },
   
   _initializeDialog: function(handler) {
      this.handler = handler;
      this.titleElement = null;
      this.element = null;
      this.cb = null;
      this.visible = false;
      this.width = COLOR_PICKER_WIDTH  + "px";
      this.height = (COLOR_PICKER_HEIGHT + 16) + "px" // 16 for the title bar
      this.firstTime = true;
      this.div = null;
      
   },
   
   close: function(ev) {
      this.hide();
   },
   
   hide: function() {
      this.visible = false;
      if (this.component) {
         this.component.changeVisibility(false);
      }
      if (this.element != null) {
         this.element.style.display = "none";
      }
   },
   
   
   show: function(container, x, y) {
      this.visible = true;
      if (this.element == null) {
         this.createElement();
      }
      
      var p = document.getElementById(container);
      p.appendChild(this.element);
      this.element.style.left = x + "px";
      this.element.style.top = (y + 2)+ "px";
      this.element.style.display = "";
      
   },
   
   createElement: function() {
      var table = document.createElement('table');
      table.cellSpacing = '0px';
      table.cellPadding = '0px';
      table.className = 'ColorPicker';
      table.style.width = this.width;
      table.style.height = this.height;
      
      var tbody = document.createElement('tbody');
      table.appendChild(tbody);
      
      var td;
      var tr;
      
      tr = document.createElement('tr');
      tbody.appendChild(tr);
      td = document.createElement('td');
      td.className = 'ColorPickerTitle';
      tr.appendChild(td);
      this.titleElement = td;
      
      td = document.createElement('td');
      td.className = 'ColorPickerTitleButtonBar';
      tr.appendChild(td);
      this.cb = document.createElement('a');
      var img = document.createElement('img');
      img.className = 'ColorPickerCloseButtonImage';
      img.src = '/images/close.gif';
      this.cb.appendChild(img);
      td.appendChild(this.cb);
      this.cb.onclick = this.close.bindAsEventListener(this);
      
      tr = document.createElement('tr');
      tbody.appendChild(tr);
      td = document.createElement('td');
      td.colSpan = "2";
      tr.appendChild(td);
      
      this.div = document.createElement("div");
      this.div.src = topURL + "common/color.html";
      this.div.width = "100%";
      this.div.height = COLOR_PICKER_HEIGHT;
      this.div.frameBorder = "0";
      td.appendChild(this.div);
      
      this.element = table;
      
      this.element.onclick = this.cellSelected.bindAsEventListener(this);
      this.element.onmousedown= this.mouseDownOnElement.bindAsEventListener(this);
      
      this.loadMyComp();
      
   },
   
   loadMyComp: function() {
      
      var req = new Af.DataRequest(topURL + 'common/color.html', this.requestCompletedMyComp.bind(this),
      requestFailedCommon, null, requestTimedoutCommon);
      ajaxEngine.processRequest(req);
   },
   
   requestCompletedMyComp: function(response) {
      this.div.innerHTML = response.responseText;
   },
   
   cellSelected: function(evt) {
      var eventTarget = evt.target ? evt.target : evt.srcElement;
      if (eventTarget.tagName == "TD" && eventTarget.className == "colorcell" && this.handler != null) {
         this.handler.colorSelected(eventTarget.title);
      }
      this.hide();
      if (evt.stopPropagation) {
         evt.stopPropagation();
         } else {
         evt.cancelBubble = true;
      }
      evt.returnValue = false;
      return false;
   },
   
   mouseDownOnElement: function(e) {
         if (e.stopPropagation) {
            e.stopPropagation();
         } else {
            e.cancelBubble = true;
         }
         
         e.returnValue = false;
         return false;
   }
}
