/** 
 * jQuery plugin JWait
 *
 *  http://moda.ru/
 *
 *  Example: 
 *  var wait = $('body').jwait();
 *  	wait.show();
 *  	wait.hide();
 *
 *  jQuery required. Download it at http://jquery.com/
 *
 */


(function(jQuery) {

	jQuery.fn.jwait = function(options) {
		
		this.options = options;
		
		if(this.length == 0) 
			return this;
		
		if(typeof arguments[0] == 'string') {
			if(this.length > 1) {
				var args = arguments;
				return this.each(function() {
					$.fn.jwait.apply($(this), args);
    			});
			};
			
			$.fn.jwait[arguments[0]].apply(this, $.makeArray(arguments).slice(1) || []);
			
			return this;
		};
		
		options = jQuery.extend({
			position : 'center',
			show : false,
			elemHide : false
		}, options);
		
		$('div.jwait', this).remove();
		
		var offset = $(this).offset();
		var padding_top = $(this).css('padding-top');
		var padding_left = $(this).css('padding-left');
		
		var css = {
			width: $(this).width(),
			height: $(this).height()
		}
		
		switch(options.position) {
			case "zero":
				css = {
					top: parseInt(padding_top),
					left: parseInt(padding_left),
					width: $(this).width(),
					height: $(this).height()
				}
			break;
			default:
				css = {
					top: offset.top + parseInt(padding_top),
					left: offset.left,
					width: $(this).width(),
					height: $(this).height()
				}
			break;
		}
		
		$('<div class="jwait"><div class="jwait_middle"><div class="jwait_inner"></div></div></div>').css(css).appendTo(this);
		
		this.wait = $('div.jwait', this);
		
		if(options.show) {
			if(options.elemHide)
				this.jwait('show');
			else
				this.wait.show();
		}
		
		return this.wait;
	}
	
	$.extend($.fn.jwait, {
		show: function() {
			this.parent().find('*').each(function() {
				if($(this).attr('class') != 'jwait' && 
				   $(this).attr('class') != 'jwait_middle' &&
				   $(this).attr('class') != 'jwait_inner'
				)
					$(this).hide();
			});
			this.show();
		},
		hide: function() {
			this.parent().find('*').each(function() {
				if($(this).attr('class') != 'jwait' && 
				   $(this).attr('class') != 'jwait_middle' &&
				   $(this).attr('class') != 'jwait_inner'
				)
					$(this).show();
			});
			this.hide();
		}
	});
		
})(jQuery);
