-
Notifications
You must be signed in to change notification settings - Fork 11
/
imageflow.js
1463 lines (1277 loc) · 38.2 KB
/
imageflow.js
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
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Name: ImageFlow
Version: 1.3.0 (March 9 2010)
Author: Finn Rudolph
Support: http://finnrudolph.de/ImageFlow
License: ImageFlow is licensed under a Creative Commons
Attribution-Noncommercial 3.0 Unported License
(http://creativecommons.org/licenses/by-nc/3.0/).
You are free:
+ to Share - to copy, distribute and transmit the work
+ to Remix - to adapt the work
Under the following conditions:
+ Attribution. You must attribute the work in the manner specified by the author or licensor
(but not in any way that suggests that they endorse you or your use of the work).
+ Noncommercial. You may not use this work for commercial purposes.
+ For any reuse or distribution, you must make clear to others the license terms of this work.
+ Any of the above conditions can be waived if you get permission from the copyright holder.
+ Nothing in this license impairs or restricts the author's moral rights.
Credits: This script is based on Michael L. Perrys Cover flow in Javascript [1].
The reflections are generated server-sided by a slightly hacked version
of Richard Daveys easyreflections [2] written in PHP. The mouse wheel
support is an implementation of Adomas Paltanavicius JavaScript mouse
wheel code [3]. It also uses the domReadyEvent from Tanny O'Haley [4].
[1] http://www.adventuresinsoftware.com/blog/?p=104#comment-1981
[2] http://reflection.corephp.co.uk/v2.php
[3] http://adomas.org/javascript-mouse-wheel/
[4] http://tanny.ica.com/ICA/TKO/tkoblog.nsf/dx/domcontentloaded-for-browsers-part-v
*/
/* ImageFlow constructor */
function ImageFlow ()
{
/* Setting option defaults */
this.defaults =
{
animationSpeed: 50, /* Animation speed in ms */
aspectRatio: 1.964, /* Aspect ratio of the ImageFlow container (width divided by height) */
buttons: false, /* Toggle navigation buttons */
captions: true, /* Toggle captions */
circular: false, /* Toggle circular rotation */
imageCursor: 'default', /* Cursor type for all images - default is 'default' */
ImageFlowID: 'imageflow', /* Default id of the ImageFlow container */
imageFocusM: 1.0, /* Multiplicator for the focussed image size in percent */
imageFocusMax: 4, /* Max number of images on each side of the focussed one */
imagePath: '', /* Path to the images relative to the reflect_.php script */
imageScaling: true, /* Toggle image scaling */
imagesHeight: 0.67, /* Height of the images div container in percent */
imagesM: 1.0, /* Multiplicator for all images in percent */
onClick: function() { document.location = this.url; }, /* Onclick behaviour */
opacity: false, /* Toggle image opacity */
opacityArray: [10,8,6,4,2], /* Image opacity (range: 0 to 10) first value is for the focussed image */
percentLandscape: 118, /* Scale landscape format */
percentOther: 100, /* Scale portrait and square format */
preloadImages: true, /* Toggles loading bar (false: requires img attributes height and width) */
reflections: true, /* Toggle reflections */
reflectionGET: '', /* Pass variables via the GET method to the reflect_.php script */
reflectionP: 0.5, /* Height of the reflection in percent of the source image */
reflectionPNG: false, /* Toggle reflect2.php or reflect3.php */
reflectPath: '', /* Path to the reflect_.php script */
scrollbarP: 0.6, /* Width of the scrollbar in percent */
slider: true, /* Toggle slider */
sliderCursor: 'e-resize', /* Slider cursor type - default is 'default' */
sliderWidth: 14, /* Width of the slider in px */
slideshow: false, /* Toggle slideshow */
slideshowSpeed: 1500, /* Time between slides in ms */
slideshowAutoplay: false, /* Toggle automatic slideshow play on startup */
startID: 1, /* Image ID to begin with */
glideToStartID: true, /* Toggle glide animation to start ID */
startAnimation: false, /* Animate images moving in from the right on startup */
xStep: 150 /* Step width on the x-axis in px */
};
/* Closure for this */
var my = this;
/* Initiate ImageFlow */
this.init = function (options)
{
/* Evaluate options */
for(var name in my.defaults)
{
this[name] = (options !== undefined && options[name] !== undefined) ? options[name] : my.defaults[name];
}
/* Try to get ImageFlow div element */
var ImageFlowDiv = document.getElementById(my.ImageFlowID);
if(ImageFlowDiv)
{
/* Set it global within the ImageFlow scope */
ImageFlowDiv.style.visibility = 'visible';
this.ImageFlowDiv = ImageFlowDiv;
/* Try to create XHTML structure */
if(this.createStructure())
{
this.imagesDiv = document.getElementById(my.ImageFlowID+'_images');
this.captionDiv = document.getElementById(my.ImageFlowID+'_caption');
this.navigationDiv = document.getElementById(my.ImageFlowID+'_navigation');
this.scrollbarDiv = document.getElementById(my.ImageFlowID+'_scrollbar');
this.sliderDiv = document.getElementById(my.ImageFlowID+'_slider');
this.buttonNextDiv = document.getElementById(my.ImageFlowID+'_next');
this.buttonPreviousDiv = document.getElementById(my.ImageFlowID+'_previous');
this.buttonSlideshow = document.getElementById(my.ImageFlowID+'_slideshow');
this.indexArray = [];
this.current = 0;
this.imageID = 0;
this.target = 0;
this.memTarget = 0;
this.firstRefresh = true;
this.firstCheck = true;
this.busy = false;
/* Set height of the ImageFlow container and center the loading bar */
var width = this.ImageFlowDiv.offsetWidth;
var height = Math.round(width / my.aspectRatio);
document.getElementById(my.ImageFlowID+'_loading_txt').style.paddingTop = ((height * 0.5) -22) + 'px';
ImageFlowDiv.style.height = height + 'px';
/* Init loading progress */
this.loadingProgress();
}
}
};
/* Create HTML Structure */
this.createStructure = function()
{
/* Create images div container */
var imagesDiv = my.Helper.createDocumentElement('div','images');
/* Shift all images into the images div */
var node, version, src, imageNode;
var max = my.ImageFlowDiv.childNodes.length;
for(var index = 0; index < max; index++)
{
node = my.ImageFlowDiv.childNodes[index];
if (node && node.nodeType == 1 && node.nodeName == 'IMG')
{
/* Add 'reflect.php?img=' */
if(my.reflections === true)
{
version = (my.reflectionPNG) ? '3' : '2';
src = my.imagePath+node.getAttribute('src',2);
src = my.reflectPath+'reflect'+version+'.php?img='+src+my.reflectionGET;
node.setAttribute('src',src);
}
/* Clone image nodes and append them to the images div */
imageNode = node.cloneNode(true);
imagesDiv.appendChild(imageNode);
}
}
/* Clone some more images to make a circular animation possible */
if(my.circular)
{
/* Create temporary elements to hold the cloned images */
var first = my.Helper.createDocumentElement('div','images');
var last = my.Helper.createDocumentElement('div','images');
/* Make sure, that there are enough images to use circular mode */
max = imagesDiv.childNodes.length;
if(max < my.imageFocusMax)
{
my.imageFocusMax = max;
}
/* Do not clone anything if there is only one image */
if(max > 1)
{
/* Clone the first and last images */
var i;
for(i = 0; i < max; i++)
{
/* Number of clones on each side equals the imageFocusMax */
node = imagesDiv.childNodes[i];
if(i < my.imageFocusMax)
{
imageNode = node.cloneNode(true);
first.appendChild(imageNode);
}
if(max-i < my.imageFocusMax+1)
{
imageNode = node.cloneNode(true);
last.appendChild(imageNode);
}
}
/* Sort the image nodes in the following order: last | originals | first */
for(i = 0; i < max; i++)
{
node = imagesDiv.childNodes[i];
imageNode = node.cloneNode(true);
last.appendChild(imageNode);
}
for(i = 0; i < my.imageFocusMax; i++)
{
node = first.childNodes[i];
imageNode = node.cloneNode(true);
last.appendChild(imageNode);
}
/* Overwrite the imagesDiv with the new order */
imagesDiv = last;
}
}
/* Create slideshow button div and append it to the images div */
if(my.slideshow)
{
var slideshowButton = my.Helper.createDocumentElement('div','slideshow');
imagesDiv.appendChild(slideshowButton);
}
/* Create loading text container */
var loadingP = my.Helper.createDocumentElement('p','loading_txt');
var loadingText = document.createTextNode(' ');
loadingP.appendChild(loadingText);
/* Create loading div container */
var loadingDiv = my.Helper.createDocumentElement('div','loading');
/* Create loading bar div container inside the loading div */
var loadingBarDiv = my.Helper.createDocumentElement('div','loading_bar');
loadingDiv.appendChild(loadingBarDiv);
/* Create captions div container */
var captionDiv = my.Helper.createDocumentElement('div','caption');
/* Create slider and button div container inside the scrollbar div */
var scrollbarDiv = my.Helper.createDocumentElement('div','scrollbar');
var sliderDiv = my.Helper.createDocumentElement('div','slider');
scrollbarDiv.appendChild(sliderDiv);
if(my.buttons)
{
var buttonPreviousDiv = my.Helper.createDocumentElement('div','previous', 'button');
var buttonNextDiv = my.Helper.createDocumentElement('div','next', 'button');
scrollbarDiv.appendChild(buttonPreviousDiv);
scrollbarDiv.appendChild(buttonNextDiv);
}
/* Create navigation div container beneath images div */
var navigationDiv = my.Helper.createDocumentElement('div','navigation');
navigationDiv.appendChild(captionDiv);
navigationDiv.appendChild(scrollbarDiv);
/* Update document structure and return true on success */
var success = false;
if (my.ImageFlowDiv.appendChild(imagesDiv) &&
my.ImageFlowDiv.appendChild(loadingP) &&
my.ImageFlowDiv.appendChild(loadingDiv) &&
my.ImageFlowDiv.appendChild(navigationDiv))
{
/* Remove image nodes outside the images div */
max = my.ImageFlowDiv.childNodes.length;
for(index = 0; index < max; index++)
{
node = my.ImageFlowDiv.childNodes[index];
if (node && node.nodeType == 1 && node.nodeName == 'IMG')
{
my.ImageFlowDiv.removeChild(node);
}
}
success = true;
}
return success;
};
/* Manage loading progress and call the refresh function */
this.loadingProgress = function()
{
var p = my.loadingStatus();
if((p < 100 || my.firstCheck) && my.preloadImages)
{
/* Insert a short delay if the browser loads rapidly from its cache */
if(my.firstCheck && p == 100)
{
my.firstCheck = false;
window.setTimeout(my.loadingProgress, 100);
}
else
{
window.setTimeout(my.loadingProgress, 40);
}
}
else
{
/* Hide loading elements */
document.getElementById(my.ImageFlowID+'_loading_txt').style.display = 'none';
document.getElementById(my.ImageFlowID+'_loading').style.display = 'none';
/* Refresh ImageFlow on window resize - delay adding this event for the IE */
window.setTimeout(my.Helper.addResizeEvent, 1000);
/* Call refresh once on startup to display images */
my.refresh();
/* Only initialize navigation elements if there is more than one image */
if(my.max > 1)
{
/* Initialize mouse, touch and key support */
my.MouseWheel.init();
my.MouseDrag.init();
my.Touch.init();
my.Key.init();
/* Toggle slideshow */
if(my.slideshow)
{
my.Slideshow.init();
}
/* Toggle scrollbar visibility */
if(my.slider)
{
my.scrollbarDiv.style.visibility = 'visible';
}
}
}
};
/* Return loaded images in percent, set loading bar width and loading text */
this.loadingStatus = function()
{
var max = my.imagesDiv.childNodes.length;
var i = 0, completed = 0;
var image = null;
for(var index = 0; index < max; index++)
{
image = my.imagesDiv.childNodes[index];
if(image && image.nodeType == 1 && image.nodeName == 'IMG')
{
if(image.complete)
{
completed++;
}
i++;
}
}
var finished = Math.round((completed/i)*100);
var loadingBar = document.getElementById(my.ImageFlowID+'_loading_bar');
loadingBar.style.width = finished+'%';
/* Do not count the cloned images */
if(my.circular)
{
i = i - (my.imageFocusMax*2);
completed = (finished < 1) ? 0 : Math.round((i/100)*finished);
}
var loadingP = document.getElementById(my.ImageFlowID+'_loading_txt');
var loadingTxt = document.createTextNode('loading images '+completed+'/'+i);
loadingP.replaceChild(loadingTxt,loadingP.firstChild);
return finished;
};
/* Cache EVERYTHING that only changes on refresh or resize of the window */
this.refresh = function()
{
/* Cache global variables */
this.imagesDivWidth = my.imagesDiv.offsetWidth+my.imagesDiv.offsetLeft;
this.maxHeight = Math.round(my.imagesDivWidth / my.aspectRatio);
this.maxFocus = my.imageFocusMax * my.xStep;
this.size = my.imagesDivWidth * 0.5;
this.sliderWidth = my.sliderWidth * 0.5;
this.scrollbarWidth = (my.imagesDivWidth - ( Math.round(my.sliderWidth) * 2)) * my.scrollbarP;
this.imagesDivHeight = Math.round(my.maxHeight * my.imagesHeight);
/* Change imageflow div properties */
my.ImageFlowDiv.style.height = my.maxHeight + 'px';
/* Change images div properties */
my.imagesDiv.style.height = my.imagesDivHeight + 'px';
/* Change images div properties */
my.navigationDiv.style.height = (my.maxHeight - my.imagesDivHeight) + 'px';
/* Change captions div properties */
my.captionDiv.style.width = my.imagesDivWidth + 'px';
my.captionDiv.style.paddingTop = Math.round(my.imagesDivWidth * 0.02) + 'px';
/* Change scrollbar div properties */
my.scrollbarDiv.style.width = my.scrollbarWidth + 'px';
my.scrollbarDiv.style.marginTop = Math.round(my.imagesDivWidth * 0.02) + 'px';
my.scrollbarDiv.style.marginLeft = Math.round(my.sliderWidth + ((my.imagesDivWidth - my.scrollbarWidth)/2)) + 'px';
/* Set slider attributes */
my.sliderDiv.style.cursor = my.sliderCursor;
my.sliderDiv.onmousedown = function () { my.MouseDrag.start(this); return false;};
if(my.buttons)
{
my.buttonPreviousDiv.onclick = function () { my.MouseWheel.handle(1); };
my.buttonNextDiv.onclick = function () { my.MouseWheel.handle(-1); };
}
/* Set the reflection multiplicator */
var multi = (my.reflections === true) ? my.reflectionP + 1 : 1;
/* Set image attributes */
var max = my.imagesDiv.childNodes.length;
var i = 0;
var image = null;
for (var index = 0; index < max; index++)
{
image = my.imagesDiv.childNodes[index];
if(image !== null && image.nodeType == 1 && image.nodeName == 'IMG')
{
this.indexArray[i] = index;
/* Set image attributes to store values */
image.url = image.getAttribute('longdesc');
image.xPosition = (-i * my.xStep);
image.i = i;
/* Add width and height as attributes only once */
if(my.firstRefresh)
{
if(image.getAttribute('width') !== null && image.getAttribute('height') !== null)
{
image.w = image.getAttribute('width');
image.h = image.getAttribute('height') * multi;
}
else{
image.w = image.width;
image.h = image.height;
}
}
/* Check source image format. Get image height minus reflection height! */
if((image.w) > (image.h / (my.reflectionP + 1)))
{
/* Landscape format */
image.pc = my.percentLandscape;
image.pcMem = my.percentLandscape;
}
else
{
/* Portrait and square format */
image.pc = my.percentOther;
image.pcMem = my.percentOther;
}
/* Change image positioning */
if(my.imageScaling === false)
{
image.style.position = 'relative';
image.style.display = 'inline';
}
/* Set image cursor type */
image.style.cursor = my.imageCursor;
i++;
}
}
this.max = my.indexArray.length;
/* Override dynamic sizes based on the first image */
if(my.imageScaling === false)
{
image = my.imagesDiv.childNodes[my.indexArray[0]];
/* Set left padding for the first image */
this.totalImagesWidth = image.w * my.max;
image.style.paddingLeft = (my.imagesDivWidth/2) + (image.w/2) + 'px';
/* Override images and navigation div height */
my.imagesDiv.style.height = image.h + 'px';
my.navigationDiv.style.height = (my.maxHeight - image.h) + 'px';
}
/* Handle startID on the first refresh */
if(my.firstRefresh)
{
/* Reset variable */
my.firstRefresh = false;
/* Set imageID to the startID */
my.imageID = my.startID-1;
if (my.imageID < 0 )
{
my.imageID = 0;
}
/* Map image id range in cicular mode (ignore the cloned images) */
if(my.circular)
{
my.imageID = my.imageID + my.imageFocusMax;
}
/* Make sure, that the id is smaller than the image count */
maxId = (my.circular) ? (my.max-(my.imageFocusMax))-1 : my.max-1;
if (my.imageID > maxId)
{
my.imageID = maxId;
}
/* Toggle glide animation to start ID */
if(my.glideToStartID === false)
{
my.moveTo(-my.imageID * my.xStep);
}
/* Animate images moving in from the right */
if(my.startAnimation)
{
my.moveTo(5000);
}
}
/* Only animate if there is more than one image */
if(my.max > 1)
{
my.glideTo(my.imageID);
}
/* Display images in current order */
my.moveTo(my.current);
};
/* Main animation function */
this.moveTo = function(x)
{
this.current = x;
this.zIndex = my.max;
/* Main loop */
for (var index = 0; index < my.max; index++)
{
var image = my.imagesDiv.childNodes[my.indexArray[index]];
var currentImage = index * -my.xStep;
/* Enabled image scaling */
if(my.imageScaling)
{
/* Don't display images that are not conf_focussed */
if ((currentImage + my.maxFocus) < my.memTarget || (currentImage - my.maxFocus) > my.memTarget)
{
image.style.visibility = 'hidden';
image.style.display = 'none';
}
else
{
var z = (Math.sqrt(10000 + x * x) + 100) * my.imagesM;
var xs = x / z * my.size + my.size;
/* Still hide images until they are processed, but set display style to block */
image.style.display = 'block';
/* Process new image height and width */
var newImageH = (image.h / image.w * image.pc) / z * my.size;
var newImageW = 0;
switch (newImageH > my.maxHeight)
{
case false:
newImageW = image.pc / z * my.size;
break;
default:
newImageH = my.maxHeight;
newImageW = image.w * newImageH / image.h;
break;
}
var newImageTop = (my.imagesDivHeight - newImageH) + ((newImageH / (my.reflectionP + 1)) * my.reflectionP);
/* Set new image properties */
image.style.left = xs - (image.pc / 2) / z * my.size + 'px';
if(newImageW && newImageH)
{
image.style.height = newImageH + 'px';
image.style.width = newImageW + 'px';
image.style.top = newImageTop + 'px';
}
image.style.visibility = 'visible';
/* Set image layer through zIndex */
switch ( x < 0 )
{
case true:
this.zIndex++;
break;
default:
this.zIndex = my.zIndex - 1;
break;
}
/* Change zIndex and onclick function of the focussed image */
switch ( image.i == my.imageID )
{
case false:
image.onclick = function() { my.glideTo(this.i);};
break;
default:
this.zIndex = my.zIndex + 1;
if(image.url !== '')
{
image.onclick = my.onClick;
}
break;
}
image.style.zIndex = my.zIndex;
}
}
/* Disabled image scaling */
else
{
if ((currentImage + my.maxFocus) < my.memTarget || (currentImage - my.maxFocus) > my.memTarget)
{
image.style.visibility = 'hidden';
}
else
{
image.style.visibility = 'visible';
/* Change onclick function of the focussed image */
switch ( image.i == my.imageID )
{
case false:
image.onclick = function() { my.glideTo(this.i);};
break;
default:
if(image.url !== '')
{
image.onclick = my.onClick;
}
break;
}
}
my.imagesDiv.style.marginLeft = (x - my.totalImagesWidth) + 'px';
}
x += my.xStep;
}
};
/* Initializes image gliding animation */
this.glideTo = function(imageID)
{
/* Check for jumppoints */
var jumpTarget, clonedImageID;
if(my.circular)
{
/* Trigger left jumppoint */
if(imageID+1 === my.imageFocusMax)
{
/* Set jump target to the same cloned image on the right */
clonedImageID = my.max - my.imageFocusMax;
jumpTarget = -clonedImageID * my.xStep;
/* Set the imageID to the last image */
imageID = clonedImageID-1 ;
}
/* Trigger right jumppoint */
if(imageID === (my.max - my.imageFocusMax))
{
/* Set jump target to the same cloned image on the left */
clonedImageID = my.imageFocusMax-1;
jumpTarget = -clonedImageID * my.xStep;
/* Set the imageID to the first image */
imageID = clonedImageID+1;
}
}
/* Calculate new image position target */
var x = -imageID * my.xStep;
this.target = x;
this.memTarget = x;
this.imageID = imageID;
/* Display new caption */
var caption = my.imagesDiv.childNodes[imageID].getAttribute('alt');
if (caption === '' || my.captions === false)
{
caption = ' ';
}
my.captionDiv.innerHTML = caption;
/* Set scrollbar slider to new position */
if (my.MouseDrag.busy === false)
{
if(my.circular)
{
this.newSliderX = ((imageID-my.imageFocusMax) * my.scrollbarWidth) / (my.max-(my.imageFocusMax*2)-1) - my.MouseDrag.newX;
}
else
{
this.newSliderX = (imageID * my.scrollbarWidth) / (my.max-1) - my.MouseDrag.newX;
}
my.sliderDiv.style.marginLeft = (my.newSliderX - my.sliderWidth) + 'px';
}
/* Only process if opacity or a multiplicator for the focussed image has been set */
if(my.opacity === true || my.imageFocusM !== my.defaults.imageFocusM)
{
/* Set opacity for centered image */
my.Helper.setOpacity(my.imagesDiv.childNodes[imageID], my.opacityArray[0]);
my.imagesDiv.childNodes[imageID].pc = my.imagesDiv.childNodes[imageID].pc * my.imageFocusM;
/* Set opacity for the other images that are displayed */
var opacityValue = 0;
var rightID = 0;
var leftID = 0;
var last = my.opacityArray.length;
for (var i = 1; i < (my.imageFocusMax+1); i++)
{
if((i+1) > last)
{
opacityValue = my.opacityArray[last-1];
}
else
{
opacityValue = my.opacityArray[i];
}
rightID = imageID + i;
leftID = imageID - i;
if (rightID < my.max)
{
my.Helper.setOpacity(my.imagesDiv.childNodes[rightID], opacityValue);
my.imagesDiv.childNodes[rightID].pc = my.imagesDiv.childNodes[rightID].pcMem;
}
if (leftID >= 0)
{
my.Helper.setOpacity(my.imagesDiv.childNodes[leftID], opacityValue);
my.imagesDiv.childNodes[leftID].pc = my.imagesDiv.childNodes[leftID].pcMem;
}
}
}
/* Move the images to the jump target */
if(jumpTarget)
{
my.moveTo(jumpTarget);
}
/* Animate gliding to new x position */
if (my.busy === false)
{
my.busy = true;
my.animate();
}
};
/* Animates image gliding */
this.animate = function()
{
switch (my.target < my.current-1 || my.target > my.current+1)
{
case true:
my.moveTo(my.current + (my.target-my.current)/3);
window.setTimeout(my.animate, my.animationSpeed);
my.busy = true;
break;
default:
my.busy = false;
break;
}
};
/* Used by user events to call the glideTo function */
this.glideOnEvent = function(imageID)
{
/* Interrupt slideshow on mouse wheel, keypress, touch and mouse drag */
if(my.slideshow)
{
my.Slideshow.interrupt();
}
/* Glide to new imageID */
my.glideTo(imageID);
};
/* Slideshow function */
this.Slideshow =
{
direction: 1,
init: function()
{
/* Call start() if autoplay is enabled, stop() if it is disabled */
(my.slideshowAutoplay) ? my.Slideshow.start() : my.Slideshow.stop();
},
interrupt: function()
{
/* Remove interrupt event */
my.Helper.removeEvent(my.ImageFlowDiv,'click',my.Slideshow.interrupt);
/* Interrupt the slideshow */
my.Slideshow.stop();
},
addInterruptEvent: function()
{
/* A click anywhere inside the ImageFlow div interrupts the slideshow */
my.Helper.addEvent(my.ImageFlowDiv,'click',my.Slideshow.interrupt);
},
start: function()
{
/* Set button style to pause */
my.Helper.setClassName(my.buttonSlideshow, 'slideshow pause');
/* Set onclick behaviour to stop */
my.buttonSlideshow.onclick = function () { my.Slideshow.stop(); };
/* Set slide interval */
my.Slideshow.action = window.setInterval(my.Slideshow.slide, my.slideshowSpeed);
/* Allow the user to always interrupt the slideshow */
window.setTimeout(my.Slideshow.addInterruptEvent, 100);
},
stop: function()
{
/* Set button style to play */
my.Helper.setClassName(my.buttonSlideshow, 'slideshow play');
/* Set onclick behaviour to start */
my.buttonSlideshow.onclick = function () { my.Slideshow.start(); };
/* Clear slide interval */
window.clearInterval(my.Slideshow.action);
},
slide: function()
{
var newImageID = my.imageID + my.Slideshow.direction;
var reverseDirection = false;
/* Reverse direction at the last image on the right */
if(newImageID === my.max)
{
my.Slideshow.direction = -1;
reverseDirection = true;
}
/* Reverse direction at the last image on the left */
if(newImageID < 0)
{
my.Slideshow.direction = 1;
reverseDirection = true;
}
/* If direction is reversed recall this method, else call the glideTo method */
(reverseDirection) ? my.Slideshow.slide() : my.glideTo(newImageID);
}
};
/* Mouse Wheel support */
this.MouseWheel =
{
init: function()
{
/* Init mouse wheel listener */
if(window.addEventListener)
{
my.ImageFlowDiv.addEventListener('DOMMouseScroll', my.MouseWheel.get, false);
}
my.Helper.addEvent(my.ImageFlowDiv,'mousewheel',my.MouseWheel.get);
},
get: function(event)
{
var delta = 0;
if (!event)
{
event = window.event;
}
if (event.wheelDelta)
{
delta = event.wheelDelta / 120;
}
else if (event.detail)
{
delta = -event.detail / 3;
}
if (delta)
{
my.MouseWheel.handle(delta);
}
my.Helper.suppressBrowserDefault(event);
},
handle: function(delta)
{
var change = false;
var newImageID = 0;
if(delta > 0)
{
if(my.imageID >= 1)
{
newImageID = my.imageID -1;
change = true;
}
}
else
{
if(my.imageID < (my.max-1))
{
newImageID = my.imageID +1;
change = true;
}
}
/* Glide to next (mouse wheel down) / previous (mouse wheel up) image */
if(change)
{
my.glideOnEvent(newImageID);
}
}
};
/* Mouse Dragging */
this.MouseDrag =
{
object: null,
objectX: 0,
mouseX: 0,
newX: 0,
busy: false,
/* Init mouse event listener */
init: function()
{
my.Helper.addEvent(my.ImageFlowDiv,'mousemove',my.MouseDrag.drag);
my.Helper.addEvent(my.ImageFlowDiv,'mouseup',my.MouseDrag.stop);
my.Helper.addEvent(document,'mouseup',my.MouseDrag.stop);
/* Avoid text and image selection while dragging */
my.ImageFlowDiv.onselectstart = function ()
{
var selection = true;
if (my.MouseDrag.busy)
{
selection = false;
}
return selection;
};
},
start: function(o)
{
my.MouseDrag.object = o;
my.MouseDrag.objectX = my.MouseDrag.mouseX - o.offsetLeft + my.newSliderX;
},
stop: function()
{
my.MouseDrag.object = null;
my.MouseDrag.busy = false;
},
drag: function(e)
{
var posx = 0;
if (!e)
{
e = window.event;
}
if (e.pageX)
{
posx = e.pageX;
}
else if (e.clientX)
{
posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
}
my.MouseDrag.mouseX = posx;