/**
 * Generate the HTML output for imagefield + imagecache images so they can be
 * opened in a lightbox by clicking on the image on the node page or in a view.
 *
 * @param $view_preset
 *   The imagecache preset to be displayed on the node or in the view.
 * @param $field
 *   The field the action is being performed on.
 * @param $item
 *   An array, keyed by column, of the data stored for this item in this field.
 * @param $node
 *   The node object.
 * @param $slideshow
 *   Whether or not a slideshow should be invoked, rather than a normal
 *   lightbox.
 */
function phptemplate_imagefield_image_imagecache_lightbox2($view_preset, $field, $item, $node, $slideshow = FALSE) {

  $rel = 'lightbox';
  if ($slideshow) {
    $rel = 'lightshow';
  }
  $imagefield_grouping = variable_get('lightbox2_imagefield_group_node_id', 1);
  if ($imagefield_grouping == 1) {
    $rel = $rel .'['. $field['field_name'] .']';
  }
  else if ($imagefield_grouping == 2 && !empty($item['nid'])) {
    $rel = $rel .'['. $item['nid'] .']';
  }
  else if ($imagefield_grouping == 3 && !empty($item['nid'])) {
    $rel = $rel .'['. $field['field_name'] . $item['nid'] .']';
  }

  $node_link = '';
  $attributes = array();
  if (!empty($item['nid'])) {
    $node = node_load($node->nid);
    $node_body = check_markup($node->teaser, $node->format);
    $target = variable_get('lightbox2_node_link_target', FALSE);
    if (!empty($target)) {
      $attributes = array('target' => $target);
    }
    $node_link_text = variable_get('lightbox2_node_link_text', 'View Image Details');
    if (!empty($node_link_text)) {
      $node_link .= '<br /><br />'. l($node_link_text, 'node/'. $item['nid'], $attributes);
    }
    if (!empty($node_body)) {
      $node_link .= '<br /><br />'. $node_body;
    }
  }

  $link_attributes = array(
    'rel' => $rel,
    'title' => $item['title'] . $node_link);

  if ($view_preset == 'original') {
    $image = theme('lightbox2_image', $item['filepath'], $item['alt'], $item['title'], $attributes);
  }
  else {
    $image = theme('imagecache', $view_preset, $item['filepath'], $item['alt'], $item['title'], $attributes);
  }
  if ($item['lightbox_preset'] == 'original') {
    $output = l($image, file_create_url($item['filepath']), $link_attributes, NULL, NULL, FALSE, TRUE);
  }
  else {
    $output = l($image, lightbox2_imagecache_create_url($item['lightbox_preset'], $item['filepath']), $link_attributes, NULL, NULL, FALSE, TRUE);
  }

  return $output;
}
