var delaiRech = 500;
$(document).ready(function() {
	$("#btRech").click(function() {
		if (valideRech()) {
			afficheWait();
			$("#rech").val("rech");
			setTimeout('$("#formcrit").submit();', delaiRech);
		 }
		 return false;
	});
	
	$("#formcrit").keypress(function(e) {
		if (e.keyCode == 13) $("#btRech").click();
	});
	
	$("#suivant").click(function() { 
		afficheWait();
		$("#senspagination").val("+");
		setTimeout('$("#formcrit2").submit();', delaiRech);
	});
	
	$("#precedent").click(function() { 
		afficheWait();
		$("#senspagination").val("-");
		setTimeout('$("#formcrit2").submit();', delaiRech);
	});
	
	$("#prem").click(function() { 
		afficheWait();
		$("#senspagination").val("-");
		$("#pagination").val("0");
		setTimeout('$("#formcrit2").submit();', delaiRech);
	});
	
	$("#fTypeVin, #fPays, #fCepage, #fNomPlat, #fNomPlat2, #fBudgetMax").change(function() { 
		afficheWait();
		setTimeout('$("#formfiltre").submit();', delaiRech);
	});
	
	$("#fTypePlatParVin, #fPaysParVin, #fNomAppellation").change(function() { 
		afficheWait();
		setTimeout('$("#formfiltrevin").submit();', delaiRech);
	});
	
	$("#dialog").dialog({
		autoOpen: false,
		resizable: false,
		width: 800,
		height: 400,
		modal: true,
		draggable: true,
		buttons: {
			Fermer: function() {
				$(this).dialog('close');
			}
		},
		show: 'clip',
		hide: 'clip'
	});
	
	$("img[name^='img_affiliation_vin']").click(function(event) {
		$("#dialog").load("inc/affiliation-vin-popup.inc.php?idvinpopup="+event.currentTarget.name, function() {
			$('#dialog').dialog('open');
			return false;
		});
	}).qtip({
		position: {
			corner: { target: 'leftBottom', tooltip: 'topRight'}
		},
		style: { name: 'light', width: 340, textAlign: 'center', tip: true, border: { radius: 4 } }
	});
	
	$("div[id^='fiche_note_accord']").click(function(event) {
		return true;
	}).qtip({
		position: {
			corner: { target: 'leftBottom', tooltip: 'topRight'}
		},
		style: { name: 'light', width: 350, tip: true, border: { radius: 4 } }
	});
	
	$("#dialog_add_note_accord").dialog({
		autoOpen: false,
		resizable: false,
		width: 700,
		height: 540,
		modal: true,
		draggable: true,
		buttons: {
			Fermer: function() {
				$(this).dialog('close');
			}
		},
		show: 'clip',
		hide: 'clip'
	});
	
	$("img[name^='add_note_accord']").click(function(event) {
		$("#dialog_add_note_accord").load("inc/add-note-accord-popup.php?idplatvin="+event.currentTarget.name, function() {
			$('#dialog_add_note_accord').dialog('open');
			return false;
		});
	}).qtip({
		position: {
			corner: { target: 'leftBottom', tooltip: 'topRight'}
		},
		style: { name: 'light', width: 310, tip: true, border: { radius: 4 } }
	});
	
	$(".IB").click(function(event) {
			return true;
	}).qtip({
		position: {
			corner: { target: 'leftBottom', tooltip: 'topRight'}
		},
		style: { name: 'light', width: 350, textAlign: 'center', tip: true, border: { radius: 4 } }
	});
	$(".IB2").click(function(event) {
			return true;
	}).qtip({
		position: {
			corner: { target: 'bottomMiddle', tooltip: 'topMiddle'}
		},
		style: { name: 'light', width: 420, textAlign: 'left', tip: true, border: { radius: 4 } }
	});
	
	$(".ClsTriable").css( 'cursor', 'pointer' ).click(function(event) {
		if (valideRech()) {
			var senstri = $("input[name='senstri']");
			if (event.currentTarget.id == $("input[name='coltri']").val()) {
				if (senstri.val() == "ASC") $("input[name='senstri']").each(function(){$(this).val("DESC")});
				else $("input[name='senstri']").each(function(){$(this).val("ASC")});
			}
			else $("input[name='senstri']").each(function(){$(this).val("ASC")});
			$("input[name='coltri']").each(function(){$(this).val(event.currentTarget.id)});
			$("input[name='gotri']").val("GO");
			afficheWait();
			setTimeout('$("#formcrit2").submit();', delaiRech);
		}
		return false;
	}).mouseenter(function() {
		var colltri = $("input[name='coltri']");
		if (colltri.val() == this.id) {
			var image = $(".ClsTrie img[id^='img_tri_']");
			if ($("input[name='senstri']").val() == "ASC") image.attr("src", "images/tri_DESC.png");
			else image.attr("src", "images/tri_ASC.png");
		}
		else $("#"+this.id+" img[id^='img_tri_']").attr("src", "images/tri_ASC.png");
		
	}).mouseleave(function() {
		var colltri = $("input[name='coltri']");
		if (colltri.val() == this.id) {
			var image = $(".ClsTrie img[id^='img_tri_']");
			if ($("input[name='senstri']").val() == "ASC") image.attr("src", "images/tri_ASC.png");
			else image.attr("src", "images/tri_DESC.png");
		}
		else $("#"+this.id+" img[id^='img_tri_']").attr("src", "images/tri_.png");
	});
	
	$("#dialog_add_accord_vin").dialog({
		autoOpen: false,
		resizable: false,
		width: 720,
		height: 520,
		modal: true,
		draggable: true,
		buttons: {
			Fermer: function() {
				$(this).dialog('close');
			}
		},
		show: 'clip',
		hide: 'clip'
	});
	
	$("#dialog_add_accord_plat").dialog({
		autoOpen: false,
		resizable: false,
		width: 720,
		height: 545,
		modal: true,
		draggable: true,
		buttons: {
			Fermer: function() {
				$(this).dialog('close');
			}
		},
		show: 'clip',
		hide: 'clip'
	});
	
	$("img[name='Actions']").click(function(event) {
		return true;
	}).qtip({
		position: {
			corner: { target: 'bottomRight', tooltip: 'topLeft'}
		},
		style: { name: 'light', width: 310, textAlign: 'center', tip: true, border: { radius: 4 } }
	});
	
	$("td[id^='ActionsVin_']").click(function(event) {
		return true;
	}).qtip({
		content: {
			prerender: true,
			title: {
				text: 'Actions sur ce vin',
				button: 'Fermer'
			}
		},
		position: {
			corner: { target: 'bottomLeft', tooltip: 'leftTop'}
		},
		show: {
			when: 'click',
			solo: true,
			effect: { type: 'slide', length: 300 }
		},
		hide: {
			fixed: true,
			delay: 1500,
			effect: { type: 'slide', length: 300 }
		},
		style: { name: 'light', width: 300, textAlign: 'left', border: { radius: 4 } }
	});
	
	$("span[id^='AddAccordVin_']").click(function(event) {
		$("#dialog_add_accord_vin").load("inc/add-accord-vin-popup.php?idvin="+this.id, function() {
			$('#dialog_add_accord_vin').dialog('open');
			return false;
		});
	});
	
	$("span[id^='img_affiliation_vin']").click(function(event) {
		$("#dialog").load("inc/affiliation-vin-popup.inc.php?idvinpopup="+this.id, function() {
			$('#dialog').dialog('open');
			return false;
		});
	});
	
	$("td[id^='ActionsPlat_']").click(function(event) {
		return true;
	}).qtip({
		content: {
			prerender: true,
			title: {
				text: 'Actions sur ce plat',
				button: 'Fermer'
			}
		},
		position: {
			corner: { target: 'bottomLeft', tooltip: 'leftTop'}
		},
		show: {
			when: 'click',
			solo: true,
			effect: { type: 'slide', length: 300 }
		},
		hide: {
			fixed: true,
			delay: 1500,
			effect: { type: 'slide', length: 300 }
		},
		style: { name: 'light', width: 300, textAlign: 'left', border: { radius: 4 } }
	});
	
	$("span[id^='AddAccordPlat_']").click(function(event) {
		$("#dialog_add_accord_plat").load("inc/add-accord-plat-popup.php?idplat="+this.id, function() {
			$('#dialog_add_accord_plat').dialog('open');
			return false;
		});
	});
	
	$("#site").bind('copy', function(e){ return false; });
	
	$("div[id^='headtab']").click(function(event) {
		var idtabclic = this.id;
		if (idtabclic == 'headtab1' && $('#headtab1').hasClass('RechAvHeadNoSelected')) {
			$('input[name="vin"]').val("");
			$('input[name="plat"]').val("");
			$("#modeRechAvancee").val("1");
			$("#rech").val("");
			$("#formcrit").submit();
		}
		else if (idtabclic == 'headtab2' && $('#headtab2').hasClass('RechAvHeadNoSelected')) {
			$('input[name="plat"]').val("");
			$('input[name="plat2"]').val("");
			$("#modeRechAvancee").val("2");
			$("#rech").val("");
			$("#formcrit").submit();
		}
	});
});

function afficheWait() {
	$.blockUI({
		message: $('#pleasewait'),
		timeout: 10000,
		css: { 	'border-radius': '10px',
				'-webkit-border-radius': '10px', 
				'-moz-border-radius': '10px', 
				opacity: .7
		}
	});
}

function valideRech() {
	if ($("#modeRechAvancee").length == 0) {
		window.document.formcrit.vin.value = $.trim(window.document.formcrit.vin.value);
		window.document.formcrit.plat.value = $.trim(window.document.formcrit.plat.value);

		if (window.document.formcrit.vin.value == "" && window.document.formcrit.plat.value == "") {
			alert("Veuillez saisir un nom de vin ou de plat.");
			return false;
		}
		if (window.document.formcrit.vin.value != "" && window.document.formcrit.plat.value != "") {
			alert("ATTENTION, vous avez saisi un nom de vin ET de plat.\nPar défaut la recherche s'effectuera sur le nom du vin saisi.");
			window.document.formcrit.plat.value = "";
		}
		if (window.document.formcrit.vin.value != "" && window.document.formcrit.vin.value.length <= 3) {
			alert("Pour une recherche plus pertinente, veuillez saisir un nom de vin d'au minimum 4 caractères.");
			return false;
		}
		if (window.document.formcrit.vin.value != "" && window.document.formcrit.vin.value.length > 3) return true;
		if (window.document.formcrit.plat.value != "" && window.document.formcrit.plat.value.length <= 2) {
			alert("Pour une recherche plus pertinente, veuillez saisir un nom de plat d'au minimum 3 caractères.\nActuce : mettez le nom du plat au pluriel pour gagner une lettre !");
			return false;
		}
	}
	else {
		if ($("#modeRechAvancee").val() == "1") {
			window.document.formcrit.plat.value = $.trim(window.document.formcrit.plat.value);
			window.document.formcrit.plat2.value = $.trim(window.document.formcrit.plat2.value);
			
			if (window.document.formcrit.plat2.value == "" || window.document.formcrit.plat2.value == "") {
				alert("Veuillez saisir un nom de plat n°1 ET n°2.");
				return false;
			}		
			if (window.document.formcrit.plat.value != "" && window.document.formcrit.plat.value.length <= 2) {
				alert("Pour une recherche plus pertinente, veuillez saisir un nom de plat n°1 d'au minimum 3 caractères.\nActuce : mettez le nom du plat au pluriel pour gagner une lettre !");
				return false;
			}
			if (window.document.formcrit.plat2.value != "" && window.document.formcrit.plat2.value.length <= 2) {
				alert("Pour une recherche plus pertinente, veuillez saisir un nom de plat n°2 d'au minimum 3 caractères.\nActuce : mettez le nom du plat au pluriel pour gagner une lettre !");
				return false;
			}
			if (window.document.formcrit.plat.value.toLowerCase() == window.document.formcrit.plat2.value.toLowerCase()) {
				alert("Le plat n°2 ne doit pas être égal au plat n°1.");
				return false;
			}
		}
		else if ($("#modeRechAvancee").val() == "2") {
			window.document.formcrit.plat.value = $.trim(window.document.formcrit.plat.value);
			window.document.formcrit.vin.value = $.trim(window.document.formcrit.vin.value);
			
			if (window.document.formcrit.plat.value == "" || window.document.formcrit.vin.value == "") {
				alert("Veuillez saisir un nom de plat ET un nom de vin.");
				return false;
			}		
			if (window.document.formcrit.plat.value != "" && window.document.formcrit.plat.value.length <= 2) {
				alert("Pour une recherche plus pertinente, veuillez saisir un nom de plat d'au minimum 3 caractères.\nActuce : mettez le nom du plat au pluriel pour gagner une lettre !");
				return false;
			}
			if (window.document.formcrit.vin.value != "" && window.document.formcrit.vin.value.length <= 3) {
				alert("Pour une recherche plus pertinente, veuillez saisir un nom de vin d'au minimum 4 caractères.");
				return false;
			}
		}
    }
    return true;
}

