(function () {
	/* ********** DEBUT CONFIG ********** */
	var INTE_CFG = {
			lang: 'fr',
			canal: LnkWcb.jQuery('.LnkWcbForm-canal').val() || 'OGF0001',
			// global selectors
			trigger: '.LnkWcbForm-trigger', // the trigger is the element that activates the container
			container: '.LnkWcbForm-container', // the container that will be activated by the trigger
			closer: '.LnkWcbForm-closer', // the container closer
			formId: 'LnkWcbForm',
			// relative selectors (relative to form root in 'formId')
			userStatus: '.user-status',
			errorStatus: '.user-errors',
			calleeInput: 'input[name="callee"]',
			dateInput: '.LnkWcbDateDiff',
			dateElem: '.lnk-wcb-date',
			timeElem: '.lnk-wcb-time'
		},
	/* ********** FIN CONFIG ********** */
		$ = LnkWcb.jQuery,
		U = LnkWcb.util;

	/**
	 * This helper function does the setup of the trigger button (aka "Le Bouton").
	 */
	function setupWcbTrigger() {
		var params = U.urlDecode(window.location.search.substring(1));

		if (params.effect === 'fade') {
			$(INTE_CFG.trigger).parent().addClass('trigger-parent'); // stick the container to a fixed posision
		}

		$(INTE_CFG.trigger).click(function () {
			var elem = $(INTE_CFG.container),
				trigger = $(INTE_CFG.trigger),
				b = LnkWcb.inte1.montrerBouton;
			if (!b) {
				trigger.fadeOut();
			}
			else if (params.effect === 'fade') {
				elem.fadeIn(); // override the 'cacher' class
				trigger.fadeOut();
			}
			else if (params.effect === 'slide') {
				elem.slideDown(); // override the 'cacher' class
				trigger.slideUp();
			}
			else {
				elem.show();
				trigger.hide();
			}
		});
		$(INTE_CFG.closer).click(function () {
			var elem = $(INTE_CFG.container),
				trigger = $(INTE_CFG.trigger),
				b = LnkWcb.inte1.montrerBouton;
			if (params.effect === 'fade') {
				if (b) {
					trigger.fadeIn();
				}
				elem.fadeOut();
			}
			else if (params.effect === 'slide') {
				if (b) {
					trigger.slideDown();
				}
				elem.slideUp();
			}
			else {
				if (b) {
					trigger.show();
				}
				elem.hide();
			}
		});
	}

	/**
	 * Setup the open/close animation for CNIL legal notice.
	 * This custom animation is required to allow this inline element to move accordingly.
	 * The jQuery UI/Effects/Slide would not suit this need.
	 * 
	 * @param F {jQuery} a jQuery search result holding the WCB form
	 */
	function setupCnilAnimation(F) {
		var opening = false, open = false, closing = false;
		F.find('.LnkWcbCnil').mouseenter(function (event) {
			var E = $(this).find('.depliant'),
				W = E.parent('.enveloppe'),
				distance;
			if (opening || open) {
				return;
			}
			opening = true;
			E.show();
			W.css('width', 'auto');
			distance = E.width();
			W.css('width', 0);
			E.css({ position: 'relative', left: -distance }).animate({ left: '+=' + distance }, {
				duration: 'slow', queue: false,
				step: function () {
					W.css('width', String(distance + parseInt(E.css('left'), 10))+'px')
				},
				complete: function () {
					E.css({ position: 'static', left: 'auto' }).show();
					W.css('width', 'auto');
					opening = false;
					open = true;
					E.dequeue();
				}
			});
		}).mouseleave(function (event) {
			var E = $(this).find('.depliant'),
				W = E.parent('.enveloppe'),
				distance;
			if (closing || !open) {
				return;
			}
			closing = true;
			E.show();
			W.css('width', 'auto');
			distance = E.width();
			E.css({ position: 'relative', left: 0 }).animate({ left: '-=' + distance }, {
				duration: 'slow', queue: false,
				step: function () {
					W.css('width', String(distance + parseInt(E.css('left'), 10))+'px')
				},
				complete: function () {
					E.css({ position: 'static', left: 'auto' }).hide();
					W.css('width', 'auto');
					closing = false;
					open = false;
					E.dequeue();
				}
			});
		});
	}

	/**
	 * This helper function creates one integration bouton.
	 * 
	 * @param I {Object} the integration namespace
	 * @param cfgBouton {Object} the bouton config
	 * @param cfgCalendar {Object} the date picker config
	 */
	function setupWcbForm(I, cfgBouton, cfgCalendar) {
		var F = $(cfgBouton.formSel),
			ackEnded;
		I.montrerBouton = false;
		/* ----- Bouton ----- */
		I.bouton = new LnkWcb.BoutonJquery(cfgBouton);
		I.bouton.showUserStatus = function (msg) { // permettre le HTML dans les messages des intermédiaires d'appel
			$(this.cfg.formSel).find(this.cfg.userStatusElemRelSel).html(msg);
		};
		$('input[name="callee"]').focus(function () {  // permettre de supprimer le contenus du champs telephone onFocus
				$(this).val('');
		});
		I.bouton.onChannelState(function (etat, etatOuverture) { // Bouton : masquer en canal inactif ou hors-limites
			if (!etat.estActif || !etat.peutRecevoirAppel) {
				$(INTE_CFG.trigger).hide();
				I.montrerBouton = false;
			}
			else { // satué, fermé, férié, ou ouvert
				if ($(INTE_CFG.container).css('display') === 'block') { // hide the container if displayed (not to have both trigger AND container displayed)
					$(INTE_CFG.container).hide();
				}
				$(INTE_CFG.trigger).show();
				I.montrerBouton = true;
			}
		});
	
		I.bouton.onChannelState(function (etat, etatOuverture) { // Formulaire : différé en état non ouvert, et désactivé en inactif ou hors-limites
			if (!etat.estActif || !etat.peutRecevoirAppel ||  etatOuverture ==="FERME" || etatOuverture ==="FERIE" || etatOuverture ==="SATURE") {
				I.afficherDiffere(false);
				F.find('.champ-wcb').hide();
				$('.LnkWcbForm-container').addClass('LnkWcbForm-container-inactif');
				
			}
			
			else {
				F.find('input').attr("disabled", false); // enable all inputs
				I.afficherDiffere(!etat.estOuvert); // refreshes the calendar
				F.find('.champ-wcb').show();
				$('.LnkWcbForm-container').removeClass('LnkWcbForm-container-inactif');
				
			}
		});
		

		I.bouton.onSendCall(function (args, attrs) {
			F.find(this.cfg.userStatusElemRelSel).addClass('busy');
			F.find('input').attr("disabled", true); // prevent duplicate submits
		});
		ackEnded = function (status, params) {
			F.find(this.cfg.userStatusElemRelSel).removeClass('busy');
			F.find('input').attr("disabled", false); // re-enable form submits
		};
		I.bouton.onError(ackEnded);
		I.bouton.onEnded(ackEnded);
		I.bouton.onFallback(ackEnded);
		I.bouton.onFallback(function (status, params) { // activer automatiquement le mode différé
			if (status.debordementCause === 'CAUSE_BLOQUE' || status.debordementCause === 'CAUSE_FERME' || status.debordementCause === 'CAUSE_FERIE' || status.debordementCause === 'CAUSE_SATURE') {
				I.afficherDiffere(false);
				F.find('input').attr("disabled", true);
				F.find('.champ-wcb').hide();
			}
			else {
				I.afficherDiffere(false);
				F.find('input').attr("disabled", true);
				F.find('.champ-wcb').hide();
			}
		});
		/* ----- Calendrier ----- */
		I.calendrier = new LnkWcb.CalendarJquery(U.putAll({
			bouton: I.bouton
		}, cfgCalendar));
		/* ----- Intégration ----- */
		I.afficherDiffere = function (afficher) {
			F.find('.panneau-date-differe').toggleClass('cacher', !afficher);
			if (afficher) {
				I.calendrier.refresh(); // vérif des horaires d'ouverture + démarrage du calendrier (destroy+init+show)
			}
			else {
				I.calendrier.hide();
			}
		};
		/* ----- Intégration Evènements Formulaire ----- */
		$('a[rel="external"]').attr('target', '_blank'); // corriger les liens ayant l'attribut rel="external"
		F.find('a.cnil').attr('href',
			// L'URL email ci-dessous est volontairement découpée en petits morceaux
			// pour emêcher que les spammeurs ne l'obtiennent trop facilement.
			[ 'mail','to',':cnil@','linkeo.','com?','subject=',encodeURIComponent('Accès données personnelles pour ' + cfgBouton.canal) ].join(''));
		setupCnilAnimation(F);
		F.find('.codeCanal').text(cfgBouton.canal);
		F.find('input[name="callee"]').keypress(function (event) { // filtrer les caractères saisis
			var c = String.fromCharCode(event.charCode);
			if (event.charCode && !/^[0-9+]$/.test(c)) { // interdire la saisie des caractères autres que '0' à '9' et '+'
				return false;
			}
		}).keyup(function (event) { // filtrer le contenu du champ pendant la saisie
			var E = $(this), val = E.val(), pos;
			val = val.replace(/[^0-9+]/g, ''); // éliminer les caractères autres que '0' à '9' et '+'
			val = (val.indexOf('+') === 0 ? '+' : '') + val.replace(/\+/g, ''); // supprimer les '+' qui ne sont pas au début
			val = val.replace(/^\+0+/, '+'); // éliminer les '0' qui suivent le '+' initial, s'il y en a un
			val = val.replace(/^000+/, '00'); // interdire plus de deux '0' initiaux
			if (val !== E.val()) {
				if (!E.parent().hasClass('warning')) {
					E.wrap('<span class="warning"/>'); // afficher un avertissement en cas de modification
				}
				E.val(val);
			}
		});
		F.submit(function () {
			var callee, date;
			callee = F.find(I.bouton.cfg.calleeInputRelSel).val();
			if (!F.find('.panneau-date-differe').hasClass('cacher')) {
				date = I.calendrier.getWcbDate(); // TODO: resolve the validation issue: input should be validated before it is converted to a JS Date object
				//LnkLog.log('date: [' + date + ']');
				//date = I.calendrier.getDateTime();
			}
			I.bouton.rappeler(callee, date);
			return false;
		});
		$(INTE_CFG.container).find(INTE_CFG.closer).click(function () {
			I.bouton.raccrocher();
		});
		/* ----- Lancement ----- */
		I.bouton.estOuvert(); // vérif de l'état du canal + refresh cal (à faire après le setup du bouton et du calendrier)
	};

	/**
	 * This helper function creates integration namespaces,
	 * and collects config options in order to create all integration boutons.
	 * <p>
	 * Customize here to add new boutons.
	 */
	function setupAllWcbForms() {
		var I, cfgBouton, cfgCalendar,
			params = U.urlDecode(window.location.search.substring(1));
		try {
			/* ----- Inits ----- */
			LnkWcb.intl.setLang(INTE_CFG.lang);
			setupWcbTrigger();
			/* ----- Lancement des enchainements d'initialisations ----- */
			I = LnkWcb.inte1 = LnkWcb.inte1 || {}; // création du namespace de l'intégration n°1
			cfgBouton = {
				canal: INTE_CFG.canal,
				grabbedFormId: INTE_CFG.formId,				// enable the 'grabForm' Trait
				formSel: '#'+INTE_CFG.formId,				// '#LnkWcbForm' by default
				userStatusElemRelSel: INTE_CFG.userStatus,	// '.user-status' by default
				userErrorsElemRelSel: INTE_CFG.errorStatus,	// '.user-errors' by default
				calleeInputRelSel: INTE_CFG.calleeInput,	// 'input[name="callee"]' by default
				dateInputRelSel: INTE_CFG.dateInput			// '.LnkWcbDateDiff' by default
			};
			cfgCalendar = {
				formSel: '#'+INTE_CFG.formId, // '#LnkWcbForm' by default
				dateContainerRelSel: INTE_CFG.dateElem, // '.lnk-wcb-date' by default
				timeContainerRelSel: INTE_CFG.timeElem // '.lnk-wcb-time' by default
				// , hoursSelectorClass: 'lnk-wcb-hours', // this is the default
				// minutesSelectorClass: 'lnk-wcb-minutes' // this is the default
				// , displayType: 'INLINE' // this is the default
			};
			setupWcbForm(I, cfgBouton, cfgCalendar);
		}
		catch (exc) {
			LnkLog.log('LnkWcb.inte', exc);
		}
	};

	/**
	 * Create WCB integration when document is ready.
	 */
	try {
		$(setupAllWcbForms); // execute when the document is ready
	} catch (ignoredExc) {
		// LnkLog.log('integration', ignoredExc);
	}
})();

