var Effects = {
	loading: function(element){
		//alert("baseLoad");
		return;
	}
};

var initLoading=function(){
	if (document.getElementById('fancy-overlay') == null) {
		var overlayBG = document.createElement('div');
		overlayBG.className = 'fancy-overlay';
		overlayBG.setAttribute('id', 'fancy-overlay');
		document.body.appendChild(overlayBG);
		var overlay = document.createElement('div');
		overlay.className = 'content-overlay';
		overlay.setAttribute('id', 'content-overlay');
		
		
		var clientHeight = 0;
		var clientWidth = 0;
		var imgHeight = 32;
		var imgWidth = 32;
		
		var imgSrc = "/static/images/popups/load.gif";
		
		var top = 0;
		var left = 0;
		
		if (typeof(window.innerWidth) == 'number') {
			clientHeight = window.innerHeight;
			clientWidth = window.innerWidth;
		}
		else 
			if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
				//IE 6+ in 'standards compliant mode'
				clientHeight = document.documentElement.clientHeight;
				clientWidth = document.documentElement.clientWidth;
			}
		
		top = (clientHeight - imgHeight) / 2;
		left = (clientWidth - imgWidth) / 2;
		
		var content = "<img height='" + imgHeight + "px' width='" + imgWidth + "px' src='" + imgSrc + "' alt='loading' style='position:fixed;top:" + top + "px;left:" + left + "px;'/>";
		overlay.innerHTML = content;
		document.body.appendChild(overlay);
		
		hideLoading();
	}	
}

var waitLoading = function() {
	displayLoading();
}

var displayLoading=function(){
	jQuery("#fancy-overlay").css("display","block");
	jQuery("#content-overlay").css("display","block");
}

var hideLoading = function() {
	jQuery("#fancy-overlay").css("display","none");
	jQuery("#content-overlay").css("display","none");
}

Tapestry.FormEventManager.prototype.handleSubmit = function(domevent)  {
        var t = $T(this.form);
        t.validationError = false;
        var firstErrorField = null;
        // Locate elements that have an event manager (and therefore, validations)
        // and let those validations execute, which may result in calls to recordError().
        this.form.getElements().each(function(element)  {
            var fem = $T(element).fieldEventManager;
            if (fem != undefined) {
                // Ask the FEM to validate input for the field, which fires
                // a number of events.
                var error = fem.validateInput();
				if (error && ! firstErrorField){
                    firstErrorField = element;
                }
            }
        });

        // Allow observers to validate the form as a whole.  The FormEvent will be visible
        // as event.memo.  The Form will not be submitted if event.result is set to false (it defaults
        // to true).  Still trying to figure out what should get focus from this
        // kind of event.
        this.form.fire(Tapestry.FORM_VALIDATE_EVENT, this.form);
        if (t.validationError) {
            hideLoading();
			Event.stop(domevent); // Should be domevent.stop(), but that fails under IE
            if (firstErrorField) firstErrorField.activate();
            // Because the submission failed, the last submit property is cleared,
            // since the form may be submitted for some other reason later.
            t.lastSubmit = null;
            return false;
        }

        this.form.fire(Tapestry.FORM_PREPARE_FOR_SUBMIT_EVENT, this.form);
        // This flag can be set to prevent the form from submitting normally.
        // This is used for some Ajax cases where the form submission must
        // run via Ajax.Request.

        if (this.preventSubmission) {
            // Prevent the normal submission.
            Event.stop(domevent);
            // Instead ...
            this.form.fire(Tapestry.FORM_PROCESS_SUBMIT_EVENT);
			
			if ($("zonePrix")) {
				waitLoading();
				$("zonePrix").observe(Tapestry.ZONE_UPDATED_EVENT, function(){
					hideLoading();
				});
			}
			
			if ($("zoneOpinion")) {
				waitLoading();
				$("zoneOpinion").observe(Tapestry.ZONE_UPDATED_EVENT, function(){
					hideLoading();
				});
			}
			
			if ($("memberZone")) {
				waitLoading();
				$("memberZone").observe(Tapestry.ZONE_UPDATED_EVENT, function(){
					hideLoading();
				});
			}
			
			if ($("gridPriceZone")) {
				waitLoading();
				$("gridPriceZone").observe(Tapestry.ZONE_UPDATED_EVENT, function(){
					hideLoading();
				});
			}
			
			if ($("emailAlertZone")){
				waitLoading();
				$("emailAlertZone").observe(Tapestry.ZONE_UPDATED_EVENT, function(){
					hideLoading();
				});				
			}
			
			if($("headerLoginZoneId")){
				waitLoading();
				$("headerLoginZoneId").observe(Tapestry.ZONE_UPDATED_EVENT, function(){
					hideLoading();
				});					
			}
			
			if($("emailAlertAmexZone")){
				waitLoading();
				$("emailAlertAmexZone").observe(Tapestry.ZONE_UPDATED_EVENT, function(){
					hideLoading();
				});					
			}
			
            return false;
        }

        // Validation is OK, not doing Ajax, continue as planned.
		waitLoading();
        return true;
}

/*ajout de l'overlayer*/
$(document).ready(function () {
	initLoading();
});

Effects.myLoading = function (element){
	var el = $T(this.element);
	if (el && el.zoneId) {
		$(el.zoneId).innerHTML = '<br/><p align="center"><img src="/static/images/popups/load.gif"></p>' +
		'<br/><br/><p align="center">Merci de patienter pendant le chargement des tarifs ...' +
		'</p><br/>';
	}
	return;
};



