<?php
// $Id: jquery_ui.module,v 1.6.2.2 2009/06/21 03:43:03 sun Exp $

/**
 * @file
 * Provides the jQuery UI plug-in to other Drupal modules.
 *
 * This module doesn't do too much, but it is a central location for any other
 * modules that implement the JQuery UI library. It ensures that multiple
 * modules will all include the same library script just once on any given page.
 */

/**
 * Path to jQuery UI library.
 *
 * During site installation, JQUERY_UI_PATH is the absolute path to the
 * jQuery UI library.  At all other times JQUERY_UI_PATH is relative, and
 * safe for use in URLs.
 */
if (defined('MAINTENANCE_MODE') && MAINTENANCE_MODE == 'install') {
  define('JQUERY_UI_PATH', dirname(__FILE__) . '/jquery.ui');
}
else {
  define('JQUERY_UI_PATH', drupal_get_path('module', 'jquery_ui') . '/jquery.ui');
}

/**
 * Add the specified jQuery UI library files to this page.
 *
 * The ui.core file is always included automatically, as well as the
 * effects.core file if any of the effects libraries are used.
 *
 * @param $files
 *   An array of what additional files (other than UI core) should be loaded
 *   on the page, or a string with a single file name.
 */
function jquery_ui_add($files = array()) {
  static $loaded_files, $ui_core, $effects_core;
  $jquery_ui_path = JQUERY_UI_PATH . '/ui';
  $compression = variable_get('jquery_update_compression_type', 'mini');

  // Convert file to an array if it's not one already, to compensate for
  // lazy developers. ;)
  if (!is_array($files)) {
    $files = array($files);
  }

  // If core hasn't been added yet, add it.
  if (!isset($ui_core)) {
    $ui_core = TRUE;
    jquery_ui_add(array('ui.core'));
  }

  // Loop through list of files to include and add them to the page.
  foreach ($files as $file) {
    // Any effects files require the effects core file.
    if (!isset($effects_core) && strpos($file, 'effects.') === 0) {
      $effects_core = TRUE;
      jquery_ui_add(array('effects.core'));
    }

    // Load other files.
    if (!isset($loaded_files[$file])) {
      switch ($compression) {
        case 'none':
          $file_path = "$file.js";
          break;

        case 'pack':
          $file_path = "packed/$file.packed.js";
          break;

        case 'mini':
        default:
          $file_path = "minified/$file.min.js";
          break;
      }
      $js_path = $jquery_ui_path . '/' . $file_path;
      drupal_add_js($js_path);
      $loaded_files[$file] = $js_path;
    }
  }
}

/**
 * Return the version of jQuery UI installed.
 */
function jquery_ui_get_version() {
  $version = 0;

  if (file_exists(JQUERY_UI_PATH . '/version.txt')) {
    $version = file_get_contents(JQUERY_UI_PATH . '/version.txt');
  }

  return $version;
}

