var delaiRech = 500;

$(document).ready(function() {
	$("#btRech").click(function() { 
		if (valideRech()) {
			afficheWait();
			$("#rech").val("rech");
			setTimeout('$("#formcrit").submit();', delaiRech);
		 }
		 return false;
	});
	
	$("#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").change(function() { 
		afficheWait();
		setTimeout('$("#formfiltre").submit();', delaiRech);
	});
	
	$("#fTypePlatParVin, #fPaysParVin, #fNomAppellation").change(function() { 
		afficheWait();
		setTimeout('$("#formfiltrevin").submit();', delaiRech);
	});
	
	$("#dialog").dialog({
		autoOpen: false,
		resizable: false,
		width: 500,
		height: 300,
		modal: true,
		draggable: true,
		buttons: {
			Close: function() {
				$(this).dialog('close');
			}
		},
		show: 'clip',
		hide: 'clip'

	});
	
	$("img[name^='img_affiliation_vin']").click(function(event) {
		$("#dialog").load("inc/affiliation-vin-popup-en.inc.php?idwinepopup="+event.currentTarget.name, function() {
			$('#dialog').dialog('open');
			return false;
		
		});	
	}).qtip({
		position: {
			corner: { target: 'leftBottom', tooltip: 'rightTop'}
		},
		style: { name: 'light', width: 330, textAlign: 'center', tip: true, border: { radius: 4 } }
	});
	
	$(".IB").click(function(event) {
			return true;
	}).qtip({
		position: {
			corner: { target: 'leftBottom', tooltip: 'rightTop'}
		},
		style: { name: 'light', width: 330, textAlign: 'center', tip: true, border: { radius: 4 } }
	});
	$(".IB2").click(function(event) {
			return true;
	}).qtip({
		position: {
			corner: { target: 'bottomRight', tooltip: 'leftTop'}
		},
		style: { name: 'light', width: 300, textAlign: 'left', tip: true, border: { radius: 4 } }
	});
	
	$("#dialog_add_accord_vin").dialog({
		autoOpen: false,
		resizable: false,
		width: 650,
		height: 480,
		modal: true,
		draggable: true,
		buttons: {
			Close: function() {
				$(this).dialog('close');
			}
		},
		show: 'clip',
		hide: 'clip'
	});
	
	$("#dialog_add_accord_plat").dialog({
		autoOpen: false,
		resizable: false,
		width: 720,
		height: 511,
		modal: true,
		draggable: true,
		buttons: {
			Close: function() {
				$(this).dialog('close');
			}
		},
		show: 'clip',
		hide: 'clip'
	});
	
	$("img[name='Actions']").click(function(event) {
		return true;
	}).qtip({
		position: {
			corner: { target: 'bottomRight', tooltip: 'leftTop'}
		},
		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 on this Wine',
				button: 'Close'
			}
		},
		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) {
		var pIdVin = this.id;

		$("#dialog_add_accord_vin").load("inc/add-accord-vin-popup-en.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-en.inc.php?idwinepopup="+this.id, function() {
			$('#dialog').dialog('open');
			return false;
		});
	});
	
	$("td[id^='ActionsPlat_']").click(function(event) {
		return true;
	}).qtip({
		content: {
			prerender: true,
			title: {
				text: 'Actions on this Food',
				button: 'Close'
			}
		},
		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) {
		var pIdPlat = this.id;
	
		$("#dialog_add_accord_plat").load("inc/add-accord-plat-popup-en.php?idplat="+this.id, function() {
			$('#dialog_add_accord_plat').dialog('open');
			return false;
		});
	});
	
	$("#site").bind('copy', function(e){ return false; });
});

function afficheWait() {
	$.blockUI({
		message: $('#pleasewait'),
		timeout: 10000,
		css: { 	'border-radius': '10px',
				'-webkit-border-radius': '10px', 
				'-moz-border-radius': '10px', 
				opacity: .7
		}
	});
}

function valideRech() {
    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("You must enter a wine name or a food name.");
        return false;
    }
    if (window.document.formcrit.vin.value != "" && window.document.formcrit.plat.value != "") {
        window.document.formcrit.plat.value = "";
        alert("WARNING, you have entered a wine name AND a food name.\nBy default the search will be made on the wine name.");
    }
    if (window.document.formcrit.vin.value != "" && window.document.formcrit.vin.value.length <= 3) {
		alert("You must enter a wine name of 4 characters minimum.");
        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("You must enter a food name of 3 characters minimum.");
        return false;
    }
    return true;
}

