
function showWaiting(elem, is_center) {
	var is_center = is_center||false;
	
	var tmp = jQuery('#footer').prev();
	if(tmp.is('.waiting')) {
		tmp.remove();
	} else {
		tmp = jQuery(elem).offset();
		jQuery('#footer').before( '<img class="waiting" src="/img/waiting.gif" style="'+
								'left:'+(tmp.left+jQuery(elem).innerWidth()/2-10)+'px;'+
								'top:'+((is_center?jQuery(elem).innerHeight()/2-10:0)+tmp.top)+'px;" />');
	}
}

function hideWaiting(elem) {
	var tmp = jQuery('#footer').prev();//elem.prev();//
	
	if(tmp.is('.waiting')) {
		tmp.remove();
	}
}

(function (jQuery) {
	jQuery.loadWait = function(options) {
		return jQuery.loadWait.init(null, options);
	}
	
	jQuery.fn.loadWait = function(options) {
		return jQuery.loadWait.init(this, options);
	}
	
	jQuery.loadWait.defaults = {
		onDone: null,
		onError: null,
		url: '',
		params: {},
		elem: null,
		rotate: true,
		full: false,
		rotateCenter: false,
		type : 'POST'
	}
	
	jQuery.loadWait.init = function(elem, options) {
		var opts = null;
		elem = elem || null;
		opts = jQuery.extend({}, jQuery.loadWait.defaults, options);
		
		if(!opts.url) return;
		
		if(opts.rotate) showWaiting(opts.elem, opts.rotateCenter);
		jQuery.ajax({
			url: opts.url,
			type: opts.type,
			data: opts.params,
			dataType: 'json',
			success: function(data) {
				if(opts.rotate) hideWaiting(opts.elem);
				if(data.status == 'ok'){
					if(opts.onDone != undefined) opts.onDone.apply(this, [data] );
				} else {
					if(opts.onError != undefined && opts.onError != null) opts.onError.apply(this, [data] );
						else if(data.html) $.fn.colorbox({ html : data.html });
				}
			},
			timeout: function(data) {
				if(opts.rotate) hideWaiting(opts.elem);
				if(opts.onError != undefined && opts.onError != null) opts.onError.apply(this, [data] );
			},
			error: function(data) {
				if(opts.rotate) hideWaiting(opts.elem);

				if(opts.onError != undefined && opts.onError != null) opts.onError.apply(this, [data] );
					else if(data.html) $.fn.colorbox({ html : data.html });
			}
		});
		
		return this;
	}
	
})(jQuery);

