<?php
function jqslideshow_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('jquery slideshow for image galleries. <em>requires image.module and image_gallery.module</em>');
  }
}

function jqslideshow_menu($may_cache) {
  $items = array();

  if ($may_cache) {
    $items[] = array('path' => 'slideshow', 'title' => t('slideshow'),
                     'access' => user_access('access content'),
                     'type' => MENU_SUGGESTED_ITEM,
                     'callback' => 'jqslideshow_page');
  }    

  return $items;
}

function jqslideshow_page($type = NULL, $tid = 0) {
  $galleries = taxonomy_get_tree(_image_gallery_get_vid(), $tid, -1, 1);
  for ($i=0; $i < count($galleries); $i++) {
    $galleries[$i]->count = taxonomy_term_count_nodes($galleries[$i]->tid, 'image');
    $tree = taxonomy_get_tree(_image_gallery_get_vid(), $galleries[$i]->tid, -1);
    $descendant_tids = array_merge(array($galleries[$i]->tid), array_map('_taxonomy_get_tid_from_term', $tree));
    $last = db_fetch_object(db_query_range(db_rewrite_sql('SELECT n.nid FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN (%s) AND n.status = 1 ORDER BY n.sticky DESC, n.created DESC'), implode(',', $descendant_tids), 0, 1));
    $galleries[$i]->latest = node_load(array('nid' => $last->nid));
  }

  $images = array();
  if ($tid) {
    $result = pager_query(db_rewrite_sql("SELECT n.nid FROM {term_node} t INNER JOIN {node} n ON t.nid=n.nid WHERE n.status=1 AND n.type='image' AND t.tid=%d ORDER BY n.sticky DESC, n.created DESC"), variable_get('image_images_per_page', 6), 0, NULL, $tid);
    while ($node = db_fetch_object($result)) {
      $images[] = node_load(array('nid' => $node->nid));
    }

    $gallery = taxonomy_get_term($tid);
    $parents = taxonomy_get_parents($tid);
    foreach ($parents as $parent) {
      $breadcrumb[] = array('path' => 'image/tid/'. $parent->tid, 'title' => $parent->name);
    }
    $breadcrumb[] = array('path' => 'image', 'title' => t('Image galleries'));
    $breadcrumb = array_reverse($breadcrumb);
    drupal_set_title($gallery->name);
  }

  $breadcrumb[] = array('path' => $_GET['q']);
  menu_set_location($breadcrumb);
  $content = theme('jqslideshow', $galleries, $images);
  return $content;
}

function theme_jqslideshow($galleries, $images) {
  drupal_set_html_head(theme('stylesheet_import', base_path() . drupal_get_path('module', 'image_gallery') .'/image_gallery.css'));

  $links = array();
  if (count($images)) {
    foreach ($images as $image) {
      $links[] = l(image_display($image, 'thumbnail'), 
        file_create_url($image->images['preview']), array(), NULL, NULL, FALSE, TRUE);
    }
  }

  jquery47_add_js(drupal_get_path('module','jqslideshow').'/jq47.slideshow.js');
  
  $script = '<script type="text/javascript">'
          . 'JQ(document).ready(function(){JQ(".jqslideshow").slideshow();});'
          . '</script>';
  
  return '<div class="jqslideshow">'.theme('item_list', $links).'</div>'.$script;
}
