/*
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('
');
}
$('#'+settings.linkscontainer).append(''+settings.settitle+'');
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('');
$('#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);