(function($) {

	 /*
 	Copyright © 2010 Richard Gazdik (richard.gazdik.name)
	All rights reserved.
 	*/

	$.jqOverlay = {
		bound: false,
		settings: null,
		
		load: function(href,settings) {
			if ($.jqOverlay.settings == null) {
				$.jqOverlay.settings = $.extend({
					overlayOpacity: 0.7,
					overlayColor: '#000',
					contentBorder: 15,
					contentWidth: 0,
					contentHeight: 0,
					animationSpeed: 'fast', 
					callback: function() {},
					onLoadComplete: function() {}
				}, settings);
			}
			
			//bound events to overlay
			if(!$.jqOverlay.bound) {
				$.jqOverlay.bound = true;
				$(window).scroll(function(){ $.jqOverlay.centerContent($.jqOverlay.settings.contentWidth, $.jqOverlay.settings.contentHeight); });
				$(window).resize(function(){ $.jqOverlay.centerContent($.jqOverlay.settings.contentWidth, $.jqOverlay.settings.contentHeight); });
			}
				
			//build overlay
			if ($('#overlay').length == 0) {
				$('body').append('<div id="overlay" style="background-color:'+$.jqOverlay.settings.overlayColor+'"></div>');
				$('body').append('<div id="overlay-preloader"></div>');
				$('#overlay').click(function(){$.jqOverlay.close();});
				$('#overlay').css('height',$(document).height());
				$('#overlay').css('opacity',0).fadeTo($.jqOverlay.settings.animationSpeed, $.jqOverlay.settings.overlayOpacity);
			}
			$('#overlay-preloader').css('opacity',0).fadeTo($.jqOverlay.settings.animationSpeed, 1);
			
			//build container
			if ($('#jqOverlay').length == 0) {
				$('body').append('<div id="jqOverlay"><div id="jqOverlay-close" class="close"><img src="images/jqoverlay_close.png" alt="" width="54" height="54" /></div><div id="jqOverlay-content"></div></div>');
				$('#jqOverlay-content').css({'padding':$.jqOverlay.settings.contentBorder}).fadeOut(0);
				$('#jqOverlay-close').fadeOut(0);
				$.jqOverlay.settings.contentWidth = 0;
				$.jqOverlay.settings.contentHeight = 0;
				$('#jqOverlay').width(0).height(0).css({
					'top': ($(window).height()-$.jqOverlay.settings.contentHeight)/2,
					'left': ($(window).width()-$.jqOverlay.settings.contentWidth)/2
				});
			} else {
				$('#jqOverlay-content').fadeOut('fast');
			}
				
			//load content
			$.get(href, function(data) {
				$('#jqOverlay-content').append(data+"<div style='clear:both'></div>");
				$('#jqOverlay .close').unbind();
				$('#jqOverlay .close').click(function(){ $.jqOverlay.close();});
				$('#overlay-preloader').fadeOut($.jqOverlay.settings.animationSpeed);
				
				$.jqOverlay.settings.onLoadComplete();
				
				$.jqOverlay.show();
			});		
		},
		
		show:function() {
			$.jqOverlay.settings.contentWidth = $('#jqOverlay-content').width()  + 2*$.jqOverlay.settings.contentBorder;
			$.jqOverlay.settings.contentHeight = $('#jqOverlay-content').height() + 2*$.jqOverlay.settings.contentBorder;
			var windowHeight = $(window).height();
			var windowWidth = $(window).width();
			var projectedLeft = (windowWidth - $.jqOverlay.settings.contentWidth)/2;
			var projectedTop = (windowHeight - $.jqOverlay.settings.contentHeight)/2;
			$('#jqOverlay').animate({
				'width' : $.jqOverlay.settings.contentWidth,
				'height' : $.jqOverlay.settings.contentHeight,
				'left' : projectedLeft,
				'top' : projectedTop
			}, $.jqOverlay.settings.animationSpeed, function() {
				$('#jqOverlay-content').fadeIn($.jqOverlay.settings.animationSpeed);
				$('#jqOverlay-close').fadeIn($.jqOverlay.settings.animationSpeed);
			});
		},
			
		close: function(cb,speed) {
			$('#overlay').fadeOut('fast',function(){ $(this).remove(); });
			$('#overlay-preloader').fadeOut('fast',function(){ $(this).remove(); });
			$('#jqOverlay').fadeOut('fast',function() { if (cb==1) $.jqOverlay.settings.callback(); $(this).remove();  $.jqOverlay.settings = null});
		},
		
		centerContent:function(cw,ch) {
			if($('#jqOverlay').length == 0) return;
			var windowHeight = $(window).height();
			var windowWidth = $(window).width();
			var projectedLeft = (windowWidth - cw)/2;
			var projectedTop = (windowHeight - ch)/2;
			$('#jqOverlay').css({ 'top': projectedTop, 'left': projectedLeft});
			$('#overlay').css({width:windowWidth,height:windowHeight});
		}
	};
})(jQuery);
