<?php
// $Id$

/**
 *  This allows for the generation of a slideshow.
 */
function mapi_slideshow_generate($reference, $layout, $items, $settings) {
  // safety checks on the incoming information
  $reference = 'slideshow-'. preg_replace('/[^\w\d\-]+/', '-', $reference);
  $items = (!is_array($items) ? array() : $items);
  $settings = (!is_array($settings) ? array() : $settings);

  // rebuilt item array to have simply key indexs
  $list = array();

  // switch filename for the display characteristics
  foreach (array_values($items) as $value) {
    // convert filename into content
    if (isset($value['filename'])) {
      $value['content'] = mapi_display($value['filename']);
    }
    // possible that other kinds of content are specified
    if (isset($value['content'])) {
      // provide standard details
      $meta = array();
      foreach ($value as $k => $v) {
        $meta[$k] = filter_xss_admin($v);
      }
      $meta['filename'] = $value['filename'];
      $meta['content']  = $value['content'];


      // run it through the layout generator
      $list[] = mapi_layout_generate($layout, $meta);
    }
  }

  // figure out the current reference in the slideshow (should always exist)
  $current = (isset($_GET[$reference]) && array_key_exists($_GET[$reference], $list)) ? $_GET[$reference] : 0;

  if (count($list)) {
    return theme('mapi_slideshow', $reference, $list, $current, $settings);
  }
}

function theme_mapi_slideshow($reference, $items, $current, $settings) {
  $indices = array_keys($items);
  $total = count($items);

  $float = '';
  $style = '';
  if (isset($settings['width'])) {
    $style .= 'width:'. $settings['width'] .'px;';
  }
  if (isset($settings['float'])) {
    $float = ' mapi-slideshow-'. $settings['float'];
    $style .= 'float:'. $settings['float'] .';clear:both;';
  }

  $output = '<div class="mapi-slideshow'. $float .'" id="'. $reference .'"';
  if ($style) {
    $output .= ' style="'. $style .'"';
  }
  $output .= '>';

  // movement band
  $output .= '<div class="mapi-slideshow-prev">';
  $output .= l('<<', $_GET['q'], array('class' => 'mapi-slideshow-prev-link '. $reference), $reference .'='. ($current ? ($current-1) : ($total-1)));
  $output .= '</div>';
  $output .= '<div class="mapi-slideshow-next">';
  $output .= l('>>', $_GET['q'], array('class' => 'mapi-slideshow-next-link '. $reference), $reference .'='. ($current+1 < $total ? ($current+1) : 0));
  $output .= '</div>';
  $output .= '<div class="mapi-slideshow-listing">';
  $output .= t('<span class="mapi-slideshow-current">!current</span> of !amount', array('!current' => $current + 1, '!amount' => count($indices)));
  $output .= '</div>';

  // content section
  $output .= '<div class="mapi-slideshow-content '. $reference .'">';
  $output .= $items[$current];
  $output .= '</div>';

  $output .= '</div>';

  $path = drupal_get_path('module', 'mapi_slideshow');
  drupal_add_js($path .'/slideshow.js');
  drupal_add_css($path .'/slideshow.css');
  drupal_add_js(array('mapi_slideshow' => array($reference => array('items' => $items, 'current' => $current, 'total' => count($items)))), 'setting');

  return $output;
}
