
if(typeof deconcept == "undefined") var deconcept = new Object();
if(typeof ColorChooserWindowUtil == "undefined" ) var ColorChooserWindowUtil = new Object();

var alerted = false;
ColorChooserWindowUtil.chooser = null;
ColorChooserWindowUtil.imageData = null;
ColorChooserWindowUtil.userSetColor = null;
ColorChooserWindowUtil.show = function( ele )
{
  var chooser = ColorChooserWindowUtil.chooser;
  if( chooser == null )
    return;

  if( chooser.layer == null )
    chooser.create();

  if( chooser.isShowing() == false || chooser.getAttachedElement() != ele )
    ele.previousValue = ele.value;

  chooser.attachTo( ele );
  chooser.show();
}

ColorChooserWindowUtil.hide = function( ele )
{
  var chooser = ColorChooserWindowUtil.chooser;
  if( chooser == null )
    return;

  chooser.hide();
}

ColorChooserWindowUtil.cancel = function()
{
  var chooser = ColorChooserWindowUtil.chooser;
  if( chooser == null )
    return;

  var ele = chooser.getAttachedElement();
  if( ele )
  {
    ele.value = ele.previousValue;
    ele.previousValue = "";
  }

  ColorChooserWindowUtil.hide();
}

ColorChooserWindowUtil.setColor = function( color )
{
  var chooser = ColorChooserWindowUtil.chooser;
  if( chooser == null )
    return;

  var ele = chooser.getAttachedElement();
  if( ! ele )
    return;

  ele.value = color;

  if( ColorChooserWindowUtil.userSetColor )
    ColorChooserWindowUtil.userSetColor( color, chooser, ele );
}

ColorChooserWindowUtil.getColor = function()
{
  var chooser = ColorChooserWindowUtil.chooser;
  if( chooser == null )
    return false;

  var ele = chooser.getAttachedElement();
  if( ! ele )
    return false;

  return ele.value;
}

ColorChooserWindowUtil.getImageData = function()
{
  return ColorChooserWindowUtil.imageData;
}

ColorChooserWindowUtil.setImageData = function( imageData )
{
  ColorChooserWindowUtil.imageData = imageData;
}

ColorChooserWindowUtil.updateImageData = function( imageData )
{
  ColorChooserWindowUtil.setImageData( imageData );
  if( ColorChooserWindowUtil.chooser && ColorChooserWindowUtil.chooser.iframe )
    ColorChooserWindowUtil.chooser.iframe.contentWindow.updateImageData();
}

ColorChooserWindowUtil.preload = function( evt )
{
  var chooser = ColorChooserWindowUtil.chooser;
  if( chooser == null )
    return;

  if( chooser.layer == null )
    chooser.create();
}

ColorChooserWindowUtil.init = function( preload )
{
  var colorChooser = new ColorChooserWindow();
  ColorChooserWindowUtil.chooser = colorChooser.getWindow();

  if( preload )
    deconcept.util.addEvent( window, 'load', ColorChooserWindowUtil.preload );
}

deconcept.ColorChooserWindow = function()
{
  this.chooser = null;
  this.width = 405;
  this.height = 280;
  this.url = '/img/color-chooser';
  this.align = 'right';
  this.verticalAlignment = 'middle';
  this.offsetX = 20;
  this.offsetY = 3;

  this.init();
}

deconcept.ColorChooserWindow.prototype = {
  init: function()
  {
    this.chooser = new WindowOverlay( this.url, this.width, this.height );
    this.chooser.setAlign( this.align );
    this.chooser.setVerticalAlign( this.verticalAlignment );
    this.chooser.setOffsetX( this.offsetX );
    this.chooser.setOffsetY( this.offsetY );
  },

  getWindow: function()
  {
    return this.chooser;
  }
}

var ColorChooserWindow = deconcept.ColorChooserWindow;

