//-------------------- ricoColor.js
if(typeof(Rico) == "undefined") Rico = {};

Rico.Color = Class.create();

Rico.Color.prototype = {

   initialize: function(red, green, blue) {
      this.rgb = { r: red, g : green, b : blue };
   },

   setRed: function(r) {
      this.rgb.r = r;
   },

   setGreen: function(g) {
      this.rgb.g = g;
   },

   setBlue: function(b) {
      this.rgb.b = b;
   },

   setHue: function(h) {

      // get an HSB model, and set the new hue...
      var hsb = this.asHSB();
      hsb.h = h;

      // convert back to RGB...
      this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, hsb.b);
   },

   setSaturation: function(s) {
      // get an HSB model, and set the new hue...
      var hsb = this.asHSB();
      hsb.s = s;

      // convert back to RGB and set values...
      this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, hsb.b);
   },

   setBrightness: function(b) {
      // get an HSB model, and set the new hue...
      var hsb = this.asHSB();
      hsb.b = b;

      // convert back to RGB and set values...
      this.rgb = Rico.Color.HSBtoRGB( hsb.h, hsb.s, hsb.b );
   },

   darken: function(percent) {
      var hsb  = this.asHSB();
      this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, Math.max(hsb.b - percent,0));
   },

   brighten: function(percent) {
      var hsb  = this.asHSB();
      this.rgb = Rico.Color.HSBtoRGB(hsb.h, hsb.s, Math.min(hsb.b + percent,1));
   },

   blend: function(other) {
      this.rgb.r = Math.floor((this.rgb.r + other.rgb.r)/2);
      this.rgb.g = Math.floor((this.rgb.g + other.rgb.g)/2);
      this.rgb.b = Math.floor((this.rgb.b + other.rgb.b)/2);
   },

   isBright: function() {
      var hsb = this.asHSB();
      return this.asHSB().b > 0.5;
   },

   isDark: function() {
      return ! this.isBright();
   },

   asRGB: function() {
      return "rgb(" + this.rgb.r + "," + this.rgb.g + "," + this.rgb.b + ")";
   },

   asHex: function() {
      return "#" + this.rgb.r.toColorPart() + this.rgb.g.toColorPart() + this.rgb.b.toColorPart();
   },

   asHSB: function() {
      return Rico.Color.RGBtoHSB(this.rgb.r, this.rgb.g, this.rgb.b);
   },

   toString: function() {
      return this.asHex();
   }

};

Rico.Color.createFromHex = function(hexCode) {

   if ( hexCode.indexOf('#') == 0 )
      hexCode = hexCode.substring(1);

   var red = "ff", green = "ff", blue="ff";
   if(hexCode.length > 4)
	{
	   red   = hexCode.substring(0,2);
	   green = hexCode.substring(2,4);
	   blue  = hexCode.substring(4,6);
	}
	else if(hexCode.length > 0 & hexCode.length < 4)
	{
	  var r = hexCode.substring(0,1);
	  var g = hexCode.substring(1,2);
	  var b = hexCode.substring(2);
	  red = r+r;
	  green = g+g;
	  blue = b+b;
	}
   return new Rico.Color( parseInt(red,16), parseInt(green,16), parseInt(blue,16) );
};

/**
 * Factory method for creating a color from the background of
 * an HTML element.
 */
Rico.Color.createColorFromBackground = function(elem) {

   var actualColor = Element.getStyle($(elem), "background-color");
  if ( actualColor == "transparent" && elem.parent )
      return Rico.Color.createColorFromBackground(elem.parent);

   if ( actualColor == null )
      return new Rico.Color(255,255,255);

   if ( actualColor.indexOf("rgb(") == 0 ) {
      var colors = actualColor.substring(4, actualColor.length - 1 );
      var colorArray = colors.split(",");
      return new Rico.Color( parseInt( colorArray[0] ),
                            parseInt( colorArray[1] ),
                            parseInt( colorArray[2] )  );

   }
   else if ( actualColor.indexOf("#") == 0 ) {
	  return Rico.Color.createFromHex(actualColor);
   }
   else
      return new Rico.Color(255,255,255);
};

Rico.Color.HSBtoRGB = function(hue, saturation, brightness) {

   var red   = 0;
	var green = 0;
	var blue  = 0;

   if (saturation == 0) {
      red = parseInt(brightness * 255.0 + 0.5);
	   green = red;
	   blue = red;
	}
	else {
      var h = (hue - Math.floor(hue)) * 6.0;
      var f = h - Math.floor(h);
      var p = brightness * (1.0 - saturation);
      var q = brightness * (1.0 - saturation * f);
      var t = brightness * (1.0 - (saturation * (1.0 - f)));

      switch (parseInt(h)) {
         case 0:
            red   = (brightness * 255.0 + 0.5);
            green = (t * 255.0 + 0.5);
            blue  = (p * 255.0 + 0.5);
            break;
         case 1:
            red   = (q * 255.0 + 0.5);
            green = (brightness * 255.0 + 0.5);
            blue  = (p * 255.0 + 0.5);
            break;
         case 2:
            red   = (p * 255.0 + 0.5);
            green = (brightness * 255.0 + 0.5);
            blue  = (t * 255.0 + 0.5);
            break;
         case 3:
            red   = (p * 255.0 + 0.5);
            green = (q * 255.0 + 0.5);
            blue  = (brightness * 255.0 + 0.5);
            break;
         case 4:
            red   = (t * 255.0 + 0.5);
            green = (p * 255.0 + 0.5);
            blue  = (brightness * 255.0 + 0.5);
            break;
          case 5:
            red   = (brightness * 255.0 + 0.5);
            green = (p * 255.0 + 0.5);
            blue  = (q * 255.0 + 0.5);
            break;
	    }
	}

   return { r : parseInt(red), g : parseInt(green) , b : parseInt(blue) };
};

Rico.Color.RGBtoHSB = function(r, g, b) {

   var hue;
   var saturaton;
   var brightness;

   var cmax = (r > g) ? r : g;
   if (b > cmax)
      cmax = b;

   var cmin = (r < g) ? r : g;
   if (b < cmin)
      cmin = b;

   brightness = cmax / 255.0;
   if (cmax != 0)
      saturation = (cmax - cmin)/cmax;
   else
      saturation = 0;

   if (saturation == 0)
      hue = 0;
   else {
      var redc   = (cmax - r)/(cmax - cmin);
    	var greenc = (cmax - g)/(cmax - cmin);
    	var bluec  = (cmax - b)/(cmax - cmin);

    	if (r == cmax)
    	   hue = bluec - greenc;
    	else if (g == cmax)
    	   hue = 2.0 + redc - bluec;
      else
    	   hue = 4.0 + greenc - redc;

    	hue = hue / 6.0;
    	if (hue < 0)
    	   hue = hue + 1.0;
   }

   return { h : hue, s : saturation, b : brightness };
};

if(typeof(CouleurFacile) == "undefined") CouleurFacile = {};

CouleurFacile.ColorPicker = Class.create();

CouleurFacile.ColorPicker.palettes = {
  Perso : [
      ["ffffff", "ffd5d5", "ffe6d5", "fff6d5", "f6ffd5", "e5ffd5", "d5ffd5", "d5ffe6", "d5fff6", "d5f6ff", "d5e5ff", "d5d5ff", "e5d5ff", "f6d5ff", "ffd5f6", "ffd5e5"],
      ["f2f2f2", "ffaaaa", "ffccaa", "ffeeaa", "eeffaa", "ccffaa", "aaffaa", "aaffcc", "aaffee", "aaeeff", "aaccff", "aaaaff", "ccaaff", "eeaaff", "ffaaee", "ffaacc"],
      ["e6e6e6", "ff8080", "ffb380", "ffe680", "e5ff80", "b3ff80", "80ff80", "80ffb3", "80ffe6", "80e5ff", "80b3ff", "8080ff", "b380ff", "e580ff", "ff80e5", "ff80b2"],
      ["cccccc", "ff5555", "ff9955", "ffdd55", "ddff55", "99ff55", "55ff55", "55ff99", "55ffdd", "55ddff", "5599ff", "5555ff", "9955ff", "dd55ff", "ff55dd", "ff5599"],
      ["b3b3b3", "ff2a2a", "ff7f2a", "ffd42a", "d4ff2a", "7fff2a", "2aff2a", "2aff80", "2affd5", "2ad4ff", "2a7fff", "2a2aff", "7f2aff", "d42aff", "ff2ad4", "ff2a7f"],
      ["999999", "ff0000", "ff6600", "ffcc00", "ccff00", "66ff00", "00ff00", "00ff66", "00ffcc", "00ccff", "0066ff", "0000ff", "6600ff", "cc00ff", "ff00cc", "ff0066"],
      ["808080", "d40000", "d45500", "d4aa00", "aad400", "55d400", "00d400", "00d455", "00d4aa", "00aad4", "0055d4", "0000d4", "5500d4", "aa00d4", "d400aa", "d40055"],
      ["666666", "aa0000", "aa4400", "aa8800", "88aa00", "44aa00", "00aa00", "00aa44", "00aa88", "0088aa", "0044aa", "0000aa", "4400aa", "8800aa", "aa0088", "aa0044"],
      ["4d4d4d", "800000", "803300", "806600", "668000", "338000", "008000", "008033", "008066", "006680", "003380", "000080", "330080", "660080", "800066", "800033"],
      ["333333", "550000", "552200", "554400", "445500", "225500", "005500", "005522", "005544", "004455", "002255", "000055", "220055", "440055", "550044", "550022"],
      ["000000", "2b0000", "2b1100", "2b2200", "222b00", "112b00", "002b00", "002b11", "002b22", "00222b", "00112b", "00002b", "11002b", "22002b", "2b0022", "2b0011"]]
}

CouleurFacile.ColorPicker.UIImages = {
  'button.gif' : 'button.gif',
  'background.png' : 'background.png'
}


CouleurFacile.ColorPicker.prototype = 
{
	initialize : function(options)
	{
		var basics =
		{
      Palette : 'Perso',
			ClassName : 'ColorPicker'
		}

		this.element = null;

		options = Object.extend(basics, options);
		this.options = options;
		this.input = $(options['ID']);
		this.finalColor = null;
    this.show();
	},
  getBasicPickerContainer : function(pickerID, palette)
	{
		var table = TABLE({className:'basic_colors palette_'+palette},TBODY());
		var colors = CouleurFacile.ColorPicker.palettes[palette];
		var pickerOnClick = this.cellOnClick.bind(this);
    colors.each(function(color)
		{
			var row = document.createElement("tr");
			color.each(function(c)
			{
				var td = document.createElement("td");
				var img = IMG({src:CouleurFacile.ColorPicker.UIImages['button.gif'],width:16,height:16});
				img.style.backgroundColor = "#"+c;
				Event.observe(img,"click", pickerOnClick);
				Event.observe(img,"mouseover", function(e)
				{
					Element.addClassName(Event.element(e), "pickerhover");
				});
				Event.observe(img,"mouseout", function(e)
				{
					Element.removeClassName(Event.element(e), "pickerhover");
				});
				td.appendChild(img);
				row.appendChild(td);
			});
			table.childNodes[0].appendChild(row);
		});
		return DIV({className:this.options['ClassName']+" BasicColorPicker",
					id:pickerID+"_picker"}, table);
	},
	show : function(event)
	{
		if(this.element == null) {
			this.element = this.getBasicPickerContainer(this.options['ID'], this.options['Palette']);
			this.input.parentNode.appendChild(this.element);
      this.finalColor = $('BlocColorMustBePaint');
    }
	},
	cellOnClick : function(e)
	{
		var el = Event.element(e);
		if(el.tagName.toLowerCase() != "img")
			return;
		var color = Rico.Color.createColorFromBackground(el);
		this.updateColor(color);
	},
	updateColor : function(color)
	{
		this.input.value = color.toString().toUpperCase();
		this.finalColor.style.backgroundColor = color.toString();
		if(typeof(this.onChange) == "function")
			this.onChange(color);
		if(this.options.OnColorSelected)
			this.options.OnColorSelected(this,color);
	}
}

CouleurFacile.LastColors = Class.create();


CouleurFacile.LastColors.Palette =
  ["ffffff", "ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff","ffffff"];

CouleurFacile.LastColors.UIImages = {
  'button.gif' : 'button.gif',
  'background.png' : 'background.png'
}

CouleurFacile.LastColors.prototype = {

   initialize: function(options) {
		var basics = {
			ClassName : 'LastColorPicker'
		}
		options = Object.extend(basics, options);
		this.options = options;
    this.input = $(options['InputID']);
    this.conteneur = $(options['ID']);
		this.finalColor = null;
    this.show();
   },
  constructPickerContainer : function()
	{
		var colors = CouleurFacile.LastColors.Palette;
		var pickerOnClick = this.cellOnClick.bind(this);
    var conteneur = this.conteneur;
    colors.each(function(color)
		{
      var img = IMG({src:CouleurFacile.LastColors.UIImages['button.gif'],width:30,height:30});
      img.style.backgroundColor = "#"+color;
      Element.addClassName(img, "ImgLastColor");
      Event.observe(img,"click", pickerOnClick);
      Event.observe(img,"mouseover", function(e)
      {
        Element.addClassName(Event.element(e), "pickerhover");
      });
      Event.observe(img,"mouseout", function(e)
      {
        Element.removeClassName(Event.element(e), "pickerhover");
      });
      conteneur.appendChild(img);
		});
	},
	show : function()
	{
		if(this.conteneur) {
			this.constructPickerContainer();
      this.finalColor = $('BlocColorMustBePaint');
    }
	},
	cellOnClick : function(e)
	{
		var el = Event.element(e);
		if(el.tagName.toLowerCase() != "img")
			return;
		var color = Rico.Color.createColorFromBackground(el);
		this.updateColor(color);
	},
	updateColor : function(color)
	{
		this.input.value = color.toString().toUpperCase();
		this.finalColor.style.backgroundColor = color.toString();
		if(typeof(this.onChange) == "function")
			this.onChange(color);
		if(this.options.OnColorSelected)
			this.options.OnColorSelected(this,color);
	}
};

CouleurFacile.LastColors.addNewColorOnTop = function()
{
  var color = Rico.Color.createColorFromBackground($('BlocColorMustBePaint'));
  var color_str = color.toString();
  var img2move = $$('.ImgLastColor').last();
  var liste = $$('.ImgLastColor');
  liste.each(function(img)
  {
    var bg = Rico.Color.createColorFromBackground(img);
    if(bg.toString() == color_str){
      img2move = img;
    }
  });
  img2move.style.backgroundColor = color_str;
  $('BlocLastColors').insert({top:img2move});
}

