/**
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * (C) 2008 Syronex / J.M. Rosengard
 * http://www.syronex.com/software/jquery-color-picker
 *
 * - Check mark is either black or white, depending on the darkness
 *   of the color selected.
 * - Fixed a bug in the original plugin that led to problems when there is
 *   more than one colorPicker in a document.
 *
 * This is based on:
 *
 * jQuery colorSelect plugin 0.9
 * http://plugins.jquery.com/project/colorPickerAgain
 * Copyright (c) 2008 Otaku RzO (Renzo Galo Castro Jurado).
 * (Original author URL & domain name no longer available.)
 *
 */


function colorpicker_set (color, label)
{
	$('#colors_'+color_picker_selected).val(label);
	$('#colors_choose_'+color_picker_selected).css('background-color', color);
	$('#color_picker').hide();

	temp=color_picker_selected.split('_');
   class_id=parseInt(temp[0]);

   if (!isNaN(class_id))
   {
   	if (label==$('#jq_const_product_options_colors_custom').val())
   	{
   		$('#colors_'+color_picker_selected).val('');
   		$('#colors_'+color_picker_selected).focus();
   	}

		product_colorpicker_set_checked (class_id);
   }
}



(function($) {
  $.fn.colorPicker = function($$options) {

    // Defaults
    var $defaults =
    {

      defaultColor: 0,
      columns: 0
    };

    var $settings = $.extend({}, $defaults, $$options);

    // Iterate and reformat each matched element
    return this.each
    (
    	function()
    	{
	      var $this = $(this);

	      // build element specific options
	      var o = $.meta ? $.extend({}, $settings, $this.data()) : $settings;

	      var $$oldIndex = typeof(o.defaultColor)=='number' ? o.defaultColor : -1;
	      var _html = "";

	      $('.jq_startup_colors').each
	      (
	      	function (i, val)
	      	{
					temp=$(this).val().split('_');
					var hex=temp[0];
					temp=$(this).attr('id').split('_');
					_html += '<input type="hidden" class="color_value" value="'+hex+'">';
					_html += '<div id="'+hex+'__'+temp[4]+'" class="color_sample tecnologia_'+temp[3]+'" style="background-color:'+hex+';"></div>';
					_html += '<div class="color_label tecnologia_'+temp[3]+'">'+temp[4]+'</div>';
					_html += '<div class="clear_left"></div>';
	      	}
	      );


	      $this.html('<div class="jColorSelect">'+_html+'</div>');

	      $this.children('.jColorSelect').children('.color_sample').each
	      (
	      	function (i, val)
	      	{
	      		$(this).click
	      		(
	      			function (event)
	      			{
	      				temp=$(this).attr('id').split('__');
	      				colorpicker_set(temp[0], temp[1]);
	      			}
	      		);
	      	}
	      );

	    }
    );
    return this;
  };


})(jQuery);

/**
 * Return true if color is dark, false otherwise.
 * (C) 2008 Syronex / J.M. Rosengard
 **/
function isdark(color){
  var colr = parseInt(color.substr(1), 16);

  return (colr >>> 16) // R
    + ((colr >>> 8) & 0x00ff) // G
    + (colr & 0x0000ff) // B
    < 500;
}
