<?php
// $Id$

// load library files
$path = drupal_get_path('module', 'mapi_video');
$files = drupal_system_listing('.*\.inc$', $path .'/library/', 'name', 0);
foreach ($files as $file) {
  require_once($file->filename);
}

/**
 *  Implementation of hook_mapi_extensions()
 */
function mapi_video_mapi_extensions($op, $name, &$data, $read) {
  $list = array(
    'flv' => 'application/x-shockwave-flash',
    'swf' => 'application/x-shockwave-flash',
    'wmv' => 'video/x-ms-wmv',
  );

  switch ($op) {
    case 'list':
      return array_keys($list);
    case 'mime':
      return $list[$name];
    case 'metadata':
    case 'dimensions':
      $details = _mapi_video_details($name);
      return $details[$op];
  }
}

/**
 *  Implementation of hook_mapi_types()
 */
function mapi_video_mapi_types($op, $type = null, $edit = null) {
  switch ($op) {
    // list of defined types for the module
    case 'list':
      return array('video');
      
    // list of extensions for the particular type
    case 'extensions':
      return array('flv', 'swf', 'wmv');
      
    // information regarding the type
    case 'info':
      return array(
        'video' => array(
          'title' => t('Video'),
          'description' => t('These are video files.'),
        ),
      );
      
    // provides the form details for the type
    case 'configure':
      break;
      
    // validates the form details from configure
    case 'validate':
      break;
    
    // allows intervention to convert form return values into something more appropriate for storage
    case 'save':
      break;
  }  
}

function _mapi_video_details($filename) {
  static $details;
  
  if (!isset($details)) {
    $details = array();
  }

  // used previously cached version  
  if (array_key_exists($filename, $details)) {
    return $details[$filename];
  }
  
  $ext = mapi_file_ext($filename);
  switch ($ext) {
    case 'flv':
      $results = _mapi_video_flv($filename);
      break;
    case 'swf':
      $results = _mapi_video_swf($filename);
      break;
  }

  if (isset($results) && is_array($results)) {
    $details[$filename] = $results;
  }

  // use it if it exists
  if (array_key_exists($filename, $details)) {
    return $details[$filename];
  }
   
  // otherwise it's not a valid extension
  return array('dimensions' => array(), 'metadata' => array());
}

