summaryrefslogtreecommitdiffstats
path: root/lightbox2.module
blob: 123a0e0fb0f21a91dda7a3dc04134dd6920d6821 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
<?php
// $Id$


/**
 * @file
 * Enables the use of lightbox2 which places images above your current page,
 * not within. This frees you from the constraints of the layout, particularly
 * column widths.
 *
 * This module is for Drupal 5.x only.
 *
 * Module by: Mark Ashmead
 * Mailto: bugzie@gmail.com
 * Co-maintainer: Stella Power (http://drupal.org/user/66894)
 *
 * Image Node Support: Steve McKenzie
 *
 */

/**
 * Implementation of hook_help().
 */
function lightbox2_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Enables Lightbox2 for Drupal');

    case 'admin/help#lightbox2':
      return t('<h3>Overview</h3>
      <p align="justify">Lightbox2 JS is a simple, unobtrusive script used to overlay images on the current page. It\'s a snap to setup and works on all modern browsers. The module comes with a Lightbox2 Lite option which does not use the jQuery libraries; it is therefore less likely to conflict with anything else.</p>
      <p align="justify">Places images above your current page, not within. This frees you from the constraints of the layout, particularly column widths. Keeps users on the same page. Clicking to view an image and then having to click the back button to return to your site is bad for continuity (and no fun!).</p>
<h3>Features</h3>
The version 2 module has several benefits over the plain Lightbox module.  Note, not all of these features are available when the "Lightbox2 Lite" option is enabled.
<ul>
<li>Image Sets: group related images and navigate through them with ease - ideal for your image galleries.</li>
<li>Slideshow Capability: automatically transition between grouped images, includes play/pause and previous and next buttons.</li>
<li>HTML Content Support: ability to show websites or other HTML content in a lightbox.</li>
<li>Visual Effects: fancy pre-loader and transition when you click on the image.</li>
<li>Keyboard Shortcuts: useful <a href="http://drupal.org/node/249827">keyboard shortcuts</a> for switching between images, toggling play / pause, etc.</li>
<li>Zoom Capability: larger images are reduced in size so they fit snugly inside the browser window.  A zoom button can then be clicked on to see it in its original size.</li>
<li>Automatic Image Detection: configurable automatic re-formatting of
image thumbnails, so there is no need to add \'rel="lightbox"\' to each
image link on your site.  <a
href="http://drupal.org/project/image">Image</a>, <a
href="http://drupal.org/project/inline">Inline</a>, <a
href="http://drupal.org/project/flickr">Flickr</a>, <a
href="http://drupal.org/project/img_assist">Image Assist</a> and <a
href="http://drupal.org/project/imagefield">CCK Imagefield</a> modules are all
supported.  It\'s also possible to configure a custom list of image classes
which should trigger the lightbox functionality.</li>
<li><a href="http://drupal.org/project/imagecache">Imagecache</a> Support: adds a Lightbox2 field formatter for <a href="http://drupal.org/project/imagefield">CCK imagefields</a> for your custom <a href="http://drupal.org/project/views">views</a>.</li>
<li>Image Page Link: a link to the image node can be provided within the lightbox itself.</li>
<li>Page Exclusion Capability: exclude certain pages on your site from having the lightbox2 functionality.</li>
<li>Gallery 2 Support: support for Gallery 2 images via the <a href="http://drupal.org/project/gallery">Gallery</a> module (beta).</li>
</ul>
      <p align="justify"><h3>Usage</h3></p>
      <h5 style="text-decoration: underline;">Adding a Basic Lightbox</h5>
      <p>Add rel=&quot;lightbox&quot; attribute to any link tag to activate the lightbox. For example: </p>
      <p>&lt;a href=&quot;image-1.jpg&quot; rel=&quot;lightbox&quot; title=&quot;my caption&quot;&gt;image #1&lt;/a&gt;</p>
      <p>Optional: Use the title attribute if you want to show a caption.</p>
<h5 style="text-decoration: underline;">Grouping Images</h5>
   <p>If you have a set of related images that you would like to group, follow
   step one but additionally include a group name between square brackets in the
   rel attribute. For example:</p><p>&lt;a href="images/image-1.jpg"
   rel="lightbox[roadtrip]"&gt;image #1&lt;/a&gt;<br />
&lt;a href="images/image-2.jpg" rel="lightbox[roadtrip]"&gt;image
#2&lt;/a&gt;<br />
&lt;a href="images/image-3.jpg" rel="lightbox[roadtrip]"&gt;image
#3&lt;/a&gt;<br /></p>
<p>No limits to the number of image sets per page or how many images are allowed
in each set.</p>

<p>If you have a set of images that you would like to group together in a lightbox, but only wish for one of these images to be visible on your page, you can assign the "lightbox_hide_image" class to hide the additional images.  For example:</p>

<p>&lt;a href="images/image-1.jpg" rel="lightbox[roadtrip]"&gt;image #1&lt;/a&gt;<br />
&lt;a href="images/image-2.jpg" rel="lightbox[roadtrip]" class="lightbox_hide_image"&gt;image #2&lt;/a&gt;<br />
&lt;a href="images/image-3.jpg" rel="lightbox[roadtrip]" class="lightbox_hide_image"&gt;image #3&lt;/a&gt;</p>

<h5 style="text-decoration: underline;">Slideshow</h5>
<p>This is very similar to the grouping functionality described above.  The only difference is that "rel" attribute should be set to "lightshow" instead of "lightbox".  Using the same example as above, we could launch the images in a slideshow by doing:</p>

<p>&lt;a href="images/image-1.jpg" rel="lightshow[roadtrip]"&gt;image #1&lt;/a&gt;<br />
&lt;a href="images/image-2.jpg" rel="lightshow[roadtrip]"&gt;image #2&lt;/a&gt;<br />
&lt;a href="images/image-3.jpg" rel="lightshow[roadtrip]"&gt;image #3&lt;/a&gt;</p>

<h3>Keyboard Shortcuts</h3>
<table border=0>
<tr><td>Close Lightbox</td><td>x</td></tr>
<tr><td></td><td>o</td></tr>
<tr><td></td><td>c</td></tr>
<tr><td></td><td>ESC</td></tr>

<tr><td>Previous Image</td><td>p</td></tr>
<tr><td></td><td>Left Arrow</td></tr>

<tr><td>Next Image</td><td>n</td></tr>
<tr><td></td><td>Right Arrow</td></tr>

<tr><td>Toggle Zoom</td><td>z (not available in slideshow)</td></tr>

<tr><td>Toggle Play / Pause</td><td>Spacebar (slideshow only)</td></tr>
</table>

<p>Not all of the keyboard shortcuts work in the Opera browser, for example "z" for toggling the zoom and "spacebar" for toggling play / pause in slideshows.  This can be overcome by updating your shortcut settings in the Opera preferences editor.</p>

');
    break;
  }
}

/**
 * Implementation of hook_perm().
 */
function lightbox2_perm() {
  return array('administer lightbox2');
}

/**
 * Implementation of hook_menu().
 */
function lightbox2_menu($may_cache) {
  $items = array();

  if ($may_cache) {
    $items[] = array('path' => 'admin/settings/lightbox2',
      'title' => t('Lightbox2'),
      'callback' => 'lightbox2_settings_page',
      'access' => user_access('administer lightbox2'),
      'description' => t('Allows the user to configure the lightbox2 settings'),
    );
    $items[] = array('path' => 'admin/settings/lightbox2/general',
      'title' => t('General'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('lightbox2_general_settings_form'),
      'access' => user_access('administer lightbox2'),
      'description' => t('Allows the user to configure the lightbox2 settings'),
      'type' => MENU_DEFAULT_LOCAL_TASK,
      'weight' => 0,
    );
    $items[] = array('path' => 'admin/settings/lightbox2/slideshow',
      'title' => t('Slideshow'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('lightbox2_slideshow_settings_form'),
      'access' => user_access('administer lightbox2'),
      'description' => t('Allows the user to configure the lightbox2 slideshow functionality.'),
      'type' => MENU_LOCAL_TASK,
      'weight' => 1,
    );
    $items[] = array('path' => 'admin/settings/lightbox2/automatic',
      'title' => t('Automatic image handling'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('lightbox2_auto_image_handling_settings_form'),
      'access' => user_access('administer lightbox2'),
      'description' => t('Allows the user to configure the lightbox2 automatic image handling settings'),
      'type' => MENU_LOCAL_TASK,
      'weight' => 2,
    );
  }
  else {
    if (lightbox2_exclude_these_paths() != 1) {
      lightbox2_add_files();
    }
  }

  return $items;
}


/**
 * The default lightbox2 settings page.
 */
function lightbox2_settings_page($op = NULL) {
  $output .= drupal_get_form('lightbox2_general_settings_form');
  return $output;
}

/**
 * General configuration form for controlling the lightbox behaviour.
 */
function lightbox2_general_settings_form() {

  // Add the javascript which disables / enables form elements.
  drupal_add_js(drupal_get_path('module', 'lightbox2') .'/js/lightbox2.js', 'module');

  // Enable translation of default strings for potx.
  $default_strings = array(t('View Image Details'), t('Image !current of !total'));

  // Define Lightbox2 Lite fieldset.
  /* ------------------------------- */
  $use_lite = variable_get('lightbox2_lite', FALSE);
  $form['lightbox2_lite_options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Lightbox2 Lite'),
    '#collapsible' => TRUE,
    '#collapsed' => !$use_lite,
  );

  // Add checkbox for Lightbox2 Lite.
  $form['lightbox2_lite_options']['lightbox2_lite'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use Lightbox2 Lite'),
    '#description' => t('Checking this box will enable Lightbox2 Lite and will disable all of the automatic image URL re-formatting features.  It also disables all grouping features.'),
    '#default_value' => $use_lite,
  );


  // Add checkbox for alternative layout.
  $form['lightbox2_use_alt_layout'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use Alternative Layout'),
    '#description' => t('Enabling this option alters the layout of the lightbox elements.  This doesn\'t apply when using Lightbox Lite.'),
    '#default_value' => variable_get('lightbox2_use_alt_layout', FALSE),
  );

  // Add checkbox for force navigation display.
  $form['lightbox2_force_show_nav'] = array(
    '#type' => 'checkbox',
    '#title' => t('Force visibility of navigation links'),
    '#description' => t('When viewing grouped images using the default layout, the navigational links to the next and previous images are only displayed when you hover over the image.  Checking this box forces these links to be displayed all the time.'),
    '#default_value' => variable_get('lightbox2_force_show_nav', FALSE),
  );

  // Add text box for image count for grouping.
  $form['lightbox2_image_count_str'] = array(
    '#type' => 'textfield',
    '#title' => t('Image Count Text'),
    '#description' => t('This text is used to display the image count underneath the image in the lightbox when image grouping is enabled.  Use !current as a placeholder for the number of the current image and !total for the total number of images in the group.  For example, "Image !current of !total".'),
    '#default_value' => variable_get('lightbox2_image_count_str', 'Image !current of !total'),
  );

  // Add checkbox for zoom image.
  $form['lightbox2_disable_zoom'] = array(
    '#type' => 'checkbox',
    '#title' => t('Disable Zoom Feature'),
    '#description' => t('By default, when the image being displayed in the lightbox is larger than the browser window, it is resized to fit within the window and a zoom button is provided for users who wish to view the image in its original size.  Checking this box will disable this feature and all images will be displayed without any resizing.'),
    '#default_value' => variable_get('lightbox2_disable_zoom', FALSE),
  );

  // Add textarea for urls without lightbox.
  $form['lightbox2_disable_these_urls'] = array(
    '#type' => 'textarea',
    '#title' => t('Disable LightBox for these urls'),
    '#description' => t('List the urls where the lightbox should be disabled, e.g. "node/add/page".  Use &lt;front&gt; to match the front page.  Put each url on a separate line. You can use the % character as a wildcard.'),
    '#default_value' => variable_get('lightbox2_disable_these_urls', ''),
  );


  // Define imagefield settings fieldset.
  /* ---------------------------------- */
  if (module_exists('imagefield') && module_exists('imagecache')) {
    $form['lightbox2_imagefield_options'] = array(
      '#type' => 'fieldset',
      '#title' => t('Imagefield settings'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );

    $imagefield_options = array(
      0 => t('No Grouping'),
      1 => t('Group by Field Name'),
      2 => t('Group by Node Id'),
      3 => t('Group by Field Name and Node Id'),
    );
    // Add checkbox for imagefield grouping.
    $form['lightbox2_imagefield_options']['lightbox2_imagefield_group_node_id'] = array(
      '#type' => 'select',
      '#title' => t('Select Imagefield Grouping in Views'),
      '#description' => t('By default, imagefields in views are grouped by the field name they appear in the view in.  You can override that grouping here.'),
      '#options' => $imagefield_options,
      '#default_value' => variable_get('lightbox2_imagefield_group_node_id', 1),
    );
  }


  // Define advanced settings fieldset.
  /* ---------------------------------- */
  $form['lightbox2_advanced_options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );

  // Add checkbox for overlay opacity.
  for ($i = 0; $i < 10; $i++) {
    $opacity_options["0.$i"] = "0.$i";
  }
  $opacity_options["1.0"] = "1.0";
  $form['lightbox2_advanced_options']['lightbox2_overlay_opacity'] = array(
    '#type' => 'select',
    '#title' => t('Overlay Opacity'),
    '#options' => $opacity_options,
    '#description' => t('The overlay opacity setting determines how visible or transparent the background page is behind the lightbox.  The opacity value can range from 0.0 to 1.0 where 0.0 is 100% transparent and 1.0 is 100% opaque.'),
    '#default_value' => variable_get('lightbox2_overlay_opacity', '0.6'),
  );

  // Allow users to decide where javascript should be loaded - header or footer.
  // Header is recommended so user can click on images before page has finished
  // loading, but footer is needed for sites in IE which use SWFObject.
  $form['lightbox2_advanced_options']['lightbox2_js_location'] = array(
    '#type' => 'select',
    '#title' => t('Location of Javscript'),
    '#options' => array('header' => t('Header'), 'footer' => t('Footer')),
    '#description' => t('By default, the lightbox javascript files are loaded in the HTML header.  However, for sites using SWFObject to load their Flash content, the footer setting is recommended to prevent "Operation Aborted" errors in IE.  If using the footer setting, please note that not all themes correctly implement the footer region and may require a small change.'),
    '#default_value' => variable_get('lightbox2_js_location', 'header'),
  );


  return system_settings_form($form);
}

/**
 * Configuration form for controlling the slideshow behaviour.
 */
function lightbox2_slideshow_settings_form() {

  // Add the javascript which disables / enables form elements.
  drupal_add_js(drupal_get_path('module', 'lightbox2') .'/js/lightbox2.js', 'module');

  // Set up a hidden variable.
  $form['lightbox2_lite'] = array(
    '#type' => 'hidden',
    '#value' => variable_get('lightbox2_lite', FALSE),
  );


  // Add text box for slideshow interval.
  $form['lightbox2_slideshow_interval'] = array(
    '#type' => 'textfield',
    '#title' => t('Interval Seconds'),
    '#description' => t('The slideshow interval is the length of time in seconds an image is visible before the slideshow shows the next image.'),
    '#default_value' => variable_get('lightbox2_slideshow_interval', 5),
    '#size' => 20,
  );

  // Add checkbox for slideshow automatic exit.
  $form['lightbox2_slideshow_automatic_exit'] = array(
    '#type' => 'checkbox',
    '#title' => t('Automatically Exit Slideshow'),
    '#description' => t('Enabling this option will cause the lightbox to automatically close after displaying the last image.'),
    '#default_value' => variable_get('lightbox2_slideshow_automatic_exit', TRUE),
  );

  // Add checkbox for showing hte play / pause button.
  $form['lightbox2_slideshow_show_play_pause'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show Play / Pause Button'),
    '#description' => t('If enabled, a play / pause button will be shown in the slideshow allowing the user more control over their viewing experience.'),
    '#default_value' => variable_get('lightbox2_slideshow_show_play_pause', TRUE),
  );

  // Add checkbox for "pausing on next click".
  $form['lightbox2_slideshow_pause_on_next_click'] = array(
    '#type' => 'checkbox',
    '#title' => t('Pause Slideshow on "Next Image" Click'),
    '#description' => t('When enabled the slideshow is automatically paused, and the following image shown, when the "Next" button is clicked.'),
    '#default_value' => variable_get('lightbox2_slideshow_pause_on_next_click', FALSE),
  );

  // Add checkbox for "pausing on prev click".
  $form['lightbox2_slideshow_pause_on_prev_click'] = array(
    '#type' => 'checkbox',
    '#title' => t('Pause Slideshow on "Previous Image" Click'),
    '#description' => t('When enabled the slideshow is automatically paused, and the previous image shown, when the "Previous" button is clicked.'),
    '#default_value' => variable_get('lightbox2_slideshow_pause_on_previous_click', TRUE),
  );

  return system_settings_form($form);
}

/**
 * Validation function for the slideshow configuration form.
 *
 * Ensure that the slideshow interval submitted is numeric.
 */
function lightbox2_slideshow_settings_form_validate($form_id, $form_values) {
  if (!is_numeric($form_values['lightbox2_slideshow_interval'])) {
    form_set_error('lightbox2_slideshow_interval', t('The "interval seconds" value must be numeric.'));
  }
}

/**
 * Configuration form the automatic image handling functionality.
 */
function lightbox2_auto_image_handling_settings_form() {

  // Add the javascript which disables / enables form elements.
  drupal_add_js(drupal_get_path('module', 'lightbox2') .'/js/lightbox2.js', 'module');

  // Set up a hidden variable.
  $form['lightbox2_lite'] = array(
    '#type' => 'hidden',
    '#value' => variable_get('lightbox2_lite', FALSE),
  );

  $automatic_options = array(
    0 => t('Disabled'),
    1 => t('Lightbox'),
    2 => t('Lightbox grouped'),
    3 => t('Slideshow'),
  );

  // Set image node options help text.
  $form['image_node_options'] = array(
    '#value' => t('These options allow automatic URL re-formatting of images.  This removes the need for you to add \'rel="lightbox"\' to each image link throughout your site.  You can select which image sizes will trigger the lightbox and configure a list of image CSS classes which should also have their URLs automatically re-formatted.  This feature is not available when using Lightbox2 Lite.'),
  );

  // Define image nodes settings fieldset.
  /* ------------------------------------- */
  $form['lightbox2_image_node_options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Image node settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );

  // Add checkbox for image nodes.
  $form['lightbox2_image_node_options']['lightbox2_image_node'] = array(
    '#type' => 'select',
    '#title' => t('Automatic handler for Image Nodes'),
    '#options' => $automatic_options,
    '#description' => t('Choose how URLs for image nodes will be automatically handled.'),
    '#default_value' => variable_get('lightbox2_image_node', 2),
  );

  // Add drop-down for list of available image sizes.
  if (module_exists('image')) {
    $sizes = _image_get_sizes();
    foreach ($sizes as $size_key => $size) {
      if ($size_key == 'original' || $size_key == '_original') {
        $size_key = '';
      }
      $size_options[$size_key] = $size['label'];
    }
    $form['lightbox2_image_node_options']['lightbox2_display_image_size'] = array(
      '#type' => 'select',
      '#title' => t('Lightbox image display size'),
      '#options' => $size_options,
      '#default_value' => variable_get('lightbox2_display_image_size', ''),
      '#description' => t('Select which image size will be loaded in the lightbox.  This only applies to images uploaded with the Image module.'),
    );

    $form['lightbox2_image_node_options']['lightbox2_trigger_image_size'] = array(
      '#type' => 'select',
      '#multiple' => TRUE,
      '#title' => t('Image trigger size'),
      '#options' => $size_options,
      '#default_value' => variable_get('lightbox2_trigger_image_size', array('thumbnail' => t('Thumbnail'))),
      '#description' => t('Select which image size, when clicked on, will automatically trigger the lightbox.  This only applies to images uploaded with the Image module.'),
    );

  }

  // Add checkbox for disabling lightbox for gallery lists.
  $form['lightbox2_image_node_options']['lightbox2_disable_nested_galleries'] = array(
    '#type' => 'checkbox',
    '#title' => t('Disable Lightbox for Gallery Lists'),
    '#description' => t('Checking this box will disable the lightbox for images in gallery lists.  This means it is possible to open a gallery by clicking on the teaser image, but the lightbox will still appear when viewing images within the gallery.  This only applies to image galleries created with the "image gallery" module.'),
    '#default_value' => variable_get('lightbox2_disable_nested_galleries', TRUE),
  );

  // Add checkbox for flickr image support.
  $form['lightbox2_flickr'] = array(
    '#type' => 'select',
    '#title' => t('Automatic handler for Flickr images'),
    '#options' => $automatic_options,
    '#description' => t('Choose how URLs for Flickr images will be automatically handled.'),
    '#default_value' => variable_get('lightbox2_flickr', 2),
  );

  // Add checkbox for gallery2 block image support.
  $form['lightbox2_gallery2_blocks'] = array(
    '#type' => 'select',
    '#title' => t('Automatic handler for Gallery2 block images'),
    '#options' => $automatic_options,
    '#description' => t('Choose how URLs for images, contained within Gallery2 image blocks, will be automatically handled.'),
    '#default_value' => variable_get('lightbox2_gallery2_blocks', 2),
  );


  // Add checkbox for inline image support.
  $form['lightbox2_inline'] = array(
    '#type' => 'select',
    '#title' => t('Automatic handler for Inline images'),
    '#options' => $automatic_options,
    '#description' => t('Choose how URLs for Inline images will be automatically handled.'),
    '#default_value' => variable_get('lightbox2_inline', 2),
  );

  // Add checkbox for image assist custom size images.
  $form['lightbox2_image_assist_custom'] = array(
    '#type' => 'select',
    '#title' => t('Automatic handler for Image Assist custom size images'),
    '#options' => $automatic_options,
    '#description' => t('Choose how URLs for custom size images, displayed by the Image Assist module, will be automatically handled.'),
    '#default_value' => variable_get('lightbox2_image_assist_custom', 2),
  );

  // Add text box for custom trigger classes.
  $form['lightbox2_custom_trigger_classes'] = array(
    '#type' => 'textarea',
    '#title' => t('Custom image trigger classes'),
    '#description' => t('List the image classes which should trigger the lightbox when clicked on.  Put each class on a separate line.'),
    '#default_value' => variable_get('lightbox2_custom_trigger_classes', ''),
  );

  // Add text box for link text to node.
  $form['lightbox2_node_link_text'] = array(
    '#type' => 'textfield',
    '#title' => t('Text for Image Page Link'),
    '#description' => t('This is the text that will appear as the link to the image page underneath the image in the lightbox.'),
    '#default_value' => variable_get('lightbox2_node_link_text', 'View Image Details'),
  );

  // Add checkbox for node link target.
  $form['lightbox2_node_link_target'] = array(
    '#type' => 'checkbox',
    '#title' => t('Open Image Page in New Window'),
    '#description' => t('This controls whether the link to the image page underneath the image is opened in a new window or the current window.'),
    '#default_value' => variable_get('lightbox2_node_link_target', FALSE),
    '#return_value' => '_blank',
  );

  // Add checkbox for disabling lightbox for acidfree gallery lists.
  if (module_exists("acidfree")) {
    $form['lightbox2_disable_nested_acidfree_galleries'] = array(
      '#type' => 'checkbox',
      '#title' => t('Disable Lightbox for Acidfree Gallery Lists'),
      '#description' => t('Checking this box will disable the lightbox for images in gallery lists.  This means it is possible to open a gallery by clicking on the teaser image, but the lightbox will still appear when viewing images within the gallery.  This only applies to image galleries created with the "acidfree" module.'),
      '#default_value' => variable_get('lightbox2_disable_nested_acidfree_galleries', TRUE),
    );
  }

  return system_settings_form($form);
}

/**
 * Implementation of hook_filter_tips().
 */
function lightbox2_filter_tips($delta, $format, $long = FALSE) {
  if ($delta == 0) {
    if (!$long) {
      return t('Image links with \'rel="lightbox"\' in the &lt;a&gt; tag will appear in a Lightbox when clicked on.');
    }
    else {
      $output = '<p>'. t('To add a lightbox to your images, add rel="lightbox" attribute to any link tag to activate the lightbox. For example:') .'</p>';
      $output .= '<p>'. t('<code>&lt;a href="image-1.jpg" rel="lightbox" title="my caption"&gt;image #1&lt;/a&gt;</code>') .'</p>';
      $output .= '<p>'. t('The title attribute in the link tag is optional. The addition of this attribute enables the display of a caption with the image displayed in the lightbox.') .'</p>';
      $output .= '<p>'. t('If you have a set of related images that you would like to group, then you will need to include a group name between square brackets in the rel attribute. For example:') .'</p>';
      $output .= '<p>'. t('<code>&lt;a href="image-1.jpg" rel="lightbox[roadtrip]"&gt;image #1&lt;/a&gt;<br /> &lt;a href="image-2.jpg" rel="lightbox[roadtrip]"&gt;image #2&lt;/a&gt;<br /> &lt;a href="image-3.jpg" rel="lightbox[roadtrip]"&gt;image #3&lt;/a&gt;<br /> </code>') .'</p>';
      $output .= '<p>'. t('There are no limits to the number of image sets per page or how many images are allowed in each set.') .'</p>';
      $output .= '<p>'. t('If you wish to turn the caption into a link, format your caption in the following way:') .'</p>';
      $output .= '<p>'. t('<code>&lt;a href="image-1.jpg" rel="lightbox" title=\'&lt;a href="http://www.yourlink.com"&gt;View Image Details&lt;/a&gt;\'&gt;image #1&lt;/a&gt;</code>') .'</p>';
      return $output;
    }
  }
  else if ($delta == 1) {
    return t('Image links from G2 are formatted for use with Lightbox2');
  }
}

/**
 * Implementation of hook_filter().
 */
function lightbox2_filter($op, $delta = 0, $format = -1, $text = '') {
  switch ($op) {
    case 'list':
      return array(0 => t('Lightbox filter'), 1 => t('Lightbox G2 filter'));

    case 'description':
      if ($delta == 1) {
        return t('Turns g2_filter links into Lightbox2 appropriate links');
      }
      else {
        return t('Image links with \'rel="lightbox"\' in the &lt;a&gt; tag will appear in a Lightbox when clicked on.');
      }

    case 'process':
      if ($delta == 1) {
        $text = ' '. $text .' ';
        $text = preg_replace('/ShowItem/', 'DownloadItem', $text);
        $text = preg_replace('/target=""/', 'rel="lightbox"', $text);
        $text = substr($text, 1, -1);
      }
      return $text;

    default:
      return $text;
  }
}

/**
 * Provide links to the CSS stylesheet and JS file associated with
 * this module.
 */
function lightbox2_add_files() {
  static $already_added = FALSE;
  if ($already_added) {
    return; // Don't add the javascript and CSS multiple times.
  }
  $already_added = TRUE;

  // Load required js and css files.
  $path = drupal_get_path('module', 'lightbox2');

  // Initialise some variables.
  $inline_image_handler = variable_get('lightbox2_inline', 2);
  $flickr_image_handler = variable_get('lightbox2_flickr', 2);
  $gallery2_block_handler = variable_get('lightbox2_gallery2_blocks', 2);
  $image_assist_handler = variable_get('lightbox2_image_assist_custom', 2);
  $image_node_handler = variable_get('lightbox2_image_node', 2);

  // Set the list of image classes to format urls for.
  $image_node_sizes = '';
  $trigger_lightbox_classes = '';
  $trigger_lightbox_group_classes = '';
  $trigger_slideshow_classes = '';

  // Inline module images.
  switch ($inline_image_handler) {
    case 1:
      $trigger_lightbox_classes .= "img.inline,";
      break;
    case 2:
      $trigger_lightbox_group_classes .= "img.inline,";
      break;
    case 3:
      $trigger_slideshow_classes .= "img.inline,";
      break;
  }

  // Flickr images.
  switch ($flickr_image_handler) {
    case 1:
      $trigger_lightbox_classes .= "img.flickr-photo-img,img.flickr-photoset-img,";
      break;
    case 2:
      $trigger_lightbox_group_classes .= "img.flickr-photo-img,img.flickr-photoset-img,";
      break;
    case 3:
      $trigger_slideshow_classes .= "img.flickr-photo-img,img.flickr-photoset-img,";
      break;
  }

  // Gallery2 block images.
  switch ($gallery2_block_handler) {
    case 1:
      $trigger_lightbox_classes .= "img.ImageFrame_image,";
      break;
    case 2:
      $trigger_lightbox_group_classes .= "img.ImageFrame_image,";
      break;
    case 3:
      $trigger_slideshow_classes .= "img.ImageFrame_image,";
      break;
  }

  // Image Assist custom size images.
  switch ($image_assist_handler) {
    case 1:
      $trigger_lightbox_classes .= "img.image-img_assist_custom,";
      break;
    case 2:
      $trigger_lightbox_group_classes .= "img.image-img_assist_custom,";
      break;
    case 3:
      $trigger_slideshow_classes .= "img.image-img_assist_custom,";
      break;
  }

  // Image nodes.
  if ($image_node_handler) {
    $trigger_sizes = variable_get('lightbox2_trigger_image_size', array('thumbnail' => t('Thumbnail')));
    foreach ($trigger_sizes as $size => $size_name) {
      $triggers = "img.$size, img.image-$size,";
      if (empty($size)) {
        $triggers = "img._original, img.image-_original,";
      }
      else {
        $image_node_sizes .= "\.$size|";
      }
      switch ($image_node_handler) {
        case 1:
          $trigger_lightbox_classes .= $triggers;
          break;
        case 2:
          $trigger_lightbox_group_classes .= $triggers;
          break;
        case 3:
          $trigger_slideshow_classes .= $triggers;
          break;
      }
    }
  }

  // Custom images.
  $custom_triggers = variable_get('lightbox2_custom_trigger_classes', '');
  if (!empty($custom_triggers)) {
    $trigger_classes = preg_split("/(\r\n|\n)/", $custom_triggers);
    foreach ($trigger_classes as $class) {
      if (!empty($class)) {
        $trigger_lightbox_classes .= "img.$class,";
      }
    }
  }
  $trigger_lightbox_classes = rtrim($trigger_lightbox_classes, ",");
  $trigger_lightbox_group_classes = rtrim($trigger_lightbox_group_classes, ",");
  $trigger_slideshow_classes = rtrim($trigger_slideshow_classes, ",");
  $image_node_sizes = '('. rtrim($image_node_sizes, "|") .')';

  // Find out if we're using a RTL language.
  $rtl = FALSE;
  if (module_exists('i18n') && module_invoke('i18n', 'language_rtl')) {
    $rtl = TRUE;
  }

  // Load the javascript settings.
  $js_settings = array(
    'rtl' => $rtl,
    'file_path' => base_path() .'(\w\w/)'. file_directory_path(),
    'base_path' => base_path(),
    'default_image' => base_path() . $path .'/images/brokenimage.jpg',
    'overlay_opacity' => variable_get('lightbox2_overlay_opacity', '0.6'),
    'use_alt_layout' => variable_get('lightbox2_use_alt_layout', FALSE),
    'disable_zoom' => variable_get('lightbox2_disable_zoom', FALSE),
    'force_show_nav' => variable_get('lightbox2_force_show_nav', FALSE),
    'node_link_text' => check_plain(t(variable_get('lightbox2_node_link_text', 'View Image Details'))),
    'node_link_target' => variable_get('lightbox2_node_link_target', FALSE),
    'image_count' => check_plain(t(variable_get('lightbox2_image_count_str', 'Image !current of !total'))),
    'lite_press_x_close' => t('press !x to close', array('!x' => '<a href="#" onclick="hideLightbox(); return FALSE;"><kbd>x</kbd></a>')),
    // Automatic image handling settings.
    'display_image_size' => variable_get('lightbox2_display_image_size', ''),
    'image_node_sizes' => $image_node_sizes,
    'trigger_lightbox_classes' => $trigger_lightbox_classes,
    'trigger_lightbox_group_classes' => $trigger_lightbox_group_classes,
    'trigger_slideshow_classes' => $trigger_slideshow_classes,
    'disable_for_gallery_lists' => variable_get('lightbox2_disable_nested_galleries', TRUE),
    'disable_for_acidfree_gallery_lists' => variable_get('lightbox2_disable_nested_acidfree_galleries', TRUE),
    // Slideshow settings.
    'slideshow_interval' => variable_get('lightbox2_slideshow_interval', 5) * 1000,
    'slideshow_automatic_exit' => variable_get('lightbox2_slideshow_automatic_exit', TRUE),
    'show_play_pause' => variable_get('lightbox2_slideshow_show_play_pause', TRUE),
    'pause_on_next_click' => variable_get('lightbox2_slideshow_pause_on_next_click', FALSE),
    'pause_on_previous_click' => variable_get('lightbox2_slideshow_pause_on_previous_click', TRUE),
  );
  drupal_add_js(array('lightbox2' => $js_settings), 'setting');

  // Check where we should load the javascript files - header or footer.
  $js_location = variable_get('lightbox2_js_location', 'header');

  // Lightbox2 Plus.
  if (!variable_get('lightbox2_lite', FALSE)) {
    $css = $path .'/css/lightbox.css';
    if (module_exists('i18n') && module_invoke('i18n', 'language_rtl')) {
      $css = $path .'/css/lightbox-rtl.css';
    }
    if (variable_get('lightbox2_use_alt_layout', FALSE)) {
      // No need for rtl version, everything is centred.
      $css = $path .'/css/lightbox_alt.css';
    }
    drupal_add_css($css);

    // Check to see if any automatic image handling options are enabled.
    if ($image_node_handler || $flickr_image_handler || $gallery2_block_handler || $inline_image_handler || $image_assist_handler || $custom_triggers != '') {
      drupal_add_js($path .'/js/auto_image_handling.js', 'module', $js_location);
    }
    drupal_add_js($path .'/js/lightbox.js', 'module', $js_location);

  }
  // Lightbox Lite.
  else {
    $css = $path .'/css/lightbox_lite.css';
    if (module_exists('i18n') && module_invoke('i18n', 'language_rtl')) {
      $css = $path .'/css/lightbox_lite-rtl.css';
    }
    drupal_add_css($css);
    drupal_add_js($path .'/js/lightbox_lite.js', 'module', $js_location);
  }
}

/**
 * Implementation of hook_field_formatter_info().
 *
 * Add certain lightbox+imagecache formatters to CCK image fields if the
 * imagefield.module and the imagecache.module exist.
 */
function lightbox2_field_formatter_info() {
  $formatter = array();
  if (module_exists('imagefield') && module_exists('imagecache')) {
    $rules = array();
    if (function_exists('imagecache_presets')) {
      $presets = imagecache_presets();
      foreach ($presets as $preset_id => $preset_info) {
        $rules[$preset_id] = $preset_info['presetname'];
      }
    }
    else {
      $rules = _imagecache_get_presets();
    }
    foreach ($rules as $ruleid => $view_rule) {
      $formatters['lightbox2][original]['. $view_rule] = array(
        'label' => 'Lightbox2: original->'. $view_rule,
        'field types' => array('image'),
      );
      $formatters['lightbox2]['. $view_rule .'][original'] = array(
        'label' => 'Lightbox2: '. $view_rule .'->original',
        'field types' => array('image'),
      );
      $formatters['lightshow2][original]['. $view_rule] = array(
        'label' => 'Lightbox2 slideshow: original->'. $view_rule,
        'field types' => array('image'),
      );
      $formatters['lightshow2]['. $view_rule .'][original'] = array(
        'label' => 'Lightbox2 slideshow: '. $view_rule .'->original',
        'field types' => array('image'),
      );
      foreach ($rules as $rid => $lightbox_rule) {
        $formatters['lightbox2]['. $view_rule .']['. $lightbox_rule] = array(
          'label' => 'Lightbox2: '. $view_rule .'->'. $lightbox_rule,
          'field types' => array('image'),
        );
        $formatters['lightshow2]['. $view_rule .']['. $lightbox_rule] = array(
          'label' => 'Lightbox2 slideshow: '. $view_rule .'->'. $lightbox_rule,
          'field types' => array('image'),
        );
      }
    }
  }

  return $formatters;
}

/**
 * Implementation of hook_field_formatter().
 */
function lightbox2_field_formatter($field, $item, $formatter) {
  if (module_exists('imagefield') && module_exists('imagecache')) {
    if (isset($item['filepath'])) {
      $filepath = &$item['filepath'];
    }
    else if (!empty($item['fid'])) {
      $file = _imagefield_file_load($item['fid']);
      $filepath = &$file['filepath'];
    }
    else {
      return '';
    }
    if (lightbox2_exclude_these_paths() != 1) {
      lightbox2_add_files();
    }

    $item = array_merge($item, (array)$file);
    $item['filepath'] = $filepath;
    if (strpos($formatter, 'lightbox2][') !== FALSE || strpos($formatter, 'lightshow2][') !== FALSE) {
      list($null, $view_preset, $lightbox_preset) = explode('][', $formatter, 3);
      $item['lightbox_preset'] = $lightbox_preset;

      $rules = array();
      if (function_exists('imagecache_presets')) {
        $presets = imagecache_presets();
        foreach ($presets as $preset_id => $preset_info) {
          $rules[$preset_id] = $preset_info['presetname'];
        }
      }
      else {
        $rules = _imagecache_get_presets();
      }
      if ($view_preset == 'original' || in_array($view_preset, (array) $rules)) {
        $slideshow = FALSE;
        if (strpos($formatter, 'lightshow2][') !== FALSE) {
          $slideshow = TRUE;
        }
        return theme('imagefield_image_imagecache_lightbox2', $view_preset, $field, $item, NULL, $slideshow);
      }
    }
  }
}

/**
 * Implementation of theme_imagefield_image_imagecache_lightbox2().
 */
function theme_imagefield_image_imagecache_lightbox2($view_preset, $field, $item, $attributes = NULL, $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 = '';
  if (!empty($item['nid'])) {
    $attributes = array();
    $target = variable_get('lightbox2_node_link_target', FALSE);
    if (!empty($target)) {
      $attributes = array('target' => $target);
    }
    $node_link = '<br /><br />'. l(t(variable_get('lightbox2_node_link_text', 'View Image Details')), 'node/'. $item['nid'], $attributes);
  }

  $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;
}

/**
 * Return TRUE if current path is disabled for lightbox according to
 * lightbox2_disable_these_urls.
 */
function lightbox2_exclude_these_paths() {
  // Use lightbox if this url is not excluded.
  $disabled_urls = variable_get('lightbox2_disable_these_urls', '');

  if (!empty($disabled_urls)) {
    $urls = explode("\n", $disabled_urls);

    foreach ($urls as $url) {
      $url = rtrim($url);
      if ($url == "<front>") {
        $url = "";
      }
      $url = preg_replace('/%/', '.*?', preg_quote($url, '/'));
      $pattern = '/^'. $url .'$/';
      if (preg_match($pattern, $_REQUEST['q'])) {
        return 1;
      }
    }
  }

  return 0;
}

/**
 * Theme function for displaying the lightbox2 trigger image in an imagefield.
 */
function theme_lightbox2_image($path, $alt = '', $title = '', $attributes = NULL) {
  $attributes = drupal_attributes($attributes);
  $image = '<img src="'. file_create_url($path) .'" alt="'.  check_plain($alt) .'" title="'. check_plain($title) .'" '. $attributes .' />';
  return $image;
}

/**
 * Implementation of the imagecache_create_url() function for integration with
 * imagecache module versions prior to imagecache 2.
 */
function lightbox2_imagecache_create_url($preset, $filepath) {
  if (function_exists('imagecache_create_url')) {
    return imagecache_create_url($preset, $filepath);
  }
  else {
    $path = _imagecache_strip_file_directory($filepath);
    return file_create_url(file_directory_path() .'/imagecache/'. $preset .'/'. $path);
  }
}