123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- /*
- Inspired by the lightbox plugin adapted to jquery by Leandro Vieira Pinho (http://leandrovieira.com)
-
- @author : Nicolas Turlais : nicolas-at-insipi.de
- @version : V0.3.1 - June 2012
- @license : Licensed under CCAttribution-ShareAlike
- @website : http://chocolat.insipi.de
-
- */
- (function ($) {
- images = [];
- var calls = 0;
- $.fn.Chocolat = function (settings) {
- settings = $.extend({
- container: $('body'),
- displayAsALink: false,
- linkImages: true,
- linksContainer: 'Choco_links_container',
- overlayOpacity: 0.9,
- overlayColor: '#fff',
- fadeInOverlayduration: 500,
- fadeInImageduration: 500,
- fadeOutImageduration: 500,
- vache: true,
- separator1: ' | ',
- separator2: '/',
- leftImg: 'images/left.gif',
- rightImg: 'images/right.gif',
- closeImg: 'images/close.gif',
- loadingImg: 'images/loading.gif',
- currentImage: 0,
- setIndex: 0,
- setTitle: '',
- lastImage: 0
- }, settings);
- calls++;
- settings.setIndex = calls;
- images[settings.setIndex] = [];
- //images:
- this.each(function (index) {
- if (index == 0 && settings.linkImages && settings.setTitle == '') {
- settings.setTitle = isSet($(this).attr('rel'), ' ');
- }
- $(this).each(function () {
- images[settings.setIndex]['displayAsALink'] = settings.displayAsALink;
- images[settings.setIndex][index] = [];
- images[settings.setIndex][index]['adress'] = isSet($(this).attr('href'), ' ');
- images[settings.setIndex][index]['caption'] = isSet($(this).attr('title'), ' ');
- if (!settings.displayAsALink) {
- $(this).unbind('click').bind('click', {
- id: settings.setIndex,
- nom: settings.setTitle,
- i: index
- }, _initialise);
- }
- })
- });
- //setIndex:
- for (var i = 0; i < images[settings.setIndex].length; i++) {
- if (images[settings.setIndex]['displayAsALink']) {
- if ($('#' + settings.linksContainer).size() == 0) {
- this.filter(":first").before('<ul id="' + settings.linksContainer + '"></ul>');
- }
- $('#' + settings.linksContainer).append('<li><a href="#" id="Choco_numsetIndex_' + settings.setIndex + '" class="Choco_link">' + settings.setTitle + '</a></li>');
- e = this.parent();
- $(this).remove();
- if ($.trim(e.html()) == "") {//If parent empty : remove it
- e.remove();
- }
- return $('#Choco_numsetIndex_' + settings.setIndex).unbind('click').bind('click', {
- id: settings.setIndex,
- nom: settings.setTitle,
- i: settings.currentImage
- }, _initialise);
- }
- }
- function _initialise(event) {
- settings.currentImage = event.data.i;
- settings.setIndex = event.data.id;
- settings.setTitle = event.data.nom;
- settings.lastImage = images[settings.setIndex].length - 1;
- showChocolat();
- return false;
- }
- function _interface() {
- //html
- clear();
- settings.container.append('<div id="Choco_overlay"></div><div id="Choco_content"><div id="Choco_close"></div><div id="Choco_loading"></div><div id="Choco_container_photo"><img id="Choco_bigImage" src="" /></div><div id="Choco_container_description"><span id="Choco_container_title"></span><span id="Choco_container_via"></span></div><div id="Choco_left_arrow" class="Choco_arrows"></div><div id="Choco_right_arrow" class="Choco_arrows"></div></div>');
- $('#Choco_left_arrow').css('background-image', 'url(' + settings.leftImg + ')');
- $('#Choco_right_arrow').css('background-image', 'url(' + settings.rightImg + ')');
- $('#Choco_close').css('background-image', 'url(' + settings.closeImg + ')');
- $('#Choco_loading').css('background-image', 'url(' + settings.loadingImg + ')');
- if (settings.container.get(0).nodeName.toLowerCase() !== 'body') {
- settings.container.css({'position': 'relative', 'overflow': 'hidden', 'line-height': 'normal'});//yes, yes
- $('#Choco_content').css('position', 'relative');
- $('#Choco_overlay').css('position', 'absolute');
- }
- //events
- $(document).unbind('keydown').bind('keydown', function (e) {
- switch (e.keyCode) {
- case 37:
- changePageChocolat(-1);
- break;
- case 39:
- changePageChocolat(1);
- break;
- case 27:
- close();
- break;
- }
- ;
- });
- if (settings.vache) {
- $('#Choco_overlay').click(function () {
- close();
- return false;
- });
- }
- $('#Choco_left_arrow').unbind().bind('click', function () {
- changePageChocolat(-1);
- return false;
- });
- $('#Choco_right_arrow').unbind().bind('click', function () {
- changePageChocolat(1);
- return false;
- });
- $('#Choco_close').unbind().bind('click', function () {
- close();
- return false;
- });
- $(window).resize(function () {
- load(settings.currentImage, true);
- });
- }
- function showChocolat() {
- _interface();
- load(settings.currentImage, false);
- $('#Choco_overlay')
- .css({
- 'background-color': settings.overlayColor,
- 'opacity': settings.overlayOpacity
- })
- .fadeIn(settings.fadeInOverlayduration);
- $('#Choco_content').fadeIn(settings.fadeInImageduration, function () {
- });
- }
- function load(image, resize) {
- settings.currentImage = image;
- $('#Choco_loading').fadeIn(settings.fadeInImageduration);
- var imgPreloader = new Image();
- imgPreloader.onload = function () {
- $('#Choco_bigImage').attr('src', images[settings.setIndex][settings.currentImage]['adress']);
- var ajustees = iWantThePerfectImageSize(imgPreloader.height, imgPreloader.width);
- ChoColat(ajustees['hauteur'], ajustees['largeur'], resize);
- $('#Choco_loading').stop().fadeOut(settings.fadeOutImageduration);
- };
- imgPreloader.src = images[settings.setIndex][settings.currentImage]['adress'];
- preload();
- upadteDescription();
- }
- function changePageChocolat(signe) {
- if (!settings.linkImages || (settings.currentImage == 0 && signe == -1) || (settings.currentImage == settings.lastImage && signe == 1)) {
- return false;
- } else {
- //$('#Choco_container_description').fadeTo(settings.fadeOutImageduration,0); making a weird bug with firefox 17
- $('#Choco_container_description').css('visibility', 'hidden');
- $('#Choco_bigImage').fadeTo(settings.fadeOutImageduration, 0, function () {
- load(settings.currentImage + parseInt(signe), false);
- });
- }
- }
- function ChoColat(hauteur_image, largeur_image, resize) {
- if (resize) {
- $('#Choco_container_photo, #Choco_content, #Choco_bigImage').stop(true, false).css({'overflow': 'visible'});
- $('#Choco_bigImage').animate({
- 'height': hauteur_image + 'px',
- 'width': largeur_image + 'px'
- }, settings.fadeInImageduration);
- }
- $('#Choco_container_photo').animate({
- 'height': hauteur_image,
- 'width': largeur_image
- }, settings.fadeInImageduration);
- $('#Choco_content').animate({
- 'height': hauteur_image,
- 'width': largeur_image,
- 'marginLeft': -largeur_image / 2,
- 'marginTop': -(hauteur_image) / 2
- }, settings.fadeInImageduration, 'swing', function () {
- $('#Choco_bigImage').fadeTo(settings.fadeInImageduration, 1).height(hauteur_image).width(largeur_image);
- if (!resize) {
- arrowsManaging();
- //$('#Choco_container_description').fadeTo(settings.fadeInImageduration,1); making a weird bug with firefox 17
- $('#Choco_container_description').css('visibility', 'visible');
- $('#Choco_close').fadeIn(settings.fadeInImageduration);
- }
- }).css('overflow', 'visible');
- }
- function arrowsManaging() {
- if (settings.linkImages) {
- var what = ['Choco_right_arrow', 'Choco_left_arrow'];
- for (var i = 0; i < what.length; i++) {
- hide = false;
- if (what[i] == 'Choco_right_arrow' && settings.currentImage == settings.lastImage) {
- hide = true;
- $('#' + what[i]).fadeOut(300);
- } else if (what[i] == 'Choco_left_arrow' && settings.currentImage == 0) {
- hide = true;
- $('#' + what[i]).fadeOut(300);
- }
- if (!hide) {
- $('#' + what[i]).fadeIn(settings.fadeOutImageduration);
- }
- }
- }
- }
- function preload() {
- if (settings.currentImage !== settings.lastImage) {
- i = new Image;
- z = settings.currentImage + 1;
- i.src = images[settings.setIndex][z]['adress'];
- }
- }
- function upadteDescription() {
- var current = settings.currentImage + 1;
- var last = settings.lastImage + 1;
- $('#Choco_container_title').html(images[settings.setIndex][settings.currentImage]['caption']);
- $('#Choco_container_via').html(settings.setTitle + settings.separator1 + current + settings.separator2 + last);
- }
- function isSet(variable, defaultValue) {
- // return variable === undefined ? defaultValue : variable; ?
- if (variable === undefined) {
- return defaultValue;
- } else {
- return variable;
- }
- }
- function iWantThePerfectImageSize(himg, limg) {
- //28% = 14% + 14% margin
- var lblock = limg + (limg * 28 / 100);
- var heightDescAndClose = $('#Choco_container_description').height() + $('#Choco_close').height();
- var hblock = himg + heightDescAndClose;
- var k = limg / himg;
- var kk = himg / limg;
- if (settings.container.get(0).nodeName.toLowerCase() == 'body') {
- windowHeight = $(window).height();
- windowWidth = $(window).width();
- } else {
- windowHeight = settings.container.height();
- windowWidth = settings.container.width();
- }
- notFitting = true;
- while (notFitting) {
- var lblock = limg + (limg * 28 / 100);
- var hblock = himg + heightDescAndClose;
- if (lblock > windowWidth) {
- limg = windowWidth * 100 / 128;
- himg = kk * limg;
- } else if (hblock > windowHeight) {
- himg = (windowHeight - heightDescAndClose);
- limg = k * himg;
- } else {
- notFitting = false;
- }
- ;
- }
- ;
- return {
- largeur: limg,
- hauteur: himg
- };
- }
- function clear() {
- $('#Choco_overlay').remove();
- $('#Choco_content').remove();
- }
- function close() {
- $('#Choco_overlay').fadeOut(900, function () {
- $('#Choco_overlay').remove()
- });
- $('#Choco_content').fadeOut(500, function () {
- $('#Choco_content').remove()
- });
- settings.currentImage = 0;
- }
- };
- })(jQuery);
|