<?php
// $Id: audio_theme.inc,v 1.5 2007/02/20 18:35:26 drewish Exp $

/**
 * Format the teaser for an audio node.
 */
function theme_audio_teaser($node){
  // make sure that all the allowed tags are included.
  foreach (audio_get_tags_allowed() as $tag) {
    $params['!'. $tag] = isset($node->audio_tags[$tag]) ? check_plain($node->audio_tags[$tag]) : '';
  }
  $params['!filelength'] = theme('audio_format_filelength', $node->audio_fileinfo);
  $params['!fileformat'] = theme('audio_format_fileformat', $node->audio_fileinfo);
  $params['!player'] = audio_get_node_player($node);
  $params['!play_count'] = check_plain($node->audio_fileinfo['play_count']);
  $params['!download_count'] = check_plain($node->audio_fileinfo['download_count']);

  $format = variable_get('audio_teaser_format', '!player<br />!filelength');

  return t($format, $params);
}

/**
 * Format the audio node for display
 */
function theme_audio_display($node) {
  $items = array();
  if ($player = audio_get_node_player($node)) {
    $items[] = $player;
  }
  foreach (audio_get_tag_settings() as $tag => $setting) {
    if (!$setting['hidden'] && $node->audio_tags[$tag]) {
      $items[] = '<strong>'. t(ucwords(str_replace('_', ' ', $tag))). ':</strong> '
        . theme('audio_format_tag', $tag, $node->audio_tags[$tag], $setting);
    }
  }
  $items[] = '<strong>'. t('Length') .':</strong> '. theme('audio_format_filelength', $node->audio_fileinfo);
  $items[] = '<strong>'. t('Format') .':</strong> '. theme('audio_format_fileformat', $node->audio_fileinfo);

  $output = "<div class='audio-node block'>\n";
  // give audio_image.module (or a theme) a chance to display the images.
  $output .= theme('audio_images', $node->audio_images);
  //$output .= theme('item_list', $items, NULL, 'ul', array('class' => 'audio-info'));
  $output .= theme('audio_item_list', $items, NULL, 'ul', array('class' => 'audio-info')); //this wil solve the shift problems with rtl
  $output .= "</div>\n";

  return $output ;
}

/**
 * Format a metadata tag.
 */
function theme_audio_format_tag($tag, $value, $setting) {
  if ($setting['browsable']) {
    return l($value, "audio/by/$tag/". audio_clean_tag($value));
  }
  else {
    switch ($tag) {
      // url tags
      case 'url_file':
      case 'url_artist':
      case 'url_source':
      case 'url_station':
        return '<a href="'. check_url($value) .'" title="'. $tag .'">'. check_plain($value) .'</a>';

      default:
        return check_plain($value);
    }
  }
}

/**
 * Return a string describing the node's size and length.
 *
 * @param $audio_fileinfo
 *   node's fileinfo array
 */
function theme_audio_format_filelength($audio_fileinfo) {
  $output = t('!playtime minutes (!filesize)',
    array(
      '!playtime' => $audio_fileinfo['playtime'],
      '!filesize' => format_size($audio_fileinfo['filesize']),
    )
  );
  return check_plain($output);
}

/**
 * Return a string describing the node's file information.
 *
 * @param $audio_fileinfo
 *   node's fileinfo array
 */
function theme_audio_format_fileformat($audio_fileinfo) {
  $output = t('!fileformat !channel_mode !sampleratekHz !bitrateKbps (!bitrate_mode)',
    array(
      '!fileformat' => strtoupper($audio_fileinfo['fileformat']),
      '!samplerate' => (integer) ($audio_fileinfo['sample_rate'] / 1000),
      '!channel_mode' => ucfirst($audio_fileinfo['channel_mode']),
      '!bitrate' => (integer) ($audio_fileinfo['bitrate'] / 1000),
      '!bitrate_mode' => strtoupper($audio_fileinfo['bitrate_mode']),
    )
  );
  return check_plain($output);
}


/* rtl problems in firefox (ul li with object is floated right
   forced me to override this list and display it as a divs */
function theme_audio_item_list($items = array(), $title = NULL, $type = 'ul', $attributes = NULL) {
  $output = '<div class="item-list">';
  if (isset($title)) {
    $output .= '<h3>'. $title .'</h3>';
  }
  $type = 'div'; 
  if (!empty($items)) {
    $output .= "<$type" . drupal_attributes($attributes) . '>';
    foreach ($items as $item) {
      $attributes = array();
      $children = array();
      if (is_array($item)) {
        foreach ($item as $key => $value) {
          if ($key == 'data') {
            $data = $value;
          }
          elseif ($key == 'children') {
            $children = $value;
          }
          else {
            $attributes[$key] = $value;
          }
        }
      }
      else {
        $data = $item;
      }
      if (count($children) > 0) {
        $data .= theme_item_list($children, NULL, $type, $attributes); // Render nested list
      }
      $output .= '<div' . drupal_attributes($attributes) . '>'. $data .'</div>';
    }
    $output .= "</$type>";
  }
  $output .= '</div>';
  return $output;
}
