<?php
/*
 * @file
 * yui_button Module for Drupal 6.x
 *
 * This module allows Drupal to replace various form elements with YUI buttons.
 */

/*
 * Implementation of hook_menu()
 */
function yui_button_menu() {
  $items['admin/settings/yui_button'] = array(
    'title' => t('YUI button settings'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('yui_button_settings_form'),
    'access callback' => 'user_access',
    'access arguments' => array('administer site configuration'),
    'description' => t("View/Modify YUI button settings"),
  );

  return $items;
}

function yui_button_form_alter(&$form, $form_state, $form_id) {
  array_walk($form, '_yui_button_form_alter');
}

function _yui_button_form_alter($element, $id, $parentId = null) {
  $id = ($parentId != null ? $parentId . '-' : '') . $id;
  if (is_array($element)) {
    if (isset($element['#tree'])) {
      array_walk($element, '_yui_button_form_alter', $id);
    }
    else {
      array_walk($element, '_yui_button_form_alter');
    }
    switch ($element['#type']) {
      case 'button':
        render_button($id);
        break;
      case 'submit':
        render_button($id, 'submit');
        break;
      case 'checkbox':
        render_button($id, 'checkbox');
        break;
      case 'radio':
        render_button($id, 'radio');
        break;
      case 'radios':
        render_button($id, 'radios', $element);
        break;
      case 'select':
      case 'weight':
        render_button($id, 'menu');
        break;
    }
  }
}

/*
 * implementation of hook_perm().
 */
function yui_button_perm() {
  $array = array('Access YUI button');

  return $array;
}

/*
 *The settings page.
 */
function yui_button_settings_form() {
  $form = array();

  return system_settings_form($form);
}

/*
 * Add the javascript/CSS needed to render the button
 */
function render_button($id, $type = 'button', $element = NULL) {
  if (!user_access('Access YUI button')) {
    return;
  }
  
  static $radios;

  $yui_source = variable_get('yui_source','http://yui.yahooapis.com/2.5.2');
  yui_add_css('button', $yui_source, '/build/fonts/fonts-min.css');
  yui_add_css('button', $yui_source, '/build/button/assets/skins/sam/button.css');
  yui_add_css('button', $yui_source, '/build/button/assets/skins/sam/menu.css');
  yui_add_js('button', $yui_source, '/build/yahoo-dom-event/yahoo-dom-event.js');
  yui_add_js('button', $yui_source, '/build/container/container_core-min.js');
  yui_add_js('button', $yui_source, '/build/menu/menu-min.js');
  yui_add_js('button', $yui_source, '/build/element/element-beta-min.js');
  yui_add_js('button', $yui_source, '/build/button/button-min.js');
  
  if ($type == 'menu') {
    drupal_add_js("
      YAHOO.util.Event.onContentReady('edit-$id', function() {
        var menu = document.createElement('input');
        var menuID = YAHOO.util.Dom.generateId(menu, 'edit-${id}-menu');
        YAHOO.util.Dom.insertBefore(menu, 'edit-$id');

        var options = YAHOO.util.Dom.get('edit-$id').options;
        for (option in options) {
          var string = options[option].text + '';
          options[option].text = string.replace(/>/, '-').replace(/</, '-').replace(/&gt;/, '-').replace(/&lt;/, '-');
        }
        var selectedValue = YAHOO.util.Dom.get('edit-$id').options[YAHOO.util.Dom.get('edit-$id').selectedIndex].value;
        var selectedText = YAHOO.util.Dom.get('edit-$id').options[YAHOO.util.Dom.get('edit-$id').selectedIndex].text;

        var button = new YAHOO.widget.Button(menuID, { label : selectedText, value : selectedValue, type : '$type'" . ($type == 'menu' ? ", menu : 'edit-$id'" : '') . " });
        var oMenu = YAHOO.widget.MenuManager.getMenu(button._menu.id);
        oMenu.subscribe('itemAdded', function (p_sType, p_aArgs) {
          var oMenuItem = p_aArgs[0];
	      oMenuItem.subscribe('click', function() {
	        button.set('label', this.cfg.getProperty('text'));
	      });
        });
      });", "inline", "footer");
  }
  else if ($type == 'radios') {
    if ($radios == NULL) {
      $radios = 0;
      drupal_add_js("
        YAHOO.util.Event.onDOMReady(function() {
          buttons = [];
          var id = 0;
          var radioGroups = YAHOO.util.Dom.getElementsByClassName('form-radios');
          for (var radioGroup in radioGroups) {
            var children = YAHOO.util.Dom.getChildren(radioGroups[radioGroup]);
            for (var radio in children) {
              YAHOO.util.Dom.setStyle(children[radio], 'display', 'none');
            }
            var radioGroupID = 'form-radios-' + id++;
            radioGroups[radioGroup].setAttribute('id', radioGroupID);
            var button = new YAHOO.widget.ButtonGroup(radioGroupID);
            buttons[radioGroupID] = button;
          }
        });", "inline", "footer");
    }
    $count = 0;
    foreach ($element['#options'] as $value => $label) {
      $labels .= "buttons['form-radios-' + $radios].getButton(" . $count++ . ").set('label', '$label');\n";
    }
    $radios++;
    drupal_add_js("
      YAHOO.util.Event.onDOMReady(function() {
        $labels;
      });", "inline", "footer");
  }
  else if ($type == 'checkbox') {
    drupal_add_js("
      YAHOO.util.Event.onContentReady('edit-$id', function() {
        var button = new YAHOO.widget.Button('edit-${id}', { label : YAHOO.util.Dom.get('edit-${id}').nextSibling.nodeValue, type : '$type' });
        YAHOO.util.Dom.get('edit-${id}').nextSibling.nodeValue = null;
      });", "inline", "footer");
  }
  else {
    drupal_add_js("
      YAHOO.util.Event.onContentReady('edit-$id', function() {
        var button = new YAHOO.widget.Button('edit-${id}', { type : '$type' });
      });", "inline", "footer");
  }
}