<?php

/**
 * @file
 *  Adds the Related plugin to JW player.
 *  More info at http://www.longtailvideo.com/addons/plugins/275/Related-Videos
 *
 * @see http://drupal.org/node/1338964 - implementing plugins
 */

/**
 * Implements hook_jw_player_plugin_info().
 *
 * Register a jw_player preset plugin.
 */
function jw_player_related_jw_player_plugin_info() {
  $plugins['related-1'] = array(
    'name' => t('Related'),
    'description' => t('Related plugin for HTML5 JW player'),
    'config options' => array(
      'file' => array(
        '#type' => 'textfield',
        '#required' => TRUE,
        '#size' => 128,
        '#default_value' => '',
        '#element_validate' => array('token_element_validate'),
        '#after_build' => array('token_element_validate'),
        '#token_types' => array('node'),
        '#min_tokens' => 0,
        '#description' => t('Location of the mRSS file with related videos. This field supports the use of tokens. e.g. http://example.com/related.xml or http://example.com/related_videos/[node:nid].')
        ),
      'onclick' => array(
        '#type' => 'select',
        '#options' => array('link' => 'link', 'play' => 'play'),
        '#default_value' => 'link',
        '#description' => t('What to do when the user clicks a thumb: play the related video in the player (play) or jump to the play page of the related video (link).')
        ),
      'oncomplete' => array(
        '#type' => 'select',
        '#options' => array('TRUE' => 'TRUE', 'FALSE' => 'FALSE'),
        '#default_value' => 'TRUE',
        '#description' => t('Whether to display the related videos screen when the video is completed. When set false, the screen does not automatically pop up.')
        ),
      'usedock' => array(
        '#type' => 'select',
        '#options' => array('TRUE' => 'TRUE', 'FALSE' => 'FALSE'),
        '#default_value' => 'TRUE',
        '#description' => t('Whether to add a button to the player to pop up the related videos screen manually. When set false, this button is omitted.')
        ),
      'heading' => array(
        '#type' => 'textfield',
        '#required' => TRUE,
        '#size' => 128,
        '#default_value' => 'watch related videos',
        '#description' => t('Single line heading displayed above the grid with related videos. Generally contains a short call-toaction.')
        ),
      'dimensions' => array(
        '#type' => 'textfield',
        '#required' => TRUE,
        '#size' => 30,
        '#default_value' => '140x80',
        '#description' => t('The width and height of each thumbnail in pixels. Set this to a different value to make the thumbnails larger (e.g. 160x90) or smaller (e.g. 120x70). Note the plugin always renders a fixed 10px spacing between the thumbs.')
        ),
      'token_help' => array(
        '#title' => t('Replacement patterns'),
        '#type' => 'fieldset',
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        'help' => array(
          '#theme' => 'token_tree',
          '#token_types' => array('node')
        ),
      ),
    ),
  );

  return $plugins;
}

/**
 * Implements template_preprocess_jw_player().
 *
 * Used to replace any tokens in the file field with the correct value
 */
function jw_player_related_preprocess_jw_player(&$variables) {
  if (isset($variables['plugins']['related-1']['enable']) && $variables['plugins']['related-1']['enable'] == 1) {
    // Attempt to replace any tokens
    $data = array('node' => menu_get_object('node'));
    $options = array('clear' => TRUE);
    $variables['config']['plugins']['related-1']['file'] = token_replace($variables['config']['plugins']['related-1']['file'], $data, $options);
  }
}
