var iSpeed = 1100;
jQuery.onAjaxCallback = function () { };
jQuery.onAjax = function (callback) {
	jQuery(document).ready(callback);
	
	var onAjaxCallback = jQuery.onAjaxCallback;
	jQuery.onAjaxCallback = function () { onAjaxCallback(); callback(); };
	
	return this;
};

jQuery.fn.ajaxLoad = function (url, data, callback) {
	if (jQuery.isFunction(callback)) {
		$Loader = this;
		$Loader.callback = callback;
		
		callback = function (responseText, textStatus, XMLHttpRequest) {
			$Loader.callback(responseText, textStatus, XMLHttpRequest);
			jQuery.onAjaxCallback();
		};
		
		return this.load(url, data, callback);
	} else
		return this.load(url, data, function (responseText, textStatus, XMLHttpRequest) {
			jQuery.onAjaxCallback(responseText, textStatus, XMLHttpRequest);
		} );
};

jQuery.rafraichir = function (expr, url) {
	jQuery("#page").append('<div id="chargement">Rafra\356chissement en cours</div>');
	jQuery("#chargement").centrer();
	
	jQuery(expr).ajaxLoad(url + ' ' + expr + ' > *', { }, function () {
		jQuery("#chargement").fadeOut(iSpeed, function () { jQuery(this).remove(); } ) ;
	} );
	
	return this;
};

jQuery.fn.rafraichir = function (expr) {
	jQuery.rafraichir(expr, this.attr('href'));
	return this;
};

jQuery.chargerImage = function (sSrcImage) {
	(new Image()).src = sSrcImage;
	return this;
};

jQuery.fn.chargerImage = function () {
	if (this.attr('src'))
		jQuery.chargerImage(this.attr('src'));
	else if (this.attr('href'))
		jQuery.chargerImage(this.attr('href'));
	
	return this;
};

jQuery.fn.actionTableau = function (callback) {
	var $Tableau = this.parents("tbody");
	var $LigneCourante = this.parents("tr");
	
	if (jQuery("tr:animated", $Tableau).length)
		return false;
	
	if (jQuery(".actionTableau", $Tableau).length > 0) {
		jQuery(".actionTableau", $Tableau).fadeOut(iSpeed, function () {
			jQuery(this).remove();
			$LigneCourante.removeClass('ligneCourante');
		
			  // S'il y a un callback...
			if (jQuery.isFunction(callback)) {
				this.callback = callback;
				this.callback();
			}
		} ) ;
		
		jQuery("tr", $Tableau).animate({ opacity: 1 }, iSpeed);
	} else {
		  // On cache toutes les autres lignes
		jQuery("tr", $Tableau).not($LigneCourante).animate({ queue: false, opacity: 0 }, iSpeed);
		
		  // On ajoute la ligne qui servira de conteneur à l'action demandée
		$LigneCourante.addClass('ligneCourante').after('<tr class="actionTableau"><td colspan="' + jQuery("td", $LigneCourante).length + '"></td></tr>');
		
		  // On la cache le temps de charger le contenu
		jQuery(".actionTableau td", $Tableau).hide();
	
		  // S'il y a un callback...
		if (jQuery.isFunction(callback)) {
			this.callback = callback;
			this.callback();
		}
	}
	
	return this;
};

jQuery.chargerImage('images/loading.gif');
jQuery.fn.formulaireAjax = function (formulaireAjaxCallback) {
	this.data('formulaireAjaxCallback', formulaireAjaxCallback);
	this.submit( function () {
		if (jQuery(":submit", this).is(":disabled") || jQuery("#chargement").is(":animated"))
			return false;
		
		  // On désactive le bouton submit pour éviter les doubles validations
		jQuery(":submit", this).attr('disabled', 'disabled');
		
		jQuery(this).append('<div id="chargement">Envoi en cours</div>');
		jQuery("#chargement").centrer(this);
		
		var sAction = jQuery(this).attr('action').replace(/^(.+)(#.*)?$/, '$1');
		var sSelector = jQuery(this).attr('id') ? '#' + jQuery(this).attr('id') : '.formulaireAjax';
		var oChamps = jQuery(":input,:submit", this).serializeArray();
		
		jQuery(this).ajaxLoad(sAction + " " + sSelector + " > *", oChamps, function () {
			jQuery("#chargement").fadeOut(iSpeed, function () { jQuery(this).remove(); } ) ;
			
			  // On réactive le bouton submit
			jQuery(":submit", this).removeAttr('disabled');
			
			  // S'il y a un callback...
			if (jQuery.isFunction(jQuery(this).data('formulaireAjaxCallback'))) {
				this.formulaireAjaxCallback = jQuery(this).data('formulaireAjaxCallback');
				this.formulaireAjaxCallback();
			}
		} ) ;
		
		return false;
	} ) ;
	
	return this;
};

jQuery.fn.centrer = function (container) {
	if (this.css('position') != 'fixed')
		this.css('position', 'absolute');
	
	var iLeft = (jQuery(container || window).width() / 2) - (this.width() / 2);
	var iTop = (jQuery(container || window).height() / 2) - (this.height() / 2);
	
	if (this.css('position') != 'fixed' && !container) {
		iLeft += jQuery(document).scrollLeft();
		iTop += jQuery(document).scrollTop();
	}
	
	this.css({ top: iTop + 'px', left: iLeft + 'px' });
	
	return this;
}
