


$(document).ready(function() {
  
  $(".field.f_color").each(function(){
    fieldType_Color({showField: !$(this).hasClass("field_disabled"), containerId: "container_"+$(this).attr("id"),fieldName: $(this).attr("id"), fieldColor: $(this).find(".ff input").attr("value")});
  });
});

function fieldType_Color(settings)
  {
    settings = jQuery.extend({
      
      fieldName: '',
      containerId: '',
      showField: true,
      fieldColor: '#FFFFFF',
      
      labelSelectColor: 'Vyberte barvu',
      labelOpenColorPicker: 'Zvolit barvu z palety'
      
    },settings);
    
    initialize();
    
    if(settings.showField){show();}
    else{remove();}
    
    function initialize()
      {
        $('#' + settings.fieldName + ' label input').unbind("click").click(function(){
          if($(this).attr("checked")){show();}
          else{remove();}
          //$('#' + settings.fieldName).toggleClass("field_disabled");
        });
        
        settings.container = $('#' + settings.containerId);
      }
    
    function show()
      {
        settings.container.html('');
        settings.container.append('<a href="javascript:void(0);" title="' + settings.labelOpenColorPicker + '"><div style="background-color: ' + settings.fieldColor + '" id="block_color_' + settings.fieldName + '"></div><span>' + settings.labelSelectColor + '</span></a>');
        settings.container.append('<input class="input_hidden" type="hidden" name="' + settings.fieldName + '" id="f_' + settings.fieldName + '" value="' + settings.fieldColor + '" />');
        settings.container.ColorPicker(
            {
                color: settings.fieldColor,
                onShow: function (colpkr) {$(colpkr).fadeIn(500);return false;},
                onHide: function (colpkr) {$('#f_' + settings.fieldName).trigger("validateField");$(colpkr).fadeOut(500);return false;},
                onChange: function (hsb, hex, rgb) {
                  $('#block_color_' + settings.fieldName).css('backgroundColor', '#' + hex);
                  $('#f_' + settings.fieldName).attr("value", '#' + hex);
                }
            });    
      }
      
    function remove()
      {
        $('#' + settings.fieldName + ' .ff').html('');
      }
  
  }
