// colorbox v1.3.6 - a full featured, light-weight, customizable lightbox based on jquery 1.3 // c) 2009 jack moore - www.colorpowered.com - jack@colorpowered.com // licensed under the mit license: http://www.opensource.org/licenses/mit-license.php (function ($) { // shortcuts (to increase compression) var colorbox = 'colorbox', hover = 'hover', true = true, false = false, cboxpublic, isie = $.browser.msie && !$.support.opacity, // feature detection alone gave false positives in some browsers isie6 = isie && $.browser.version < 7, // event strings (to increase compression) cbox_open = 'cbox_open', cbox_load = 'cbox_load', cbox_complete = 'cbox_complete', cbox_cleanup = 'cbox_cleanup', cbox_closed = 'cbox_closed', cbox_resize = 'resize.cbox_resize', // cached jquery object variables $overlay, $cbox, $wrap, $content, $topborder, $leftborder, $rightborder, $bottomborder, $related, $window, $loaded, $loadingbay, $loadingoverlay, $loadinggraphic, $title, $current, $slideshow, $next, $prev, $close, // variables for cached values or use across multiple functions interfaceheight, interfacewidth, loadedheight, loadedwidth, element, bookmark, index, settings, open, active, // colorbox default settings. // see http://colorpowered.com/colorbox for details. defaults = { transition: "elastic", speed: 350, width: false, height: false, innerwidth: false, innerheight: false, initialwidth: "400", initialheight: "400", maxwidth: false, maxheight: false, scalephotos: true, scrolling: true, inline: false, html: false, iframe: false, photo: false, href: false, title: false, rel: false, opacity: 0.9, preloading: true, current: "{current}/{total}", previous: "previous", next: "next", close: "close", open: false, overlayclose: true, slideshow: false, slideshowauto: true, slideshowspeed: 2500, slideshowstart: "start slideshow", slideshowstop: "stop slideshow", onopen: false, onload: false, oncomplete: false, oncleanup: false, onclosed: false }; // **************** // helper functions // **************** // convert % values to pixels function setsize(size, dimension) { dimension = dimension === 'x' ? $window.width() : $window.height();//document.documentelement.clientwidth : document.documentelement.clientheight; return (typeof size === 'string') ? math.round((size.match(/%/) ? (dimension / 100) * parseint(size, 10) : parseint(size, 10))) : size; } // checks an href to see if it is a photo. // there is a force photo option (photo: true) for hrefs that cannot be matched by this regex. function isimage(url) { url = $.isfunction(url) ? url.call(element) : url; return settings.photo || url.match(/\.(gif|png|jpg|jpeg|bmp)(?:\?([^#]*))?(?:#(\.*))?$/i); } // assigns functions results to their respective settings. this allows functions to be used to set colorbox options. function process() { for (var i in settings) { if ($.isfunction(settings[i]) && i.substring(0, 2) !== 'on') { // checks to make sure the function isn't one of the callbacks, they will be handled at the appropriate time. settings[i] = settings[i].call(element); } } settings.rel = settings.rel || element.rel; settings.href = settings.href || element.href; settings.title = settings.title || element.title; } function launch(elem) { element = elem; settings = $(element).data(colorbox); process(); // convert functions to their returned values. if (settings.rel && settings.rel !== 'nofollow') { $related = $('.cboxelement').filter(function () { var relrelated = $(this).data(colorbox).rel || this.rel; return (relrelated === settings.rel); }); index = $related.index(element); // check direct calls to colorbox. if (index < 0) { $related = $related.add(element); index = $related.length - 1; } } else { $related = $(element); index = 0; } if (!open) { open = true; active = true; // prevents the page-change action from queuing up if the visitor holds down the left or right keys. bookmark = element; bookmark.blur(); // remove the focus from the calling element. // set navigation key bindings $(document).bind("keydown.cbox_close", function (e) { if (e.keycode === 27) { e.preventdefault(); cboxpublic.close(); } }).bind("keydown.cbox_arrows", function (e) { if ($related.length > 1) { if (e.keycode === 37) { e.preventdefault(); $prev.click(); } else if (e.keycode === 39) { e.preventdefault(); $next.click(); } } }); if (settings.overlayclose) { $overlay.css({"cursor": "pointer"}).one('click', cboxpublic.close); } $.event.trigger(cbox_open); if (settings.onopen) { settings.onopen.call(element); } $overlay.css({"opacity": settings.opacity}).show(); // opens inital empty colorbox prior to content being loaded. settings.w = setsize(settings.initialwidth, 'x'); settings.h = setsize(settings.initialheight, 'y'); cboxpublic.position(0); if (isie6) { $window.bind('resize.cboxie6 scroll.cboxie6', function () { $overlay.css({width: $window.width(), height: $window.height(), top: $window.scrolltop(), left: $window.scrollleft()}); }).trigger("scroll.cboxie6"); } } $current.add($prev).add($next).add($slideshow).add($title).hide(); $close.html(settings.close).show(); cboxpublic.slideshow(); cboxpublic.load(); } // **************** // public functions // usage format: $.fn.colorbox.close(); // usage from within an iframe: parent.$.fn.colorbox.close(); // **************** cboxpublic = $.fn.colorbox = function (options, callback) { var $this = this; if (!$this.length) { if ($this.selector === '') { // empty selector means a direct call, ie: $.fn.colorbox(); $this = $(''); options.open = true; } else { // else the selector didn't match anything, and colorbox should go ahead and return. return this; } } $this.each(function () { var data = $.extend({}, $(this).data(colorbox) ? $(this).data(colorbox) : defaults, options); $(this).data(colorbox, data).addclass("cboxelement"); if (callback) { $(this).data(colorbox).oncomplete = callback; } }); if (options && options.open) { launch($this); } return this; }; // initialize colorbox: store common calculations, preload the interface graphics, append the html. // this preps colorbox for a speedy open when clicked, and lightens the burdon on the browser by only // having to run once, instead of each time colorbox is opened. cboxpublic.init = function () { // jquery object generator to save a bit of space function $div(id) { return $('
'); } // create & append jquery objects $window = $(window); $cbox = $(''); $overlay = $div("overlay").hide(); $wrap = $div("wrapper"); $content = $div("content").append( $loaded = $div("loadedcontent").css({width: 0, height: 0}), $loadingoverlay = $div("loadingoverlay"), $loadinggraphic = $div("loadinggraphic"), $title = $div("title"), $current = $div("current"), $slideshow = $div("slideshow"), $next = $div("next"), $prev = $div("previous"), $close = $div("close") ); $wrap.append( // the 3x3 grid that makes up colorbox $('').append( $div("topleft"), $topborder = $div("topcenter"), $div("topright") ), $('').append( $leftborder = $div("middleleft"), $content, $rightborder = $div("middleright") ), $('').append( $div("bottomleft"), $bottomborder = $div("bottomcenter"), $div("bottomright") ) ).children().children().css({'float': 'left'}); $loadingbay = $(""); $('body').prepend($overlay, $cbox.append($wrap, $loadingbay)); if (isie) { $cbox.addclass('cboxie'); if (isie6) { $overlay.css('position', 'absolute'); } } // add rollover event to navigation elements $content.children() .bind('mouseover mouseout', function(){ $(this).toggleclass(hover); }).addclass(hover); // cache values needed for size calculations interfaceheight = $topborder.height() + $bottomborder.height() + $content.outerheight(true) - $content.height();//subtraction needed for ie6 interfacewidth = $leftborder.width() + $rightborder.width() + $content.outerwidth(true) - $content.width(); loadedheight = $loaded.outerheight(true); loadedwidth = $loaded.outerwidth(true); // setting padding to remove the need to do size conversions during the animation step. $cbox.css({"padding-bottom": interfaceheight, "padding-right": interfacewidth}).hide(); // setup button & key events. $next.click(cboxpublic.next); $prev.click(cboxpublic.prev); $close.click(cboxpublic.close); // adding the 'hover' class allowed the browser to load the hover-state // background graphics. the class can now can be removed. $content.children().removeclass(hover); $('.cboxelement').live('click', function (e) { if (e.button !== 0 && typeof e.button !== 'undefined') {// checks to see if it was a non-left mouse-click. return true; } else { launch(this); return false; } }); }; cboxpublic.position = function (speed, loadedcallback) { var animate_speed, winheight = $window.height(), // keeps the top and left positions within the browser's viewport. postop = math.max(winheight - settings.h - loadedheight - interfaceheight,0)/2 + $window.scrolltop(), posleft = math.max(document.documentelement.clientwidth - settings.w - loadedwidth - interfacewidth,0)/2 + $window.scrollleft(); // setting the speed to 0 to reduce the delay between same-sized content. animate_speed = ($cbox.width() === settings.w+loadedwidth && $cbox.height() === settings.h+loadedheight) ? 0 : speed; // this gives the wrapper plenty of breathing room so it's floated contents can move around smoothly, // but it has to be shrank down around the size of div#colorbox when it's done. if not, // it can invoke an obscure ie bug when using iframes. $wrap[0].style.width = $wrap[0].style.height = "9999px"; function modaldimensions (that) { // loading overlay size has to be sure that ie6 uses the correct height. $topborder[0].style.width = $bottomborder[0].style.width = $content[0].style.width = that.style.width; $loadinggraphic[0].style.height = $loadingoverlay[0].style.height = $content[0].style.height = $leftborder[0].style.height = $rightborder[0].style.height = that.style.height; } $cbox.dequeue().animate({width:settings.w+loadedwidth, height:settings.h+loadedheight, top:postop, left:posleft}, {duration: animate_speed, complete: function(){ modaldimensions(this); active = false; // shrink the wrapper down to exactly the size of colorbox to avoid a bug in ie's iframe implementation. $wrap[0].style.width = (settings.w+loadedwidth+interfacewidth) + "px"; $wrap[0].style.height = (settings.h+loadedheight+interfaceheight) + "px"; if (loadedcallback) {loadedcallback();} }, step: function(){ modaldimensions(this); } }); }; cboxpublic.resize = function (object) { if(!open){ return; } var topmargin, prev, prevsrc, next, nextsrc, photo, timeout, speed = settings.transition==="none" ? 0 : settings.speed; $window.unbind(cbox_resize); if(!object){ timeout = settimeout(function(){ // timer allows ie to render the dimensions before attempting to calculate the height var $child = $loaded.wrapinner("").children(); // temporary wrapper to get an accurate estimate of just how high the total content should be. settings.h = $child.height(); $loaded.css({height:settings.h}); $child.replacewith($child.children()); // ditch the temporary wrapper div used in height calculation cboxpublic.position(speed); }, 1); return; } $loaded.remove(); $loaded = $('').html(object); function getwidth(){ settings.w = settings.w || $loaded.width(); settings.w = settings.mw && settings.mw < settings.w ? settings.mw : settings.w; return settings.w; } function getheight(){ settings.h = settings.h || $loaded.height(); settings.h = settings.mh && settings.mh < settings.h ? settings.mh : settings.h; return settings.h; } $loaded.hide() .appendto($loadingbay)// content has to be appended to the dom for accurate size calculations. appended to an absolutely positioned element, rather than body, which avoids an extremely brief display of the vertical scrollbar in firefox that can occur for a small minority of websites. .css({width:getwidth(), overflow:settings.scrolling ? 'auto' : 'hidden'}) .css({height:getheight()})// sets the height independently from the width in case the new width influences the value of height. .prependto($content); $('#cboxphoto').css({cssfloat:'none'});// floating the img removes the bottom line-height and fixed a problem where ie miscalculates the width of the parent element as 100% of the document width. // hides select elements in ie6 because they would otherwise sit on top of the overlay. if (isie6) { $('select:not(#colorbox select)').filter(function(){ return this.style.visibility !== 'hidden'; }).css({'visibility':'hidden'}).one(cbox_cleanup, function(){ this.style.visibility = 'inherit'; }); } function setposition (s) { cboxpublic.position(s, function(){ if (!open) { return; } if (isie) { //this fadein helps the bicubic resampling to kick-in. if( photo ){$loaded.fadein(100);} //ie adds a filter when colorbox fades in and out that can cause problems if the loaded content contains transparent pngs. $cbox[0].style.removeattribute("filter"); } //waited until the iframe is added to the dom & it is visible before setting the src. //this increases compatability with pages using dom dependent javascript. if(settings.iframe){ $loaded.append(""); } $loaded.show(); $title.show().html(settings.title); if ($related.length>1) { $current.html(settings.current.replace(/\{current\}/, index+1).replace(/\{total\}/, $related.length)).show(); $next.html(settings.next).show(); $prev.html(settings.previous).show(); if(settings.slideshow){ $slideshow.show(); } } $loadingoverlay.hide(); $loadinggraphic.hide(); $.event.trigger(cbox_complete); if (settings.oncomplete) { settings.oncomplete.call(element); } if (settings.transition === 'fade'){ $cbox.fadeto(speed, 1, function(){ if(isie){$cbox[0].style.removeattribute("filter");} }); } $window.bind(cbox_resize, function(){ cboxpublic.position(0); }); }); } if((settings.transition === 'fade' && $cbox.fadeto(speed, 0, function(){setposition(0);})) || setposition(speed)){} // preloads images within a rel group if (settings.preloading && $related.length>1) { prev = index > 0 ? $related[index-1] : $related[$related.length-1]; next = index < $related.length-1 ? $related[index+1] : $related[0]; nextsrc = $(next).data(colorbox).href || next.href; prevsrc = $(prev).data(colorbox).href || prev.href; if(isimage(nextsrc)){ $('').attr('src', nextsrc); } if(isimage(prevsrc)){ $('').attr('src', prevsrc); } } }; cboxpublic.load = function () { var href, img, setresize, resize = cboxpublic.resize; active = true; /* // i decided to comment this out because i can see it causing problems as users // really should just set the dimensions on their img elements instead, // but i'm leaving the code in as it may be useful to someone. // to use, uncomment the function and change 'if(textstatus === "success"){ resize(this); }' // to 'if(textstatus === "success"){ preload(this); }' // preload loops through the html to find img elements and loads their sources. // this allows the resize method to accurately estimate the dimensions of the new content. function preload(html){ var $ajax = $(html), $imgs = $ajax.find('img'), x = $imgs.length; function loadloop(){ var img = new image(); x = x-1; if(x >= 0){ img.onload = loadloop; img.src = $imgs[x].src; } else { resize($ajax); } } loadloop(); } */ element = $related[index]; settings = $(element).data(colorbox); //convert functions to static values process(); $.event.trigger(cbox_load); if (settings.onload) { settings.onload.call(element); } // evaluate the height based on the optional height and width settings. settings.h = settings.height ? setsize(settings.height, 'y') - loadedheight - interfaceheight : settings.innerheight ? setsize(settings.innerheight, 'y') : false; settings.w = settings.width ? setsize(settings.width, 'x') - loadedwidth - interfacewidth : settings.innerwidth ? setsize(settings.innerwidth, 'x') : false; // sets the minimum dimensions for use in image scaling settings.mw = settings.w; settings.mh = settings.h; // re-evaluate the minimum width and height based on maxwidth and maxheight values. // if the width or height exceed the maxwidth or maxheight, use the maximum values instead. if(settings.maxwidth){ settings.mw = setsize(settings.maxwidth, 'x') - loadedwidth - interfacewidth; settings.mw = settings.w && settings.w < settings.mw ? settings.w : settings.mw; } if(settings.maxheight){ settings.mh = setsize(settings.maxheight, 'y') - loadedheight - interfaceheight; settings.mh = settings.h && settings.h < settings.mh ? settings.h : settings.mh; } href = settings.href; $loadingoverlay.show(); $loadinggraphic.show(); if (settings.inline) { // inserts an empty placeholder where inline content is being pulled from. // an event is bound to put inline content back when colorbox closes or loads new content. $('').hide().insertbefore($(href)[0]).bind(cbox_load+' '+cbox_cleanup, function(){ $(this).replacewith($loaded.children()); }); resize($(href)); } else if (settings.iframe) { // iframe element won't be added to the dom until it is ready to be displayed, // to avoid problems with dom-ready js that might be trying to run in that iframe. resize(" "); } else if (settings.html) { resize(settings.html); } else if (isimage(href)){ img = new image(); img.onload = function(){ var percent; img.onload = null; img.id = 'cboxphoto'; $(img).css({margin:'auto', border:'none', display:'block', cssfloat:'left'}); if(settings.scalephotos){ setresize = function(){ img.height -= img.height * percent; img.width -= img.width * percent; }; if(settings.mw && img.width > settings.mw){ percent = (img.width - settings.mw) / img.width; setresize(); } if(settings.mh && img.height > settings.mh){ percent = (img.height - settings.mh) / img.height; setresize(); } } if (settings.h) { img.style.margintop = math.max(settings.h - img.height,0)/2 + 'px'; } resize(img); if($related.length > 1){ $(img).css({cursor:'pointer'}).click(cboxpublic.next); } if(isie){ img.style.msinterpolationmode='bicubic'; } }; img.src = href; } else { $('').appendto($loadingbay).load(href, function(data, textstatus){ if(textstatus === "success"){ resize(this); } else { resize($("request unsuccessful.
")); } }); } }; // navigates to the next page/image in a set. cboxpublic.next = function () { if(!active){ index = index < $related.length-1 ? index+1 : 0; cboxpublic.load(); } }; cboxpublic.prev = function () { if(!active){ index = index > 0 ? index-1 : $related.length-1; cboxpublic.load(); } }; cboxpublic.slideshow = function () { var stop, timeout, classname = 'cboxslideshow_'; $slideshow.bind(cbox_closed, function(){ $slideshow.unbind(); cleartimeout(timeout); $cbox.removeclass(classname+"off"+" "+classname+"on"); }); function start(){ $slideshow .text(settings.slideshowstop) .bind(cbox_complete, function(){ timeout = settimeout(cboxpublic.next, settings.slideshowspeed); }) .bind(cbox_load, function(){ cleartimeout(timeout); }).one("click", function(){ stop(); $(this).removeclass(hover); }); $cbox.removeclass(classname+"off").addclass(classname+"on"); } stop = function(){ cleartimeout(timeout); $slideshow .text(settings.slideshowstart) .unbind(cbox_complete+' '+cbox_load) .one("click", function(){ start(); timeout = settimeout(cboxpublic.next, settings.slideshowspeed); $(this).removeclass(hover); }); $cbox.removeclass(classname+"on").addclass(classname+"off"); }; if(settings.slideshow && $related.length>1){ if(settings.slideshowauto){ start(); } else { stop(); } } }; // note: to use this within an iframe use the following format: parent.$.fn.colorbox.close(); cboxpublic.close = function () { $.event.trigger(cbox_cleanup); if (settings.oncleanup) { settings.oncleanup.call(element); } open = false; $(document).unbind("keydown.cbox_close keydown.cbox_arrows"); $window.unbind(cbox_resize+' resize.cboxie6 scroll.cboxie6'); $overlay.css({cursor: 'auto'}).fadeout('fast'); $cbox .stop(true, false) .fadeout('fast', function () { $('#colorbox iframe').attr('src', 'about:blank'); $loaded.remove(); $cbox.css({'opacity': 1}); try{ bookmark.focus(); } catch (er){ // do nothing } $.event.trigger(cbox_closed); if (settings.onclosed) { settings.onclosed.call(element); } }); }; // a method for fetching the current element colorbox is referencing. // returns a jquery object. cboxpublic.element = function(){ return $(element); }; cboxpublic.settings = defaults; // initializes colorbox when the dom has loaded $(cboxpublic.init); }(jquery));