1 /* Copyright (c) 2009 Alvaro A. Lima Jr http://alvarojunior.com/jquery/joverlay.html
2 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
3 * Version: 0.8 (OUT 19, 2009)
4 * Requires: jQuery 1.3+
10 var isIE6 = $.browser.msie && $.browser.version == 6.0; // =(
11 var JOVERLAY_TIMER = null;
13 $.fn.jOverlay = function(options) {
16 if ( $('#jOverlay').length ) {$.closeOverlay();}
19 if (JOVERLAY_TIMER !== null) {
20 clearTimeout( JOVERLAY_TIMER );
24 var options = $.extend({}, $.fn.jOverlay.options, options || {});
26 // success deprecated !!! Use onSuccess
27 var onSuccess = options.onSuccess || options.success;
29 var element = this.is('*') ? this : '#jOverlayContent';
31 var position = isIE6 ? 'absolute' : 'fixed';
33 var isImage = /([^\/\\]+)\.(png|gif|jpeg|jpg|bmp)$/i.test( options.url );
35 var imgLoading = options.imgLoading ? "<img id='jOverlayLoading' src='"+options.imgLoading+"' style='position:"+position+"; z-index:"+(options.zIndex + 9)+";'/>" : '';
42 $.center('#jOverlayContent',{
43 'top' : $(window).scrollTop() + 'px',
51 $('body').prepend(imgLoading + "<div id='jOverlay' />"
52 + "<div id='jOverlayContent' style='position:"+position+"; z-index:"+(options.zIndex + 5)+"; display:none;'/>"
56 $('#jOverlayContent').data('options', options);
59 $('#jOverlayLoading').load(function() {
66 $('#jOverlayContent select').show();
71 'backgroundColor' : options.color,
72 'position' : position,
75 'filter' : 'alpha(opacity='+ (options.opacity * 100) +')', // IE =(
76 'opacity' : options.opacity, // Good Browser =D
77 '-khtml-opacity' : options.opacity,
78 '-moz-opacity' : options.opacity,
79 'zIndex' : options.zIndex,
80 'width' : !isIE6 ? '100%' : $(window).width() + 'px',
81 'height' : !isIE6 ? '100%' : $(document).height() + 'px'
85 if ( $.trim(options.html) ) {
86 $(element).html(options.html);
92 $('#jOverlayContent').data('jOverlayElementPrev', this.prev() );
94 $('#jOverlayContent').html(
95 this.show().data('display', options.autoHide ? 'none' : this.css('display') )
100 center('#jOverlayContent');
102 $('#jOverlayContent').show();
105 if ( !options.url && $.isFunction( onSuccess ) ) {
116 $('<img/>').load(function(){
117 var resize = $.resize(this.width, this.height);
120 width : resize.width,
121 height : resize.height
124 $( element ).html(this);
126 center('#jOverlayContent');
127 center('#jOverlayLoading');
129 $('#jOverlayLoading').fadeOut(500);
130 $('#jOverlayContent').show();
133 if ( $.isFunction( onSuccess ) ) {
134 onSuccess( $(element) );
138 alert('Image ('+options.url+') not found.');
140 }).attr({'src' : options.url, 'alt' : options.url});
145 if ( options.url && !isImage ) {
148 type: options.method,
151 success: function(responseText) {
153 $('#jOverlayLoading').fadeOut(500);
155 $( element ).html(responseText).show();
157 center('#jOverlayContent');
160 if ( $.isFunction( onSuccess ) ) {
161 onSuccess( responseText );
166 alert('URL ('+options.url+') not found.');
177 $(window).scroll(function(){
178 center('#jOverlayContent');
182 $(window).resize(function(){
185 'width' : $(window).width() + 'px',
186 'height' : $(document).height() + 'px'
189 center('#jOverlayContent');
195 // Press ESC to close
196 if ( options.closeOnEsc ) {
197 $(document).keydown(function(event){
198 if ( event.keyCode == 27 ) {
203 $(document).unbind('keydown');
207 if ( options.bgClickToClose ) {
208 $('#jOverlay').click($.closeOverlay);
211 // Timeout (auto-close)
212 // time in millis to wait before auto-close
213 // set to 0 to disable
214 if ( options.timeout && Number(options.timeout) > 0 ) {
215 JOVERLAY_TIMER = window.setTimeout( $.closeOverlay, Number(options.timeout) );
219 $('#jOverlayContent').css(options.css || {});
223 // Resizing large images - orginal by Christian Montoya.
224 // Edited by - Cody Lindley (http://www.codylindley.com) (Thickbox 3.1)
225 $.resize = function(imageWidth, imageHeight) {
226 var x = $(window).width() - 150;
227 var y = $(window).height() - 150;
228 if (imageWidth > x) {
229 imageHeight = imageHeight * (x / imageWidth);
231 if (imageHeight > y) {
232 imageWidth = imageWidth * (y / imageHeight);
235 } else if (imageHeight > y) {
236 imageWidth = imageWidth * (y / imageHeight);
238 if (imageWidth > x) {
239 imageHeight = imageHeight * (x / imageWidth);
243 return {'width':imageWidth, 'height':imageHeight};
247 $.center = function(element, css) {
248 var element = $(element);
249 var elemWidth = element.width();
251 element.css($.extend({},{
252 'width' : elemWidth + 'px',
253 'marginLeft' : '-' + (elemWidth / 2) + 'px',
254 'marginTop' : '-' + element.height() / 2 + 'px',
256 'top' : !isIE6 ? '50%' : $(window).scrollTop() + ($(window).height() / 2) + 'px',
262 $.fn.jOverlay.options = {
271 'bgClickToClose' : true,
272 'success' : null, // Deprecated : use onSuccess
281 // Set default options (GLOBAL)
282 // Overiding the default values.
283 $.fn.jOverlay.setDefaults = function(options) {
284 $.fn.jOverlay.options = $.extend({}, $.fn.jOverlay.options, options || {});
288 $.closeOverlay = function() {
290 var content = $('#jOverlayContent');
291 var options = content.data('options');
292 var elementPrev = content.data('jOverlayElementPrev');
295 if (isIE6) { $("select").show(); }
299 var contentChildren = content.children();
300 elementPrev.after( contentChildren.css('display', contentChildren.data('display') ) );
302 contentChildren.removeData('display');
303 content.removeData('jOverlayElementPrev');
306 // Clear options cache
307 content.removeData('options');
309 // Remove joverlay elements
310 $('#jOverlayLoading, #jOverlayContent, #jOverlay').remove();