<?php
/**
 * @file
 * Views Slideshow: RoyalSlider module.
 */

/**
 * Implements hook_theme().
 */
function views_slideshow_royalslider_theme() {
  return array(
    'views_slideshow_royalslider_main_frame' => array(
      'variables' => array('vss_id' => NULL, 'view' => NULL, 'settings' => NULL, 'rows' => NULL),
      'file' => 'views_slideshow_royalslider.theme.inc',
      ),
    );
}

/**
 * Implements hook_views_prerender().
 */
function views_slideshow_royalslider_views_pre_render(&$view) {
  $display = $view->display[$view->current_display];
  if ($view->style_plugin->plugin_name == 'slideshow' && $view->style_options['slideshow_type'] == 'views_slideshow_royalslider') {
    // Get the caption field.
    $caption_field = $view->style_options['views_slideshow_royalslider']['caption'];
    if ($caption_field) {
      // Add the rsCaption class.
      $field = $display->handler->handlers['field'][$caption_field];
      $field->options['element_class'] .= (strlen($field->options['element_class']) > 0) ? ' rsCaption' : 'rsCaption';
    }

    $main_image_field = $view->style_options['views_slideshow_royalslider']['main_image'];
    if ($main_image_field) {
      // @TODO: Check if there's a cleaner way to get the field name OR change the render array.
      $view_field_name = 'field_' . $main_image_field;
      foreach ($view->result as $i => $result) {
        $image_render_field = current($result->$view_field_name);

        // Add the rsImg class to the image field.
        $original_attributes = array();
        if (array_key_exists('attributes', $image_render_field['rendered']['#item'])) {
          $original_attributes = $image_render_field['rendered']['#item']['attributes'];
        }
        $slider_class = array('class' => array('rsImg'));
        $new_attributes = array_merge($original_attributes, $slider_class);
        $image_render_field['rendered']['#item']['attributes'] = $new_attributes;

        // Convert the image to a preloaded one if preloading is enabled.
        $optionset_name = $view->style_options['views_slideshow_royalslider']['optionset'];
        $optionset = royalslider_optionset_load($optionset_name);
        if ($optionset->options['usePreloader'] == TRUE) {
          // @TODO Is there a cleaner way to get the image style into the view to use in theme_views_slideshow_royalslider_main_frame() ?
          $image_render_field['rendered'] = array(
            '#theme' => 'link',
            '#path' => file_create_url($image_render_field['raw']['uri']),
            '#text' => $image_render_field['raw']['alt'],
            '#options' => array(
              'attributes' => $new_attributes,
              'html' => FALSE,
            ),
            '#image_style' => $image_render_field['rendered']['#image_style'],
          );
        }
        $view->result[$i]->$view_field_name = array($image_render_field);
      }
    }

    // @TODO: figure out how to wrap the loaded image in a link again (to enable clickable slides linking to other content).
  }
}