| 1 |
/* VikFXThumbSlide v1.1 */ |
| 2 |
|
| 3 |
jQuery.noConflict(); |
| 4 |
jQuery.VikFXThumbSlide = function () { |
| 5 |
var thumbslides = [], |
| 6 |
defaultsettings = { |
| 7 |
// Default Settings |
| 8 |
fadeTime : 600, |
| 9 |
timeForSlideInSlideshow : 2500, |
| 10 |
startIndex : 1, |
| 11 |
startSlideshowAtLoad : false, |
| 12 |
thumbnailActivationEvent : "click", |
| 13 |
useFadingIn : true, |
| 14 |
useFadingOut : true, |
| 15 |
useFadeWhenNotSlideshow : true, |
| 16 |
useFadeForSlideshow : true, |
| 17 |
loopSlideshow : true, |
| 18 |
usePreloading : true, |
| 19 |
useAltAsTooltip : false, |
| 20 |
mainImageClass : "vikfx-thumbslide-image", |
| 21 |
captionContainerClass : "vikfx-thumbslide-caption", |
| 22 |
fadeContainerClass : "vikfx-thumbslide-fade-container", |
| 23 |
thumbnailContainerClass: "vikfx-thumbslide-thumbnails", |
| 24 |
useNavigationControls: true, |
| 25 |
previousLinkClass : "vikfx-thumbslide-previous-image", |
| 26 |
nextLinkClass : "vikfx-thumbslide-next-image", |
| 27 |
startSlideShowClass : "vikfx-thumbslide-start-slideshow", |
| 28 |
stopSlideShowClass : "vikfx-thumbslide-stop-slideshow" |
| 29 |
}, |
| 30 |
|
| 31 |
set = function (settings) { |
| 32 |
thumbslides.push(jQuery.extend({}, defaultsettings, settings)); |
| 33 |
}, |
| 34 |
|
| 35 |
fix = function (fxselector) { |
| 36 |
var nowc = 0; |
| 37 |
jQuery(fxselector).each(function () { |
| 38 |
if(thumbslides.length < jQuery(fxselector).length && !thumbslides[nowc]) { |
| 39 |
thumbslides[nowc] = thumbslides[(nowc - 1)]; |
| 40 |
} |
| 41 |
nowc++; |
| 42 |
}); |
| 43 |
}, |
| 44 |
|
| 45 |
init = function (fxselector) { |
| 46 |
fix(fxselector); |
| 47 |
var counter = 0; |
| 48 |
jQuery(fxselector).each(function () { |
| 49 |
var elm = jQuery(this), |
| 50 |
|
| 51 |
// Element references |
| 52 |
settings = thumbslides[counter++], |
| 53 |
mainImage = elm.find("." + settings.mainImageClass), |
| 54 |
captionContainer = elm.find("." + settings.captionContainerClass), |
| 55 |
fadeContainer = elm.find("." + settings.fadeContainerClass), |
| 56 |
useNavigationControls = settings.useNavigationControls, |
| 57 |
previousLink = elm.find("." + settings.previousLinkClass), |
| 58 |
nextLink = elm.find("." + settings.nextLinkClass), |
| 59 |
startSlideShowLink = elm.find("." + settings.startSlideShowClass), |
| 60 |
stopSlideShowLink = elm.find("." + settings.stopSlideShowClass), |
| 61 |
thumbnailContainer = elm.find("." + settings.thumbnailContainerClass), |
| 62 |
thumbnailEvent = settings.thumbnailActivationEvent, |
| 63 |
thumbnailLinks, |
| 64 |
|
| 65 |
// General image settings |
| 66 |
usePreloading = settings.usePreloading, |
| 67 |
useAltAsTooltip = settings.useAltAsTooltip, |
| 68 |
images = settings.images, |
| 69 |
startIndex = (settings.startIndex > 0)? (settings.startIndex - 1) : settings.startIndex, |
| 70 |
imageIndex = startIndex, |
| 71 |
currentImageIndex = imageIndex, |
| 72 |
|
| 73 |
// General fade settings |
| 74 |
useFadingIn = settings.useFadingIn, |
| 75 |
useFadingOut = settings.useFadingOut, |
| 76 |
useFadeWhenNotSlideshow = settings.useFadeWhenNotSlideshow, |
| 77 |
useFadeForSlideshow = settings.useFadeForSlideshow, |
| 78 |
loopSlideshow = settings.loopSlideshow, |
| 79 |
fadeTime = settings.fadeTime, |
| 80 |
timeForSlideInSlideshow = settings.timeForSlideInSlideshow, |
| 81 |
startSlideshowAtLoad = settings.startSlideshowAtLoad, |
| 82 |
slideshowPlaying = false, |
| 83 |
timer, |
| 84 |
|
| 85 |
// Sets main image |
| 86 |
setImage = function () { |
| 87 |
if (!images.hasOwnProperty(imageIndex)) { |
| 88 |
return; |
| 89 |
} |
| 90 |
// Set main image values |
| 91 |
var imageItem = images[imageIndex]; |
| 92 |
mainImage.attr({ |
| 93 |
src : imageItem.image, |
| 94 |
alt : imageItem.alt |
| 95 |
}); |
| 96 |
|
| 97 |
//Caption |
| 98 |
if(imageItem.hasOwnProperty("caption") && imageItem.caption.length > 0) { |
| 99 |
captionContainer.html("<div>"+imageItem.caption+"</div>"); |
| 100 |
}else { |
| 101 |
captionContainer.html(""); |
| 102 |
} |
| 103 |
|
| 104 |
// If the alt text should be used as the tooltip |
| 105 |
if (useAltAsTooltip) { |
| 106 |
mainImage.attr("title", imageItem.alt); |
| 107 |
} |
| 108 |
|
| 109 |
if (!loopSlideshow) { |
| 110 |
// Enabling/disabling previous link |
| 111 |
if (imageIndex === 0) { |
| 112 |
previousLink.addClass("vikfx-thumbslide-disabled"); |
| 113 |
} |
| 114 |
else { |
| 115 |
previousLink.removeClass("vikfx-thumbslide-disabled"); |
| 116 |
} |
| 117 |
|
| 118 |
// Enabling/disabling next link |
| 119 |
if (imageIndex === (images.length - 1)) { |
| 120 |
nextLink.addClass("vikfx-thumbslide-disabled"); |
| 121 |
} |
| 122 |
else { |
| 123 |
nextLink.removeClass("vikfx-thumbslide-disabled"); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
// Keeping a reference to the current image index |
| 128 |
currentImageIndex = imageIndex; |
| 129 |
|
| 130 |
// Adding/removing classes from thumbnail |
| 131 |
if (thumbnailContainer[0]) { |
| 132 |
thumbnailLinks.removeClass("vikfx-thumbslide-selected-thumbnail"); |
| 133 |
jQuery(thumbnailLinks[imageIndex]).addClass("vikfx-thumbslide-selected-thumbnail"); |
| 134 |
} |
| 135 |
}, |
| 136 |
|
| 137 |
// Navigate to previous image |
| 138 |
prev = function () { |
| 139 |
if (imageIndex > 0 || loopSlideshow) { |
| 140 |
if (imageIndex === 0) { |
| 141 |
imageIndex = (images.length -1); |
| 142 |
} |
| 143 |
else { |
| 144 |
imageIndex = --imageIndex; |
| 145 |
} |
| 146 |
if (useFadingOut && (useFadeWhenNotSlideshow || slideshowPlaying) && imageIndex !== currentImageIndex) { |
| 147 |
fadeContainer.stop(); |
| 148 |
fadeContainer.fadeTo(fadeTime, 0, function () { |
| 149 |
setImage(imageIndex); |
| 150 |
}); |
| 151 |
} |
| 152 |
else { |
| 153 |
if (useFadingIn && imageIndex !== currentImageIndex) { |
| 154 |
fadeContainer.css("opacity", "0"); |
| 155 |
} |
| 156 |
setImage(imageIndex); |
| 157 |
} |
| 158 |
} |
| 159 |
}, |
| 160 |
|
| 161 |
// Navigate to next image |
| 162 |
next = function (specifiedIndex) { |
| 163 |
if (imageIndex < (images.length -1) || typeof specifiedIndex !== "undefined" || loopSlideshow) { |
| 164 |
if (typeof specifiedIndex !== "undefined") { |
| 165 |
imageIndex = specifiedIndex; |
| 166 |
} |
| 167 |
else if (imageIndex === (images.length-1)) { |
| 168 |
imageIndex = 0; |
| 169 |
} |
| 170 |
else { |
| 171 |
imageIndex = ++imageIndex; |
| 172 |
} |
| 173 |
if (useFadingOut && (useFadeWhenNotSlideshow || slideshowPlaying) && imageIndex !== currentImageIndex) { |
| 174 |
fadeContainer.stop(); |
| 175 |
fadeContainer.fadeTo(fadeTime, 0, function () { |
| 176 |
setImage(imageIndex); |
| 177 |
}); |
| 178 |
} |
| 179 |
else { |
| 180 |
if (useFadingIn && imageIndex !== currentImageIndex) { |
| 181 |
fadeContainer.css("opacity", "0"); |
| 182 |
} |
| 183 |
setImage(imageIndex); |
| 184 |
} |
| 185 |
} |
| 186 |
else { |
| 187 |
stopSlideshow(); |
| 188 |
} |
| 189 |
}, |
| 190 |
|
| 191 |
// Start slideshow |
| 192 |
startSlideshow = function () { |
| 193 |
slideshowPlaying = true; |
| 194 |
startSlideShowLink.css('display', 'none'); |
| 195 |
stopSlideShowLink.css('display', 'inline-block'); |
| 196 |
clearTimeout(timer); |
| 197 |
timer = setTimeout(function () { |
| 198 |
next(); |
| 199 |
}, timeForSlideInSlideshow); |
| 200 |
}, |
| 201 |
|
| 202 |
// Stop slideshow |
| 203 |
stopSlideshow = function () { |
| 204 |
clearTimeout(timer); |
| 205 |
slideshowPlaying = false; |
| 206 |
startSlideShowLink.css('display', 'inline-block'); |
| 207 |
stopSlideShowLink.css('display', 'none'); |
| 208 |
}; |
| 209 |
|
| 210 |
// Fade in/show image when it has loaded |
| 211 |
mainImage[0].onload = function () { |
| 212 |
if (useFadingIn && (useFadeWhenNotSlideshow || slideshowPlaying)) { |
| 213 |
fadeContainer.fadeTo(fadeTime, 1, function () { |
| 214 |
if (slideshowPlaying) { |
| 215 |
clearTimeout(timer); |
| 216 |
timer = setTimeout(function () { |
| 217 |
next(); |
| 218 |
}, timeForSlideInSlideshow); |
| 219 |
} |
| 220 |
}); |
| 221 |
} |
| 222 |
else { |
| 223 |
fadeContainer.css("opacity", "1"); |
| 224 |
fadeContainer.show(); |
| 225 |
if (slideshowPlaying) { |
| 226 |
clearTimeout(timer); |
| 227 |
timer = setTimeout(function () { |
| 228 |
next(); |
| 229 |
}, timeForSlideInSlideshow); |
| 230 |
} |
| 231 |
} |
| 232 |
}; |
| 233 |
|
| 234 |
mainImage[0].onerror = function () { |
| 235 |
fadeContainer.css("opacity", "1"); |
| 236 |
if (slideshowPlaying) { |
| 237 |
clearTimeout(timer); |
| 238 |
timer = setTimeout(function () { |
| 239 |
next(); |
| 240 |
}, timeForSlideInSlideshow); |
| 241 |
} |
| 242 |
}; |
| 243 |
|
| 244 |
// Previous image click |
| 245 |
previousLink.click(function (evt) { |
| 246 |
prev(); |
| 247 |
return false; |
| 248 |
}); |
| 249 |
|
| 250 |
// Next image click |
| 251 |
nextLink.click(function (evt) { |
| 252 |
next(); |
| 253 |
return false; |
| 254 |
}); |
| 255 |
|
| 256 |
// Start slideshow click |
| 257 |
startSlideShowLink.click(function () { |
| 258 |
startSlideshow(); |
| 259 |
return false; |
| 260 |
}); |
| 261 |
|
| 262 |
// Stop slideshow click |
| 263 |
stopSlideShowLink.click(function () { |
| 264 |
stopSlideshow(); |
| 265 |
return false; |
| 266 |
}); |
| 267 |
|
| 268 |
// Shows navigation links |
| 269 |
if(useNavigationControls) { |
| 270 |
previousLink.css('display', 'inline-block'); |
| 271 |
nextLink.css('display', 'inline-block'); |
| 272 |
startSlideShowLink.css('display', 'inline-block'); |
| 273 |
} |
| 274 |
|
| 275 |
// Thumbnail references |
| 276 |
if (thumbnailContainer[0]) { |
| 277 |
thumbnailLinks = jQuery(thumbnailContainer).find("a"); |
| 278 |
jQuery(thumbnailLinks[imageIndex]).addClass("vikfx-thumbslide-selected-thumbnail"); |
| 279 |
for (var i=0, il=thumbnailLinks.length, thumbnailLink; i<il; i++) { |
| 280 |
thumbnailLink = jQuery(thumbnailLinks[i]); |
| 281 |
thumbnailLink.data("linkIndex", i); |
| 282 |
thumbnailLink.bind(thumbnailEvent, function (evt) { |
| 283 |
next(jQuery(this).data("linkIndex")); |
| 284 |
this.blur(); |
| 285 |
return false; |
| 286 |
}); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
// Sets initial image |
| 291 |
setImage(); |
| 292 |
|
| 293 |
// If play slideshow at load |
| 294 |
if (startSlideshowAtLoad) { |
| 295 |
startSlideshow(); |
| 296 |
} |
| 297 |
|
| 298 |
if (usePreloading) { |
| 299 |
var imagePreLoadingContainer = jQuery("<div />").appendTo(document.body).css("display", "none"); |
| 300 |
for (var j=0, jl=images.length, image; j<jl; j++) { |
| 301 |
jQuery('<img src="' + images[j].image + '" alt="" />').appendTo(imagePreLoadingContainer); |
| 302 |
} |
| 303 |
} |
| 304 |
}); |
| 305 |
}; |
| 306 |
return { |
| 307 |
set : set, |
| 308 |
init : init |
| 309 |
}; |
| 310 |
}(); |
| 311 |
|