/**
* jquery lightbox
* version 0.5 - 11/29/2007
* @author warren krewenki
*
* this package is distributed under the bsd license.
* for full license information, see license.txt
*
* based on lightbox 2 by lokesh dhakar (http://www.huddletogether.com/projects/lightbox2/)
* originally written to make use of the prototype framework, and script.acalo.us, now altered to use jquery.
*
*
**/
(function($){
$.fn.lightbox = function(options){
// build main options
var opts = $.extend({}, $.fn.lightbox.defaults, options);
return this.each(function(){
$(this).click(function(){
// initalize the lightbox
initialize();
start(this);
return false;
});
});
/**
* initalize()
*
* @return void
* @author warren krewenki
*/
function initialize() {
$('#overlay').remove();
$('#lightbox').remove();
opts.inprogress = false;
// if jsondata, build the imagearray from data provided in json format
if(opts.jsondata && opts.jsondata.length > 0) {
var parser = opts.jsondataparser ? opts.jsondataparser : $.fn.lightbox.parsejsondata;
opts.imagearray = [];
opts.imagearray = parser(opts.jsondata);
}
var outerimage = '
';
var imagedata = '
';
if (opts.displayhelp)
imagedata += '
' + opts.strings.help + '';
imagedata += '
';
var string;
if (opts.navbarontop) {
string = '' + imagedata + outerimage + '
';
$("body").append(string);
$("#imagedatacontainer").addclass('ontop');
} else {
string = '' + outerimage + imagedata + '
';
$("body").append(string);
}
$("#overlay").click(function(){ end(); }).hide();
$("#lightbox").click(function(){ end();}).hide();
$("#loadinglink").click(function(){ end(); return false;});
$("#bottomnavclose").click(function(){ end(); return false; });
$('#outerimagecontainer').width(opts.widthcurrent).height(opts.heightcurrent);
$('#imagedatacontainer').width(opts.widthcurrent);
if (!opts.imageclickclose) {
$("#lightboximage").click(function(){ return false; });
$("#hovernav").click(function(){ return false; });
}
};
function getpagesize() {
var jquerypagesize = new array($(document).width(),$(document).height(), $(window).width(), $(window).height());
return jquerypagesize;
};
function getpagescroll() {
var xscroll, yscroll;
if (self.pageyoffset) {
yscroll = self.pageyoffset;
xscroll = self.pagexoffset;
} else if (document.documentelement && document.documentelement.scrolltop){ // explorer 6 strict
yscroll = document.documentelement.scrolltop;
xscroll = document.documentelement.scrollleft;
} else if (document.body) {// all other explorers
yscroll = document.body.scrolltop;
xscroll = document.body.scrollleft;
}
var arraypagescroll = new array(xscroll,yscroll);
return arraypagescroll;
};
function pause(ms) {
var date = new date();
var curdate = null;
do{curdate = new date();}
while(curdate - date < ms);
};
function start(imagelink) {
$("select, embed, object").hide();
var arraypagesize = getpagesize();
$("#overlay").hide().css({width: '100%', height: arraypagesize[1]+'px', opacity : opts.overlayopacity}).fadein();
imagenum = 0;
// if data is not provided by jsondata parameter
if(!opts.jsondata) {
opts.imagearray = [];
// if image is not part of a set..
if(!imagelink.rel || (imagelink.rel == '')){
// add single image to lightbox.imagearray
opts.imagearray.push(new array(imagelink.href, opts.displaytitle ? imagelink.title : ''));
} else {
// if image is part of a set..
$("a").each(function(){
if(this.href && (this.rel == imagelink.rel)){
opts.imagearray.push(new array(this.href, opts.displaytitle ? this.title : ''));
}
});
}
}
if(opts.imagearray.length > 1) {
for(i = 0; i < opts.imagearray.length; i++){
for(j = opts.imagearray.length-1; j>i; j--){
if(opts.imagearray[i][0] == opts.imagearray[j][0]){
opts.imagearray.splice(j,1);
}
}
}
while(opts.imagearray[imagenum][0] != imagelink.href) { imagenum++;}
}
// calculate top and left offset for the lightbox
var arraypagescroll = getpagescroll();
var lightboxtop = arraypagescroll[1] + (arraypagesize[3] / 10);
var lightboxleft = arraypagescroll[0];
$('#lightbox').css({top: lightboxtop+'px', left: lightboxleft+'px'}).show();
if (!opts.slidenavbar)
$('#imagedata').hide();
changeimage(imagenum);
};
function changeimage(imagenum) {
if(opts.inprogress == false){
opts.inprogress = true;
opts.activeimage = imagenum; // update global var
// hide elements during transition
$('#loading').show();
$('#lightboximage').hide();
$('#hovernav').hide();
$('#prevlink').hide();
$('#nextlink').hide();
if (opts.slidenavbar) { // delay preloading image until navbar will slide up
// $('#imagedatacontainer').slideup(opts.navbarslidespeed, $.fn.dochangeimage);
$('#imagedatacontainer').hide();
$('#imagedata').hide();
dochangeimage();
} else {
dochangeimage();
}
}
};
function dochangeimage() {
imgpreloader = new image();
// once image is preloaded, resize image container
imgpreloader.onload=function(){
var newwidth = imgpreloader.width;
var newheight = imgpreloader.height;
if (opts.fittoscreen) {
var arraypagesize = getpagesize();
var ratio;
var initialpagewidth = arraypagesize[2] - 2 * opts.bordersize;
var initialpageheight = arraypagesize[3] - 200;
if (imgpreloader.height > initialpageheight)
{
newwidth = parseint((initialpageheight/imgpreloader.height) * imgpreloader.width);
newheight = initialpageheight;
}
else if (imgpreloader.width > initialpagewidth)
{
newheight = parseint((initialpagewidth/imgpreloader.width) * imgpreloader.height);
newwidth = initialpagewidth;
}
}
$('#lightboximage').attr('src', opts.imagearray[opts.activeimage][0])
.width(newwidth).height(newheight);
resizeimagecontainer(newwidth, newheight);
};
imgpreloader.src = opts.imagearray[opts.activeimage][0];
};
function end() {
disablekeyboardnav();
$('#lightbox').hide();
$('#overlay').fadeout();
$('select, object, embed').show();
};
function preloadneighborimages(){
if(opts.loopimages && opts.imagearray.length > 1) {
preloadnextimage = new image();
preloadnextimage.src = opts.imagearray[(opts.activeimage == (opts.imagearray.length - 1)) ? 0 : opts.activeimage + 1][0]
preloadprevimage = new image();
preloadprevimage.src = opts.imagearray[(opts.activeimage == 0) ? (opts.imagearray.length - 1) : opts.activeimage - 1][0]
} else {
if((opts.imagearray.length - 1) > opts.activeimage){
preloadnextimage = new image();
preloadnextimage.src = opts.imagearray[opts.activeimage + 1][0];
}
if(opts.activeimage > 0){
preloadprevimage = new image();
preloadprevimage.src = opts.imagearray[opts.activeimage - 1][0];
}
}
};
function resizeimagecontainer(imgwidth, imgheight) {
// get current width and height
opts.widthcurrent = $("#outerimagecontainer").outerwidth();
opts.heightcurrent = $("#outerimagecontainer").outerheight();
// get new width and height
var widthnew = math.max(350, imgwidth + (opts.bordersize * 2));
var heightnew = (imgheight + (opts.bordersize * 2));
// scalars based on change from old to new
opts.xscale = ( widthnew / opts.widthcurrent) * 100;
opts.yscale = ( heightnew / opts.heightcurrent) * 100;
// calculate size difference between new and old image, and resize if necessary
wdiff = opts.widthcurrent - widthnew;
hdiff = opts.heightcurrent - heightnew;
$('#imagedatacontainer').animate({width: widthnew},opts.resizespeed,'linear');
$('#outerimagecontainer').animate({width: widthnew},opts.resizespeed,'linear',function(){
$('#outerimagecontainer').animate({height: heightnew},opts.resizespeed,'linear',function(){
showimage();
});
});
// if new and old image are same size and no scaling transition is necessary,
// do a quick pause to prevent image flicker.
if((hdiff == 0) && (wdiff == 0)){
if (jquery.browser.msie){ pause(250); } else { pause(100);}
}
$('#prevlink').height(imgheight);
$('#nextlink').height(imgheight);
};
function showimage() {
$('#loading').hide();
$('#lightboximage').fadein("fast");
updatedetails();
preloadneighborimages();
opts.inprogress = false;
};
function updatedetails() {
$('#numberdisplay').html('');
if(opts.imagearray[opts.activeimage][1]){
$('#caption').html(opts.imagearray[opts.activeimage][1]).show();
}
// if image is part of set display 'image x of x'
if(opts.imagearray.length > 1){
var nav_html;
nav_html = opts.strings.image + (opts.activeimage + 1) + opts.strings.of + opts.imagearray.length;
if (!opts.disablenavbarlinks) {
// display previous / next text links
if ((opts.activeimage) > 0 || opts.loopimages) {
nav_html = '' + opts.strings.prevlinktext + "" + nav_html;
}
if (((opts.activeimage + 1) < opts.imagearray.length) || opts.loopimages) {
nav_html += '' + opts.strings.nextlinktext + "";
}
}
$('#numberdisplay').html(nav_html).show();
}
if (opts.slidenavbar) {
$("#imagedata").slidedown(opts.navbarslidespeed);
} else {
$("#imagedata").show();
}
var arraypagesize = getpagesize();
$('#overlay').height(arraypagesize[1]);
updatenav();
};
function updatenav() {
if(opts.imagearray.length > 1){
$('#hovernav').show();
// if loopimages is true, always show next and prev image buttons
if(opts.loopimages) {
$('#prevlink,#prevlinktext').show().click(function(){
changeimage((opts.activeimage == 0) ? (opts.imagearray.length - 1) : opts.activeimage - 1); return false;
});
$('#nextlink,#nextlinktext').show().click(function(){
changeimage((opts.activeimage == (opts.imagearray.length - 1)) ? 0 : opts.activeimage + 1); return false;
});
} else {
// if not first image in set, display prev image button
if(opts.activeimage != 0){
$('#prevlink,#prevlinktext').show().click(function(){
changeimage(opts.activeimage - 1); return false;
});
}
// if not last image in set, display next image button
if(opts.activeimage != (opts.imagearray.length - 1)){
$('#nextlink,#nextlinktext').show().click(function(){
changeimage(opts.activeimage +1); return false;
});
}
}
enablekeyboardnav();
}
};
function keyboardaction(e) {
var o = e.data.opts
var keycode = e.keycode;
var escapekey = 27;
var key = string.fromcharcode(keycode).tolowercase();
if((key == 'x') || (key == 'o') || (key == 'c') || (keycode == escapekey)){ // close lightbox
end();
} else if((key == 'p') || (keycode == 37)){ // display previous image
if(o.loopimages) {
disablekeyboardnav();
changeimage((o.activeimage == 0) ? (o.imagearray.length - 1) : o.activeimage - 1);
}
else if(o.activeimage != 0){
disablekeyboardnav();
changeimage(o.activeimage - 1);
}
} else if((key == 'n') || (keycode == 39)){ // display next image
if (opts.loopimages) {
disablekeyboardnav();
changeimage((o.activeimage == (o.imagearray.length - 1)) ? 0 : o.activeimage + 1);
}
else if(o.activeimage != (o.imagearray.length - 1)){
disablekeyboardnav();
changeimage(o.activeimage + 1);
}
}
};
function enablekeyboardnav() {
$(document).bind('keydown', {opts: opts}, keyboardaction);
};
function disablekeyboardnav() {
$(document).unbind('keydown');
};
};
$.fn.lightbox.parsejsondata = function(data) {
var imagearray = [];
$.each(data, function(){
imagearray.push(new array(this.url, this.title));
});
return imagearray;
};
$.fn.lightbox.defaults = {
fileloadingimage : 'html/cn/jquery-lightbox/images/loading.gif',
filebottomnavcloseimage : 'html/cn/jquery-lightbox/images/closelabel.gif',
overlayopacity : 0.8,
bordersize : 10,
imagearray : new array,
activeimage : null,
inprogress : false,
resizespeed : 350,
widthcurrent: 250,
heightcurrent: 250,
xscale : 1,
yscale : 1,
displaytitle: true,
navbarontop: false,
slidenavbar: false, // slide nav bar up/down between image resizing transitions
navbarslidespeed: 350,
displayhelp: false,
strings : {
help: ' \u2190 / p - previous image\u00a0\u00a0\u00a0\u00a0\u2192 / n - next image\u00a0\u00a0\u00a0\u00a0esc / x - close image gallery',
prevlinktitle: 'previous image',
nextlinktitle: 'next image',
prevlinktext: '« previous',
nextlinktext: 'next »',
closetitle: 'close image gallery',
image: 'image ',
of: ' of '
},
fittoscreen: false, // resize images if they are bigger than window
disablenavbarlinks: false,
loopimages: false,
imageclickclose: true,
jsondata: null,
jsondataparser: null
};
})(jquery);