<?php

/**
 * @file
 * General functions and hook implementations.
 *
 * @see http://harvesthq.github.com/chosen/
 */

/**
 * Widget that Chosen can be applied to
 */
define('CHOSEN_WIDGET', 'options_select');

/**
 * Implements hook_init().
 */
function chosen_init() {
  drupal_add_css(libraries_get_path('chosen') . '/chosen/chosen.css');
  drupal_add_js(chosen_js_path());
  drupal_add_js(array(
    'chosen' => array(
      'selector' => variable_get('chosen_jquery_selector', 'select:visible'),
      'minimum' => variable_get('chosen_minimum', 20),
      'minimum_width' => variable_get('chosen_minimum_width', 200),
      'search_contains' => (variable_get('chosen_search_contains', FALSE)) ? TRUE : FALSE,
    ),
  ), 'setting');
  drupal_add_js(drupal_get_path('module', 'chosen') . '/chosen.js');
}

/**
 * Implement hook_menu()
 */
function chosen_menu() {
  $items = array();

  $items['admin/config/user-interface/chosen'] = array(
    'title' => 'Chosen',
    'description' => 'Configuration for Chosen plugin',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('chosen_admin_settings'),
    'access arguments' => array('administer site configuration'),
    'file' => 'chosen.admin.inc',
  );

  return $items;
}

/**
 * Implements hook_form_alter().
 */
function chosen_form_field_ui_field_edit_form_alter(&$form, $form_state) {
  $type = $form['instance']['widget']['type']['#value'];
  if ($type == CHOSEN_WIDGET) {
    $field = $form['#field'];
    $instance = field_info_instance($form['instance']['entity_type']['#value'], $form['instance']['field_name']['#value'], $form['instance']['bundle']['#value']);
    $form['instance'] += chosen_field_widget_settings_form($field, $instance);
  }
}

/**
 * Implements hook_field_widget_info_alter().
 *
 * A list of settings needed by Chosen module for widgets.
 */
function chosen_field_widget_info_alter(&$info) {
  $settings = array(
    'apply_chosen' => 0,
  );

  if (isset($info[CHOSEN_WIDGET]['settings'])) {
    $info[CHOSEN_WIDGET]['settings'] += $settings;
  }
}

function chosen_field_widget_settings_form($field, $instance) {
  $widget = $instance['widget'];
  $settings = $widget['settings'];

  $form['chosen'] = array(
    '#type' => 'fieldset',
    '#title' => t('Chosen'),
    '#collapsed' => TRUE,
    '#collapsible' => TRUE,
    '#parents' => array('instance', 'widget', 'settings'),
  );

  $form['chosen']['apply_chosen'] = array(
    '#type' => 'checkbox',
    '#title' => t('Apply Chosen on this field'),
    '#default_value' => $settings['apply_chosen'],
  );

  return $form;
}

/**
 * Implements hook_field_widget_form_alter().
 */
function chosen_field_widget_form_alter(&$element, &$form_state, $context) {
  $type = $context['instance']['widget']['type'];
  if ($type == CHOSEN_WIDGET) {
    $settings = $context['instance']['widget']['settings'];
    if ($settings['apply_chosen']) {
      $element['#attributes']['class'][] = 'chosen-widget';
    }
  }
}

/**
 * Returns the path to the Chosen JavaScript file.
 *
 * @return mixed
 *   The path to the JavaScript file or FALSE if no file could be found.
 */
function chosen_js_path() {
  $path = libraries_get_path('chosen');

  // Use the minified file if it exists to preserve bandwidth.
  if (file_exists($path . '/chosen/chosen.jquery.min.js')) {
    return $path . '/chosen/chosen.jquery.min.js';
  }
  elseif (file_exists($path . '/chosen/chosen.jquery.js')) {
    return $path . '/chosen/chosen.jquery.js';
  }

  return FALSE;
}
