<?php
/*
Plugin Name: Color Picker
Description: Adds a Color Picker fieldtype
Author: SynesisDigital: http://www.synesisdigital.com
Version: 0.01
Requires at least: 2.06
*/

/* Usage:
 * Either
 * Create specific fields and add them to the array below
 * Or
 * Create any fieldname which starts 'color_picker_' or ends '_color_picker'
*/

// UPDATE THESE VALUES
$GLOBALS['COLOR_PICKER_FIELDNAMES'] = array(
  'color_picker'
);
$GLOBALS['PATH_TO_COLOR_PICKER_IMAGES'] = '/cms/plugins/colorPicker/images/';


// DON'T UPDATE ANYTHING BELOW THIS LINE

// get 3rd party js and css
require_once "colorPicker.js.php";
require_once "colorPicker.css.php";

// register hooks
addFilter('edit_buttonsRight', 'colorPicker_addJavascript', null, 3);

// add javascript to edit pages
function colorPicker_addJavascript($html, $tablename, $record) {
	colorPickerAddJavascript();
	colorPickerAddCSS();
  ?>
  <link href="/cms" rel="stylesheet" type="text/css" />
  <script>
    var colorPickerFieldnames = <?php print json_encode(array_values($GLOBALS['COLOR_PICKER_FIELDNAMES'])); ?>;

    $(function(){
			// for the specific fieldnames added above
      for (var i = 0; i < colorPickerFieldnames.length; i++) {
        $('input[name="' + colorPickerFieldnames[i] + '"]').miniColors();
      }
			// any field whose 'name' attribute starts 'color_picker_'
			$('input[name^="color_picker_"]').miniColors();
			// any field whose 'name' attribute ends '_color_picker'
			$('input[name$="_color_picker"]').miniColors();
			
    });
  </script>
  
  <?php

  return $html;
}

?>
