<?php
// $Id$

/**
 *  Implementation of hook_mapi_layout()
 */
function mapi_layout_mapi_layout($op) {
  switch ($op) {
    case 'list':
      // some standard layouts
      $items = array(
        'generic' => '_mapi_layout_handler',
        'generic-left' => '_mapi_layout_handler',
        'generic-right' => '_mapi_layout_handler',

        // captioned
        'captioned' => '_mapi_layout_handler',
        'captioned-left' => '_mapi_layout_handler',
        'captioned-right' => '_mapi_layout_handler',

        // figured
        'figured' => '_mapi_layout_handler',
        'figured-left' => '_mapi_layout_handler',
        'figured-right' => '_mapi_layout_handler',
      );
      return $items;
      break;
  }
}

/**
 *  Use this function to generate a layout with a given set of details.
 */
function mapi_layout_generate($name, $details) {
  return mapi_layout_invoke('process', $name, $details);
}

function mapi_layout_list() {
  return mapi_layout_invoke('list', null, null);
}

/**
 *  Invokes hook_mapi_layout()
 */
function mapi_layout_invoke($op, $name, $details) {
  static $references;

  if (!isset($references)) {
    $references = module_invoke_all('mapi_layout', 'list');
    // check that list is made up of functions
    foreach ($references as $key => $func) {
      if (!function_exists($func)) {
        unset($references[$key]);
      }
    }
  }

  // calls the list of names
  switch ($op) {
    // list the available layouts
    case 'list':
      return array_keys($references);
    // process the layouts
    case 'process':
      if (array_key_exists($name, $references)) {
        $func = $references[$name];
        $result = $func($name, $details);
        return $result;
      }
      break;
  }
}

/**
 *  This handles the layouts that the mapi_layout module supports
 */
function _mapi_layout_handler($name, $details) {
  drupal_add_css(drupal_get_path('module', 'mapi_layout') .'/layout.css');
  switch ($name) {
    case 'generic':
      return theme('layout_generic', 'none', $details);
    case 'generic-left':
      return theme('layout_generic', 'left', $details);
    case 'generic-right':
      return theme('layout_generic', 'right', $details);
    case 'captioned':
      return theme('layout_captioned', 'none', $details);
    case 'captioned-left':
      return theme('layout_captioned', 'left', $details);
    case 'captioned-right':
      return theme('layout_captioned', 'right', $details);
    case 'figured':
      return theme('layout_figured', 'none', $details);
    case 'figured-left':
      return theme('layout_figured', 'left', $details);
    case 'figured-right':
      return theme('layout_figured', 'right', $details);
  }
}

/**
 *  Theming functions for various layouts
 */
function theme_layout_generic($float, $details) {
  $filename = mapi_context_display($details['filename']);

  $style = '';
  if ($float != 'none') {
    $dimensions = mapi_file_dimensions($filename);
    $width = (isset($settings['width']) ? $settings['width'] : (isset($dimensions['width']) ? $dimensions['width'] : 0));
    if ($width) {
      $style .= 'width:'. $width .'px;';
    }
  }

  $output = '<div class="layout-generic-'. $float .'"';
  if ($style) {
    $output .= ' style="'. $style .'"';
  }
  $output .= '>'. mapi_display($details['filename']) .'</div>';

  return $output;
}

function theme_layout_captioned($float, $details) {
  $filename = mapi_context_display($details['filename']);
  $settings = $details['settings'];

  $style = '';
  if ($float != 'none') {
    $dimensions = mapi_file_dimensions($filename);
    $width = (isset($settings['width']) ? $settings['width'] : (isset($dimensions['width']) ? $dimensions['width'] : 0));
    if ($width) {
      $style .= 'width:'. $width .'px;';
    }
  }

  $output = '<div class="layout-captioned-'. $float .'"';
  // allow floated boxes
  if ($style) {
    $output .= ' style="'. $style .'"';
  }
  $output .= '><div class="layout-item-display">';
  $output .= $details['content'];
  $output .= '</div><div class="layout-item-caption">';
  $output .= $details['caption'];
  $output .= '</div></div>';

  return $output;
}

function theme_layout_figured($float, $details) {
  static $figure = 1;

  $filename = mapi_context_display($details['filename']);
  $settings = $details['settings'];

  $style = '';
  if ($float != 'none') {
    $dimensions = mapi_file_dimensions($filename);
    $width = (isset($settings['width']) ? $settings['width'] : (isset($dimensions['width']) ? $dimensions['width'] : 0));
    if ($width) {
      $style .= 'width:'. $width .'px;';
    }
  }

  $output = '<div class="layout-figured-'. $float .'"';
  // allow floated boxes
  if ($style) {
    $output .= ' style="'. $style .'"';
  }
  $output .= '><div class="layout-figured-heading">';
  $output .= t('Figure !figure', array('!figure' => $figure));
  $output .= '</div><div class="layout-item-display">';
  $output .=  $details['content'];
  $output .= '</div><div class="layout-item-caption">';
  $output .= $details['caption'];
  $output .= '</div></div>';

  // allow for next one
  $figure += 1;

  return $output;
}