// Surbrillance des recherches
function highlight(s, t) {
	var matcher = new RegExp("("+$.ui.autocomplete.escapeRegex(t)+")", "ig" );
	return s.replace(matcher, "<strong>$1</strong>");
}

// Debug
function trace(msg) {
	if (window.console) console.log(msg);
	else alert(msg);
}

function hl(elt,pwik) {
	elt.animate({
		opacity: (pwik == true ? 0.50 : 1)	
	}, 200, function() {});
}

$(function() {
	// Activation des menus de sous-pages
	$('#bann-menu li').hover(
		function() {
			$(this).find('.ss_nav').css('display', 'block');
		},
		function() {
			$(this).find('.ss_nav').css('display', 'none');
		}
	);
	
	///////////////////////////////////////
	// Activation du moteur de recherche //
	//               (Début)             //
	///////////////////////////////////////
	
	// Suppression du texte par défaut au focus
	$('#moteur_bienvenue').focusin(function() {
		if ($(this).val() == trad_val_moteur) {
			$(this).val('');
		}
	});
	
	// Ajout du texte par défaut a la perte du focus
	$('#moteur_bienvenue').focusout(function() {
		if ($(this).val() == '') {
			$(this).val(trad_val_moteur);
		}
	});
	
	// Désactivation de l'autocomplete du champ de recherche
	$('#moteur_bienvenue').attr('autocomplete', 'off');
	
	// Activation de l'autocomplete
	$('#moteur_bienvenue').autocomplete({
		minLength: 3,
		position: {
			my: 'left top',
			at: 'left bottom',
			offset: '0 5'
		},
		source: 'ajax/moteur.php',
		select: function(event, ui) {
			if (ui.item.link != '') {
				location.href = ui.item.link;
			}
		}
	}).data('autocomplete')._renderItem = function(ul, item) {
		var label = item.label;
		if (item.label != trad_resultats_ko) {
			label = highlight(item.label, $('#moteur_bienvenue').val());
		}
		return $('<li></li>').data('item.autocomplete', item).append('<a>'+label+'</a>').appendTo(ul);
	};
	
	// Activation du copier-coller
	$('#moteur_bienvenue').bind('paste', function() {
		$(this).keydown();
	});
	
	///////////////////////////////////////
	// Activation du moteur de recherche //
	//                (Fin)              //
	///////////////////////////////////////
	
	// Gestion du slide d'informations
	if ($('#msg_slide').size() > 0) {
		$('#msg_slide').dialog({
			close: function() {
				if ($('#msg_slide').hasClass('warn_ami')) {
					$('#friend_mail').focus();
				}
				if ($('#msg_slide').hasClass('nsl_focus')) {
					$('#adresse_contact').focus();
				}
				$(this).dialog('destroy');
			},
			draggable: false,
			minHeight: 'auto',
			minWidth: 'auto',
			hide: 'fade',
			modal: true,
			open: function() {
				$this = $(this);
				$('.ui-widget-overlay').bind('click', function() {
					$this.dialog('close');
				});
			},
			resizable: false,
			show: 'fade',
			width: 'auto'
		});
	}
	
	// Suppression du texte par défaut au focus de la newsletter
	$('#adresse_contact').focusin(function() {
		if ($(this).val() == trad_val_email) {
			$(this).val('');
		}
	});
	
	// Ajout du texte par défaut a la perte du focus de la newsletter
	$('#adresse_contact').focusout(function() {
		if ($(this).val() == '') {
			$(this).val(trad_val_email);
		}
	});
	
	$('#adresse_contact').keydown(function(e){
		if (e.keyCode == 13) {
			$('#subscribe_nsl').trigger('click');
		}
	});
	
	$('#ctn_abo_nsl input').keydown(function(e){
		trace(e.keyCode);
		if (e.keyCode == 13) {
			$('#ctn_abo_nsl').find('form').submit();
		}
	});
	
	// Suppression du texte par défaut au focus de warn_ami
	$('#friend_mail').focusin(function() {
		if ($(this).val() == trad_val_friend_email) {
			$(this).val('');
		}
	});
	
	// Ajout du texte par défaut a la perte du focus de warn_ami
	$('#friend_mail').focusout(function() {
		if ($(this).val() == '') {
			$(this).val(trad_val_friend_email);
		}
	});
	
	$('#friend_mail').keydown(function(e){
		if (e.keyCode == 13) {
			$('#warn_ami').trigger('click');
		}
	});
	
	// Désactivation de l'autocomplete du champ de newsletter
	$('#adresse_contact').attr('autocomplete', 'off');
	
	// Popup inscription newsletter
	$('#subscribe_nsl').click(function() {
		var valmail = $('#adresse_contact').val();
		if(valmail == '' || valmail == trad_val_email) {
			$('<div id="msg_slide" title="'+trad_informations+'">'+trad_email_ko+'</div>').dialog({
				close: function() { $(this).dialog('destroy'); },
				draggable: false,
				minHeight: 'auto',
				hide: 'fade',
				modal: true,
				open: function() {
					$this = $(this);
					$('.ui-widget-overlay').bind('click', function() {
						$this.dialog('close');
					});
				},
				resizable: false,
				show: 'fade',
				width: 'auto'
			});
		}
		else {
			$.ajax({
				type: 'POST',
				url: './pages/form_abo_nsl.php',
				success: function(data) {
					$('<div id="ctn_abo_nsl" class="ctn_popup">'+data+'</div>').dialog({
						close: function() { $(this).dialog('destroy'); },
						buttons: [
							{ 
								text: trad_fermer,
								click: function() { $(this).dialog('close'); }
							},
							{
								text: trad_inscrire,
								click: function() {	 $('#ctn_abo_nsl').find('form').submit(); }
							}
						],
						dialogClass: 'noTitleDialog',
						draggable: false,
						minHeight: 'auto',
						hide: 'fade',
						modal: true,
						open: function() {
							$this = $(this);
							$('.ui-widget-overlay').bind('click', function() {
								$this.dialog('close');
							});
						},
						resizable: false,
						show: 'fade',
						width: 'auto'	
					});
					$('#ctn_abo_nsl input[name="email"]').val(valmail);
				},
				error: function(request, status, xhr) {
					var msg = "<p>"+trad_erreur_ajax+" : "+xhr.status+" "+xhr.statusText+'</p>';
					$('body').append('<div id="ctn_abo_nsl" class="ctn_popup">'+msg+'</div>');
					$('#ctn_abo_nsl').dialog({
						close: function() { $(this).dialog('destroy'); },
						buttons: [
							{ 
								text: trad_fermer,
								click: function() {
									$(this).dialog('close');
								}
							}
						],
						dialogClass: 'noTitleDialog',
						draggable: false,
						minHeight: 'auto',
						modal: true,
						open: function() {
							$this = $(this);
							$('.ui-widget-overlay').bind('click', function() {
								$this.dialog('close');
							});
						},
						resizable: false,
						width: 'auto'	
					});
					
				}
			});
		}
	});
	
	$('#warn_ami').click(function() {
		var valmail = $('#friend_mail').val();
		if(valmail == '' || valmail == trad_val_email) {
			$('<div id="msg_slide" title="'+trad_informations+'">'+trad_email_ko+'</div>').dialog({
				close: function() { $(this).dialog('destroy'); },
				draggable: false,
				minHeight: 'auto',
				hide: 'fade',
				modal: true,
				open: function() {
					$this = $(this);
					$('.ui-widget-overlay').bind('click', function() {
						$this.dialog('close');
					});
				},
				resizable: false,
				show: 'fade',
				width: 'auto'
			});
		}
		else {
			$.ajax({
				type: 'POST',
				url: './pages/form_warn_ami.php',
				success: function(data) {
					$('<div id="ctn_abo_nsl" class="ctn_popup">'+data+'</div>').dialog({
						close: function() { $(this).dialog('destroy'); },
						dialogClass: 'noTitleDialog',
						draggable: false,
						minHeight: 'auto',
						hide: 'fade',
						modal: true,
						open: function() {
							$this = $(this);
							$('.ui-widget-overlay').bind('click', function() {
								$this.dialog('close');
							});
						},
						resizable: false,
						show: 'fade',
						width: 'auto'	
					});
					$('#ctn_abo_nsl input[name="dest_email"]').val(valmail);
				},
				error: function(request, status, xhr) {
					var msg = "<p>"+trad_erreur_ajax+" : "+xhr.status+" "+xhr.statusText+'</p>';
					$('body').append('<div id="ctn_abo_nsl" class="ctn_popup">'+msg+'</div>');
					$('#ctn_abo_nsl').dialog({
						close: function() { $(this).dialog('destroy'); },
						buttons: [
							{ 
								text: trad_fermer,
								click: function() {
									$(this).dialog('close');
								}
							}
						],
						dialogClass: 'noTitleDialog',
						draggable: false,
						minHeight: 'auto',
						modal: true,
						open: function() {
							$this = $(this);
							$('.ui-widget-overlay').bind('click', function() {
								$this.dialog('close');
							});
						},
						resizable: false,
						width: 'auto'	
					});
					
				}
			});
		}
	});
	
	$('.wrapper_img img').mouseover(function() { hl($(this),true); });
	$('.wrapper_img img').mouseout(function() { hl($(this),false); });
	
	// liens externes
	$('.target_blank').attr('target', '_blank');
	
	// Activation du lien externe DSoft
	$('#lien_dsoft').click(function(e) {
		e.preventDefault();
		window.open($(this).attr('href'));
	});
});
