<?php
//$Id: imagefield_gallery_lightbox2.module,v 1.7 2008/05/04 16:09:20 eclipsegc Exp $

/**
 * implementation of hook_enable()
 */

function imagefield_gallery_scrollbox_enable() {
  imagefield_gallery_scrollbox_settings();
}

/**
 * Implementation of imagefield_gallery_hook_settings()
 * function checks to see if the proper key existings
 * in the imagefield_gallery_galleries variable, and
 * if it does, does nothing, and if it doesn't it adds
 * the proper settings to the variable.
 */

function imagefield_gallery_scrollbox_settings() {
  $settings = variable_get('imagefield_gallery_galleries', array());
  if (!array_key_exists('scrollbox', $settings)) {
    $settings['scrollbox'] = t('Scrollbox Gallery');
    variable_set('imagefield_gallery_galleries', $settings);
  }
}

/**
 * imagefield_gallery_hook_details is meant to keep track of
 * all of our stylistic variables for a given gallery type
 * these variables then need to be rendered in an acceptable
 * fashion for display on the content types admin page
 */

function imagefield_gallery_scrollbox_details($type) {
  $output = '';
  $styles = variable_get('imagefield_gallery_scrollbox_css', array());
  $headers = array(t('Attribute'), t('Value'));
  $rows = array();
  $rows[] = array(t('Thumbnail Width'), $styles[$type]['width'] .'px');
  $rows[] = array(t('Thumbnail Height'), $styles[$type]['height'] .'px');
  $rows[] = array(t('Thumbnail Top Margin'), $styles[$type]['margins']['top'] .'px');
  $rows[] = array(t('Thumbnail Right Margin'), $styles[$type]['margins']['right'] .'px');
  $rows[] = array(t('Thumbnail Bottom Margin'), $styles[$type]['margins']['bottom'] .'px');
  $rows[] = array(t('Thumbnail Left Margin'), $styles[$type]['margins']['left'] .'px');
  $rows[] = array(t('Thumbnail Borders'), $styles[$type]['border']['border_width'] .' '. $styles[$type]['border']['border_style'] .' '. $styles[$type]['border']['color_style_textfield']);
  
  $output .= theme('table', $headers, $rows);
  return $output;
}

/**
 * implementation of imagefield_gallery_hook_content_types()
 */

function imagefield_gallery_scrollbox_content_types($form = array(), $var = array(), $type, $cache = array()) {
  drupal_add_css(drupal_get_path('module', 'imagefield_gallery_scrollbox') .'/css/content_types.css');

  $form[$type] = array(
    '#type' => 'fieldset',
    '#title' => t('@type settings', array('@type' => $type)),
    '#collapsible' => FALSE,
    '#description' => t('Please select the gallery style you would like for the @type content type.', array('@type' => $type)),
    '#tree' => TRUE,
    '#attributes' => array('class' => 'scrollbox_fieldset'),
  );
  $form[$type]['gallery'] = array(
    '#type' => 'select',
    '#title' => t('@type gallery type', array('@type' => $type)),
    '#default_value' => isset($var[$type]['gallery']) ? $var[$type]['gallery'] : 'none',
    '#options' => array(
      imagefield_gallery_get_gallery_types(),
    ),
    '#tree' => TRUE,
  );
  $form[$type]['images'] = array(
    '#type' => 'fieldset',
    '#title' => t('@type image settings', array('@type' => $type)),
    '#collapsible' => FALSE,
    '#description' => t('Select the imagecache settings you would like to use (if any) for the @type content type', array('@type' => $type)),
    '#tree' => TRUE,
  );
  $form[$type]['images']['thumbnail'] = array(
    '#type' => 'select',
    '#title' => t('@type thumbnail', array('@type' => $type)),
    '#default_value' => isset($var[$type]['images']['thumbnail']) ? $var[$type]['images']['thumbnail'] : 'none',
    '#options' => $cache,
    '#tree' => TRUE,
  );
  $form[$type]['images']['preview'] = array(
    '#type' => 'select',
    '#title' => t('@type preview', array('@type' => $type)),
    '#default_value' => isset($var[$type]['images']['preview']) ? $var[$type]['images']['preview'] : 'none',
    '#options' => $cache,
    '#tree' => TRUE,
  );
  return $form;
}

/**
 * implementation of imagefield_gallery_hook_content_types_validate()
 */

function imagefield_gallery_scrollbox_content_types_validate($form_id, $form_values, $form) {
  $types = imagefield_gallery_nodetypes();
  foreach ($types as $type) {
    if (is_array($form_values[$type['type']]) && $form_values[$type['type']]['gallery'] != 'none') {
      if ($form_values[$type['type']]['images']['thumbnail'] == 'none' || $form_values[$type['type']]['images']['preview'] == 'none') {
        form_set_error('', t('You must select an imagecache setting for the @type content type!', array('@type' => $type['type'])));
      }
    }
  }
}

/**
 * implementation of imagefield_gallery_hook_admin_validate()
 */

function imagefield_gallery_scrollbox_admin_validate($form_id, $form_values) {
  $types = imagefield_gallery_nodetypes();
  foreach ($types as $type) {
    if (!empty($form_values['scrollbox'][$type['type']]['width'])) {
      if (!is_numeric($form_values['scrollbox'][$type['type']]['width'])) {
        form_set_error('', t('The @type width value must be a number!', array('@type' => $type['type'])));
      }
    }
    if (!empty($form_values['scrollbox'][$type['type']]['height'])) {
      if (!is_numeric($form_values['scrollbox'][$type['type']]['height'])) {
        form_set_error('', t('The @type height value must be a number!', array('@type' => $type['type'])));
      }
    }
    if (!empty($form_values['scrollbox'][$type['type']]['margins']['top'])) {
      if (!is_numeric($form_values['scrollbox'][$type['type']]['margins']['top'])) {
        form_set_error('', t('The @type top margin value must be a number!', array('@type' => $type['type'])));
      }
    }
    if (!empty($form_values['scrollbox'][$type['type']]['margins']['right'])) {
      if (!is_numeric($form_values['scrollbox'][$type['type']]['margins']['right'])) {
        form_set_error('', t('The @type right margin value must be a number!', array('@type' => $type['type'])));
      }
    }
    if (!empty($form_values['scrollbox'][$type['type']]['margins']['bottom'])) {
      if (!is_numeric($form_values['scrollbox'][$type['type']]['margins']['bottom'])) {
        form_set_error('', t('The @type bottom margin value must be a number!', array('@type' => $type['type'])));
      }
    }
    if (!empty($form_values['scrollbox'][$type['type']]['margins']['left'])) {
      if (!is_numeric($form_values['scrollbox'][$type['type']]['margins']['left'])) {
        form_set_error('', t('The @type left margin value must be a number!', array('@type' => $type['type'])));
      }
    }
    if (!empty($form_values['scrollbox'][$type['type']]['border']['color_style_textfield'])) {
      if (!preg_match('/^#[0-9A-Fa-f]{6}$/', $form_values['scrollbox'][$type['type']]['border']['color_style_textfield'])) {
        form_set_error('', t('The @type border color value must be a hexidecimal number!', array('@type' => $type['type'])));
      }
    }
  }
}

/**
 * implementation of imagefield_gallery_hook_admin()
 * @param $type
 *   the node type so that the validate and submit functions
 *   can operate more easily.  '#tree' => TRUE is highly
 *   recommended in this case to eliminate potential
 *   conflicts in naming convention.
 *
 * this entire form function will be used to help create
 * the css for this gallery type.  the imagefield_gallery_lightbox2_css
 * variable stores all the css for this gallery type by node type.
 * this function is a fairly good example of what you should be
 * striving for in creating your own admin pages.
 *
 * also, you'll noted that the primary form elements are all contained
 * within $form['lightbox2'].  It is imperative that you also name a
 * containing form element with the same naming convention as your module.
 * 
 */

function imagefield_gallery_scrollbox_admin($type) {
  drupal_add_css(drupal_get_path('module', 'imagefield_gallery_scrollbox') .'/css/admin.css');
  $var = variable_get('imagefield_gallery_scrollbox_css', array());
  $form['variable'] = array(
    '#type' => 'value',
    '#value' => 'scrollbox',
  );
  $form['scrollbox'] = array(
    '#type' => 'fieldset',
    '#title' => t('Lightbox2 @type Attributes', array('@type' => $type)),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => TRUE,
    '#attributes' => array('class' => 'scrollbox_admin_fieldset'),
  );
  $form['scrollbox'][$type] = array(
    '#type' => 'fieldset',
    '#title' => t('Thumbnail Attributes'),
    '#collapsible' => FALSE,
    '#description' => t('Set the width and height, in pixels, you desire your thumbnails to be.'),
    '#tree' => TRUE,
    '#attributes' => array('class' => 'scrollbox_thumbnail_fieldset'),
  );
  $form['scrollbox'][$type]['width'] = array(
    '#type' => 'textfield',
    '#title' => t('Thumbnail Width'),
    '#size' => 10,
    '#maxlength' => 4,
    '#default_value' => isset($var[$type]['width']) ? $var[$type]['width'] : 100,
    '#required' => TRUE,
    '#tree' => TRUE,
  );
  $form['scrollbox'][$type]['height'] = array(
    '#type' => 'textfield',
    '#title' => t('Thumbnail Height'),
    '#size' => 10,
    '#maxlength' => 4,
    '#default_value' => isset($var[$type]['height']) ? $var[$type]['height'] : 100,
    '#required' => TRUE,
    '#tree' => TRUE,
  );
  $form['scrollbox'][$type]['margins'] = array(
    '#prefix' => '<div style="clear:both"></div>',
    '#type' => 'fieldset',
    '#title' => t('Margin Attributes'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => TRUE,
    '#attributes' => array('class' => 'scrollbox_margin_fieldset'),
  );
  $form['scrollbox'][$type]['margins']['top'] = array(
    '#type' => 'textfield',
    '#title' => t('Top Margin'),
    '#default_value' => isset($var[$type]['margins']['top']) ? $var[$type]['margins']['top'] : 10,
    '#size' => '5',
    '#maxlength' => 3,
    '#required' => TRUE,
    '#tree' => TRUE,
  );
  $form['scrollbox'][$type]['margins']['right'] = array(
    '#type' => 'textfield',
    '#title' => t('Right Margin'),
    '#default_value' => isset($var[$type]['margins']['right']) ? $var[$type]['margins']['right'] : 10,
    '#size' => '5',
    '#maxlength' => 3,
    '#required' => TRUE,
    '#tree' => TRUE,
  );
  $form['scrollbox'][$type]['margins']['bottom'] = array(
    '#type' => 'textfield',
    '#title' => t('Bottom Margin'),
    '#default_value' => isset($var[$type]['margins']['bottom']) ? $var[$type]['margins']['bottom'] : 10,
    '#size' => '5',
    '#maxlength' => 3,
    '#required' => TRUE,
    '#tree' => TRUE,
  );
  $form['scrollbox'][$type]['margins']['left'] = array(
    '#type' => 'textfield',
    '#title' => t('Left Margin'),
    '#default_value' => isset($var[$type]['margins']['left']) ? $var[$type]['margins']['left'] : 10,
    '#size' => '5',
    '#maxlength' => 3,
    '#required' => TRUE,
    '#tree' => TRUE,
  );

  $form['scrollbox'][$type]['border'] = array(
    '#type' => 'fieldset',
    '#title' => t('Border Attributes'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => TRUE,
  );
  if (module_exists('colorpicker')) {
    $form['scrollbox'][$type]['border']['color_style'] = array(
      '#type' => 'colorpicker',
      '#title' => t('Border Color Picker'),
    );

    $form['scrollbox'][$type]['border']['color_style_textfield'] = array(
      '#type' => 'colorpicker_textfield',
      '#title' => t('Border Color'),
      '#default_value' => isset($var[$type]['border']['color_style_textfield']) ? $var[$type]['border']['color_style_textfield'] : '#ffffff',
      '#colorpicker' => 'color_style',
      '#tree' => TRUE,
    );
  }
  else {
    $form['scrollbox'][$type]['border']['color_style_textfield'] = array(
      '#type' => 'textfield',
      '#title' => t('Border Color'),
      '#default_value' => isset($var[$type]['border']['color_style_textfield']) ? $var[$type]['border']['color_style_textfield'] : '#ffffff',
      '#size' => '8',
      '#maxlength' => 7,
      '#description' => t('Please insert a color in hexidecimal value.  Do NOT forget the #.'),
      '#required' => TRUE,
      '#tree' => TRUE,
    );
    drupal_set_message('Installation of the colorpicker module will make your overal experience much more pleasent.');
  }
  $form['scrollbox'][$type]['border']['border_style'] = array(
    '#type' => 'select',
    '#title' => t('Border Style'),
    '#default_value' => isset($var[$type]['border']['border_style']) ? $var[$type]['border']['border_style'] : 'none',
    '#options' => array(
      'none' => t('No Border'),
      'hidden' => t('Hidden'),
      'dotted' => t('Dotted'),
      'dashed' => t('Dashed'),
      'solid' => t('Solid'),
      'double' => t('Double'),
      'groove' => t('Groove'),
      'ridge' => t('Ridge'),
      'inset' => t('Inset'),
      'outset' => t('Outset'),
    ),
    '#description' => t('Select the border style you would like.'),
    '#tree' => TRUE,
  );
  $form['scrollbox'][$type]['border']['border_width'] = array(
    '#type' => 'select',
    '#title' => t('Border Width'),
    '#default_value' => isset($var[$type]['border']['border_width']) ? $var[$type]['border']['border_width'] : '1px',
    '#options' => array(
      '0' => t('No Border'),
      '1px' => t('1 Pixel'),
      '2px' => t('2 Pixels'),
      '3px' => t('3 Pixels'),
      '4px' => t('4 Pixels'),
      '5px' => t('5 Pixels'),
      '6px' => t('6 Pixels'),
      '7px' => t('7 Pixels'),
      '8px' => t('8 Pixels'),
      '9px' => t('9 Pixels'),
      '10px' => t('10 Pixels'),
    ),
    '#description' => t('The Number of pixels wide you would like your border to be.'),
    '#tree' => TRUE,
  );
  return $form;
}

/**
 * implementation of imagefield_gallery_hook_admin_submit()
 * because we keyed the form by node type, we can just set the
 * variable equal to $form_values['lightbox2']
 */

function imagefield_gallery_scrollbox_admin_submit($form_id, $form_values) {
  $var = variable_get('imagefield_gallery_scrollbox_css', array());
  foreach($form_values['scrollbox'] as $name => $type) {
    $var[$name] = $type;
  }
  variable_set('imagefield_gallery_scrollbox_css', $var);
}

/* ---------- Theme Functions ----------- */

/**
 * implementation of theme_imagefield_gallery_hook()
 * @param $images
 *   $images is an array of all the images in the imagefield
 *   associated with the node type.
 *
 * @param $node
 *   the entire node object is passed in for a number of reasons
 *   primarily this would allow a using with a specific need to
 *   still leverage this module to manage their gallery types
 *   even if they weren't using imagefield.  This isn't plausible
 *   with this version of the module, but should be more so soon
 *
 * the end intention is to unify the output of a number of different
 * modules so that imagefield isn't the only option.
 */

function theme_imagefield_gallery_scrollbox($images = array(), $node) {
  $output = '';
  $count = count($images);
  $var = variable_get('imagefield_gallery', array());
  if ($count) {
    $i = 0;
    static $js_added = FALSE;
    if (!$js_added) {
      $js_added = TRUE;
      drupal_add_js(drupal_get_path('module', 'imagefield_gallery_scrollbox') .'/js/scrollbox.js');
    }
    $output .= '<div class="imagefield-gallery-scrollbox">'."\n";
    $output .= '<div class="nav"><a class="left">&lt;</a> <a class="right">&gt;</a></div>'."\n";
    $output .= '<div class="thumbnail_browser">'."\n";
    $thumbnail = $var[$node->type]['images']['thumbnail'];
    $preview = $var[$node->type]['images']['preview'];
    while ($i < $count) {
      if ($images[$i]['filepath']) {
        $file['filepath'] = $images[$i]['filepath'];
        if ($preview == '_original') {
          $imagecache_path = base_path() . $file['filepath'];
        }
        else {
          $imagecache_path = base_path() . file_directory_path() .'/imagecache/'. $preview .'/'. $file['filepath'];
        }
        $output .= '<div class="thumbnail">'."\n";
        $output .= '<a href="'. $imagecache_path .'" title="'. $images[$i]['title'] .'">'."\n";
        if ($thumbnail == '_original') {
          $output .= theme('image', $file['filepath'], $images[$i]['alt'], $images[$i]['title']) ."\n";
        }
        else {
          $output .= theme('imagecache', $thumbnail, $file['filepath'], $images[$i]['alt'], $images[$i]['title']) ."\n";
        }
        $output .= '</a>'."\n";
        $output .= '</div>'."\n";
      }
      $i++;
    }
    
  $output .= '</div>'."\n";
  $output .= '<div class="clear-both"></div>'."\n";
  $output .= '<div class="title-box"></div>'."\n";
  $output .= '<div class="preview-display"></div>'."\n";
  $output .= '</div>'."\n";
  }
  return $output;
}

/**
 * implementation of imagefield_gallery_hook_css_render()
 * @param $type
 *   a node type for node-specific css via the !type variable
 *   in the t() function.
 *
 * the intent of this function is to set up the necessary
 * css that a gallery type will need, this may include a
 * number of other theme functions called by this function
 * as necessary.
 */

function theme_imagefield_gallery_scrollbox_css_render($type) {
  return <<<EOF
      div.imagefield-gallery-scrollbox {
        width:200px;
        overflow:hidden;
        margin-left:auto;
        margin-right:auto;
      }
      div.imagefield-gallery-scrollbox div.preview-display {
        width:478px;
        height:359px;
        clear:both;
        background-repeat:no-repeat;
				margin: 0 0 5px 5px;
				border: 1px solid #000;
      }
      div.imagefield-gallery-scrollbox div.thumbnail-browser {
        padding:5px 0 0 5px;
        position:relative;
      }
      div.imagefield-gallery-scrollbox div.thumbnail {
        float:left;
        width:86px;
        height:86px;
        overflow:hidden;
				border: 1px solid #000;
				margin: 0 10px 10px 0;
      }
		
      div.imagefield-gallery-scrollbox div.thumbnail a img {
        margin: 0px;
        border: 0px;
      }

		div.imagefield-gallery-scrollbox .title-box {padding-top: 2px;}
		div.imagefield-gallery-scrollbox .nav {display: block; position: relative; height: 22px;}
		div.imagefield-gallery-scrollbox .nav a.left {display: block; width: 16px; height: 16px; background: url(images/b_left_arrow.png) no-repeat; position: absolute; top: 3px; left: 3px; cursor: pointer;}
		div.imagefield-gallery-scrollbox .nav a.right {display: block; width: 16px; height: 16px; background: url(images/b_rt_arrow.png) no-repeat; position: absolute; top: 3px; right: 3px; cursor: pointer;}


EOF;
}

/**
 * helper function imagefield_gallery_scrollbox_css_render()
 */

function theme_imagefield_gallery_scrollbox_css_margins($type) {
  $var = variable_get('imagefield_gallery_scrollbox_css', array());
  if (isset($var[$type])) {
    return '  margin:'. $var[$type]['margins']['top'] .'px '. $var[$type]['margins']['right'] .'px '. $var[$type]['margins']['bottom'] .'px '.  $var[$type]['margins']['left'] .'px;';
  }
  else {
    return '  margin: 10px;';
  }
}

/**
 * helper function imagefield_gallery_scrollbox_css_render()
 */

function theme_imagefield_gallery_scrollbox_css_borders($type) {
  $var = variable_get('imagefield_gallery_scrollbox_css', array());
  if (isset($var[$type])) {
    return '  border: '. $var[$type]['border']['border_width'] .' '. $var[$type]['border']['border_style'] .' '. $var[$type]['border']['color_style_textfield'] .';';
  }
  else {
    return '  border: 1px solid #000000;';
  }
}

/**
 * helper function imagefield_gallery_scrollbox_css_render()
 */

function theme_imagefield_gallery_scrollbox_css_width($type) {
  $var = variable_get('imagefield_gallery_scrollbox_css', array());
  if (isset($var[$type])) {
    return '  width: '. $var[$type]['width'] .'px;';
  }
  else {
    return '  width: 100px;';
  }
}

/**
 * helper function imagefield_gallery_scrollbox_css_render()
 */

function theme_imagefield_gallery_scrollbox_css_height($type) {
  $var = variable_get('imagefield_gallery_scrollbox_css', array());
  if (isset($var[$type])) {
    return '  height: '. $var[$type]['height'] .'px;';
  }
  else {
    return '  height: 100px;';
  }
}
