/***
@title:
Ajax Loader

@version:
1.0

@author:
Andreas Lagerkvist

@date:
2008-09-25

@url:
http://andreaslagerkvist.com/jquery/ajax-loader/

@license:
http://creativecommons.org/licenses/by/3.0/

@copyright:
2008 Andreas Lagerkvist (andreaslagerkvist.com)

@requires:
jquery, jquery.ajaxLoader.css, jquery.ajaxLoader.gif

@does:
Use this plug-in when you want to inform your visitors that a certain part of your page is currently loading. The plug-in adds a faded 'loading-div' on top of the selected element(s). The div is of course completely stylable.

@howto:
jQuery('#contact').ajaxLoader(); would add the overlay on top of the #contact-element.

When you want to remove the loader simply run jQuery('#contact').ajaxLoaderRemove();

@exampleHTML:
I should start loading after a couple of seconds, then load for a couple more and then stop loading only to start again after a couple of seconds. And so on.

@exampleJS:
setInterval(function () {
	jQuery('#jquery-ajax-loader-example').ajaxLoader();
	setTimeout(function () {
		jQuery('#jquery-ajax-loader-example').ajaxLoaderRemove();
	}, 2000);
}, 4000);
***/
jQuery.fn.ajaxLoader = function (conf) {
	var config = jQuery.extend({
		className:		'jquery-ajax-loader', 
		fadeDuration:	500
	}, conf);

	return this.each(function () {
		var t = jQuery(this);
		
		var scrollHeight,
			offsetHeight;
		// handle IE 6
		if ($.browser.msie && $.browser.version < 7) {
			scrollHeight = Math.max(
				document.documentElement.scrollHeight,
				document.body.scrollHeight
			);
			offsetHeight = Math.max(
				document.documentElement.offsetHeight,
				document.body.offsetHeight
			);

			if (scrollHeight < offsetHeight) {
				height= $(window).height() + 'px';
			} else {
				height= scrollHeight + 'px';
			}
		// handle "good" browsers
		} else {
			height= $(document).height() + 'px';
		}
		
		
		var scrollWidth,
			offsetWidth;
		// handle IE 6
		if ($.browser.msie && $.browser.version < 7) {
			scrollWidth = Math.max(
				document.documentElement.scrollWidth,
				document.body.scrollWidth
			);
			offsetWidth = Math.max(
				document.documentElement.offsetWidth,
				document.body.offsetWidth
			);

			if (scrollWidth < offsetWidth) {
				width= $(window).width() + 'px';
			} else {
				width= scrollWidth + 'px';
			}
		// handle "good" browsers
		} else {
			width= $(document).width() + 'px';
		}
		
		
		
        
		if (!this.ajaxLoaderObject) {
			var offset = t.offset();
			var dim = {
				left:	offset.left, 
				top:	offset.top, 
				width:	t.outerWidth(), 
				height:	height
			};
                  
			this.ajaxLoaderObject = jQuery('<div id="loader" class="' + config.className + '"></div>').css({
				position:	'absolute', 
				left:		0 + 'px', 
				top:		 0 + 'px',
				width:		width,
				height:		height,
				opacity:	.8
			}).appendTo(document.body).hide();
		}
		else
		{
		
		jQuery('#loader').css({position:	'absolute', 
				left:		0 + 'px', 
				top:		 0 + 'px',
				width:		width,
				height:		height,
				opacity:	.8});
		
		}

		this.ajaxLoaderObject.fadeIn(config.fadeDuration);
	});
};

jQuery.fn.ajaxLoaderRemove = function () {
	return this.each(function () {
		if (this.ajaxLoaderObject) {
			this.ajaxLoaderObject.fadeOut(500);
		}
	});
};

