/** * interface elements for jquery * imagebox * * http://interface.eyecon.ro * * copyright (c) 2006 stefan petre * dual licensed under the mit (mit-license.txt) * and gpl (gpl-license.txt) licenses. * */ /** * this a jquery equivalent for lightbox2. alternative to image popups that will display images in an overlay. all links that have attribute 'rel' starting with 'imagebox' and link to an image will display the image inside the page. galleries can by build buy giving the value 'imagebox-galname' to attribute 'rel'. attribute 'title' will be used as caption. * keyboard navigation: * - next image: arrow right, page down, 'n' key, space * - previous image: arrow left, page up, 'p' key, backspace * - close: escape * * css * #imageboxoverlay * { * background-color: #000; * } * #imageboxcaption * { * background-color: #f4f4ec; * } * #imageboxcontainer * { * width: 250px; * height: 250px; * background-color: #f4f4ec; * } * #imageboxcaptiontext * { * font-weight: bold; * padding-bottom: 5px; * font-size: 13px; * color: #000; * } * #imageboxcaptionimages * { * margin: 0; * } * #imageboxnextimage * { * background-image: url(spacer.gif); * background-color: transparent; * } * #imageboxprevimage * { * background-image: url(spacer.gif); * background-color: transparent; * } * #imageboxnextimage:hover * { * background-image: url(next_image.jpg); * background-repeat: no-repeat; * background-position: right top; * } * #imageboxprevimage:hover * { * background-image: url(prev_image.jpg); * background-repeat: no-repeat; * background-position: left bottom; * } * * @name imagebox * @description this a jquery equivalent for lightbox2. alternative to image popups that will display images in an overlay. all links that have attribute 'rel' starting with 'imagebox' and link to an image will display the image inside the page. galleries can by build buy giving the value 'imagebox-galname' to attribute 'rel'. attribute 'title' will be used as caption. * @param hash hash a hash of parameters * @option integer border border width * @option string loadersrc path to loading image * @option string closehtml path to close overlay image * @option float overlayopacity opacity for overlay * @option string textimage when a galalry it is build then the iteration is displayed * @option string textimagefrom when a galalry it is build then the iteration is displayed * @option integer fadeduration fade duration in miliseconds * @option integer showtextimage 0/1 : show or not the text 'image n from z' * * @type jquery * @cat plugins/interface * @author stefan petre */ var jqimagebox_imageel=""; //rustine pour safari jquery.imagebox = { options : { border : 10, loadersrc : 'loading.gif', closehtml : '', overlayopacity : 0.8, textimage : 'showing image', textimagefrom : 'from', fadeduration : 400, showtextimage : true }, imageloaded : false, firstresize : false, currentrel : null, animationinprogress : false, opened : false, minwidth : 0, heightclose : 0, keypressed : function(event) { if(!jquery.imagebox.opened || jquery.imagebox.animationinprogress) return; var pressedkey = event.charcode || event.keycode || -1; switch (pressedkey) { //end case 35: if (jquery.imagebox.currentrel) jquery.imagebox.start(null, jquery('a[@rel=' + jquery.imagebox.currentrel+ ']:last').get(0)); break; //home case 36: if (jquery.imagebox.currentrel) jquery.imagebox.start(null, jquery('a[@rel=' + jquery.imagebox.currentrel+ ']:first').get(0)); break; //left case 37: //backspace case 8: //page up case 33: //p case 80: case 112: var prevel = jquery('#imageboxprevimage'); if(prevel.get(0).onclick != null) { prevel.get(0).onclick.apply(prevel.get(0)); } break; //up case 38: break; //right case 39: //page down case 34: //space case 32: //n case 110: case 78: var nextel = jquery('#imageboxnextimage'); if(nextel.get(0).onclick != null) { nextel.get(0).onclick.apply(nextel.get(0)); } break; //down; case 40: break; //escape case 27: jquery.imagebox.hideimage(); break; } }, init : function(options) { if (options) jquery.extend(jquery.imagebox.options, options); if (window.event) { jquery('body',document).bind('keyup', jquery.imagebox.keypressed); } else { jquery(document).bind('keyup', jquery.imagebox.keypressed); } jquery('a').each( function() { el = jquery(this); relattr = el.attr('rel')||''; hrefattr = el.attr('href')||''; imagetypes = /\.jpg|\.jpeg|\.png|\.gif|\.bmp/g; if (hrefattr.tolowercase().match(imagetypes) != null && relattr.tolowercase().indexof('imagebox') == 0) { el.bind('click', jquery.imagebox.start); } } ); if (jquery.browser.msie) { iframe = document.createelement('iframe'); jquery(iframe) .attr( { id : 'imageboxiframe', src : 'javascript:false;', frameborder : 'no', scrolling : 'no' } ) .css ( { display : 'none', position : 'absolute', top : '0', left : '0', filter : 'progid:dximagetransform.microsoft.alpha(opacity=0)' } ); jquery('body').append(iframe); } overlay = document.createelement('div'); jquery(overlay) .attr('id', 'imageboxoverlay') .css( { position : 'absolute', display : 'none', top : '0', left : '0', opacity : 0 } ) .append(document.createtextnode(' ')) .bind('click', jquery.imagebox.hideimage); captiontext = document.createelement('div'); jquery(captiontext) .attr('id', 'imageboxcaptiontext') .css( { paddingleft : jquery.imagebox.options.border + 'px' } ) .append(document.createtextnode(' ')); captionimages = document.createelement('div'); jquery(captionimages) .attr('id', 'imageboxcaptionimages') .css( { paddingleft : jquery.imagebox.options.border + 'px', paddingbottom : jquery.imagebox.options.border + 'px' } ) .append(document.createtextnode(' ')); closeel = document.createelement('a'); jquery(closeel) .attr( { id : 'imageboxclose', href : '#' } ) .css( { position : 'absolute', right : jquery.imagebox.options.border + 'px', top : '0' } ) .append(jquery.imagebox.options.closehtml) .bind('click', jquery.imagebox.hideimage); captionel = document.createelement('div'); jquery(captionel) .attr('id', 'imageboxcaption') .css( { position : 'relative', textalign : 'left', margin : '0 auto', zindex : 1 } ) .append(captiontext) .append(captionimages) .append(closeel); loader = document.createelement('img'); loader.src = jquery.imagebox.options.loadersrc; jquery(loader) .attr('id', 'imageboxloader') .css( { position : 'absolute' } ); previmage = document.createelement('a'); jquery(previmage) .attr( { id : 'imageboxprevimage', href : '#' } ) .css( { position : 'absolute', display : 'none', overflow : 'hidden', textdecoration : 'none' } ) .append(document.createtextnode(' ')); nextimage = document.createelement('a'); jquery(nextimage) .attr( { id : 'imageboxnextimage', href : '#' } ) .css( { position : 'absolute', overflow : 'hidden', textdecoration : 'none' } ) .append(document.createtextnode(' ')); container = document.createelement('div'); jquery(container) .attr('id', 'imageboxcontainer') .css( { display : 'none', position : 'relative', overflow : 'hidden', textalign : 'left', margin : '0 auto', top : '0', left : '0', zindex : 2 } ) .append([loader, previmage, nextimage]); outercontainer = document.createelement('div'); jquery(outercontainer) .attr('id', 'imageboxoutercontainer') .css( { display : 'none', position : 'absolute', overflow : 'hidden', top : '0', left : '0', textalign : 'center', backgroundcolor : 'transparent', lineheigt : '0' } ) .append([container,captionel]); jquery('body') .append(overlay) .append(outercontainer); //minimum width : previmageel = jquery('#imageboxprevimage'); prevwidth = previmageel.css("width"); if (!prevwidth) { prevwidth=''; } else{ if(prevwidth!=''){ prevwidth = prevwidth.replace(/px/g,''); //on eleve le texte 'px' pour pouvoir faire des calculs } } nextimageel = jquery('#imageboxnextimage'); nextwidth = nextimageel.css("width"); if (!nextwidth) { nextwidth=''; } else{ if(nextwidth!=''){ nextwidth = nextwidth.replace(/px/g,''); //on eleve le texte 'px' pour pouvoir faire des calculs } } jquery.imagebox.minwidth=-(-(jquery.imagebox.options.border * 2)-nextwidth-prevwidth); //2*border+nextwidth+prevwidth }, start : function(e, elm) { el = elm ? jquery(elm) : jquery(this); linkrel = el.attr('rel'); var totalimages, iteration, previmage, nextimage; if (linkrel != 'imagebox') { jquery.imagebox.currentrel = linkrel; gallery = jquery('a[@rel=' + linkrel + ']'); totalimages = gallery.size(); iteration = gallery.index(elm ? elm : this); previmage = gallery.get(iteration - 1); nextimage = gallery.get(iteration + 1); } imagesrc = el.attr('href'); captiontext = el.attr('title'); caption2 = el.attr('content') || ""; if (caption2!="") { captiontext+="
"+caption2; } pagesize = jquery.iutil.getscroll(); overlay = jquery('#imageboxoverlay'); if (!jquery.imagebox.opened) { jquery.imagebox.opened = true; if (jquery.browser.msie) { jquery('#imageboxiframe') .css ('height', math.max(pagesize.ih,pagesize.h) + 'px') .css ('width', math.max(pagesize.iw,pagesize.w) + 'px') .show(); } overlay .css ('height', math.max(pagesize.ih,pagesize.h) + 'px') .css ('width', math.max(pagesize.iw,pagesize.w) + 'px') .show() .fadeto( 300, jquery.imagebox.options.overlayopacity, function() { jquery.imagebox.loadimage( imagesrc, captiontext, pagesize, totalimages, iteration, previmage, nextimage ); } ); jquery('#imageboxoutercontainer').css ('width', math.max(pagesize.iw,pagesize.w) + 'px'); } else { jquery('#imageboxprevimage').get(0).onclick = null; jquery('#imageboxnextimage').get(0).onclick = null; jquery.imagebox.loadimage( imagesrc, captiontext, pagesize, totalimages, iteration, previmage, nextimage ); } return false; }, loadimage : function(imagesrc, captiontext, pagesize, totalimages, iteration, previmage, nextimage) { jquery('#imageboxcurrentimage').remove(); previmageel = jquery('#imageboxprevimage'); previmageel.hide(); nextimageel = jquery('#imageboxnextimage'); nextimageel.hide(); loader = jquery('#imageboxloader'); container = jquery('#imageboxcontainer'); outercontainer = jquery('#imageboxoutercontainer'); captionel = jquery('#imageboxcaption').css('visibility', 'hidden'); //avoid safari bug : //jquery('#imageboxcaptiontext').html(captiontext); jquery('#imageboxcaptiontext').html("
"+captiontext+"
"); jquery.imagebox.animationinprogress = true; if (totalimages && jquery.imagebox.options.showtextimage) jquery('#imageboxcaptionimages').html( jquery.imagebox.options.textimage + ' ' + (iteration + 1) + ' ' + jquery.imagebox.options.textimagefrom + ' ' + totalimages ); if (previmage) { previmageel.get(0).onclick = function() { this.blur(); jquery.imagebox.start(null, previmage); return false; }; } if (nextimage) { nextimageel.get(0).onclick =function() { this.blur(); jquery.imagebox.start(null, nextimage); return false; }; } loader.show(); containersize = jquery.iutil.getsize(container.get(0)); containerw = math.max(containersize.wb, loader.get(0).width + jquery.imagebox.options.border * 2); containerh = math.max(containersize.hb, loader.get(0).height + jquery.imagebox.options.border * 2); loader .css( { left : (containerw - loader.get(0).width)/2 + 'px', top : (containerh - loader.get(0).height)/2 + 'px' } ); container .css( { width : containerw + 'px', height : containerh + 'px' } ) .show(); clientsize = jquery.iutil.getclient(); outercontainer .css('top', pagesize.t + (clientsize.h / 15) + 'px'); if (outercontainer.css('display') == 'none') { outercontainer .show() .fadein( jquery.imagebox.options.fadeduration ); } imageel = new image; // avoid safari bug : imageel.id='imageboxcurrentimage'; imageel.onload = function(){ if (jquery.browser.safari) { containerw = jqimagebox_imageel.width + jquery.imagebox.options.border * 2; containerh = jqimagebox_imageel.height + jquery.imagebox.options.border * 2; } containerw = imageel.width + jquery.imagebox.options.border * 2; containerh = imageel.height + jquery.imagebox.options.border * 2; //min width : if (jquery.imagebox.minwidth > containerw ) { containerw = jquery.imagebox.minwidth + jquery.imagebox.options.border * 2; } loader.hide(); container.animate( { height : containerh }, containersize.hb != containerh ? jquery.imagebox.options.fadeduration : 1, function() { container.animate( { width : containerw }, containersize.wb != containerw ? jquery.imagebox.options.fadeduration : 1, function() { if (jquery.browser.safari) { var imgtoprepend=""; container.prepend(imgtoprepend); var jqi_width = jquery('#imgboxtmp').width(); while(jqi_width==0){ jqi_width = jquery('#imgboxtmp').width(); } jqi_height = jquery('#imgboxtmp').height(); jquery('#imgboxtmp').remove(); jqi_width = jqi_width - 2* jquery.imagebox.options.border; var imgtoprepend=""; container.prepend(imgtoprepend); //pour safari... } else{ container.prepend(imageel); } jquery('#imageboxcurrentimage') .css( { position : 'absolute', left : (containerw-jquery('#imageboxcurrentimage').width())/2+'px', //jquery.imagebox.options.border + 'px', top : jquery.imagebox.options.border + 'px' } ) .fadein( jquery.imagebox.options.fadeduration, function() { captionsize = jquery.iutil.getsize(captionel.get(0)); //min width : if (jquery.imagebox.minwidth > containerw ) { containerw = jquery.imagebox.minwidth; } if (previmage) { previmageel .css( { left : jquery.imagebox.options.border + 'px', top : jquery.imagebox.options.border + 'px', width : containerw/2 ,//- jquery.imagebox.options.border * 3 + 'px', height : containerh - jquery.imagebox.options.border * 2 + 'px' } ) .show(); } if (nextimage) { nextimageel .css( { left : containerw/2 + jquery.imagebox.options.border * 2 + 1 + 'px', top : jquery.imagebox.options.border + 'px', width : containerw/2 - jquery.imagebox.options.border * 3 + 'px', height : containerh - jquery.imagebox.options.border * 2 + 'px' } ) .show(); } jquery("#imageboxcaptextcontainer").css('padding-top',jquery("#imageboxclose").height()); captionel .css( { width : containerw + 'px', top : - captionsize.hb + 'px', visibility : 'visible' } ) .animate( { top : -1 }, jquery.imagebox.options.fadeduration, function() { jquery.imagebox.animationinprogress = false; } ); } ); } ); } ); } imageel.src = imagesrc; jqimagebox_imageel = imageel; }, hideimage : function() { jquery('#imageboxcurrentimage').remove(); jquery('#imageboxoutercontainer').hide(); jquery('#imageboxcaption').css('visibility', 'hidden'); jquery('#imageboxoverlay').fadeto( 300, 0, function(){ jquery(this).hide(); if (jquery.browser.msie) { jquery('#imageboxiframe').hide(); } } ); jquery('#imageboxprevimage').get(0).onclick = null; jquery('#imageboxnextimage').get(0).onclick = null; jquery.imagebox.currentrel = null; jquery.imagebox.opened = false; jquery.imagebox.animationinprogress = false; return false; } };