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

$(document).ready(function() {
	// Suppression du texte par défaut au focus
	$('#valeur_recherche').focusin(function() {
		if ($(this).val() == 'Mot-clé, expression, etc.') {
			$(this).val('');
		}
	});
	
	// Ajout du texte par défaut a la perte du focus
	$('#valeur_recherche').focusout(function() {
		if ($(this).val() == '') {
			$(this).val('Mot-clé, expression, etc.');
		}
	});
	
	// Désactivation de l'autocomplete du champ de recherche
	$('#valeur_recherche').attr('autocomplete', 'off');
	
	// les liens vers les PDF s'ouvrent dans une nouvelle fenetre
	$('a.voyage_pdf').attr('target','_blank');
	
	// calendrier JS (pour les promotions)
	$(function() {
		$('.dpick').datepicker({
			showOn: "button",
			buttonImage: "images/calendar.gif",
			buttonImageOnly: true,
			changeMonth: true,
			changeYear: true,
			dateFormat: 'dd/mm/yy'
		});
	});
	
	// pop-up information demande de devis
	$('#submit_devis').click(function() {
		loadPopUp('ctn_devis','form_devis');
		$('#submit_devis2').live('click', function() {
			// copie des valeurs du pop up
			$('#nom_dev').val($('#nom_temp').val());
			$('#prenom_dev').val($('#prenom_temp').val());
			$('#mail_dev').val($('#mail_temp').val());
			$('#tel_dev').val($('#tel_temp').val());
			$('#com_dev').val($('#com_temp').val());
			$('#coddev').val($('#codverif').val());
			$('#form_dev').submit();
			return;
		});
	});
	
	$('#submit_avis').live('click', function() {
		var idv = parseInt($('input.idv_inp').val());
		$('input#id_voyage_avis').val(idv);
		$('#form_avis').submit();
		return;	
	});
	
	// remplissage auto du formulaire de demande de devis
	$('.voyage_devis').click(function() {
		if($(this).parent().find('.idv_inp').length > 0) {
			var idv = parseInt($(this).parent().find('.idv_inp').val());
			$(this).attr('href','index.php?action=fill_devis&id_voyage='+idv);
		}
		return;
	});
	
	// pop-up pour l'abonnement à la newsletter
	$('#vcea_nsl').click(function() {
		loadPopUp('ctn_abo_nsl','form_abo_nsl');
	});
	
	// pop-up pour signaler le site à un ami
	$('#vcea_ami').click(function() {
		loadPopUp('ctn_warn_ami','form_warn_ami');
	});
	
	// pop-up pour appel telephonique
	$('#vcea_appel').click(function() {
		loadPopUp('ctn_telephone','form_telephone');
	});
	
	// pop-up pour avis
	$('#voyage_link a.voyage_avis').click(function() {
		loadPopUp('ctn_avis','form_avis');
	});
	
	///////////////////////////////////////
	// Activation du moteur de recherche //
	//               (Début)             //
	///////////////////////////////////////
	
	// Suppression du texte par défaut au focus
	$('#recherche').focusin(function() {
		if ($(this).val() == 'Rechercher...') {
			$(this).val('');
			$(this).css('color', '#000000');
		}
	});
	
	// Ajout du texte par défaut a la perte du focus
	$('#recherche').focusout(function() {
		if ($(this).val() == '') {
			$(this).val('Rechercher...');
			$(this).css('color', '#BBBBBB');
		}
	});
	
	// Désactivation de l'autocomplete du champ de recherche
	$('#recherche').css('color', '#BBBBBB');
	$('#recherche').attr('autocomplete', 'off');
	
	function validRech() {
		$('#form_recherche').submit();
		return;
	}
	
	$('#recherche').autocomplete({
		minLength: 3,
		position: {
			my: "left top",
			at: "left bottom"
		},
		source: "ajax/moteur.php",
		focus: function() {
			// interdire les valeurs insérées au focus
			return false;
		},
		select: function(event, ui) {
			if(ui.item) {
				if(ui.item.id == 0) {
					$('#recherche').val('');
				}
				else {
					location.href = ui.item.href;
				}
			}
			else {
				validRech();
			}
			return;
		}
	});
	
	$('#recherche').bind('paste', function() {
		$(this).keydown();
	});
	
	// mise en surbrillance du motif de recherche
	function __highlight(s, t) {
		var matcher = new RegExp("("+$.ui.autocomplete.escapeRegex(t)+")", "ig" );
		return s.replace(matcher, "<strong>$1</strong>");
	}
	
	$.ui.autocomplete.prototype._renderItem = function( ul, item ) {
		$( "<li></li>" )
			.data( "item.autocomplete", item )
			.append( "<a href=\"" + item.href + "\">" + __highlight(item.label, $('#recherche').val()) +  "</a>" )
			.appendTo( ul );
		//$('#recherche').css('paddingBottom','6px');
		return;
	}
	
	$.ui.autocomplete.prototype._renderMenu = function(ul, items) {
		var self = this;
		var nbv = 0;
		var nbv_submit = 0;
		$.each( items, function( index, item ) {
			if(item.id >= 0) {					
				self._renderItem( ul, item );
			}
			if(item.id == -1) {
				nbv = item.value;
			}
			if(item.id < -1) {
				nbv_submit = item.value;
			}
		});
	};
	
	///////////////////////////////////////
	// Activation du moteur de recherche //
	//                (Fin)              //
	///////////////////////////////////////
	
	// Gestion du slide d'informations
	if ($('#message_vcea').size() > 0) {
		var message = $('#message_vcea').html();
		$('#message_vcea').remove();
		createSlide('<p>'+message+'</p>');
	}
	
	$('select[name=nb_page]').change(function() {
		// change de l'autre select
		$('select[name=nb_page]').val($(this).val());
		$('#filtre_produit').submit();
	}); 
});

// fonction de remplissage de la popup
function loadPopUp(id_contenu, load_file) {
	var form = '<div class="ctn_popup" id="'+id_contenu+'"></div>';
	createSlide(form);
	$('#'+id_contenu).load('./pages/'+load_file+'.php', function(response, status, xhr) {
		if(status == 'error') {
			var msg = "<p>Une erreur a été rencontrée : "+xhr.status+" "+xhr.statusText+'</p>';
			$('#'+id_contenu).html(msg);
		}
		// pour le replacement du slide au milieu de l'écran
		$(window).trigger('resize');
	});
}

// Fonction de création d'un slide
function createSlide(texte) {
	
	// delai d'animation
	var delay = 500;
	
	var masque = $('<div>', {
		css: {
			'position': 'absolute',
			'z-index': '999',
			'top': '0',
			'left': '0',
			'width': $('body').width()+'px',
			'height': $('body').height()+'px',
			'background-color': 'gray',
			'opacity': '0.5',
			'filter': 'alpha(opacity=50)',
			'cursor': 'pointer',
			'display': 'none'
		},
		click: function() {
			slide.fadeOut(delay, function() { $(this).remove() });
			masque.fadeOut(delay, function() { $(this).remove() });
		}
	});
	$('body').append(masque);
	masque.fadeIn(delay);
	var slide = $('<div>', {
		css: {
			'position': 'absolute',
			'z-index': '1000',
			'padding': '10px',
			'background-color': '#FFFFFF',
			'text-align': 'center',
			'font-weight': 'bold',
			'display': 'none',
			'border-color': '#91cde9',
			'border-width': '2px',
			'border-style': 'solid'
		}
	});
	slide.append($('<div>', { html: texte }));
	slide.append($('<img>', {
		'class': 'png_bg',
		id: 'slide_close',
		src: 'images/fermer.png',
		alt: 'Fermer',
		title: 'Fermer',
		css: {
			'cursor': 'pointer'
		},
		click: function() {
			slide.fadeOut(delay, function() { $(this).remove() });
			masque.fadeOut(delay, function() { $(this).remove() });
		}
	}));
	$('#slide_close').live('mouseover', function() {
		$(this).attr('src','images/fermer_hover.png');
	});
	$('#slide_close').live('mouseout', function() {
		$(this).attr('src','images/fermer.png');
	});
	var ctn_close = $('<div>', {
		css: {
			'position': 'absolute',
			'top': '-15px',
			'right': '-15px'
		}
	});
	var link_close = $('<a>', {
		id: 'link_close',
		alt: 'Fermer',
		title: 'Fermer',
		href: 'javascript:void(0);',
		click: function() {
			slide.fadeOut(delay, function() { $(this).remove() });
			masque.fadeOut(delay, function() { $(this).remove() });
		}
	});
	var img_close = $('<img>', {
		'class': 'png_bg',
		src: 'images/btn_close_slide.png',
		alt: 'Fermer',
		title: 'Fermer',
		width: '30',
		height: '30'
	});
	link_close.append(img_close);
	ctn_close.append(link_close);
	slide.append(ctn_close);
	
	if ($('div img', slide).size() > 0) {
		$('body').append(slide);
		slide.fadeIn(delay, function () {
			var loader = $('<div>', {
				html: '<img src="images/ajax-loader.gif" alt="" /> Chargement...',
				css: {
					'position': 'absolute',
					'z-index': '1000',
					'padding': '10px',
					'width': '200px',
					'height': '20px',
					'left': ($(window).width() - 200) / 2 + $(window).scrollLeft()+'px',
					'top': ($(window).height() - 20) / 2 + $(window).scrollTop()+'px',
					'background-color': 'white',
					'text-align': 'center',
					'font-weight': 'bold'
				}
			});
			$('body').append(loader);
			//trace('loader loaded');
			loader.corner();
			$('div img', slide).one('load',function(){
				//trace('remove');
				loader.remove();
				slide.css({
					'top': ($(window).height() - slide.height()) / 2 + $(window).scrollTop()+'px',
					'left': ($(window).width() - slide.width()) / 2 + $(window).scrollLeft()+'px',
					'display': 'block'
				});
			})
			.each(function(){
				// contournement bug IE6
				if(this.complete || (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6))
				$(this).trigger("load");
			});	
		});
		
	} else {
		$('body').append(slide);
		slide.fadeIn(delay, function() {
			slide.css({
				'top': ($(window).height() - slide.height()) / 2 + $(window).scrollTop()+'px',
				'left': ($(window).width() - slide.width()) / 2 + $(window).scrollLeft()+'px'
			});
		});
	}
	slide.corner();
	$(window).resize(function() {
		masque.css({
			'width': $('body').width()+'px',
			'height': $('body').height()+'px'
		});
		slide.css({
			'top': ($(window).height() - slide.height()) / 2 + $(window).scrollTop()+'px',
			'left': ($(window).width() - slide.width()) / 2 + $(window).scrollLeft()+'px'
		});
	});
	
	$(document).keyup(function(e) {
		if(e.keyCode == 27) {
			$('#link_close').trigger('click');
		}
	});
}
