var countAjaxCall = 0;

jQuery.extend({
	doAjax : function(url,callbackSuccess,callbackError) {
		callbackSuccess = callbackSuccess || function(html){};
		callbackError = callbackError || function(){};
	   	notifyStartAjaxCall();
		$.ajax({
			type: "POST",
			url: url,
			dataType : "html",			
			data: {}, // n�cessaire pour passer les proxys sous FF
			success : function(html) {
	           	notifyStopAjaxCall();				
				callbackSuccess(html);
			},
			error : function( httprequest, textStatus, errorThrown){
	           	notifyStopAjaxCall();
	           	callbackError();
				displayAjaxError(httprequest);
			}
		});
	},
	doAjaxSync : function(url,callbackSuccess,callbackError) {
		callbackSuccess = callbackSuccess || function(html){};
		callbackError = callbackError || function(){};
	   	notifyStartAjaxCall();
		$.ajax({
			type: "POST",
			url: url,
			dataType : "html",
			data: {}, // n�cessaire pour passer les proxys sous FF
			success : function(html) {
	           	notifyStopAjaxCall();				
				callbackSuccess(html);
			},
			async : false,
			error : function( httprequest, textStatus, errorThrown){
	           	notifyStopAjaxCall();
	           	callbackError();
				displayAjaxError(httprequest);
			}
		});
	}
});


jQuery.fn.makeAjaxCall= function(url,callback) {
	var target=$(this);
	
	if (target.attr("id")=="mainPanel"||target.attr("id")=="ongletPanel") {
		if(pageModified==true&&$(document.body).hasClass("alertonexit")) {
			var idConfirmPageChange = $.openDialog({
				title : constants.labels.modifiedTitle,
				content : constants.labels.modifiedContent,
				icon : constants.images.questionMark,
				type : "modal",
				buttons : {
					ok : {
						img: constants.images.accept,
						label: "Poursuivre",
						js: function() {
							$("#"+idConfirmPageChange).closeDialog();
							jQuery.ignoreAjaxErrors = true;
							document.location.href=url;
							return;
						}
					},
					nok : {
						img: constants.images.cancel,
						label: "Annuler",
						js : function() { 
							$("#"+idConfirmPageChange).closeDialog();
							return;
						}
					}
				},
				position:"center",
				draggable:true,
				width:400,
				height:180
			});
		} else {
			jQuery.ignoreAjaxErrors = true;
			document.location.href=url;
			return;
		}
	} else {
		callback = callback || function(){};
	   	notifyStartAjaxCall();
	   	
	   	// supprime les draggable/droppable existants dans le target
		target.find(".ui-draggable,.ui-draggable-disabled").draggable("destroy");				
		target.find(".ui-droppable,.ui-droppable-disabled").droppable("destroy");
        
	   	target.load(url, {}, function(){
	   		notifyStopAjaxCall();
        	callback();
		});
	}
}

function displayAjaxError(httprequest) {
	if (jQuery.ignoreAjaxErrors) {
		// on vient de changer de page alors que certaines boites ajax n'�taient pas totalement charg�es. On ignore donc l'erreur...
	}
	else {
		var report = $("#messageajax");
		frames['frameerror'].document.body.innerHTML=httprequest.responseText;
		report.css("top",$("#page").height()/2-report.height()/2);
		report.css("left",$("#page").width()/2-report.width()/2);
		report.showElement();	
		blockPage();
	}
}


function notifyStartAjaxCall() {
	if (countAjaxCall==0) {
		$("#ajaxload").showElement();
		//$("#messageload").showElement();
	}
	countAjaxCall++;
}

function notifyStopAjaxCall() {
	countAjaxCall--;
	if (countAjaxCall==0) {
		$("#ajaxload").hideElement();
	}
}

/**
 * Fonctions pour g�rer l'apparation et la disparition d'un message en bas de la fen�tre.
 * Il y a plusieurs div de message dans la page
 */
jQuery.fn.showElement = function () {
	$(this).show();
	$(this).fadeTo("slow","1");
}

jQuery.fn.hideElement = function() {
	$(this).fadeTo("slow","0",function() { 
		$(this).hide();
	});

}


