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.7.1 (JUN 15, 2009)
4 * Requires: jQuery 1.3+
10 var isIE6 = $.browser.msie && $.browser.version == 6.0; // =(
11 var JOVERLAY_TIMER = null;
12 var JOVERLAY_ELEMENT_PREV = null;
14 $.fn.jOverlay = function(options) {
17 if ( $('#jOverlay').length ) {$.closeOverlay();}
20 JOVERLAY_ELEMENT_PREV = null;
23 if (JOVERLAY_TIMER !== null) {
24 clearTimeout( JOVERLAY_TIMER );
28 var options = $.extend({}, $.fn.jOverlay.options, options);
37 var element = this.is('*') ? this : '#jOverlayContent';
38 var position = isIE6 ? 'absolute' : 'fixed';
39 var isImage = /([^\/\\]+)\.(png|gif|jpeg|jpg|bmp)$/i.test( options.url );
41 var imgLoading = options.imgLoading ? "<img id='jOverlayLoading' src='"+options.imgLoading+"' style='position:"+position+"; z-index:"+(options.zIndex + 9)+";'/>" : '';
43 $('body').prepend(imgLoading + "<div id='jOverlay' />"
44 + "<div id='jOverlayContent' style='position:"+position+"; z-index:"+(options.zIndex + 5)+"; display:none;'/>"
48 $('#jOverlayLoading').load(function(){
55 $('#jOverlayContent select').show();
60 backgroundColor : options.color,
64 filter : 'alpha(opacity='+ (options.opacity * 100) +')', // IE =(
65 opacity : options.opacity, // Good Browser =D
66 zIndex : options.zIndex,
67 width : !isIE6 ? '100%' : $(window).width() + 'px',
68 height : !isIE6 ? '100%' : $(document).height() + 'px'
74 JOVERLAY_ELEMENT_PREV = this.prev();
76 $('#jOverlayContent').html(
77 this.show().attr('display', options.autoHide ? 'none' : this.css('display') )
82 center('#jOverlayContent');
84 $('#jOverlayContent').show();
87 if ( !options.url && $.isFunction( options.success ) ) {
88 options.success( this );
98 $('<img/>').load(function(){
99 var resize = $.resize(this.width, this.height);
102 width : resize.width,
103 height : resize.height
106 $( element ).html(this);
108 center('#jOverlayContent');
110 $('#jOverlayLoading').fadeOut(500);
111 $('#jOverlayContent').show();
114 if ( $.isFunction( options.success ) ) {
115 options.success( this );
119 alert('Image ('+options.url+') not found.');
121 }).attr({'src' : options.url, 'alt' : options.url});
126 if ( options.url && !isImage ) {
129 type: options.method,
132 success: function(responseText) {
134 $('#jOverlayLoading').fadeOut(500);
136 $( element ).html(responseText).show();
138 center('#jOverlayContent');
141 if ($.isFunction( options.success )) {
142 options.success(responseText);
147 alert('URL ('+options.url+') not found.');
158 $(window).scroll(function(){
159 center('#jOverlayContent');
163 $(window).resize(function(){
166 width: $(window).width() + 'px',
167 height: $(document).height() + 'px'
170 center('#jOverlayContent');
176 // Press ESC to close
177 $(document).keydown(function(event){
178 if (event.keyCode == 27) {
184 if ( options.bgClickToClose ) {
185 $('#jOverlay').click($.closeOverlay);
188 // Timeout (auto-close)
189 // time in millis to wait before auto-close
190 // set to 0 to disable
191 if ( Number(options.timeout) > 0 ) {
192 jOverlayTimer = setTimeout( $.closeOverlay, Number(options.timeout) );
196 $('#jOverlayContent').css(options.css || {});
199 // Resizing large images - orginal by Christian Montoya.
200 // Edited by - Cody Lindley (http://www.codylindley.com) (Thickbox 3.1)
201 $.resize = function(imageWidth, imageHeight) {
202 var x = $(window).width() - 150;
203 var y = $(window).height() - 150;
204 if (imageWidth > x) {
205 imageHeight = imageHeight * (x / imageWidth);
207 if (imageHeight > y) {
208 imageWidth = imageWidth * (y / imageHeight);
211 } else if (imageHeight > y) {
212 imageWidth = imageWidth * (y / imageHeight);
214 if (imageWidth > x) {
215 imageHeight = imageHeight * (x / imageWidth);
219 return {width:imageWidth, height:imageHeight};
223 $.center = function(element) {
224 var element = $(element);
225 var elemWidth = element.width();
228 width : elemWidth + 'px',
229 marginLeft : '-' + (elemWidth / 2) + 'px',
230 marginTop : '-' + element.height() / 2 + 'px',
232 top : !isIE6 ? '50%' : $(window).scrollTop() + ($(window).height() / 2) + 'px',
238 $.fn.jOverlay.options = {
247 bgClickToClose : true,
255 $.closeOverlay = function() {
257 if (isIE6) { $("select").show(); }
259 if ( JOVERLAY_ELEMENT_PREV !== null ) {
260 if ( JOVERLAY_ELEMENT_PREV !== null ) {
261 var element = $('#jOverlayContent').children();
262 JOVERLAY_ELEMENT_PREV.after( element.css('display', element.attr('display') ) );
263 element.removeAttr('display');
267 $('#jOverlayLoading, #jOverlayContent, #jOverlay').remove();