$(function() {
	
	/* FONCTION PERMETTANT DE MASQUER LES MESSAGES D'INFORMATION AJAX */
	$.hideMsg = function hideMsg() {
		$('.ajax-msg').slideUp(function() {
			$(this).remove();
		});
	}

	/* FONCTION DE FERMETURE DES DIALOG BOX */
	$.closeBox = function closeBox() {
		$(".dialog-modal").dialog("close");
	}
	
	/* FONCTION DE REINITIALISATION DES CHAMPS DE FORMULAIRES */
	$.reset = function reset(elt) {
		$(elt).find(':input').each(function() {
			switch(this.type) {
				case 'password':
				case 'select-multiple':
				case 'select-one':
				case 'text':
				case 'textarea':
					$(this).val('');
					break;
				case 'checkbox':
				case 'radio':
					this.checked = false;
			}
		});
	}
	
	/* PLACEHOLDER FORMULAIRES DE CONTACT */
	var inputs = $('#contactForm .placeholder, #NewsletterAddForm .placeholder');
	inputs
		.each(function() {
			var input = $(this).find(':input'); // textareas, inputs, selects et buttons
			var label = $(this).find('label'); // textarea label, input label...
			var html = label.html();
			
			if(input.val() != '') {
				label.hide();
				var posTop = input.position().top;
				$(this).hover(function() {
					//console.log('prefull hover');
					if(!label.hasClass('tooltip')) {
						label
							.stop()
							.wrapInner('<span class="tooltip"></span>')
							.addClass('tooltip')
							.css({
								top:posTop - 43,
								left:0,
								opacity:0,
								display:'block', // permet l'affichage de la petite pointe de l'infobulle
								position:'relative',
								zIndex:3,
								width:$(this).contents().width()
							})
							.delay(1000)
							.animate({
								top:posTop - 33,
								opacity:1
							});
					} else if(label.is(':hidden') && label.hasClass('tooltip')) {
						label
							.stop()
							.css({
								top:posTop - 43,
								left:0,
								opacity:0,
								display:'block', // permet l'affichage de la petite pointe de l'infobulle
								position:'relative',
								zIndex:3,
								width:$(this).contents().width()
							})
							.delay(1000)
							.animate({
								top:posTop - 33,
								opacity:1
							});
					}
				}, function() {
					//console.log('prefull leave');
					if(label.hasClass('tooltip')) {
						label
							.stop()
							.delay(1000)
							.animate({
									top:posTop,
									opacity:0
								},
								150,
								"linear",
								function() {
									label.hide();
								}
							);
					}
				});
				
				input.blur(function() {
					//console.log('prefull blur');
					if(input.val() == '') {
						var defaultValue = $(this)[0].defaultValue;
						$(this).val(defaultValue);
					}
				});
			} else {
				var posTop = input.position().top;
				input
					.focus(function() {
						//console.log('empty focus');
						if(!label.hasClass('tooltip')) {
							label.fadeTo(200, 0.5);
						}
					})
					.blur(function() {
						var defaultValue = $(this)[0].defaultValue;
						if(label.hasClass('tooltip') && input.val() == '' && defaultValue == '') {
							tooltip = label.find('span.tooltip');
							tooltip.remove();
							label
								.stop()
								.css({
									display:'inline',
									zIndex:0
								})
								.removeClass('tooltip')
								.html(html)
								.delay(1000)
								.animate(
									{
										opacity:1,
										top:0,
										left:5
									},
									150,
									"linear"
								);
						} else if(input.val() == '') {
							if(!label.hasClass('tooltip')) {
								label.fadeTo(200, 1);
							}
						} else if(label.hasClass('tooltip')) {
							label
								.stop()
								.delay(1000)
								.animate({
										top:posTop,
										opacity:0
									},
									150,
									"linear",
									function() {
										label.hide();
									}
								);
						}
					})
					.keypress(function() {
						if(!label.hasClass('tooltip')) {
							label.hide();
						}
					})
					.keyup(function() {
						if(input.val() == '' && !label.hasClass('tooltip')) {
							label.stop().fadeTo(200, 0.5);
						}
					})
					.hover(function() {
						//console.log('full hover');
						if(!label.hasClass('tooltip') && input.val() != '') {
							label
								.stop()
								.wrapInner('<span class="tooltip"></span>')
								.addClass('tooltip')
								.css({
									top:posTop - 43,
									left:0,
									opacity:0,
									display:'block', // permet l'affichage de la petite pointe de l'infobulle
									position:'relative',
									zIndex:3,
									width:$(this).contents().width()
								})
								.delay(1000)
								.animate({
									top:posTop - 33,
									opacity:1
								});
						} else if(label.hasClass('tooltip')) {
							label
								.stop()
								.css({
									top:posTop - 43,
									left:0,
									opacity:0,
									display:'block', // permet l'affichage de la petite pointe de l'infobulle
									position:'relative',
									zIndex:3,
									width:$(this).contents().width()
								})
								.delay(1000)
								.animate({
									top:posTop - 33,
									opacity:1
								});
						}
					}, function() {
						//console.log('full leave');
						if(label.hasClass('tooltip') && input.val() != '') {
							label
								.stop()
								.delay(1000)
								.animate({
										top:posTop,
										opacity:0
									},
									150,
									"linear",
									function() {
										label.hide();
									}
								);
						}
					});
			}
		});
});
