summaryrefslogtreecommitdiff
path: root/js/jquery.joverlay.js
blob: cf4e159986d95d96fae0887fb8de90e88081d414 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* Copyright (c) 2009 Alvaro A. Lima Jr http://alvarojunior.com/jquery/joverlay.html
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * Version: 0.8 (OUT 19, 2009)
 * Requires: jQuery 1.3+
 */

(function($) {

	// Global vars
	var isIE6 = $.browser.msie && $.browser.version == 6.0; // =(
	var JOVERLAY_TIMER = null;

	$.fn.jOverlay = function(options) {

		// Element exist?
		if ( $('#jOverlay').length ) {$.closeOverlay();}

		// Clear Timer
		if (JOVERLAY_TIMER !== null) {
			clearTimeout( JOVERLAY_TIMER );
		}

		// Set Options
		var options = $.extend({}, $.fn.jOverlay.options, options || {});

		// success deprecated !!! Use onSuccess
		var onSuccess =  options.onSuccess || options.success;

		var element = this.is('*') ? this : '#jOverlayContent';

		var position = isIE6 ? 'absolute' : 'fixed';

		var isImage = /([^\/\\]+)\.(png|gif|jpeg|jpg|bmp)$/i.test( options.url );

		var imgLoading = options.imgLoading ? "<img id='jOverlayLoading' src='"+options.imgLoading+"' style='position:"+position+"; z-index:"+(options.zIndex + 9)+";'/>" : '';

		// private function
		function center(id) {
			if (options.center) {
				$.center(id);
			} else if( isIE6 ) {
				$.center('#jOverlayContent',{
					'top' : $(window).scrollTop() + 'px',
					'marginLeft' : '',
					'marginTop' : '',
					'left' : ''
				});
			}
		}

		$('body').prepend(imgLoading + "<div id='jOverlay' />"
			+ "<div id='jOverlayContent' style='position:"+position+"; z-index:"+(options.zIndex + 5)+"; display:none;'/>"
		);

		// Cache options
		$('#jOverlayContent').data('options', options);

		// Loading Centered
		$('#jOverlayLoading').load(function() {
			center(this);
		});

		//IE 6 FIX
		if ( isIE6 ) {
			$('select').hide();
			$('#jOverlayContent select').show();
		}

		// Overlay Style
		$('#jOverlay').css({
			'backgroundColor' : options.color,
			'position' : position,
			'top' : '0px',
			'left' : '0px',
			'filter' : 'alpha(opacity='+ (options.opacity * 100) +')', // IE =(
			'opacity' : options.opacity, // Good Browser =D
			'-khtml-opacity' : options.opacity,
			'-moz-opacity' : options.opacity,
			'zIndex' : options.zIndex,
			'width' : !isIE6 ? '100%' : $(window).width() + 'px',
			'height' : !isIE6 ? '100%' : $(document).height() + 'px'
		}).show();

		// INNER HTML
		if ( $.trim(options.html) ) {
			$(element).html(options.html);
		}

		// ELEMENT
		if ( this.is('*') ) {

			$('#jOverlayContent').data('jOverlayElementPrev', this.prev() );

			$('#jOverlayContent').html(
				this.show().data('display', options.autoHide ? 'none' : this.css('display') )
			);

			if ( !isImage ) {

				center('#jOverlayContent');

				$('#jOverlayContent').show();

				// Execute callback
				if ( !options.url && $.isFunction( onSuccess ) ) {
					onSuccess( this );
				}

			}

		}

		// IMAGE
		if ( isImage ) {

			$('<img/>').load(function(){
				var resize = $.resize(this.width, this.height);

				$(this).css({
					width : resize.width,
					height : resize.height
				});

				$( element ).html(this);

				center('#jOverlayContent');
				center('#jOverlayLoading');

				$('#jOverlayLoading').fadeOut(500);
				$('#jOverlayContent').show();

				// Execute callback
				if ( $.isFunction( onSuccess ) ) {
					onSuccess( $(element) );
				}

			}).error(function(){
				alert('Image ('+options.url+') not found.');
				$.closeOverlay();
			}).attr({'src' : options.url, 'alt' : options.url});

		}

		// AJAX
		if ( options.url && !isImage ) {

			$.ajax({
				type: options.method,
				data: options.data,
				url: options.url,
				success: function(responseText) {

					$('#jOverlayLoading').fadeOut(500);

					$( element ).html(responseText).show();

					center('#jOverlayContent');

					// Execute callback
					if ( $.isFunction( onSuccess ) ) {
						onSuccess( responseText );
					}

				},
				error : function() {
					alert('URL ('+options.url+') not found.');
					$.closeOverlay();
				}
			});

		}

		// :(
		if ( isIE6 ) {

			// Window scroll
			$(window).scroll(function(){
				center('#jOverlayContent');
			});

			// Window resize
			$(window).resize(function(){

				$('#jOverlay').css({
					'width' : $(window).width() + 'px',
					'height' : $(document).height() + 'px'
				});

				center('#jOverlayContent');

			});

		}

		// Press ESC to close
		if ( options.closeOnEsc ) {
			$(document).keydown(function(event){
				if ( event.keyCode == 27 ) {
					$.closeOverlay();
				}
			});
		} else {
			$(document).unbind('keydown');
		}

		// Click to close
		if ( options.bgClickToClose ) {
			$('#jOverlay').click($.closeOverlay);
		}

		// Timeout (auto-close)
		// time in millis to wait before auto-close
		// set to 0 to disable
		if ( options.timeout && Number(options.timeout) > 0 ) {
			JOVERLAY_TIMER = window.setTimeout( $.closeOverlay, Number(options.timeout) );
		}

		// ADD CSS
		$('#jOverlayContent').css(options.css || {});

	};

	// Resizing large images - orginal by Christian Montoya.
	// Edited by - Cody Lindley (http://www.codylindley.com) (Thickbox 3.1)
	$.resize = function(imageWidth, imageHeight) {
		var x = $(window).width() - 150;
		var y = $(window).height() - 150;
		if (imageWidth > x) {
			imageHeight = imageHeight * (x / imageWidth);
			imageWidth = x;
			if (imageHeight > y) {
				imageWidth = imageWidth * (y / imageHeight);
				imageHeight = y;
			}
		} else if (imageHeight > y) {
			imageWidth = imageWidth * (y / imageHeight);
			imageHeight = y;
			if (imageWidth > x) {
				imageHeight = imageHeight * (x / imageWidth);
				imageWidth = x;
			}
		}
		return {'width':imageWidth, 'height':imageHeight};
	};

	// Centered Element
	$.center = function(element, css) {
		var element = $(element);
		var elemWidth = element.width();

		element.css($.extend({},{
			'width' : elemWidth + 'px',
			'marginLeft' : '-' + (elemWidth / 2) + 'px',
			'marginTop' : '-' + element.height() / 2 + 'px',
		 	'height' : 'auto',
         	'top' : !isIE6 ? '50%' : $(window).scrollTop() + ($(window).height() / 2) + 'px',
         	'left' : '50%'
		}, css || {}));
	};

	// Options default
	$.fn.jOverlay.options = {
		'method' : 'GET',
		'data' : '',
		'url' : '',
		'color' : '#000',
		'opacity' : '0.6',
		'zIndex' : 9999,
		'center' : true,
		'imgLoading' : '',
		'bgClickToClose' : true,
		'success' : null, // Deprecated : use onSuccess
		'onSuccess' : null,
		'timeout' : 0,
		'autoHide' : true,
		'css' : {},
		'html' : '',
		'closeOnEsc' : true
	};

	// Set default options (GLOBAL)
	// Overiding the default values.
	$.fn.jOverlay.setDefaults = function(options) {
		$.fn.jOverlay.options = $.extend({}, $.fn.jOverlay.options, options || {});
	};

	// Close
	$.closeOverlay = function() {

		var content = $('#jOverlayContent');
		var options = content.data('options');
		var elementPrev = content.data('jOverlayElementPrev');

		// Fix IE6 (SELECT)
		if (isIE6) { $("select").show(); }

		// Restore position
		if ( elementPrev ) {
			var contentChildren = content.children();
			elementPrev.after( contentChildren.css('display', contentChildren.data('display') ) );
			// Clear cache
			contentChildren.removeData('display');
			content.removeData('jOverlayElementPrev');
		}

		// Clear options cache
		content.removeData('options');

		// Remove joverlay elements
		$('#jOverlayLoading, #jOverlayContent, #jOverlay').remove();

	};

})(jQuery);