-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathplusgallery.js
892 lines (747 loc) · 28.2 KB
/
plusgallery.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
/*
* +Gallery Javascript Photo gallery v0.9.4
* http://plusgallery.net/
*
* Copyright 2013, Jeremiah Martin | Twitter: @jeremiahjmartin
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*/
jQuery.ajaxSetup({ cache: false });
/*
SLIDEFADE
------------------------------------------------------------------------------------------------------*/
/* Custom plugin for a slide/in out animation with a fade - JJM */
(function ($) {
$.fn.slideFade = function (speed, callback) {
var slideSpeed;
for (var i = 0; i < arguments.length; i++) {
if (typeof arguments[i] == "number") {
slideSpeed = arguments[i];
}
else {
var callBack = arguments[i];
}
}
if(!slideSpeed) {
slideSpeed = 500;
}
this.animate({
opacity: 'toggle',
height: 'toggle'
}, slideSpeed,
function(){
if( typeof callBack != "function" ) { callBack = function(){}; }
callBack.call(this);
}
);
};
})( jQuery );
(function ($){
$.fn.plusGallery = function(options){
var lmnt = this;
if(lmnt.length === 0) { return false; }
var pg = {
/*user defined Defaults*/
imagePath: 'images/plusgallery',
type: null,
albumTitle: false, //show the album title in single album mode
albumLimit: 16, //Limit amout of albums to load initially.
limit: 30, //Limit of photos to load for gallery / more that 60 is dumb, separate them into different albums
apiKey: '', //used with Flickr
exclude: null,
include: null,
imageData: null,
/*don't touch*/
imgArray: [],
titleArray: [],
t: '', //timer
idx: 0,
imgCount: 0,
imgTotal: 0,
winWidth: 1024, //resets
touch: false,
titleText: '',
init: function(){
var _doc = $(document);
//check for touch device
if ("ontouchstart" in document.documentElement) {
window.scrollTo(0, 1);
pg.touch = true;
}
pg.winWidth = $(window).width();
//reset some shit in case there is another copy that was loaded.
$('#pgzoomview').remove();
//Unbind everything first?
_doc.off("click", ".pgalbumlink, #pgthumbhome, .pgthumb, .pgzoomarrow, .pgzoomclose, #pgzoomview, #pgzoomslide, .pgzoomimg");
pg.getDataAttr();
pg.writeHTML();
if(pg.albumId
|| pg.type == 'instagram'
|| (pg.type == 'local' && !pg.imageData.hasOwnProperty('albums'))){
//load single Album
pg.loadSingleAlbum();
}
else if(pg.type == 'local') {
pg.parseAlbumData(pg.imageData);
}
else {
pg.loadAlbumData();
}
//attach loadGallery to the album links
_doc.on("click", ".pgalbumlink",function(e){
e.preventDefault();
$(this).append('<span class="pgloading"></span>');
var galleryTitle = $(this).children('span').html();
if(pg.type == 'local') {
var galleryID = $(this).attr('data-album-index').replace('http://', '').replace('//', '').replace('https://', '');
pg.parseData(pg.imageData.albums[galleryID],galleryTitle);
} else {
var galleryURL = this.href;
pg.loadGallery(galleryURL,galleryTitle);
}
});
_doc.on("click", "#pgthumbhome",function(e){
e.preventDefault();
$('#pgthumbview').slideFade(700);
$('#pgalbums').slideFade(700);
});
//attach links load detail image
_doc.on('click','.pgthumb',function(e){
e.preventDefault();
var idx = $('.pgthumb').index(this);
pg.loadZoom(idx);
});
/*zoom events*/
_doc.on('click','.pgzoomarrow',function(e){
e.preventDefault();
var dir = this.rel;
pg.prevNext(dir);
return false;
});
_doc.on('click','.pgzoomclose',function(e){
e.preventDefault();
pg.unloadZoom();
});
_doc.on("click", "#pgzoomview",function(e){
e.preventDefault();
pg.unloadZoom();
});
_doc.on("click", "#pgzoomslide",function(){
pg.unloadZoom();
});
_doc.on("click", ".pgzoomimg",function(){
if($(this).attr('id').replace('pgzoomimg', '') < pg.imgTotal - 1) {
pg.prevNext('next');
}
return false;
});
clearTimeout(pg.t);
},
/*--------------------------
get all the user defined
variables from the HTML element
----------------------------*/
getDataAttr: function(){
//Gallery Type *required
var dataAttr = lmnt.attr('data-type');
if(pg.type == null && dataAttr) {
pg.type = dataAttr;
}
else if ( pg.type == null ) {
throw('You must enter a data type.');
}
//Gallery User Id *required if not local
dataAttr = lmnt.attr('data-userid');
if(dataAttr) {
pg.userId = dataAttr;
}
else if(pg.type != 'local') {
throw('You must enter a valid User ID');
}
//Limit on the amount photos per gallery
dataAttr = lmnt.attr('data-limit');
if(dataAttr) {
pg.limit = dataAttr;
}
//Limit on the amount albums
dataAttr = lmnt.attr('data-album-limit');
if(dataAttr) {
pg.albumLimit = dataAttr;
}
//album id to exclude
dataAttr = lmnt.attr('data-exclude');
if(dataAttr) {
pg.exclude = dataAttr.split(',');
}
//album ids to include
dataAttr = lmnt.attr('data-include');
if(dataAttr) {
pg.include = dataAttr.split(',');
}
//Api key - used with Flickr
dataAttr = lmnt.attr('data-api-key');
if(dataAttr) {
pg.apiKey = dataAttr;
}
//Access Token - used with instagram
dataAttr = lmnt.attr('data-access-token');
if(dataAttr) {
pg.accessToken = dataAttr;
}
dataAttr = lmnt.attr('data-album-id');
if(dataAttr) {
pg.albumId = dataAttr;
//show hide the album title if we are in single gallery mode
titleAttr = lmnt.attr('data-album-title');
if(titleAttr == 'true') {
pg.albumTitle = true;
} else {
pg.albumTitle = false;
}
}
dataAttr = lmnt.attr('data-credit');
if(dataAttr == 'false') {
pg.credit = false;
}
//Image path
dataAttr = lmnt.attr('data-image-path');
if(dataAttr) {
pg.imagePath = dataAttr;
}
//JSON string containing image data *required only for local
dataAttr = lmnt.attr('data-image-data');
if(dataAttr) {
pg.imageData = JSON.parse(dataAttr);
}
},
/*--------------------------
set up the initial HTML
----------------------------*/
writeHTML: function(){
var touchClass;
if(pg.touch){
touchClass = 'touch';
lmnt.addClass('touch');
}
else {
touchClass = 'no-touch';
lmnt.addClass('no-touch');
}
lmnt.append(
'<ul id="pgalbums" class="clearfix"></ul>' +
'<div id="pgthumbview">' +
'<ul id="pgthumbs" class="clearfix"></ul>' +
'</div>'
);
$('body').prepend(
'<div id="pgzoomview" class="pg ' + touchClass + '">' +
'<a href="#" rel="previous" id="pgzoomclose" title="Close">Close</a>' +
'<a href="#" rel="previous" id="pgprevious" class="pgzoomarrow" title="previous">Previous</a>' +
'<a href="#" rel="next" id="pgnext" class="pgzoomarrow" title="Next">Next</a>' +
'<div id="pgzoomscroll">' +
'<ul id="pgzoom"></ul>' +
'</div>' +
'</div>'
);
lmnt.addClass('pg');
if(pg.credit === true) {
lmnt.append('<div id="pgcredit"><a href="http://www.plusgallery.net" target="_blank" title="Powered by +GALLERY"><span>+</span>Gallery</a></div>');
}
//console.log('pg.albumTitle: ' + pg.albumTitle);
if(pg.albumTitle === true) {
$('#pgthumbview').prepend('<ul id="pgthumbcrumbs" class="clearfix"><li id="pgthumbhome">«</li></ul>');
}
},
/*--------------------------
Parse the album data from
the given json string.
----------------------------*/
parseAlbumData: function(json) {
lmnt.addClass('loaded');
var objPath,
albumTotal,
galleryImage,
galleryTitle,
galleryJSON;
switch(pg.type)
{
//have to load differently for for google/facebook/flickr
case 'google':
objPath = json.feed.entry;
albumTotal = objPath.length;
if(albumTotal > pg.albumLimit) {
albumTotal = pg.albumLimit;
}
//remove excluded galleries if there are any.
//albumTotal = albumTotal - pg.exclude.length;
if(albumTotal > 0){
$.each(objPath,function(i,obj){
//obj is entry
if(i < albumTotal){
galleryTitle = obj.title.$t;
galleryJSON = obj.link[0].href;
galleryImage = obj.media$group.media$thumbnail[0].url;
galleryImage = galleryImage.replace('s160','s210');
pg.loadAlbums(galleryTitle,galleryImage,galleryJSON,i);
}
});
}
else { //else if albumTotal == 0
throw('There are either no results for albums with this user ID or there was an error loading the data. \n' + galleryJSON);
}
break;
case 'flickr':
objPath = json.photosets.photoset;
albumTotal = objPath.length;
if(albumTotal > pg.albumLimit) {
albumTotal = pg.albumLimit;
}
if(albumTotal > 0 ) {
$.each(objPath,function(i,obj){
//obj is entry
if(i < albumTotal){
galleryTitle = obj.title._content;
galleryImage = 'https://farm' + obj.farm + '.staticflickr.com/' + obj.server + '/' + obj.primary + '_' + obj.secret + '_n.jpg';
galleryJSON = 'https://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key=' + pg.apiKey + '&photoset_id=' + obj.id + '&format=json&jsoncallback=?';
pg.loadAlbums(galleryTitle,galleryImage,galleryJSON);
}
});
}
else { //else if albumTotal == 0
throw('There are either no results for albums with this user ID or there was an error loading the data. \n' + galleryJSON);
}
break;
case 'facebook':
objPath = json.data;
albumTotal = objPath.length;
if(albumTotal > pg.albumLimit) {
albumTotal = pg.albumLimit;
}
if(albumTotal > 0) {
$.each(objPath,function(i,obj){
if(i < albumTotal){
galleryTitle = obj.name;
galleryJSON = 'https://graph.facebook.com/' + obj.id + '/photos?limit=' + pg.limit + '&access_token=' + pg.accessToken;
galleryImage = 'http://graph.facebook.com/' + obj.id + '/picture?type=album';
pg.loadAlbums(galleryTitle,galleryImage,galleryJSON);
}
});
}
else {
throw('There are either no results for albums with this user ID or there was an error loading the data. \n' + albumURL);
}
break;
case 'local':
objPath = json.albums;
albumTotal = objPath.length;
if(albumTotal > pg.albumLimit) {
albumTotal = pg.albumLimit;
}
if(albumTotal > 0 ) {
$.each(objPath,function(i,obj){
//obj is entry
if(i < albumTotal){
galleryTitle = obj.title;
galleryImage = obj.images[0].th;
galleryJSON = 'http://'+i;
pg.loadAlbums(galleryTitle,galleryImage,galleryJSON);
}
});
}
else { //else if albumTotal == 0
throw('There are no albums available in the specified JSON.');
}
break;
}
},
/*--------------------------
Load up Album Data JSON
before Albums
----------------------------*/
loadAlbumData: function() {
var albumURL;
switch(pg.type)
{
case 'google':
albumURL = 'https://picasaweb.google.com/data/feed/base/user/' + pg.userId + '?alt=json&kind=album&hl=en_US&max-results=' + pg.albumLimit + '&callback=?';
break;
case 'flickr':
albumURL = 'https://api.flickr.com/services/rest/?&method=flickr.photosets.getList&api_key=' + pg.apiKey + '&user_id=' + pg.userId + '&format=json&jsoncallback=?';
break;
case 'facebook':
albumURL = 'https://graph.facebook.com/' + pg.userId + '/albums?limit=' + pg.albumLimit + '&access_token=' + pg.accessToken + '&callback=?';
break;
case 'instagram':
//we ain't got no albums in instagram
albumURL = null;
break;
case 'local':
// No album support yet, but url wont be needed anyway.
albumURL = null;
break;
default:
throw('Please define a gallery type.');
}
$.getJSON(albumURL,function(json) {
pg.parseAlbumData(json);
});
},
/*--------------------------
Load all albums to the page
----------------------------*/
loadAlbums: function(galleryTitle,galleryImage,galleryJSON) {
var displayAlbum = true;
var imgHTML;
//exclude albums if pg.exclude is set
if(pg.exclude !== null) {
$.each(pg.exclude,function(index, value){ //exclude albums if pg.exclude is set
if(galleryJSON.indexOf(value) > 0){
displayAlbum = false;
}
});
}
//include only specified albums if pg.include is set
if(pg.include !== null) {
displayAlbum = false;
$.each(pg.include,function(index, value){ //exclude albums if pg.exclude is set
if(galleryJSON.indexOf(value) > 0){
displayAlbum = true;
}
});
}
if (displayAlbum){
if (pg.type == 'facebook' || pg.type == 'flickr') {
imgHTML = '<img src="'+ pg.imagePath + '/square.png" style="background-image: url(' + galleryImage + ');" title="' + galleryTitle + '" title="' + galleryTitle + '" class="pgalbumimg">';
}
else {
imgHTML = '<img src="' + galleryImage + '" title="' + galleryTitle + '" title="' + galleryTitle + '" class="pgalbumimg">';
}
if(pg.type == 'local') {
$('#pgalbums').append(
'<li class="pgalbumthumb">' +
'<a href="#" data-album-index="' + galleryJSON + '" class="pgalbumlink">' + imgHTML + '<span class="pgalbumtitle">' + galleryTitle + '</span><span class="pgplus">+</span></a>' +
'</li>'
);
} else {
$('#pgalbums').append(
'<li class="pgalbumthumb">' +
'<a href="' + galleryJSON + '" class="pgalbumlink">' + imgHTML + '<span class="pgalbumtitle">' + galleryTitle + '</span><span class="pgplus">+</span></a>' +
'</li>'
);
}
}
}, //End loadAlbums
/*--------------------------
Load all the images within
a specific gallery
----------------------------*/
loadSingleAlbum:function(){
var url;
switch(pg.type)
{
case 'google':
url = 'https://picasaweb.google.com/data/feed/base/user/' + pg.userId + '/albumid/' + pg.albumId + '?alt=json&hl=en_US';
pg.loadGallery(url);
break;
case 'flickr':
url = 'https://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key=' + pg.apiKey + '&photoset_id=' + pg.albumId + '&format=json&jsoncallback=?';
pg.loadGallery(url);
break;
case 'facebook':
url = 'https://graph.facebook.com/' + pg.albumId + '/photos?limit=' + pg.limit + '&access_token=' + pg.accessToken;
pg.loadGallery(url);
break;
case 'instagram':
url = 'https://api.instagram.com/v1/users/' + pg.userId + '/media/recent/?access_token=' + pg.accessToken + '&count=' + pg.limit;
pg.loadGallery(url);
break;
case 'local':
pg.parseData(pg.imageData);
break;
}
lmnt.addClass('loaded');
$('#pgthumbhome').hide();
},
/*--------------------------
Load all the images within
a specific gallery
----------------------------*/
loadGallery: function(url,title){
pg.imgArray = [];
pg.titleArray = [];
$('#pgzoom').empty();
$.ajax({
url: url,
cache: false,
dataType: "jsonp",
success: function(json){
pg.parseData(json,title);
}, //end success
error: function(jqXHR, textStatus, errorThrown){
console.log('Error: \njqXHR:' + jqXHR + '\ntextStatus: ' + textStatus + '\nerrorThrown: ' + errorThrown);
}
});
}, //End loadGallery
/*--------------------------
Parse and convert the data
of the gallery
----------------------------*/
parseData: function(json,title){
var obPath,
imgTitle,
imgSrc,
imgTh,
imgBg = '',
thumbsLoaded = 0,
zoomWidth,
flickrImgExt;
$('.crumbtitle').remove();
$('#pgthumbs').empty();
if(title === undefined){
title = ' ';
}
$('#pgthumbcrumbs').append('<li class="crumbtitle">' + title + '</li>');
switch(pg.type)
{
case 'google':
objPath = json.feed.entry;
break;
case 'flickr':
objPath = json.photoset.photo;
break;
case 'facebook':
objPath = json.data;
break;
case 'instagram':
objPath = json.data;
break;
case 'local':
objPath = json.images;
break;
}
pg.imgTotal = objPath.length;
//limit the results
if(pg.limit < pg.imgTotal){
pg.imgTotal = pg.limit;
}
if(pg.imgTotal === 0) {
throw('Please check your photo permissions,\nor make sure there are photos in this gallery.');
}
if(pg.winWidth > 1100) {
zoomWidth = 1024;
flickrImgExt = '_b';
} else if(pg.winWidth > 620) {
zoomWidth = 768;
flickrImgExt = '_b';
} else {
zoomWidth = 540;
flickrImgExt = '_z';
}
$.each(objPath,function(i,obj){
//limit the results
if(i < pg.limit) {
switch(pg.type)
{
case 'google':
imgTitle = obj.title.$t;
imgSrc = obj.media$group.media$content[0].url;
var lastSlash = imgSrc.lastIndexOf('/');
var imgSrcSubString =imgSrc.substring(lastSlash);
//show the max width image 1024 in this case
imgSrc = imgSrc.replace(imgSrcSubString, '/s' + zoomWidth + imgSrcSubString);
imgTh = obj.media$group.media$thumbnail[1].url;
imgTh = imgTh.replace('s144','s160-c');
break;
case 'flickr':
imgTitle = obj.title;
imgSrc = 'http://farm' + obj.farm + '.staticflickr.com/' + obj.server + '/' + obj.id + '_' + obj.secret + flickrImgExt + '.jpg';
imgTh = 'http://farm' + obj.farm + '.staticflickr.com/' + obj.server + '/' + obj.id + '_' + obj.secret + '_q.jpg';
break;
case 'facebook':
imgTitle = obj.name;
imgSrc = obj.images[1].source;
imgTh = pg.imagePath + '/square.png';
imgBg = ' style="background: url(' + obj.images[3].source + ') no-repeat 50% 50%; background-size: cover;"';
break;
case 'instagram':
if(obj.caption !== null){
imgTitle = obj.caption.text;
}
imgSrc = obj.images.standard_resolution.url;
imgTh = obj.images.low_resolution.url;
break;
case 'local':
if(obj.caption !== null){
imgTitle = obj.caption;
}
imgSrc = obj.src;
imgTh = obj.th;
break;
}
if(!imgTitle) {
imgTitle = '';
}
pg.imgArray[i] = imgSrc;
pg.titleArray[i] = imgTitle;
$('#pgthumbs').append('<li class="pgthumb"><a href="' + imgSrc + '"><img src="' + imgTh + '" id="pgthumbimg' + i + '" class="pgthumbimg" alt="' + imgTitle + '" title="' + imgTitle + '"' + imgBg + '></a></li>');
//check to make sure all the images are loaded and if so show the thumbs
$('#pgthumbimg' + i).load(function(){
thumbsLoaded++;
if(thumbsLoaded == pg.imgTotal) {
$('#pgalbums').slideFade(700,function(){
$('.pgalbumthumb .pgloading').remove();
});
$('#pgthumbview').slideFade(700);
}
});
} //end if(i < pg.limit)
}); //end each
},
zoomIdx: null, //the zoom index
zoomImagesLoaded: [],
zoomScrollDir: null,
zoomScrollLeft: 0,
loadZoom: function(idx){
pg.zoomIdx = idx;
pg.winWidth = $(window).width();
var pgZoomView = $('#pgzoomview'),
pgZoomScroll = $('#pgzoomscroll'),
pgPrevious = $('#pgprevious'),
pgNext = $('#pgnext'),
pgZoom = $('#pgzoom'),
pgZoomHTML = '',
totalImages = pg.imgArray.length;
pgZoomView.addClass('fixed');
//show/hide the prev/next links
if(idx === 0) {
pgPrevious.hide();
}
if(idx == totalImages - 1) {
pgNext.hide();
}
var pgzoomWidth = pg.imgArray.length * pg.winWidth;
$('#pgzoom').width(pgzoomWidth);
var scrollLeftInt = parseInt(idx * pg.winWidth);
pgZoomView.fadeIn(function(){
//this has gotta come in after the fade or iOS blows up.
$(window).on('resize',pg.resizeZoom);
$.each(pg.imgArray,function(i){
pgZoomHTML = pgZoomHTML + '<li class="pgzoomslide loading" id="pgzoomslide' + i + '" style="width: ' + pg.winWidth + 'px;"><img src="' + pg.imagePath + '/square.gif" class="pgzoomspacer"><span class="pgzoomcaption">' + pg.titleArray[i] + '</span></li>';
if(i + 1 == pg.imgArray.length) {
//at the end of the loop
$('#pgzoom').html(pgZoomHTML);
pg.zoomKeyPress();
$('#pgzoomscroll').scrollLeft(scrollLeftInt);
pg.zoomScrollLeft = scrollLeftInt;
pg.loadZoomImg(idx);
pg.zoomScroll();
//load siblings
if((idx - 1) >= 0){
pg.loadZoomImg(idx - 1);
}
if((idx + 1) < pg.imgArray.length){
pg.loadZoomImg(idx + 1);
}
}
});
});
},
loadZoomImg:function(idx){
if($('#pgzoomimg' + idx).length === 0){
$('#pgzoomslide' + idx + ' .pgzoomspacer').after('<img src="' + pg.imgArray[idx] + '" data-src="' + pg.imgArray[idx] + '" title="' + pg.titleArray[idx] + '" alt="' + pg.titleArray[idx] + '" id="pgzoomimg' + idx + '" class="pgzoomimg">');
$('#pgzoomimg' + idx).load(function(){
$(this).addClass('active');
});
}
},
zoomScroll:function(){
var pgPrevious = $('#pgprevious'),
pgNext = $('#pgnext'),
scrollTimeout,
canLoadZoom = true;
$('#pgzoomscroll').on('scroll',function(){
currentScrollLeft = $(this).scrollLeft();
if(canLoadZoom === true) {
canLoadZoom = false;
scrollTimeout = setTimeout(function(){
if(currentScrollLeft === 0){
pgPrevious.fadeOut();
}
else {
pgPrevious.fadeIn();
}
if(currentScrollLeft >= (pg.imgTotal - 1) * pg.winWidth){
pgNext.fadeOut();
}
else {
pgNext.fadeIn();
}
/*Check if we have scrolled left and if so load up the zoom image*/
if(currentScrollLeft % pg.zoomScrollLeft > 20 || (currentScrollLeft > 0 && pg.zoomScrollLeft === 0)){
pg.zoomScrollLeft = currentScrollLeft;
var currentIdx = pg.zoomScrollLeft / pg.winWidth;
var currentIdxCeil = Math.ceil(currentIdx);
var currentIdxFloor = Math.floor(currentIdx);
//Lazy load siblings on scroll.
if(!pg.zoomImagesLoaded[currentIdxCeil]) {
pg.loadZoomImg(currentIdxCeil);
}
if(!pg.zoomImagesLoaded[currentIdxFloor]){
pg.loadZoomImg(currentIdxFloor);
}
}
canLoadZoom = true;
},200);
}
});
},
zoomKeyPress: function(){
$(document).on('keyup','body',function(e){
if(e.which == 27){
pg.unloadZoom();
}
else
if(e.which == 37){
pg.prevNext('previous');
}
else
if(e.which == 39){
pg.prevNext('next');
}
});
},
resizeZoom: function(){
pg.winWidth = $(window).width();
var pgzoomWidth = pg.imgArray.length * pg.winWidth;
$('#pgzoom').width(pgzoomWidth);
$('.pgzoomslide').width(pg.winWidth);
var scrollLeftInt = parseInt(pg.zoomIdx * pg.winWidth);
$('#pgzoomscroll').scrollLeft(scrollLeftInt);
},
unloadZoom: function(){
$(document).off('keyup','body');
$(window).off('resize',pg.resizeZoom);
$('#pgzoomscroll').off('scroll');
$('#pgzoomview').fadeOut(function(){
$('#pgzoom').empty();
$('#pgzoomview').off('keyup');
$('#pgzoomview').removeClass('fixed');
});
},
prevNext: function(dir){
var currentIdx = $('#pgzoomscroll').scrollLeft() / pg.winWidth;
if(dir == "previous"){
pg.zoomIdx = Math.round(currentIdx) - 1;
}
else {
pg.zoomIdx = Math.round(currentIdx) + 1;
}
var newScrollLeft = pg.zoomIdx * pg.winWidth;
$('#pgzoomscroll').stop().animate({scrollLeft:newScrollLeft});
}
};
$.extend(pg, options);
pg.init();
};
})( jQuery );