<?php
// $Id: jquery_media.module,v 1.6 2008/06/30 20:58:53 aaron Exp $

define('JQUERY_MEDIA_BGCOLOR_DEFAULT', '#ffffff');
define('JQUERY_MEDIA_AUTOPLAY_DEFAULT', 0);
define('JQUERY_MEDIA_FLVPLAYER_DEFAULT', 'mediaplayer.swf');
define('JQUERY_MEDIA_MP3PLAYER_DEFAULT', 'mediaplayer.swf');
define('JQUERY_MEDIA_CLASS_MEDIA_DEFAULT', 'a.media, .filefield-item a');
define('JQUERY_MEDIA_CLASS_MEDIABOX_DEFAULT', 'a.mediabox');
define('JQUERY_MEDIA_CLASS_MEDIA_AUTOINVOKE_DEFAULT', TRUE);
define('JQUERY_MEDIA_CLASS_MEDIABOX_AUTOINVOKE_DEFAULT', FALSE);
define('JQUERY_MEDIA_MEDIA_WIDTH_DEFAULT', '');
define('JQUERY_MEDIA_MEDIA_HEIGHT_DEFAULT', '');
define('JQUERY_MEDIA_MEDIABOX_WIDTH_DEFAULT', '');
define('JQUERY_MEDIA_MEDIABOX_HEIGHT_DEFAULT', '');
define('JQUERY_MEDIA_USE_DEFAULT_JS_FILE', FALSE);
define('JQUERY_MEDIA_DEFAULT_JS_FILEPATH', 'jquery_media.defaults.js');

/**
 *  Implement hook_jq (hook supplied by the jQ module)
 *
 *  The preferred method to invoke this plugin is to use the jQ module, available at http://drupal.org/project/jq
 *  and then call jq_add('jquery_media'); on the pages requiring this.
 *
 *  If you don't wish the jQ module installed, then use jquery_media_add() instead.
 */
function jquery_media_jq($op, $plugin = NULL, $options = array()) {
  include_once(drupal_get_path('module', 'jquery_media') .'/jquery_media.jq.inc');
  return _jquery_media_jq($op, $plugin, $options);
}

function jquery_media_menu($may_cache) {
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/jquery_media',
      'title' => t('jQuery Media'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('_jquery_media_settings'),
    );
    $items[] = array(
      'path' => JQUERY_MEDIA_DEFAULT_JS_FILEPATH,
      'type' => MENU_CALLBACK,
      'access' => user_access('access content'),
      'callback' => '_jquery_media_default_js_file',
    );
    return $items;
  }
}

/**
 *  Implements hook_help
 */
function jquery_media_help($section) {
  include_once(drupal_get_path('module', 'jquery_media') .'/jquery_media.settings.inc');
  return _jquery_media_help($section);
}

/**
 *  Callback for /admin/settings/jquery_media
 */
function _jquery_media_settings() {
  include_once(drupal_get_path('module', 'jquery_media') .'/jquery_media.settings.inc');
  if (!module_exists('jq')) {
    drupal_set_message(t('You may wish to install and enable the !jq for a more centralized administration and development of jQuery plugin User Interface modules such as jQuery Media.', array('!jq' => l(t('jQ Module'), 'http://drupal.org/project/jq', array('target' => '_blank')))));
  }
  else {
    drupal_set_message(t('You may wish to configure these settings at the !jq.', array('!jq' => l(t('jQ administrative settings page'), 'admin/settings/jq'))));
  }
  return system_settings_form(_jquery_media_settings_form());
}

/**
 *  Attempt to go through jQ first. Then add our files manually.
 */
function jquery_media_add($options = array()) {
  static $installed = FALSE;
  if (module_exists('jq')) {
    return module_invoke('jq', 'add', 'jquery_media');
  }
  if (!$installed) {
    drupal_add_js(drupal_get_path('module', 'jquery_media') .'/js/jquery.media.js');
  }
  include_once(drupal_get_path('module', 'jquery_media') .'/jquery_media.jq.inc');
  _jquery_media_add($options);
  return TRUE;
}

/**
 *  Print the default js file.
 */
function _jquery_media_default_js_file() {
  include_once(drupal_get_path('module', 'jquery_media') .'/jquery_media.jq.inc');
  $js = _jquery_media_add(array(), TRUE);
  header('Content-Length: '. strlen($js));
  header('Content-Type: text/javascript');
  header('Date: '.date('r'));
  print $js;
}

/**
 *  Invoke the plugin if we support it for this node type.
 */
function jquery_media_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if ($op == 'view') {
    if (in_array($node->type, variable_get('jquery_media_node_types', array()))) {
      jquery_media_add();
    }
  }
}

/**
 *  Invoke the plugin on any page listed from the configuration page.
 *  Code swiped from block_list
 */
function jquery_media_footer() {
  $path = drupal_get_path_alias($_GET['q']);
  $regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote(variable_get('jquery_media_pages', ''), '/')) .')$/';
  // Compare with the internal and path alias (if any).
  $page_match = preg_match($regexp, $path);
  if ($path != $_GET['q']) {
    $page_match = $page_match || preg_match($regexp, $_GET['q']);
  }
  if ($page_match) {
    jquery_media_add();
  }
}
