annotate bin/js/jquery.galleriffic.js @ 52:945d903dad5a draft

Uploaded
author romaingred
date Fri, 01 Dec 2017 09:33:36 -0500
parents 198009598544
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
198009598544 Uploaded
romaingred
parents:
diff changeset
1 /**
198009598544 Uploaded
romaingred
parents:
diff changeset
2 * jQuery Galleriffic plugin
198009598544 Uploaded
romaingred
parents:
diff changeset
3 *
198009598544 Uploaded
romaingred
parents:
diff changeset
4 * Copyright (c) 2008 Trent Foley (http://trentacular.com)
198009598544 Uploaded
romaingred
parents:
diff changeset
5 * Licensed under the MIT License:
198009598544 Uploaded
romaingred
parents:
diff changeset
6 * http://www.opensource.org/licenses/mit-license.php
198009598544 Uploaded
romaingred
parents:
diff changeset
7 *
198009598544 Uploaded
romaingred
parents:
diff changeset
8 * Much thanks to primary contributer Ponticlaro (http://www.ponticlaro.com)
198009598544 Uploaded
romaingred
parents:
diff changeset
9 */
198009598544 Uploaded
romaingred
parents:
diff changeset
10 ;(function($) {
198009598544 Uploaded
romaingred
parents:
diff changeset
11 // Globally keep track of all images by their unique hash. Each item is an image data object.
198009598544 Uploaded
romaingred
parents:
diff changeset
12 var allImages = {};
198009598544 Uploaded
romaingred
parents:
diff changeset
13 var imageCounter = 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
14
198009598544 Uploaded
romaingred
parents:
diff changeset
15 // Galleriffic static class
198009598544 Uploaded
romaingred
parents:
diff changeset
16 $.galleriffic = {
198009598544 Uploaded
romaingred
parents:
diff changeset
17 version: '2.0.1',
198009598544 Uploaded
romaingred
parents:
diff changeset
18
198009598544 Uploaded
romaingred
parents:
diff changeset
19 // Strips invalid characters and any leading # characters
198009598544 Uploaded
romaingred
parents:
diff changeset
20 normalizeHash: function(hash) {
198009598544 Uploaded
romaingred
parents:
diff changeset
21 return hash.replace(/^.*#/, '').replace(/\?.*$/, '');
198009598544 Uploaded
romaingred
parents:
diff changeset
22 },
198009598544 Uploaded
romaingred
parents:
diff changeset
23
198009598544 Uploaded
romaingred
parents:
diff changeset
24 getImage: function(hash) {
198009598544 Uploaded
romaingred
parents:
diff changeset
25 if (!hash)
198009598544 Uploaded
romaingred
parents:
diff changeset
26 return undefined;
198009598544 Uploaded
romaingred
parents:
diff changeset
27
198009598544 Uploaded
romaingred
parents:
diff changeset
28 hash = $.galleriffic.normalizeHash(hash);
198009598544 Uploaded
romaingred
parents:
diff changeset
29 return allImages[hash];
198009598544 Uploaded
romaingred
parents:
diff changeset
30 },
198009598544 Uploaded
romaingred
parents:
diff changeset
31
198009598544 Uploaded
romaingred
parents:
diff changeset
32 // Global function that looks up an image by its hash and displays the image.
198009598544 Uploaded
romaingred
parents:
diff changeset
33 // Returns false when an image is not found for the specified hash.
198009598544 Uploaded
romaingred
parents:
diff changeset
34 // @param {String} hash This is the unique hash value assigned to an image.
198009598544 Uploaded
romaingred
parents:
diff changeset
35 gotoImage: function(hash) {
198009598544 Uploaded
romaingred
parents:
diff changeset
36 var imageData = $.galleriffic.getImage(hash);
198009598544 Uploaded
romaingred
parents:
diff changeset
37 if (!imageData)
198009598544 Uploaded
romaingred
parents:
diff changeset
38 return false;
198009598544 Uploaded
romaingred
parents:
diff changeset
39
198009598544 Uploaded
romaingred
parents:
diff changeset
40 var gallery = imageData.gallery;
198009598544 Uploaded
romaingred
parents:
diff changeset
41 gallery.gotoImage(imageData);
198009598544 Uploaded
romaingred
parents:
diff changeset
42
198009598544 Uploaded
romaingred
parents:
diff changeset
43 return true;
198009598544 Uploaded
romaingred
parents:
diff changeset
44 },
198009598544 Uploaded
romaingred
parents:
diff changeset
45
198009598544 Uploaded
romaingred
parents:
diff changeset
46 // Removes an image from its respective gallery by its hash.
198009598544 Uploaded
romaingred
parents:
diff changeset
47 // Returns false when an image is not found for the specified hash or the
198009598544 Uploaded
romaingred
parents:
diff changeset
48 // specified owner gallery does match the located images gallery.
198009598544 Uploaded
romaingred
parents:
diff changeset
49 // @param {String} hash This is the unique hash value assigned to an image.
198009598544 Uploaded
romaingred
parents:
diff changeset
50 // @param {Object} ownerGallery (Optional) When supplied, the located images
198009598544 Uploaded
romaingred
parents:
diff changeset
51 // gallery is verified to be the same as the specified owning gallery before
198009598544 Uploaded
romaingred
parents:
diff changeset
52 // performing the remove operation.
198009598544 Uploaded
romaingred
parents:
diff changeset
53 removeImageByHash: function(hash, ownerGallery) {
198009598544 Uploaded
romaingred
parents:
diff changeset
54 var imageData = $.galleriffic.getImage(hash);
198009598544 Uploaded
romaingred
parents:
diff changeset
55 if (!imageData)
198009598544 Uploaded
romaingred
parents:
diff changeset
56 return false;
198009598544 Uploaded
romaingred
parents:
diff changeset
57
198009598544 Uploaded
romaingred
parents:
diff changeset
58 var gallery = imageData.gallery;
198009598544 Uploaded
romaingred
parents:
diff changeset
59 if (ownerGallery && ownerGallery != gallery)
198009598544 Uploaded
romaingred
parents:
diff changeset
60 return false;
198009598544 Uploaded
romaingred
parents:
diff changeset
61
198009598544 Uploaded
romaingred
parents:
diff changeset
62 return gallery.removeImageByIndex(imageData.index);
198009598544 Uploaded
romaingred
parents:
diff changeset
63 }
198009598544 Uploaded
romaingred
parents:
diff changeset
64 };
198009598544 Uploaded
romaingred
parents:
diff changeset
65
198009598544 Uploaded
romaingred
parents:
diff changeset
66 var defaults = {
198009598544 Uploaded
romaingred
parents:
diff changeset
67 delay: 3000,
198009598544 Uploaded
romaingred
parents:
diff changeset
68 numThumbs: 20,
198009598544 Uploaded
romaingred
parents:
diff changeset
69 preloadAhead: 40, // Set to -1 to preload all images
198009598544 Uploaded
romaingred
parents:
diff changeset
70 enableTopPager: false,
198009598544 Uploaded
romaingred
parents:
diff changeset
71 enableBottomPager: true,
198009598544 Uploaded
romaingred
parents:
diff changeset
72 maxPagesToShow: 7,
198009598544 Uploaded
romaingred
parents:
diff changeset
73 imageContainerSel: '',
198009598544 Uploaded
romaingred
parents:
diff changeset
74 captionContainerSel: '',
198009598544 Uploaded
romaingred
parents:
diff changeset
75 controlsContainerSel: '',
198009598544 Uploaded
romaingred
parents:
diff changeset
76 loadingContainerSel: '',
198009598544 Uploaded
romaingred
parents:
diff changeset
77 renderSSControls: true,
198009598544 Uploaded
romaingred
parents:
diff changeset
78 renderNavControls: true,
198009598544 Uploaded
romaingred
parents:
diff changeset
79 playLinkText: 'Play',
198009598544 Uploaded
romaingred
parents:
diff changeset
80 pauseLinkText: 'Pause',
198009598544 Uploaded
romaingred
parents:
diff changeset
81 prevLinkText: 'Previous',
198009598544 Uploaded
romaingred
parents:
diff changeset
82 nextLinkText: 'Next',
198009598544 Uploaded
romaingred
parents:
diff changeset
83 nextPageLinkText: 'Next ›',
198009598544 Uploaded
romaingred
parents:
diff changeset
84 prevPageLinkText: '‹ Prev',
198009598544 Uploaded
romaingred
parents:
diff changeset
85 enableHistory: false,
198009598544 Uploaded
romaingred
parents:
diff changeset
86 enableKeyboardNavigation: true,
198009598544 Uploaded
romaingred
parents:
diff changeset
87 autoStart: false,
198009598544 Uploaded
romaingred
parents:
diff changeset
88 syncTransitions: false,
198009598544 Uploaded
romaingred
parents:
diff changeset
89 defaultTransitionDuration: 1000,
198009598544 Uploaded
romaingred
parents:
diff changeset
90 onSlideChange: undefined, // accepts a delegate like such: function(prevIndex, nextIndex) { ... }
198009598544 Uploaded
romaingred
parents:
diff changeset
91 onTransitionOut: undefined, // accepts a delegate like such: function(slide, caption, isSync, callback) { ... }
198009598544 Uploaded
romaingred
parents:
diff changeset
92 onTransitionIn: undefined, // accepts a delegate like such: function(slide, caption, isSync) { ... }
198009598544 Uploaded
romaingred
parents:
diff changeset
93 onPageTransitionOut: undefined, // accepts a delegate like such: function(callback) { ... }
198009598544 Uploaded
romaingred
parents:
diff changeset
94 onPageTransitionIn: undefined, // accepts a delegate like such: function() { ... }
198009598544 Uploaded
romaingred
parents:
diff changeset
95 onImageAdded: undefined, // accepts a delegate like such: function(imageData, $li) { ... }
198009598544 Uploaded
romaingred
parents:
diff changeset
96 onImageRemoved: undefined // accepts a delegate like such: function(imageData, $li) { ... }
198009598544 Uploaded
romaingred
parents:
diff changeset
97 };
198009598544 Uploaded
romaingred
parents:
diff changeset
98
198009598544 Uploaded
romaingred
parents:
diff changeset
99 // Primary Galleriffic initialization function that should be called on the thumbnail container.
198009598544 Uploaded
romaingred
parents:
diff changeset
100 $.fn.galleriffic = function(settings) {
198009598544 Uploaded
romaingred
parents:
diff changeset
101 // Extend Gallery Object
198009598544 Uploaded
romaingred
parents:
diff changeset
102 $.extend(this, {
198009598544 Uploaded
romaingred
parents:
diff changeset
103 // Returns the version of the script
198009598544 Uploaded
romaingred
parents:
diff changeset
104 version: $.galleriffic.version,
198009598544 Uploaded
romaingred
parents:
diff changeset
105
198009598544 Uploaded
romaingred
parents:
diff changeset
106 // Current state of the slideshow
198009598544 Uploaded
romaingred
parents:
diff changeset
107 isSlideshowRunning: false,
198009598544 Uploaded
romaingred
parents:
diff changeset
108 slideshowTimeout: undefined,
198009598544 Uploaded
romaingred
parents:
diff changeset
109
198009598544 Uploaded
romaingred
parents:
diff changeset
110 // This function is attached to the click event of generated hyperlinks within the gallery
198009598544 Uploaded
romaingred
parents:
diff changeset
111 clickHandler: function(e, link) {
198009598544 Uploaded
romaingred
parents:
diff changeset
112 this.pause();
198009598544 Uploaded
romaingred
parents:
diff changeset
113
198009598544 Uploaded
romaingred
parents:
diff changeset
114 if (!this.enableHistory) {
198009598544 Uploaded
romaingred
parents:
diff changeset
115 // The href attribute holds the unique hash for an image
198009598544 Uploaded
romaingred
parents:
diff changeset
116 var hash = $.galleriffic.normalizeHash($(link).attr('href'));
198009598544 Uploaded
romaingred
parents:
diff changeset
117 $.galleriffic.gotoImage(hash);
198009598544 Uploaded
romaingred
parents:
diff changeset
118 e.preventDefault();
198009598544 Uploaded
romaingred
parents:
diff changeset
119 }
198009598544 Uploaded
romaingred
parents:
diff changeset
120 },
198009598544 Uploaded
romaingred
parents:
diff changeset
121
198009598544 Uploaded
romaingred
parents:
diff changeset
122 // Appends an image to the end of the set of images. Argument listItem can be either a jQuery DOM element or arbitrary html.
198009598544 Uploaded
romaingred
parents:
diff changeset
123 // @param listItem Either a jQuery object or a string of html of the list item that is to be added to the gallery.
198009598544 Uploaded
romaingred
parents:
diff changeset
124 appendImage: function(listItem) {
198009598544 Uploaded
romaingred
parents:
diff changeset
125 this.addImage(listItem, false, false);
198009598544 Uploaded
romaingred
parents:
diff changeset
126 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
127 },
198009598544 Uploaded
romaingred
parents:
diff changeset
128
198009598544 Uploaded
romaingred
parents:
diff changeset
129 // Inserts an image into the set of images. Argument listItem can be either a jQuery DOM element or arbitrary html.
198009598544 Uploaded
romaingred
parents:
diff changeset
130 // @param listItem Either a jQuery object or a string of html of the list item that is to be added to the gallery.
198009598544 Uploaded
romaingred
parents:
diff changeset
131 // @param {Integer} position The index within the gallery where the item shouold be added.
198009598544 Uploaded
romaingred
parents:
diff changeset
132 insertImage: function(listItem, position) {
198009598544 Uploaded
romaingred
parents:
diff changeset
133 this.addImage(listItem, false, true, position);
198009598544 Uploaded
romaingred
parents:
diff changeset
134 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
135 },
198009598544 Uploaded
romaingred
parents:
diff changeset
136
198009598544 Uploaded
romaingred
parents:
diff changeset
137 // Adds an image to the gallery and optionally inserts/appends it to the DOM (thumbExists)
198009598544 Uploaded
romaingred
parents:
diff changeset
138 // @param listItem Either a jQuery object or a string of html of the list item that is to be added to the gallery.
198009598544 Uploaded
romaingred
parents:
diff changeset
139 // @param {Boolean} thumbExists Specifies whether the thumbnail already exists in the DOM or if it needs to be added.
198009598544 Uploaded
romaingred
parents:
diff changeset
140 // @param {Boolean} insert Specifies whether the the image is appended to the end or inserted into the gallery.
198009598544 Uploaded
romaingred
parents:
diff changeset
141 // @param {Integer} position The index within the gallery where the item shouold be added.
198009598544 Uploaded
romaingred
parents:
diff changeset
142 addImage: function(listItem, thumbExists, insert, position) {
198009598544 Uploaded
romaingred
parents:
diff changeset
143 var $li = ( typeof listItem === "string" ) ? $(listItem) : listItem;
198009598544 Uploaded
romaingred
parents:
diff changeset
144 var $aThumb = $li.find('a.thumb');
198009598544 Uploaded
romaingred
parents:
diff changeset
145 var slideUrl = $aThumb.attr('href');
198009598544 Uploaded
romaingred
parents:
diff changeset
146 var title = $aThumb.attr('title');
198009598544 Uploaded
romaingred
parents:
diff changeset
147 var $caption = $li.find('.caption').remove();
198009598544 Uploaded
romaingred
parents:
diff changeset
148 var hash = $aThumb.attr('name');
198009598544 Uploaded
romaingred
parents:
diff changeset
149
198009598544 Uploaded
romaingred
parents:
diff changeset
150 // Increment the image counter
198009598544 Uploaded
romaingred
parents:
diff changeset
151 imageCounter++;
198009598544 Uploaded
romaingred
parents:
diff changeset
152
198009598544 Uploaded
romaingred
parents:
diff changeset
153 // Autogenerate a hash value if none is present or if it is a duplicate
198009598544 Uploaded
romaingred
parents:
diff changeset
154 if (!hash || allImages[''+hash]) {
198009598544 Uploaded
romaingred
parents:
diff changeset
155 hash = imageCounter;
198009598544 Uploaded
romaingred
parents:
diff changeset
156 }
198009598544 Uploaded
romaingred
parents:
diff changeset
157
198009598544 Uploaded
romaingred
parents:
diff changeset
158 // Set position to end when not specified
198009598544 Uploaded
romaingred
parents:
diff changeset
159 if (!insert)
198009598544 Uploaded
romaingred
parents:
diff changeset
160 position = this.data.length;
198009598544 Uploaded
romaingred
parents:
diff changeset
161
198009598544 Uploaded
romaingred
parents:
diff changeset
162 var imageData = {
198009598544 Uploaded
romaingred
parents:
diff changeset
163 title:title,
198009598544 Uploaded
romaingred
parents:
diff changeset
164 slideUrl:slideUrl,
198009598544 Uploaded
romaingred
parents:
diff changeset
165 caption:$caption,
198009598544 Uploaded
romaingred
parents:
diff changeset
166 hash:hash,
198009598544 Uploaded
romaingred
parents:
diff changeset
167 gallery:this,
198009598544 Uploaded
romaingred
parents:
diff changeset
168 index:position
198009598544 Uploaded
romaingred
parents:
diff changeset
169 };
198009598544 Uploaded
romaingred
parents:
diff changeset
170
198009598544 Uploaded
romaingred
parents:
diff changeset
171 // Add the imageData to this gallery's array of images
198009598544 Uploaded
romaingred
parents:
diff changeset
172 if (insert) {
198009598544 Uploaded
romaingred
parents:
diff changeset
173 this.data.splice(position, 0, imageData);
198009598544 Uploaded
romaingred
parents:
diff changeset
174
198009598544 Uploaded
romaingred
parents:
diff changeset
175 // Reset index value on all imageData objects
198009598544 Uploaded
romaingred
parents:
diff changeset
176 this.updateIndices(position);
198009598544 Uploaded
romaingred
parents:
diff changeset
177 }
198009598544 Uploaded
romaingred
parents:
diff changeset
178 else {
198009598544 Uploaded
romaingred
parents:
diff changeset
179 this.data.push(imageData);
198009598544 Uploaded
romaingred
parents:
diff changeset
180 }
198009598544 Uploaded
romaingred
parents:
diff changeset
181
198009598544 Uploaded
romaingred
parents:
diff changeset
182 var gallery = this;
198009598544 Uploaded
romaingred
parents:
diff changeset
183
198009598544 Uploaded
romaingred
parents:
diff changeset
184 // Add the element to the DOM
198009598544 Uploaded
romaingred
parents:
diff changeset
185 if (!thumbExists) {
198009598544 Uploaded
romaingred
parents:
diff changeset
186 // Update thumbs passing in addition post transition out handler
198009598544 Uploaded
romaingred
parents:
diff changeset
187 this.updateThumbs(function() {
198009598544 Uploaded
romaingred
parents:
diff changeset
188 var $thumbsUl = gallery.find('ul.thumbs');
198009598544 Uploaded
romaingred
parents:
diff changeset
189 if (insert)
198009598544 Uploaded
romaingred
parents:
diff changeset
190 $thumbsUl.children(':eq('+position+')').before($li);
198009598544 Uploaded
romaingred
parents:
diff changeset
191 else
198009598544 Uploaded
romaingred
parents:
diff changeset
192 $thumbsUl.append($li);
198009598544 Uploaded
romaingred
parents:
diff changeset
193
198009598544 Uploaded
romaingred
parents:
diff changeset
194 if (gallery.onImageAdded)
198009598544 Uploaded
romaingred
parents:
diff changeset
195 gallery.onImageAdded(imageData, $li);
198009598544 Uploaded
romaingred
parents:
diff changeset
196 });
198009598544 Uploaded
romaingred
parents:
diff changeset
197 }
198009598544 Uploaded
romaingred
parents:
diff changeset
198
198009598544 Uploaded
romaingred
parents:
diff changeset
199 // Register the image globally
198009598544 Uploaded
romaingred
parents:
diff changeset
200 allImages[''+hash] = imageData;
198009598544 Uploaded
romaingred
parents:
diff changeset
201
198009598544 Uploaded
romaingred
parents:
diff changeset
202 // Setup attributes and click handler
198009598544 Uploaded
romaingred
parents:
diff changeset
203 $aThumb.attr('rel', 'history')
198009598544 Uploaded
romaingred
parents:
diff changeset
204 .attr('href', '#'+hash)
198009598544 Uploaded
romaingred
parents:
diff changeset
205 .removeAttr('name')
198009598544 Uploaded
romaingred
parents:
diff changeset
206 .click(function(e) {
198009598544 Uploaded
romaingred
parents:
diff changeset
207 gallery.clickHandler(e, this);
198009598544 Uploaded
romaingred
parents:
diff changeset
208 });
198009598544 Uploaded
romaingred
parents:
diff changeset
209
198009598544 Uploaded
romaingred
parents:
diff changeset
210 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
211 },
198009598544 Uploaded
romaingred
parents:
diff changeset
212
198009598544 Uploaded
romaingred
parents:
diff changeset
213 // Removes an image from the gallery based on its index.
198009598544 Uploaded
romaingred
parents:
diff changeset
214 // Returns false when the index is out of range.
198009598544 Uploaded
romaingred
parents:
diff changeset
215 removeImageByIndex: function(index) {
198009598544 Uploaded
romaingred
parents:
diff changeset
216 if (index < 0 || index >= this.data.length)
198009598544 Uploaded
romaingred
parents:
diff changeset
217 return false;
198009598544 Uploaded
romaingred
parents:
diff changeset
218
198009598544 Uploaded
romaingred
parents:
diff changeset
219 var imageData = this.data[index];
198009598544 Uploaded
romaingred
parents:
diff changeset
220 if (!imageData)
198009598544 Uploaded
romaingred
parents:
diff changeset
221 return false;
198009598544 Uploaded
romaingred
parents:
diff changeset
222
198009598544 Uploaded
romaingred
parents:
diff changeset
223 this.removeImage(imageData);
198009598544 Uploaded
romaingred
parents:
diff changeset
224
198009598544 Uploaded
romaingred
parents:
diff changeset
225 return true;
198009598544 Uploaded
romaingred
parents:
diff changeset
226 },
198009598544 Uploaded
romaingred
parents:
diff changeset
227
198009598544 Uploaded
romaingred
parents:
diff changeset
228 // Convenience method that simply calls the global removeImageByHash method.
198009598544 Uploaded
romaingred
parents:
diff changeset
229 removeImageByHash: function(hash) {
198009598544 Uploaded
romaingred
parents:
diff changeset
230 return $.galleriffic.removeImageByHash(hash, this);
198009598544 Uploaded
romaingred
parents:
diff changeset
231 },
198009598544 Uploaded
romaingred
parents:
diff changeset
232
198009598544 Uploaded
romaingred
parents:
diff changeset
233 // Removes an image from the gallery.
198009598544 Uploaded
romaingred
parents:
diff changeset
234 removeImage: function(imageData) {
198009598544 Uploaded
romaingred
parents:
diff changeset
235 var index = imageData.index;
198009598544 Uploaded
romaingred
parents:
diff changeset
236
198009598544 Uploaded
romaingred
parents:
diff changeset
237 // Remove the image from the gallery data array
198009598544 Uploaded
romaingred
parents:
diff changeset
238 this.data.splice(index, 1);
198009598544 Uploaded
romaingred
parents:
diff changeset
239
198009598544 Uploaded
romaingred
parents:
diff changeset
240 // Remove the global registration
198009598544 Uploaded
romaingred
parents:
diff changeset
241 delete allImages[''+imageData.hash];
198009598544 Uploaded
romaingred
parents:
diff changeset
242
198009598544 Uploaded
romaingred
parents:
diff changeset
243 // Remove the image's list item from the DOM
198009598544 Uploaded
romaingred
parents:
diff changeset
244 this.updateThumbs(function() {
198009598544 Uploaded
romaingred
parents:
diff changeset
245 var $li = gallery.find('ul.thumbs')
198009598544 Uploaded
romaingred
parents:
diff changeset
246 .children(':eq('+index+')')
198009598544 Uploaded
romaingred
parents:
diff changeset
247 .remove();
198009598544 Uploaded
romaingred
parents:
diff changeset
248
198009598544 Uploaded
romaingred
parents:
diff changeset
249 if (gallery.onImageRemoved)
198009598544 Uploaded
romaingred
parents:
diff changeset
250 gallery.onImageRemoved(imageData, $li);
198009598544 Uploaded
romaingred
parents:
diff changeset
251 });
198009598544 Uploaded
romaingred
parents:
diff changeset
252
198009598544 Uploaded
romaingred
parents:
diff changeset
253 // Update each image objects index value
198009598544 Uploaded
romaingred
parents:
diff changeset
254 this.updateIndices(index);
198009598544 Uploaded
romaingred
parents:
diff changeset
255
198009598544 Uploaded
romaingred
parents:
diff changeset
256 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
257 },
198009598544 Uploaded
romaingred
parents:
diff changeset
258
198009598544 Uploaded
romaingred
parents:
diff changeset
259 // Updates the index values of the each of the images in the gallery after the specified index
198009598544 Uploaded
romaingred
parents:
diff changeset
260 updateIndices: function(startIndex) {
198009598544 Uploaded
romaingred
parents:
diff changeset
261 for (i = startIndex; i < this.data.length; i++) {
198009598544 Uploaded
romaingred
parents:
diff changeset
262 this.data[i].index = i;
198009598544 Uploaded
romaingred
parents:
diff changeset
263 }
198009598544 Uploaded
romaingred
parents:
diff changeset
264
198009598544 Uploaded
romaingred
parents:
diff changeset
265 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
266 },
198009598544 Uploaded
romaingred
parents:
diff changeset
267
198009598544 Uploaded
romaingred
parents:
diff changeset
268 // Scraped the thumbnail container for thumbs and adds each to the gallery
198009598544 Uploaded
romaingred
parents:
diff changeset
269 initializeThumbs: function() {
198009598544 Uploaded
romaingred
parents:
diff changeset
270 this.data = [];
198009598544 Uploaded
romaingred
parents:
diff changeset
271 var gallery = this;
198009598544 Uploaded
romaingred
parents:
diff changeset
272
198009598544 Uploaded
romaingred
parents:
diff changeset
273 this.find('ul.thumbs > li').each(function(i) {
198009598544 Uploaded
romaingred
parents:
diff changeset
274 gallery.addImage($(this), true, false);
198009598544 Uploaded
romaingred
parents:
diff changeset
275 });
198009598544 Uploaded
romaingred
parents:
diff changeset
276
198009598544 Uploaded
romaingred
parents:
diff changeset
277 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
278 },
198009598544 Uploaded
romaingred
parents:
diff changeset
279
198009598544 Uploaded
romaingred
parents:
diff changeset
280 isPreloadComplete: false,
198009598544 Uploaded
romaingred
parents:
diff changeset
281
198009598544 Uploaded
romaingred
parents:
diff changeset
282 // Initalizes the image preloader
198009598544 Uploaded
romaingred
parents:
diff changeset
283 preloadInit: function() {
198009598544 Uploaded
romaingred
parents:
diff changeset
284 if (this.preloadAhead == 0) return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
285
198009598544 Uploaded
romaingred
parents:
diff changeset
286 this.preloadStartIndex = this.currentImage.index;
198009598544 Uploaded
romaingred
parents:
diff changeset
287 var nextIndex = this.getNextIndex(this.preloadStartIndex);
198009598544 Uploaded
romaingred
parents:
diff changeset
288 return this.preloadRecursive(this.preloadStartIndex, nextIndex);
198009598544 Uploaded
romaingred
parents:
diff changeset
289 },
198009598544 Uploaded
romaingred
parents:
diff changeset
290
198009598544 Uploaded
romaingred
parents:
diff changeset
291 // Changes the location in the gallery the preloader should work
198009598544 Uploaded
romaingred
parents:
diff changeset
292 // @param {Integer} index The index of the image where the preloader should restart at.
198009598544 Uploaded
romaingred
parents:
diff changeset
293 preloadRelocate: function(index) {
198009598544 Uploaded
romaingred
parents:
diff changeset
294 // By changing this startIndex, the current preload script will restart
198009598544 Uploaded
romaingred
parents:
diff changeset
295 this.preloadStartIndex = index;
198009598544 Uploaded
romaingred
parents:
diff changeset
296 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
297 },
198009598544 Uploaded
romaingred
parents:
diff changeset
298
198009598544 Uploaded
romaingred
parents:
diff changeset
299 // Recursive function that performs the image preloading
198009598544 Uploaded
romaingred
parents:
diff changeset
300 // @param {Integer} startIndex The index of the first image the current preloader started on.
198009598544 Uploaded
romaingred
parents:
diff changeset
301 // @param {Integer} currentIndex The index of the current image to preload.
198009598544 Uploaded
romaingred
parents:
diff changeset
302 preloadRecursive: function(startIndex, currentIndex) {
198009598544 Uploaded
romaingred
parents:
diff changeset
303 // Check if startIndex has been relocated
198009598544 Uploaded
romaingred
parents:
diff changeset
304 if (startIndex != this.preloadStartIndex) {
198009598544 Uploaded
romaingred
parents:
diff changeset
305 var nextIndex = this.getNextIndex(this.preloadStartIndex);
198009598544 Uploaded
romaingred
parents:
diff changeset
306 return this.preloadRecursive(this.preloadStartIndex, nextIndex);
198009598544 Uploaded
romaingred
parents:
diff changeset
307 }
198009598544 Uploaded
romaingred
parents:
diff changeset
308
198009598544 Uploaded
romaingred
parents:
diff changeset
309 var gallery = this;
198009598544 Uploaded
romaingred
parents:
diff changeset
310
198009598544 Uploaded
romaingred
parents:
diff changeset
311 // Now check for preloadAhead count
198009598544 Uploaded
romaingred
parents:
diff changeset
312 var preloadCount = currentIndex - startIndex;
198009598544 Uploaded
romaingred
parents:
diff changeset
313 if (preloadCount < 0)
198009598544 Uploaded
romaingred
parents:
diff changeset
314 preloadCount = this.data.length-1-startIndex+currentIndex;
198009598544 Uploaded
romaingred
parents:
diff changeset
315 if (this.preloadAhead >= 0 && preloadCount > this.preloadAhead) {
198009598544 Uploaded
romaingred
parents:
diff changeset
316 // Do this in order to keep checking for relocated start index
198009598544 Uploaded
romaingred
parents:
diff changeset
317 setTimeout(function() { gallery.preloadRecursive(startIndex, currentIndex); }, 500);
198009598544 Uploaded
romaingred
parents:
diff changeset
318 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
319 }
198009598544 Uploaded
romaingred
parents:
diff changeset
320
198009598544 Uploaded
romaingred
parents:
diff changeset
321 var imageData = this.data[currentIndex];
198009598544 Uploaded
romaingred
parents:
diff changeset
322 if (!imageData)
198009598544 Uploaded
romaingred
parents:
diff changeset
323 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
324
198009598544 Uploaded
romaingred
parents:
diff changeset
325 // If already loaded, continue
198009598544 Uploaded
romaingred
parents:
diff changeset
326 if (imageData.image)
198009598544 Uploaded
romaingred
parents:
diff changeset
327 return this.preloadNext(startIndex, currentIndex);
198009598544 Uploaded
romaingred
parents:
diff changeset
328
198009598544 Uploaded
romaingred
parents:
diff changeset
329 // Preload the image
198009598544 Uploaded
romaingred
parents:
diff changeset
330 var image = new Image();
198009598544 Uploaded
romaingred
parents:
diff changeset
331
198009598544 Uploaded
romaingred
parents:
diff changeset
332 image.onload = function() {
198009598544 Uploaded
romaingred
parents:
diff changeset
333 imageData.image = this;
198009598544 Uploaded
romaingred
parents:
diff changeset
334 gallery.preloadNext(startIndex, currentIndex);
198009598544 Uploaded
romaingred
parents:
diff changeset
335 };
198009598544 Uploaded
romaingred
parents:
diff changeset
336
198009598544 Uploaded
romaingred
parents:
diff changeset
337 image.alt = imageData.title;
198009598544 Uploaded
romaingred
parents:
diff changeset
338 image.src = imageData.slideUrl;
198009598544 Uploaded
romaingred
parents:
diff changeset
339
198009598544 Uploaded
romaingred
parents:
diff changeset
340 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
341 },
198009598544 Uploaded
romaingred
parents:
diff changeset
342
198009598544 Uploaded
romaingred
parents:
diff changeset
343 // Called by preloadRecursive in order to preload the next image after the previous has loaded.
198009598544 Uploaded
romaingred
parents:
diff changeset
344 // @param {Integer} startIndex The index of the first image the current preloader started on.
198009598544 Uploaded
romaingred
parents:
diff changeset
345 // @param {Integer} currentIndex The index of the current image to preload.
198009598544 Uploaded
romaingred
parents:
diff changeset
346 preloadNext: function(startIndex, currentIndex) {
198009598544 Uploaded
romaingred
parents:
diff changeset
347 var nextIndex = this.getNextIndex(currentIndex);
198009598544 Uploaded
romaingred
parents:
diff changeset
348 if (nextIndex == startIndex) {
198009598544 Uploaded
romaingred
parents:
diff changeset
349 this.isPreloadComplete = true;
198009598544 Uploaded
romaingred
parents:
diff changeset
350 } else {
198009598544 Uploaded
romaingred
parents:
diff changeset
351 // Use setTimeout to free up thread
198009598544 Uploaded
romaingred
parents:
diff changeset
352 var gallery = this;
198009598544 Uploaded
romaingred
parents:
diff changeset
353 setTimeout(function() { gallery.preloadRecursive(startIndex, nextIndex); }, 100);
198009598544 Uploaded
romaingred
parents:
diff changeset
354 }
198009598544 Uploaded
romaingred
parents:
diff changeset
355
198009598544 Uploaded
romaingred
parents:
diff changeset
356 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
357 },
198009598544 Uploaded
romaingred
parents:
diff changeset
358
198009598544 Uploaded
romaingred
parents:
diff changeset
359 // Safe way to get the next image index relative to the current image.
198009598544 Uploaded
romaingred
parents:
diff changeset
360 // If the current image is the last, returns 0
198009598544 Uploaded
romaingred
parents:
diff changeset
361 getNextIndex: function(index) {
198009598544 Uploaded
romaingred
parents:
diff changeset
362 var nextIndex = index+1;
198009598544 Uploaded
romaingred
parents:
diff changeset
363 if (nextIndex >= this.data.length)
198009598544 Uploaded
romaingred
parents:
diff changeset
364 nextIndex = 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
365 return nextIndex;
198009598544 Uploaded
romaingred
parents:
diff changeset
366 },
198009598544 Uploaded
romaingred
parents:
diff changeset
367
198009598544 Uploaded
romaingred
parents:
diff changeset
368 // Safe way to get the previous image index relative to the current image.
198009598544 Uploaded
romaingred
parents:
diff changeset
369 // If the current image is the first, return the index of the last image in the gallery.
198009598544 Uploaded
romaingred
parents:
diff changeset
370 getPrevIndex: function(index) {
198009598544 Uploaded
romaingred
parents:
diff changeset
371 var prevIndex = index-1;
198009598544 Uploaded
romaingred
parents:
diff changeset
372 if (prevIndex < 0)
198009598544 Uploaded
romaingred
parents:
diff changeset
373 prevIndex = this.data.length-1;
198009598544 Uploaded
romaingred
parents:
diff changeset
374 return prevIndex;
198009598544 Uploaded
romaingred
parents:
diff changeset
375 },
198009598544 Uploaded
romaingred
parents:
diff changeset
376
198009598544 Uploaded
romaingred
parents:
diff changeset
377 // Pauses the slideshow
198009598544 Uploaded
romaingred
parents:
diff changeset
378 pause: function() {
198009598544 Uploaded
romaingred
parents:
diff changeset
379 this.isSlideshowRunning = false;
198009598544 Uploaded
romaingred
parents:
diff changeset
380 if (this.slideshowTimeout) {
198009598544 Uploaded
romaingred
parents:
diff changeset
381 clearTimeout(this.slideshowTimeout);
198009598544 Uploaded
romaingred
parents:
diff changeset
382 this.slideshowTimeout = undefined;
198009598544 Uploaded
romaingred
parents:
diff changeset
383 }
198009598544 Uploaded
romaingred
parents:
diff changeset
384
198009598544 Uploaded
romaingred
parents:
diff changeset
385 if (this.$controlsContainer) {
198009598544 Uploaded
romaingred
parents:
diff changeset
386 this.$controlsContainer
198009598544 Uploaded
romaingred
parents:
diff changeset
387 .find('div.ss-controls a').removeClass().addClass('play')
198009598544 Uploaded
romaingred
parents:
diff changeset
388 .attr('title', this.playLinkText)
198009598544 Uploaded
romaingred
parents:
diff changeset
389 .attr('href', '#play')
198009598544 Uploaded
romaingred
parents:
diff changeset
390 .html(this.playLinkText);
198009598544 Uploaded
romaingred
parents:
diff changeset
391 }
198009598544 Uploaded
romaingred
parents:
diff changeset
392
198009598544 Uploaded
romaingred
parents:
diff changeset
393 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
394 },
198009598544 Uploaded
romaingred
parents:
diff changeset
395
198009598544 Uploaded
romaingred
parents:
diff changeset
396 // Plays the slideshow
198009598544 Uploaded
romaingred
parents:
diff changeset
397 play: function() {
198009598544 Uploaded
romaingred
parents:
diff changeset
398 this.isSlideshowRunning = true;
198009598544 Uploaded
romaingred
parents:
diff changeset
399
198009598544 Uploaded
romaingred
parents:
diff changeset
400 if (this.$controlsContainer) {
198009598544 Uploaded
romaingred
parents:
diff changeset
401 this.$controlsContainer
198009598544 Uploaded
romaingred
parents:
diff changeset
402 .find('div.ss-controls a').removeClass().addClass('pause')
198009598544 Uploaded
romaingred
parents:
diff changeset
403 .attr('title', this.pauseLinkText)
198009598544 Uploaded
romaingred
parents:
diff changeset
404 .attr('href', '#pause')
198009598544 Uploaded
romaingred
parents:
diff changeset
405 .html(this.pauseLinkText);
198009598544 Uploaded
romaingred
parents:
diff changeset
406 }
198009598544 Uploaded
romaingred
parents:
diff changeset
407
198009598544 Uploaded
romaingred
parents:
diff changeset
408 if (!this.slideshowTimeout) {
198009598544 Uploaded
romaingred
parents:
diff changeset
409 var gallery = this;
198009598544 Uploaded
romaingred
parents:
diff changeset
410 this.slideshowTimeout = setTimeout(function() { gallery.ssAdvance(); }, this.delay);
198009598544 Uploaded
romaingred
parents:
diff changeset
411 }
198009598544 Uploaded
romaingred
parents:
diff changeset
412
198009598544 Uploaded
romaingred
parents:
diff changeset
413 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
414 },
198009598544 Uploaded
romaingred
parents:
diff changeset
415
198009598544 Uploaded
romaingred
parents:
diff changeset
416 // Toggles the state of the slideshow (playing/paused)
198009598544 Uploaded
romaingred
parents:
diff changeset
417 toggleSlideshow: function() {
198009598544 Uploaded
romaingred
parents:
diff changeset
418 if (this.isSlideshowRunning)
198009598544 Uploaded
romaingred
parents:
diff changeset
419 this.pause();
198009598544 Uploaded
romaingred
parents:
diff changeset
420 else
198009598544 Uploaded
romaingred
parents:
diff changeset
421 this.play();
198009598544 Uploaded
romaingred
parents:
diff changeset
422
198009598544 Uploaded
romaingred
parents:
diff changeset
423 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
424 },
198009598544 Uploaded
romaingred
parents:
diff changeset
425
198009598544 Uploaded
romaingred
parents:
diff changeset
426 // Advances the slideshow to the next image and delegates navigation to the
198009598544 Uploaded
romaingred
parents:
diff changeset
427 // history plugin when history is enabled
198009598544 Uploaded
romaingred
parents:
diff changeset
428 // enableHistory is true
198009598544 Uploaded
romaingred
parents:
diff changeset
429 ssAdvance: function() {
198009598544 Uploaded
romaingred
parents:
diff changeset
430 if (this.isSlideshowRunning)
198009598544 Uploaded
romaingred
parents:
diff changeset
431 this.next(true);
198009598544 Uploaded
romaingred
parents:
diff changeset
432
198009598544 Uploaded
romaingred
parents:
diff changeset
433 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
434 },
198009598544 Uploaded
romaingred
parents:
diff changeset
435
198009598544 Uploaded
romaingred
parents:
diff changeset
436 // Advances the gallery to the next image.
198009598544 Uploaded
romaingred
parents:
diff changeset
437 // @param {Boolean} dontPause Specifies whether to pause the slideshow.
198009598544 Uploaded
romaingred
parents:
diff changeset
438 // @param {Boolean} bypassHistory Specifies whether to delegate navigation to the history plugin when history is enabled.
198009598544 Uploaded
romaingred
parents:
diff changeset
439 next: function(dontPause, bypassHistory) {
198009598544 Uploaded
romaingred
parents:
diff changeset
440 this.gotoIndex(this.getNextIndex(this.currentImage.index), dontPause, bypassHistory);
198009598544 Uploaded
romaingred
parents:
diff changeset
441 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
442 },
198009598544 Uploaded
romaingred
parents:
diff changeset
443
198009598544 Uploaded
romaingred
parents:
diff changeset
444 // Navigates to the previous image in the gallery.
198009598544 Uploaded
romaingred
parents:
diff changeset
445 // @param {Boolean} dontPause Specifies whether to pause the slideshow.
198009598544 Uploaded
romaingred
parents:
diff changeset
446 // @param {Boolean} bypassHistory Specifies whether to delegate navigation to the history plugin when history is enabled.
198009598544 Uploaded
romaingred
parents:
diff changeset
447 previous: function(dontPause, bypassHistory) {
198009598544 Uploaded
romaingred
parents:
diff changeset
448 this.gotoIndex(this.getPrevIndex(this.currentImage.index), dontPause, bypassHistory);
198009598544 Uploaded
romaingred
parents:
diff changeset
449 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
450 },
198009598544 Uploaded
romaingred
parents:
diff changeset
451
198009598544 Uploaded
romaingred
parents:
diff changeset
452 // Navigates to the next page in the gallery.
198009598544 Uploaded
romaingred
parents:
diff changeset
453 // @param {Boolean} dontPause Specifies whether to pause the slideshow.
198009598544 Uploaded
romaingred
parents:
diff changeset
454 // @param {Boolean} bypassHistory Specifies whether to delegate navigation to the history plugin when history is enabled.
198009598544 Uploaded
romaingred
parents:
diff changeset
455 nextPage: function(dontPause, bypassHistory) {
198009598544 Uploaded
romaingred
parents:
diff changeset
456 var page = this.getCurrentPage();
198009598544 Uploaded
romaingred
parents:
diff changeset
457 var lastPage = this.getNumPages() - 1;
198009598544 Uploaded
romaingred
parents:
diff changeset
458 if (page < lastPage) {
198009598544 Uploaded
romaingred
parents:
diff changeset
459 var startIndex = page * this.numThumbs;
198009598544 Uploaded
romaingred
parents:
diff changeset
460 var nextPage = startIndex + this.numThumbs;
198009598544 Uploaded
romaingred
parents:
diff changeset
461 this.gotoIndex(nextPage, dontPause, bypassHistory);
198009598544 Uploaded
romaingred
parents:
diff changeset
462 }
198009598544 Uploaded
romaingred
parents:
diff changeset
463
198009598544 Uploaded
romaingred
parents:
diff changeset
464 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
465 },
198009598544 Uploaded
romaingred
parents:
diff changeset
466
198009598544 Uploaded
romaingred
parents:
diff changeset
467 // Navigates to the previous page in the gallery.
198009598544 Uploaded
romaingred
parents:
diff changeset
468 // @param {Boolean} dontPause Specifies whether to pause the slideshow.
198009598544 Uploaded
romaingred
parents:
diff changeset
469 // @param {Boolean} bypassHistory Specifies whether to delegate navigation to the history plugin when history is enabled.
198009598544 Uploaded
romaingred
parents:
diff changeset
470 previousPage: function(dontPause, bypassHistory) {
198009598544 Uploaded
romaingred
parents:
diff changeset
471 var page = this.getCurrentPage();
198009598544 Uploaded
romaingred
parents:
diff changeset
472 if (page > 0) {
198009598544 Uploaded
romaingred
parents:
diff changeset
473 var startIndex = page * this.numThumbs;
198009598544 Uploaded
romaingred
parents:
diff changeset
474 var prevPage = startIndex - this.numThumbs;
198009598544 Uploaded
romaingred
parents:
diff changeset
475 this.gotoIndex(prevPage, dontPause, bypassHistory);
198009598544 Uploaded
romaingred
parents:
diff changeset
476 }
198009598544 Uploaded
romaingred
parents:
diff changeset
477
198009598544 Uploaded
romaingred
parents:
diff changeset
478 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
479 },
198009598544 Uploaded
romaingred
parents:
diff changeset
480
198009598544 Uploaded
romaingred
parents:
diff changeset
481 // Navigates to the image at the specified index in the gallery
198009598544 Uploaded
romaingred
parents:
diff changeset
482 // @param {Integer} index The index of the image in the gallery to display.
198009598544 Uploaded
romaingred
parents:
diff changeset
483 // @param {Boolean} dontPause Specifies whether to pause the slideshow.
198009598544 Uploaded
romaingred
parents:
diff changeset
484 // @param {Boolean} bypassHistory Specifies whether to delegate navigation to the history plugin when history is enabled.
198009598544 Uploaded
romaingred
parents:
diff changeset
485 gotoIndex: function(index, dontPause, bypassHistory) {
198009598544 Uploaded
romaingred
parents:
diff changeset
486 if (!dontPause)
198009598544 Uploaded
romaingred
parents:
diff changeset
487 this.pause();
198009598544 Uploaded
romaingred
parents:
diff changeset
488
198009598544 Uploaded
romaingred
parents:
diff changeset
489 if (index < 0) index = 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
490 else if (index >= this.data.length) index = this.data.length-1;
198009598544 Uploaded
romaingred
parents:
diff changeset
491
198009598544 Uploaded
romaingred
parents:
diff changeset
492 var imageData = this.data[index];
198009598544 Uploaded
romaingred
parents:
diff changeset
493
198009598544 Uploaded
romaingred
parents:
diff changeset
494 if (!bypassHistory && this.enableHistory)
198009598544 Uploaded
romaingred
parents:
diff changeset
495 $.historyLoad(String(imageData.hash)); // At the moment, historyLoad only accepts string arguments
198009598544 Uploaded
romaingred
parents:
diff changeset
496 else
198009598544 Uploaded
romaingred
parents:
diff changeset
497 this.gotoImage(imageData);
198009598544 Uploaded
romaingred
parents:
diff changeset
498
198009598544 Uploaded
romaingred
parents:
diff changeset
499 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
500 },
198009598544 Uploaded
romaingred
parents:
diff changeset
501
198009598544 Uploaded
romaingred
parents:
diff changeset
502 // This function is garaunteed to be called anytime a gallery slide changes.
198009598544 Uploaded
romaingred
parents:
diff changeset
503 // @param {Object} imageData An object holding the image metadata of the image to navigate to.
198009598544 Uploaded
romaingred
parents:
diff changeset
504 gotoImage: function(imageData) {
198009598544 Uploaded
romaingred
parents:
diff changeset
505 var index = imageData.index;
198009598544 Uploaded
romaingred
parents:
diff changeset
506
198009598544 Uploaded
romaingred
parents:
diff changeset
507 if (this.onSlideChange)
198009598544 Uploaded
romaingred
parents:
diff changeset
508 this.onSlideChange(this.currentImage.index, index);
198009598544 Uploaded
romaingred
parents:
diff changeset
509
198009598544 Uploaded
romaingred
parents:
diff changeset
510 this.currentImage = imageData;
198009598544 Uploaded
romaingred
parents:
diff changeset
511 this.preloadRelocate(index);
198009598544 Uploaded
romaingred
parents:
diff changeset
512
198009598544 Uploaded
romaingred
parents:
diff changeset
513 this.refresh();
198009598544 Uploaded
romaingred
parents:
diff changeset
514
198009598544 Uploaded
romaingred
parents:
diff changeset
515 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
516 },
198009598544 Uploaded
romaingred
parents:
diff changeset
517
198009598544 Uploaded
romaingred
parents:
diff changeset
518 // Returns the default transition duration value. The value is halved when not
198009598544 Uploaded
romaingred
parents:
diff changeset
519 // performing a synchronized transition.
198009598544 Uploaded
romaingred
parents:
diff changeset
520 // @param {Boolean} isSync Specifies whether the transitions are synchronized.
198009598544 Uploaded
romaingred
parents:
diff changeset
521 getDefaultTransitionDuration: function(isSync) {
198009598544 Uploaded
romaingred
parents:
diff changeset
522 if (isSync)
198009598544 Uploaded
romaingred
parents:
diff changeset
523 return this.defaultTransitionDuration;
198009598544 Uploaded
romaingred
parents:
diff changeset
524 return this.defaultTransitionDuration / 2;
198009598544 Uploaded
romaingred
parents:
diff changeset
525 },
198009598544 Uploaded
romaingred
parents:
diff changeset
526
198009598544 Uploaded
romaingred
parents:
diff changeset
527 // Rebuilds the slideshow image and controls and performs transitions
198009598544 Uploaded
romaingred
parents:
diff changeset
528 refresh: function() {
198009598544 Uploaded
romaingred
parents:
diff changeset
529 var imageData = this.currentImage;
198009598544 Uploaded
romaingred
parents:
diff changeset
530 if (!imageData)
198009598544 Uploaded
romaingred
parents:
diff changeset
531 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
532
198009598544 Uploaded
romaingred
parents:
diff changeset
533 var index = imageData.index;
198009598544 Uploaded
romaingred
parents:
diff changeset
534
198009598544 Uploaded
romaingred
parents:
diff changeset
535 // Update Controls
198009598544 Uploaded
romaingred
parents:
diff changeset
536 if (this.$controlsContainer) {
198009598544 Uploaded
romaingred
parents:
diff changeset
537 this.$controlsContainer
198009598544 Uploaded
romaingred
parents:
diff changeset
538 .find('div.nav-controls a.prev').attr('href', '#'+this.data[this.getPrevIndex(index)].hash).end()
198009598544 Uploaded
romaingred
parents:
diff changeset
539 .find('div.nav-controls a.next').attr('href', '#'+this.data[this.getNextIndex(index)].hash);
198009598544 Uploaded
romaingred
parents:
diff changeset
540 }
198009598544 Uploaded
romaingred
parents:
diff changeset
541
198009598544 Uploaded
romaingred
parents:
diff changeset
542 var previousSlide = this.$imageContainer.find('span.current').addClass('previous').removeClass('current');
198009598544 Uploaded
romaingred
parents:
diff changeset
543 var previousCaption = 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
544
198009598544 Uploaded
romaingred
parents:
diff changeset
545 if (this.$captionContainer) {
198009598544 Uploaded
romaingred
parents:
diff changeset
546 previousCaption = this.$captionContainer.find('span.current').addClass('previous').removeClass('current');
198009598544 Uploaded
romaingred
parents:
diff changeset
547 }
198009598544 Uploaded
romaingred
parents:
diff changeset
548
198009598544 Uploaded
romaingred
parents:
diff changeset
549 // Perform transitions simultaneously if syncTransitions is true and the next image is already preloaded
198009598544 Uploaded
romaingred
parents:
diff changeset
550 var isSync = this.syncTransitions && imageData.image;
198009598544 Uploaded
romaingred
parents:
diff changeset
551
198009598544 Uploaded
romaingred
parents:
diff changeset
552 // Flag we are transitioning
198009598544 Uploaded
romaingred
parents:
diff changeset
553 var isTransitioning = true;
198009598544 Uploaded
romaingred
parents:
diff changeset
554 var gallery = this;
198009598544 Uploaded
romaingred
parents:
diff changeset
555
198009598544 Uploaded
romaingred
parents:
diff changeset
556 var transitionOutCallback = function() {
198009598544 Uploaded
romaingred
parents:
diff changeset
557 // Flag that the transition has completed
198009598544 Uploaded
romaingred
parents:
diff changeset
558 isTransitioning = false;
198009598544 Uploaded
romaingred
parents:
diff changeset
559
198009598544 Uploaded
romaingred
parents:
diff changeset
560 // Remove the old slide
198009598544 Uploaded
romaingred
parents:
diff changeset
561 previousSlide.remove();
198009598544 Uploaded
romaingred
parents:
diff changeset
562
198009598544 Uploaded
romaingred
parents:
diff changeset
563 // Remove old caption
198009598544 Uploaded
romaingred
parents:
diff changeset
564 if (previousCaption)
198009598544 Uploaded
romaingred
parents:
diff changeset
565 previousCaption.remove();
198009598544 Uploaded
romaingred
parents:
diff changeset
566
198009598544 Uploaded
romaingred
parents:
diff changeset
567 if (!isSync) {
198009598544 Uploaded
romaingred
parents:
diff changeset
568 if (imageData.image && imageData.hash == gallery.data[gallery.currentImage.index].hash) {
198009598544 Uploaded
romaingred
parents:
diff changeset
569 gallery.buildImage(imageData, isSync);
198009598544 Uploaded
romaingred
parents:
diff changeset
570 } else {
198009598544 Uploaded
romaingred
parents:
diff changeset
571 // Show loading container
198009598544 Uploaded
romaingred
parents:
diff changeset
572 if (gallery.$loadingContainer) {
198009598544 Uploaded
romaingred
parents:
diff changeset
573 gallery.$loadingContainer.show();
198009598544 Uploaded
romaingred
parents:
diff changeset
574 }
198009598544 Uploaded
romaingred
parents:
diff changeset
575 }
198009598544 Uploaded
romaingred
parents:
diff changeset
576 }
198009598544 Uploaded
romaingred
parents:
diff changeset
577 };
198009598544 Uploaded
romaingred
parents:
diff changeset
578
198009598544 Uploaded
romaingred
parents:
diff changeset
579 if (previousSlide.length == 0) {
198009598544 Uploaded
romaingred
parents:
diff changeset
580 // For the first slide, the previous slide will be empty, so we will call the callback immediately
198009598544 Uploaded
romaingred
parents:
diff changeset
581 transitionOutCallback();
198009598544 Uploaded
romaingred
parents:
diff changeset
582 } else {
198009598544 Uploaded
romaingred
parents:
diff changeset
583 if (this.onTransitionOut) {
198009598544 Uploaded
romaingred
parents:
diff changeset
584 this.onTransitionOut(previousSlide, previousCaption, isSync, transitionOutCallback);
198009598544 Uploaded
romaingred
parents:
diff changeset
585 } else {
198009598544 Uploaded
romaingred
parents:
diff changeset
586 previousSlide.fadeTo(this.getDefaultTransitionDuration(isSync), 0.0, transitionOutCallback);
198009598544 Uploaded
romaingred
parents:
diff changeset
587 if (previousCaption)
198009598544 Uploaded
romaingred
parents:
diff changeset
588 previousCaption.fadeTo(this.getDefaultTransitionDuration(isSync), 0.0);
198009598544 Uploaded
romaingred
parents:
diff changeset
589 }
198009598544 Uploaded
romaingred
parents:
diff changeset
590 }
198009598544 Uploaded
romaingred
parents:
diff changeset
591
198009598544 Uploaded
romaingred
parents:
diff changeset
592 // Go ahead and begin transitioning in of next image
198009598544 Uploaded
romaingred
parents:
diff changeset
593 if (isSync)
198009598544 Uploaded
romaingred
parents:
diff changeset
594 this.buildImage(imageData, isSync);
198009598544 Uploaded
romaingred
parents:
diff changeset
595
198009598544 Uploaded
romaingred
parents:
diff changeset
596 if (!imageData.image) {
198009598544 Uploaded
romaingred
parents:
diff changeset
597 var image = new Image();
198009598544 Uploaded
romaingred
parents:
diff changeset
598
198009598544 Uploaded
romaingred
parents:
diff changeset
599 // Wire up mainImage onload event
198009598544 Uploaded
romaingred
parents:
diff changeset
600 image.onload = function() {
198009598544 Uploaded
romaingred
parents:
diff changeset
601 imageData.image = this;
198009598544 Uploaded
romaingred
parents:
diff changeset
602
198009598544 Uploaded
romaingred
parents:
diff changeset
603 // Only build image if the out transition has completed and we are still on the same image hash
198009598544 Uploaded
romaingred
parents:
diff changeset
604 if (!isTransitioning && imageData.hash == gallery.data[gallery.currentImage.index].hash) {
198009598544 Uploaded
romaingred
parents:
diff changeset
605 gallery.buildImage(imageData, isSync);
198009598544 Uploaded
romaingred
parents:
diff changeset
606 }
198009598544 Uploaded
romaingred
parents:
diff changeset
607 };
198009598544 Uploaded
romaingred
parents:
diff changeset
608
198009598544 Uploaded
romaingred
parents:
diff changeset
609 // set alt and src
198009598544 Uploaded
romaingred
parents:
diff changeset
610 image.alt = imageData.title;
198009598544 Uploaded
romaingred
parents:
diff changeset
611 image.src = imageData.slideUrl;
198009598544 Uploaded
romaingred
parents:
diff changeset
612 }
198009598544 Uploaded
romaingred
parents:
diff changeset
613
198009598544 Uploaded
romaingred
parents:
diff changeset
614 // This causes the preloader (if still running) to relocate out from the currentIndex
198009598544 Uploaded
romaingred
parents:
diff changeset
615 this.relocatePreload = true;
198009598544 Uploaded
romaingred
parents:
diff changeset
616
198009598544 Uploaded
romaingred
parents:
diff changeset
617 return this.syncThumbs();
198009598544 Uploaded
romaingred
parents:
diff changeset
618 },
198009598544 Uploaded
romaingred
parents:
diff changeset
619
198009598544 Uploaded
romaingred
parents:
diff changeset
620 // Called by the refresh method after the previous image has been transitioned out or at the same time
198009598544 Uploaded
romaingred
parents:
diff changeset
621 // as the out transition when performing a synchronous transition.
198009598544 Uploaded
romaingred
parents:
diff changeset
622 // @param {Object} imageData An object holding the image metadata of the image to build.
198009598544 Uploaded
romaingred
parents:
diff changeset
623 // @param {Boolean} isSync Specifies whether the transitions are synchronized.
198009598544 Uploaded
romaingred
parents:
diff changeset
624 buildImage: function(imageData, isSync) {
198009598544 Uploaded
romaingred
parents:
diff changeset
625 var gallery = this;
198009598544 Uploaded
romaingred
parents:
diff changeset
626 var nextIndex = this.getNextIndex(imageData.index);
198009598544 Uploaded
romaingred
parents:
diff changeset
627
198009598544 Uploaded
romaingred
parents:
diff changeset
628 // Construct new hidden span for the image
198009598544 Uploaded
romaingred
parents:
diff changeset
629 var newSlide = this.$imageContainer
198009598544 Uploaded
romaingred
parents:
diff changeset
630 .append('<span class="image-wrapper current"><a class="advance-link" rel="history" href="#'+this.data[nextIndex].hash+'" title="'+imageData.title+'">&nbsp;</a></span>')
198009598544 Uploaded
romaingred
parents:
diff changeset
631 .find('span.current').css('opacity', '0');
198009598544 Uploaded
romaingred
parents:
diff changeset
632
198009598544 Uploaded
romaingred
parents:
diff changeset
633 newSlide.find('a')
198009598544 Uploaded
romaingred
parents:
diff changeset
634 .append(imageData.image)
198009598544 Uploaded
romaingred
parents:
diff changeset
635 .click(function(e) {
198009598544 Uploaded
romaingred
parents:
diff changeset
636 gallery.clickHandler(e, this);
198009598544 Uploaded
romaingred
parents:
diff changeset
637 });
198009598544 Uploaded
romaingred
parents:
diff changeset
638
198009598544 Uploaded
romaingred
parents:
diff changeset
639 var newCaption = 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
640 if (this.$captionContainer) {
198009598544 Uploaded
romaingred
parents:
diff changeset
641 // Construct new hidden caption for the image
198009598544 Uploaded
romaingred
parents:
diff changeset
642 newCaption = this.$captionContainer
198009598544 Uploaded
romaingred
parents:
diff changeset
643 .append('<span class="image-caption current"></span>')
198009598544 Uploaded
romaingred
parents:
diff changeset
644 .find('span.current').css('opacity', '0')
198009598544 Uploaded
romaingred
parents:
diff changeset
645 .append(imageData.caption);
198009598544 Uploaded
romaingred
parents:
diff changeset
646 }
198009598544 Uploaded
romaingred
parents:
diff changeset
647
198009598544 Uploaded
romaingred
parents:
diff changeset
648 // Hide the loading conatiner
198009598544 Uploaded
romaingred
parents:
diff changeset
649 if (this.$loadingContainer) {
198009598544 Uploaded
romaingred
parents:
diff changeset
650 this.$loadingContainer.hide();
198009598544 Uploaded
romaingred
parents:
diff changeset
651 }
198009598544 Uploaded
romaingred
parents:
diff changeset
652
198009598544 Uploaded
romaingred
parents:
diff changeset
653 // Transition in the new image
198009598544 Uploaded
romaingred
parents:
diff changeset
654 if (this.onTransitionIn) {
198009598544 Uploaded
romaingred
parents:
diff changeset
655 this.onTransitionIn(newSlide, newCaption, isSync);
198009598544 Uploaded
romaingred
parents:
diff changeset
656 } else {
198009598544 Uploaded
romaingred
parents:
diff changeset
657 newSlide.fadeTo(this.getDefaultTransitionDuration(isSync), 1.0);
198009598544 Uploaded
romaingred
parents:
diff changeset
658 if (newCaption)
198009598544 Uploaded
romaingred
parents:
diff changeset
659 newCaption.fadeTo(this.getDefaultTransitionDuration(isSync), 1.0);
198009598544 Uploaded
romaingred
parents:
diff changeset
660 }
198009598544 Uploaded
romaingred
parents:
diff changeset
661
198009598544 Uploaded
romaingred
parents:
diff changeset
662 if (this.isSlideshowRunning) {
198009598544 Uploaded
romaingred
parents:
diff changeset
663 if (this.slideshowTimeout)
198009598544 Uploaded
romaingred
parents:
diff changeset
664 clearTimeout(this.slideshowTimeout);
198009598544 Uploaded
romaingred
parents:
diff changeset
665
198009598544 Uploaded
romaingred
parents:
diff changeset
666 this.slideshowTimeout = setTimeout(function() { gallery.ssAdvance(); }, this.delay);
198009598544 Uploaded
romaingred
parents:
diff changeset
667 }
198009598544 Uploaded
romaingred
parents:
diff changeset
668
198009598544 Uploaded
romaingred
parents:
diff changeset
669 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
670 },
198009598544 Uploaded
romaingred
parents:
diff changeset
671
198009598544 Uploaded
romaingred
parents:
diff changeset
672 // Returns the current page index that should be shown for the currentImage
198009598544 Uploaded
romaingred
parents:
diff changeset
673 getCurrentPage: function() {
198009598544 Uploaded
romaingred
parents:
diff changeset
674 return Math.floor(this.currentImage.index / this.numThumbs);
198009598544 Uploaded
romaingred
parents:
diff changeset
675 },
198009598544 Uploaded
romaingred
parents:
diff changeset
676
198009598544 Uploaded
romaingred
parents:
diff changeset
677 // Applies the selected class to the current image's corresponding thumbnail.
198009598544 Uploaded
romaingred
parents:
diff changeset
678 // Also checks if the current page has changed and updates the displayed page of thumbnails if necessary.
198009598544 Uploaded
romaingred
parents:
diff changeset
679 syncThumbs: function() {
198009598544 Uploaded
romaingred
parents:
diff changeset
680 var page = this.getCurrentPage();
198009598544 Uploaded
romaingred
parents:
diff changeset
681 if (page != this.displayedPage)
198009598544 Uploaded
romaingred
parents:
diff changeset
682 this.updateThumbs();
198009598544 Uploaded
romaingred
parents:
diff changeset
683
198009598544 Uploaded
romaingred
parents:
diff changeset
684 // Remove existing selected class and add selected class to new thumb
198009598544 Uploaded
romaingred
parents:
diff changeset
685 var $thumbs = this.find('ul.thumbs').children();
198009598544 Uploaded
romaingred
parents:
diff changeset
686 $thumbs.filter('.selected').removeClass('selected');
198009598544 Uploaded
romaingred
parents:
diff changeset
687 $thumbs.eq(this.currentImage.index).addClass('selected');
198009598544 Uploaded
romaingred
parents:
diff changeset
688
198009598544 Uploaded
romaingred
parents:
diff changeset
689 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
690 },
198009598544 Uploaded
romaingred
parents:
diff changeset
691
198009598544 Uploaded
romaingred
parents:
diff changeset
692 // Performs transitions on the thumbnails container and updates the set of
198009598544 Uploaded
romaingred
parents:
diff changeset
693 // thumbnails that are to be displayed and the navigation controls.
198009598544 Uploaded
romaingred
parents:
diff changeset
694 // @param {Delegate} postTransitionOutHandler An optional delegate that is called after
198009598544 Uploaded
romaingred
parents:
diff changeset
695 // the thumbnails container has transitioned out and before the thumbnails are rebuilt.
198009598544 Uploaded
romaingred
parents:
diff changeset
696 updateThumbs: function(postTransitionOutHandler) {
198009598544 Uploaded
romaingred
parents:
diff changeset
697 var gallery = this;
198009598544 Uploaded
romaingred
parents:
diff changeset
698 var transitionOutCallback = function() {
198009598544 Uploaded
romaingred
parents:
diff changeset
699 // Call the Post-transition Out Handler
198009598544 Uploaded
romaingred
parents:
diff changeset
700 if (postTransitionOutHandler)
198009598544 Uploaded
romaingred
parents:
diff changeset
701 postTransitionOutHandler();
198009598544 Uploaded
romaingred
parents:
diff changeset
702
198009598544 Uploaded
romaingred
parents:
diff changeset
703 gallery.rebuildThumbs();
198009598544 Uploaded
romaingred
parents:
diff changeset
704
198009598544 Uploaded
romaingred
parents:
diff changeset
705 // Transition In the thumbsContainer
198009598544 Uploaded
romaingred
parents:
diff changeset
706 if (gallery.onPageTransitionIn)
198009598544 Uploaded
romaingred
parents:
diff changeset
707 gallery.onPageTransitionIn();
198009598544 Uploaded
romaingred
parents:
diff changeset
708 else
198009598544 Uploaded
romaingred
parents:
diff changeset
709 gallery.show();
198009598544 Uploaded
romaingred
parents:
diff changeset
710 };
198009598544 Uploaded
romaingred
parents:
diff changeset
711
198009598544 Uploaded
romaingred
parents:
diff changeset
712 // Transition Out the thumbsContainer
198009598544 Uploaded
romaingred
parents:
diff changeset
713 if (this.onPageTransitionOut) {
198009598544 Uploaded
romaingred
parents:
diff changeset
714 this.onPageTransitionOut(transitionOutCallback);
198009598544 Uploaded
romaingred
parents:
diff changeset
715 } else {
198009598544 Uploaded
romaingred
parents:
diff changeset
716 this.hide();
198009598544 Uploaded
romaingred
parents:
diff changeset
717 transitionOutCallback();
198009598544 Uploaded
romaingred
parents:
diff changeset
718 }
198009598544 Uploaded
romaingred
parents:
diff changeset
719
198009598544 Uploaded
romaingred
parents:
diff changeset
720 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
721 },
198009598544 Uploaded
romaingred
parents:
diff changeset
722
198009598544 Uploaded
romaingred
parents:
diff changeset
723 // Updates the set of thumbnails that are to be displayed and the navigation controls.
198009598544 Uploaded
romaingred
parents:
diff changeset
724 rebuildThumbs: function() {
198009598544 Uploaded
romaingred
parents:
diff changeset
725 var needsPagination = this.data.length > this.numThumbs;
198009598544 Uploaded
romaingred
parents:
diff changeset
726
198009598544 Uploaded
romaingred
parents:
diff changeset
727 // Rebuild top pager
198009598544 Uploaded
romaingred
parents:
diff changeset
728 if (this.enableTopPager) {
198009598544 Uploaded
romaingred
parents:
diff changeset
729 var $topPager = this.find('div.top');
198009598544 Uploaded
romaingred
parents:
diff changeset
730 if ($topPager.length == 0)
198009598544 Uploaded
romaingred
parents:
diff changeset
731 $topPager = this.prepend('<div class="top pagination"></div>').find('div.top');
198009598544 Uploaded
romaingred
parents:
diff changeset
732 else
198009598544 Uploaded
romaingred
parents:
diff changeset
733 $topPager.empty();
198009598544 Uploaded
romaingred
parents:
diff changeset
734
198009598544 Uploaded
romaingred
parents:
diff changeset
735 if (needsPagination)
198009598544 Uploaded
romaingred
parents:
diff changeset
736 this.buildPager($topPager);
198009598544 Uploaded
romaingred
parents:
diff changeset
737 }
198009598544 Uploaded
romaingred
parents:
diff changeset
738
198009598544 Uploaded
romaingred
parents:
diff changeset
739 // Rebuild bottom pager
198009598544 Uploaded
romaingred
parents:
diff changeset
740 if (this.enableBottomPager) {
198009598544 Uploaded
romaingred
parents:
diff changeset
741 var $bottomPager = this.find('div.bottom');
198009598544 Uploaded
romaingred
parents:
diff changeset
742 if ($bottomPager.length == 0)
198009598544 Uploaded
romaingred
parents:
diff changeset
743 $bottomPager = this.append('<div class="bottom pagination"></div>').find('div.bottom');
198009598544 Uploaded
romaingred
parents:
diff changeset
744 else
198009598544 Uploaded
romaingred
parents:
diff changeset
745 $bottomPager.empty();
198009598544 Uploaded
romaingred
parents:
diff changeset
746
198009598544 Uploaded
romaingred
parents:
diff changeset
747 if (needsPagination)
198009598544 Uploaded
romaingred
parents:
diff changeset
748 this.buildPager($bottomPager);
198009598544 Uploaded
romaingred
parents:
diff changeset
749 }
198009598544 Uploaded
romaingred
parents:
diff changeset
750
198009598544 Uploaded
romaingred
parents:
diff changeset
751 var page = this.getCurrentPage();
198009598544 Uploaded
romaingred
parents:
diff changeset
752 var startIndex = page*this.numThumbs;
198009598544 Uploaded
romaingred
parents:
diff changeset
753 var stopIndex = startIndex+this.numThumbs-1;
198009598544 Uploaded
romaingred
parents:
diff changeset
754 if (stopIndex >= this.data.length)
198009598544 Uploaded
romaingred
parents:
diff changeset
755 stopIndex = this.data.length-1;
198009598544 Uploaded
romaingred
parents:
diff changeset
756
198009598544 Uploaded
romaingred
parents:
diff changeset
757 // Show/Hide thumbs
198009598544 Uploaded
romaingred
parents:
diff changeset
758 var $thumbsUl = this.find('ul.thumbs');
198009598544 Uploaded
romaingred
parents:
diff changeset
759 $thumbsUl.find('li').each(function(i) {
198009598544 Uploaded
romaingred
parents:
diff changeset
760 var $li = $(this);
198009598544 Uploaded
romaingred
parents:
diff changeset
761 if (i >= startIndex && i <= stopIndex) {
198009598544 Uploaded
romaingred
parents:
diff changeset
762 $li.show();
198009598544 Uploaded
romaingred
parents:
diff changeset
763 } else {
198009598544 Uploaded
romaingred
parents:
diff changeset
764 $li.hide();
198009598544 Uploaded
romaingred
parents:
diff changeset
765 }
198009598544 Uploaded
romaingred
parents:
diff changeset
766 });
198009598544 Uploaded
romaingred
parents:
diff changeset
767
198009598544 Uploaded
romaingred
parents:
diff changeset
768 this.displayedPage = page;
198009598544 Uploaded
romaingred
parents:
diff changeset
769
198009598544 Uploaded
romaingred
parents:
diff changeset
770 // Remove the noscript class from the thumbs container ul
198009598544 Uploaded
romaingred
parents:
diff changeset
771 $thumbsUl.removeClass('noscript');
198009598544 Uploaded
romaingred
parents:
diff changeset
772
198009598544 Uploaded
romaingred
parents:
diff changeset
773 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
774 },
198009598544 Uploaded
romaingred
parents:
diff changeset
775
198009598544 Uploaded
romaingred
parents:
diff changeset
776 // Returns the total number of pages required to display all the thumbnails.
198009598544 Uploaded
romaingred
parents:
diff changeset
777 getNumPages: function() {
198009598544 Uploaded
romaingred
parents:
diff changeset
778 return Math.ceil(this.data.length/this.numThumbs);
198009598544 Uploaded
romaingred
parents:
diff changeset
779 },
198009598544 Uploaded
romaingred
parents:
diff changeset
780
198009598544 Uploaded
romaingred
parents:
diff changeset
781 // Rebuilds the pager control in the specified matched element.
198009598544 Uploaded
romaingred
parents:
diff changeset
782 // @param {jQuery} pager A jQuery element set matching the particular pager to be rebuilt.
198009598544 Uploaded
romaingred
parents:
diff changeset
783 buildPager: function(pager) {
198009598544 Uploaded
romaingred
parents:
diff changeset
784 var gallery = this;
198009598544 Uploaded
romaingred
parents:
diff changeset
785 var numPages = this.getNumPages();
198009598544 Uploaded
romaingred
parents:
diff changeset
786 var page = this.getCurrentPage();
198009598544 Uploaded
romaingred
parents:
diff changeset
787 var startIndex = page * this.numThumbs;
198009598544 Uploaded
romaingred
parents:
diff changeset
788 var pagesRemaining = this.maxPagesToShow - 1;
198009598544 Uploaded
romaingred
parents:
diff changeset
789
198009598544 Uploaded
romaingred
parents:
diff changeset
790 var pageNum = page - Math.floor((this.maxPagesToShow - 1) / 2) + 1;
198009598544 Uploaded
romaingred
parents:
diff changeset
791 if (pageNum > 0) {
198009598544 Uploaded
romaingred
parents:
diff changeset
792 var remainingPageCount = numPages - pageNum;
198009598544 Uploaded
romaingred
parents:
diff changeset
793 if (remainingPageCount < pagesRemaining) {
198009598544 Uploaded
romaingred
parents:
diff changeset
794 pageNum = pageNum - (pagesRemaining - remainingPageCount);
198009598544 Uploaded
romaingred
parents:
diff changeset
795 }
198009598544 Uploaded
romaingred
parents:
diff changeset
796 }
198009598544 Uploaded
romaingred
parents:
diff changeset
797
198009598544 Uploaded
romaingred
parents:
diff changeset
798 if (pageNum < 0) {
198009598544 Uploaded
romaingred
parents:
diff changeset
799 pageNum = 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
800 }
198009598544 Uploaded
romaingred
parents:
diff changeset
801
198009598544 Uploaded
romaingred
parents:
diff changeset
802 // Prev Page Link
198009598544 Uploaded
romaingred
parents:
diff changeset
803 if (page > 0) {
198009598544 Uploaded
romaingred
parents:
diff changeset
804 var prevPage = startIndex - this.numThumbs;
198009598544 Uploaded
romaingred
parents:
diff changeset
805 pager.append('<a rel="history" href="#'+this.data[prevPage].hash+'" title="'+this.prevPageLinkText+'">'+this.prevPageLinkText+'</a>');
198009598544 Uploaded
romaingred
parents:
diff changeset
806 }
198009598544 Uploaded
romaingred
parents:
diff changeset
807
198009598544 Uploaded
romaingred
parents:
diff changeset
808 // Create First Page link if needed
198009598544 Uploaded
romaingred
parents:
diff changeset
809 if (pageNum > 0) {
198009598544 Uploaded
romaingred
parents:
diff changeset
810 this.buildPageLink(pager, 0, numPages);
198009598544 Uploaded
romaingred
parents:
diff changeset
811 if (pageNum > 1)
198009598544 Uploaded
romaingred
parents:
diff changeset
812 pager.append('<span class="ellipsis">&hellip;</span>');
198009598544 Uploaded
romaingred
parents:
diff changeset
813
198009598544 Uploaded
romaingred
parents:
diff changeset
814 pagesRemaining--;
198009598544 Uploaded
romaingred
parents:
diff changeset
815 }
198009598544 Uploaded
romaingred
parents:
diff changeset
816
198009598544 Uploaded
romaingred
parents:
diff changeset
817 // Page Index Links
198009598544 Uploaded
romaingred
parents:
diff changeset
818 while (pagesRemaining > 0) {
198009598544 Uploaded
romaingred
parents:
diff changeset
819 this.buildPageLink(pager, pageNum, numPages);
198009598544 Uploaded
romaingred
parents:
diff changeset
820 pagesRemaining--;
198009598544 Uploaded
romaingred
parents:
diff changeset
821 pageNum++;
198009598544 Uploaded
romaingred
parents:
diff changeset
822 }
198009598544 Uploaded
romaingred
parents:
diff changeset
823
198009598544 Uploaded
romaingred
parents:
diff changeset
824 // Create Last Page link if needed
198009598544 Uploaded
romaingred
parents:
diff changeset
825 if (pageNum < numPages) {
198009598544 Uploaded
romaingred
parents:
diff changeset
826 var lastPageNum = numPages - 1;
198009598544 Uploaded
romaingred
parents:
diff changeset
827 if (pageNum < lastPageNum)
198009598544 Uploaded
romaingred
parents:
diff changeset
828 pager.append('<span class="ellipsis">&hellip;</span>');
198009598544 Uploaded
romaingred
parents:
diff changeset
829
198009598544 Uploaded
romaingred
parents:
diff changeset
830 this.buildPageLink(pager, lastPageNum, numPages);
198009598544 Uploaded
romaingred
parents:
diff changeset
831 }
198009598544 Uploaded
romaingred
parents:
diff changeset
832
198009598544 Uploaded
romaingred
parents:
diff changeset
833 // Next Page Link
198009598544 Uploaded
romaingred
parents:
diff changeset
834 var nextPage = startIndex + this.numThumbs;
198009598544 Uploaded
romaingred
parents:
diff changeset
835 if (nextPage < this.data.length) {
198009598544 Uploaded
romaingred
parents:
diff changeset
836 pager.append('<a rel="history" href="#'+this.data[nextPage].hash+'" title="'+this.nextPageLinkText+'">'+this.nextPageLinkText+'</a>');
198009598544 Uploaded
romaingred
parents:
diff changeset
837 }
198009598544 Uploaded
romaingred
parents:
diff changeset
838
198009598544 Uploaded
romaingred
parents:
diff changeset
839 pager.find('a').click(function(e) {
198009598544 Uploaded
romaingred
parents:
diff changeset
840 gallery.clickHandler(e, this);
198009598544 Uploaded
romaingred
parents:
diff changeset
841 });
198009598544 Uploaded
romaingred
parents:
diff changeset
842
198009598544 Uploaded
romaingred
parents:
diff changeset
843 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
844 },
198009598544 Uploaded
romaingred
parents:
diff changeset
845
198009598544 Uploaded
romaingred
parents:
diff changeset
846 // Builds a single page link within a pager. This function is called by buildPager
198009598544 Uploaded
romaingred
parents:
diff changeset
847 // @param {jQuery} pager A jQuery element set matching the particular pager to be rebuilt.
198009598544 Uploaded
romaingred
parents:
diff changeset
848 // @param {Integer} pageNum The page number of the page link to build.
198009598544 Uploaded
romaingred
parents:
diff changeset
849 // @param {Integer} numPages The total number of pages required to display all thumbnails.
198009598544 Uploaded
romaingred
parents:
diff changeset
850 buildPageLink: function(pager, pageNum, numPages) {
198009598544 Uploaded
romaingred
parents:
diff changeset
851 var pageLabel = pageNum + 1;
198009598544 Uploaded
romaingred
parents:
diff changeset
852 var currentPage = this.getCurrentPage();
198009598544 Uploaded
romaingred
parents:
diff changeset
853 if (pageNum == currentPage)
198009598544 Uploaded
romaingred
parents:
diff changeset
854 pager.append('<span class="current">'+pageLabel+'</span>');
198009598544 Uploaded
romaingred
parents:
diff changeset
855 else if (pageNum < numPages) {
198009598544 Uploaded
romaingred
parents:
diff changeset
856 var imageIndex = pageNum*this.numThumbs;
198009598544 Uploaded
romaingred
parents:
diff changeset
857 pager.append('<a rel="history" href="#'+this.data[imageIndex].hash+'" title="'+pageLabel+'">'+pageLabel+'</a>');
198009598544 Uploaded
romaingred
parents:
diff changeset
858 }
198009598544 Uploaded
romaingred
parents:
diff changeset
859
198009598544 Uploaded
romaingred
parents:
diff changeset
860 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
861 }
198009598544 Uploaded
romaingred
parents:
diff changeset
862 });
198009598544 Uploaded
romaingred
parents:
diff changeset
863
198009598544 Uploaded
romaingred
parents:
diff changeset
864 // Now initialize the gallery
198009598544 Uploaded
romaingred
parents:
diff changeset
865 $.extend(this, defaults, settings);
198009598544 Uploaded
romaingred
parents:
diff changeset
866
198009598544 Uploaded
romaingred
parents:
diff changeset
867 // Verify the history plugin is available
198009598544 Uploaded
romaingred
parents:
diff changeset
868 if (this.enableHistory && !$.historyInit)
198009598544 Uploaded
romaingred
parents:
diff changeset
869 this.enableHistory = false;
198009598544 Uploaded
romaingred
parents:
diff changeset
870
198009598544 Uploaded
romaingred
parents:
diff changeset
871 // Select containers
198009598544 Uploaded
romaingred
parents:
diff changeset
872 if (this.imageContainerSel) this.$imageContainer = $(this.imageContainerSel);
198009598544 Uploaded
romaingred
parents:
diff changeset
873 if (this.captionContainerSel) this.$captionContainer = $(this.captionContainerSel);
198009598544 Uploaded
romaingred
parents:
diff changeset
874 if (this.loadingContainerSel) this.$loadingContainer = $(this.loadingContainerSel);
198009598544 Uploaded
romaingred
parents:
diff changeset
875
198009598544 Uploaded
romaingred
parents:
diff changeset
876 // Initialize the thumbails
198009598544 Uploaded
romaingred
parents:
diff changeset
877 this.initializeThumbs();
198009598544 Uploaded
romaingred
parents:
diff changeset
878
198009598544 Uploaded
romaingred
parents:
diff changeset
879 if (this.maxPagesToShow < 3)
198009598544 Uploaded
romaingred
parents:
diff changeset
880 this.maxPagesToShow = 3;
198009598544 Uploaded
romaingred
parents:
diff changeset
881
198009598544 Uploaded
romaingred
parents:
diff changeset
882 this.displayedPage = -1;
198009598544 Uploaded
romaingred
parents:
diff changeset
883 this.currentImage = this.data[0];
198009598544 Uploaded
romaingred
parents:
diff changeset
884 var gallery = this;
198009598544 Uploaded
romaingred
parents:
diff changeset
885
198009598544 Uploaded
romaingred
parents:
diff changeset
886 // Hide the loadingContainer
198009598544 Uploaded
romaingred
parents:
diff changeset
887 if (this.$loadingContainer)
198009598544 Uploaded
romaingred
parents:
diff changeset
888 this.$loadingContainer.hide();
198009598544 Uploaded
romaingred
parents:
diff changeset
889
198009598544 Uploaded
romaingred
parents:
diff changeset
890 // Setup controls
198009598544 Uploaded
romaingred
parents:
diff changeset
891 if (this.controlsContainerSel) {
198009598544 Uploaded
romaingred
parents:
diff changeset
892 this.$controlsContainer = $(this.controlsContainerSel).empty();
198009598544 Uploaded
romaingred
parents:
diff changeset
893
198009598544 Uploaded
romaingred
parents:
diff changeset
894 if (this.renderSSControls) {
198009598544 Uploaded
romaingred
parents:
diff changeset
895 if (this.autoStart) {
198009598544 Uploaded
romaingred
parents:
diff changeset
896 this.$controlsContainer
198009598544 Uploaded
romaingred
parents:
diff changeset
897 .append('<div class="ss-controls"><a href="#pause" class="pause" title="'+this.pauseLinkText+'">'+this.pauseLinkText+'</a></div>');
198009598544 Uploaded
romaingred
parents:
diff changeset
898 } else {
198009598544 Uploaded
romaingred
parents:
diff changeset
899 this.$controlsContainer
198009598544 Uploaded
romaingred
parents:
diff changeset
900 .append('<div class="ss-controls"><a href="#play" class="play" title="'+this.playLinkText+'">'+this.playLinkText+'</a></div>');
198009598544 Uploaded
romaingred
parents:
diff changeset
901 }
198009598544 Uploaded
romaingred
parents:
diff changeset
902
198009598544 Uploaded
romaingred
parents:
diff changeset
903 this.$controlsContainer.find('div.ss-controls a')
198009598544 Uploaded
romaingred
parents:
diff changeset
904 .click(function(e) {
198009598544 Uploaded
romaingred
parents:
diff changeset
905 gallery.toggleSlideshow();
198009598544 Uploaded
romaingred
parents:
diff changeset
906 e.preventDefault();
198009598544 Uploaded
romaingred
parents:
diff changeset
907 return false;
198009598544 Uploaded
romaingred
parents:
diff changeset
908 });
198009598544 Uploaded
romaingred
parents:
diff changeset
909 }
198009598544 Uploaded
romaingred
parents:
diff changeset
910
198009598544 Uploaded
romaingred
parents:
diff changeset
911 if (this.renderNavControls) {
198009598544 Uploaded
romaingred
parents:
diff changeset
912 this.$controlsContainer
198009598544 Uploaded
romaingred
parents:
diff changeset
913 .append('<div class="nav-controls"><a class="prev" rel="history" title="'+this.prevLinkText+'">'+this.prevLinkText+'</a><a class="next" rel="history" title="'+this.nextLinkText+'">'+this.nextLinkText+'</a></div>')
198009598544 Uploaded
romaingred
parents:
diff changeset
914 .find('div.nav-controls a')
198009598544 Uploaded
romaingred
parents:
diff changeset
915 .click(function(e) {
198009598544 Uploaded
romaingred
parents:
diff changeset
916 gallery.clickHandler(e, this);
198009598544 Uploaded
romaingred
parents:
diff changeset
917 });
198009598544 Uploaded
romaingred
parents:
diff changeset
918 }
198009598544 Uploaded
romaingred
parents:
diff changeset
919 }
198009598544 Uploaded
romaingred
parents:
diff changeset
920
198009598544 Uploaded
romaingred
parents:
diff changeset
921 var initFirstImage = !this.enableHistory || !location.hash;
198009598544 Uploaded
romaingred
parents:
diff changeset
922 if (this.enableHistory && location.hash) {
198009598544 Uploaded
romaingred
parents:
diff changeset
923 var hash = $.galleriffic.normalizeHash(location.hash);
198009598544 Uploaded
romaingred
parents:
diff changeset
924 var imageData = allImages[hash];
198009598544 Uploaded
romaingred
parents:
diff changeset
925 if (!imageData)
198009598544 Uploaded
romaingred
parents:
diff changeset
926 initFirstImage = true;
198009598544 Uploaded
romaingred
parents:
diff changeset
927 }
198009598544 Uploaded
romaingred
parents:
diff changeset
928
198009598544 Uploaded
romaingred
parents:
diff changeset
929 // Setup gallery to show the first image
198009598544 Uploaded
romaingred
parents:
diff changeset
930 if (initFirstImage)
198009598544 Uploaded
romaingred
parents:
diff changeset
931 this.gotoIndex(0, false, true);
198009598544 Uploaded
romaingred
parents:
diff changeset
932
198009598544 Uploaded
romaingred
parents:
diff changeset
933 // Setup Keyboard Navigation
198009598544 Uploaded
romaingred
parents:
diff changeset
934 if (this.enableKeyboardNavigation) {
198009598544 Uploaded
romaingred
parents:
diff changeset
935 $(document).keydown(function(e) {
198009598544 Uploaded
romaingred
parents:
diff changeset
936 var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
937 switch(key) {
198009598544 Uploaded
romaingred
parents:
diff changeset
938 case 32: // space
198009598544 Uploaded
romaingred
parents:
diff changeset
939 gallery.next();
198009598544 Uploaded
romaingred
parents:
diff changeset
940 e.preventDefault();
198009598544 Uploaded
romaingred
parents:
diff changeset
941 break;
198009598544 Uploaded
romaingred
parents:
diff changeset
942 case 33: // Page Up
198009598544 Uploaded
romaingred
parents:
diff changeset
943 gallery.previousPage();
198009598544 Uploaded
romaingred
parents:
diff changeset
944 e.preventDefault();
198009598544 Uploaded
romaingred
parents:
diff changeset
945 break;
198009598544 Uploaded
romaingred
parents:
diff changeset
946 case 34: // Page Down
198009598544 Uploaded
romaingred
parents:
diff changeset
947 gallery.nextPage();
198009598544 Uploaded
romaingred
parents:
diff changeset
948 e.preventDefault();
198009598544 Uploaded
romaingred
parents:
diff changeset
949 break;
198009598544 Uploaded
romaingred
parents:
diff changeset
950 case 35: // End
198009598544 Uploaded
romaingred
parents:
diff changeset
951 gallery.gotoIndex(gallery.data.length-1);
198009598544 Uploaded
romaingred
parents:
diff changeset
952 e.preventDefault();
198009598544 Uploaded
romaingred
parents:
diff changeset
953 break;
198009598544 Uploaded
romaingred
parents:
diff changeset
954 case 36: // Home
198009598544 Uploaded
romaingred
parents:
diff changeset
955 gallery.gotoIndex(0);
198009598544 Uploaded
romaingred
parents:
diff changeset
956 e.preventDefault();
198009598544 Uploaded
romaingred
parents:
diff changeset
957 break;
198009598544 Uploaded
romaingred
parents:
diff changeset
958 case 37: // left arrow
198009598544 Uploaded
romaingred
parents:
diff changeset
959 gallery.previous();
198009598544 Uploaded
romaingred
parents:
diff changeset
960 e.preventDefault();
198009598544 Uploaded
romaingred
parents:
diff changeset
961 break;
198009598544 Uploaded
romaingred
parents:
diff changeset
962 case 39: // right arrow
198009598544 Uploaded
romaingred
parents:
diff changeset
963 gallery.next();
198009598544 Uploaded
romaingred
parents:
diff changeset
964 e.preventDefault();
198009598544 Uploaded
romaingred
parents:
diff changeset
965 break;
198009598544 Uploaded
romaingred
parents:
diff changeset
966 }
198009598544 Uploaded
romaingred
parents:
diff changeset
967 });
198009598544 Uploaded
romaingred
parents:
diff changeset
968 }
198009598544 Uploaded
romaingred
parents:
diff changeset
969
198009598544 Uploaded
romaingred
parents:
diff changeset
970 // Auto start the slideshow
198009598544 Uploaded
romaingred
parents:
diff changeset
971 if (this.autoStart)
198009598544 Uploaded
romaingred
parents:
diff changeset
972 this.play();
198009598544 Uploaded
romaingred
parents:
diff changeset
973
198009598544 Uploaded
romaingred
parents:
diff changeset
974 // Kickoff Image Preloader after 1 second
198009598544 Uploaded
romaingred
parents:
diff changeset
975 setTimeout(function() { gallery.preloadInit(); }, 1000);
198009598544 Uploaded
romaingred
parents:
diff changeset
976
198009598544 Uploaded
romaingred
parents:
diff changeset
977 return this;
198009598544 Uploaded
romaingred
parents:
diff changeset
978 };
198009598544 Uploaded
romaingred
parents:
diff changeset
979 })(jQuery);