$.ajaxSetup({async: true} );

function empty(mixed_var)
{
    if (mixed_var === "" 
        || mixed_var === 0   
        || mixed_var === "0"
        || mixed_var === null  
        || mixed_var === false
        || mixed_var === undefined    
        || ((typeof mixed_var == 'array' || typeof mixed_var == 'object') && mixed_var.length === 0) ){
        return true;
    }
    return false;
}

function isset() {
    var a=arguments; var l=a.length; var i=0;
    
    if (l==0) { 
        throw new Error('Empty isset'); 
    }
    
    while (i!=l) {
        if (typeof(a[i])=='undefined' || a[i]===null) { 
            return false; 
        } else { 
            i++; 
        }
    }
    return true;
}

function affAccueil()
{
	$("#Accueil").load('visite/accueil.php');
	switchToDiv("#Accueil");
}

function affGICAN()
{
	$("#GICAN").load('visite/gican.php');
	switchToDiv("#GICAN");
}

function affPresse()
{
	$("#Presse").load('visite/presse.php');
	switchToDiv("#Presse");
}

function affListeClasse()
{
	$("#resultatRecherche").load('visite/liste_classes.php');
	switchToDiv("#resultatRecherche");
}

function affListeSoc()
{
	$("#ListeSociete").load('visite/liste_soc.php');
	switchToDiv("#ListeSociete");
}

function affListeProd()
{
	$("#ListeProduit").load('visite/liste_prod.php');
	switchToDiv("#ListeProduit");
}

function affRecherche()
{
	$.ajax({
		url : "visite/recherche.php",
		success : function (data)
		{
			$("#Recherche").html(data);
			$("#checkSoc").bind('click', function(e)
			{
				if ($(this).attr("checked"))
				{
					$("#checkCont").attr('disabled', false);
					$("#checkActiv").attr('disabled', false);
				}
				else
				{
					$("#checkCont").attr('disabled', true);
					$("#checkActiv").attr('disabled', true);
					$("#checkCont").attr('checked', false);
					$("#checkActiv").attr('checked', false);
				}
			});
			$("#checkProd").bind('click', function(e)
			{
				if ($(this).attr("checked"))
				{
					$("#checkMiss").attr('disabled', false);
					$("#checkDesc").attr('disabled', false);
				}
				else
				{
					$("#checkMiss").attr('disabled', true);
					$("#checkDesc").attr('disabled', true);
					$("#checkMiss").attr('checked', false);
					$("#checkDesc").attr('checked', false);
				}
			});
			$("#formRecherche").bind('submit', function(e){ affResRecherche(); return false; });
		}
	});
	switchToDiv("#Recherche");
}

function affProd(id, back)
{
	$.ajax({
		url : "visite/ficheProduit.php",
		type : "POST",
		data : "id="+id+"&back="+back,
		success : function (data)
		{
			$("#FicheProduit").html(data);
			$("#logo").bind('load', function()
			{
				if($("#logo").height() > 80)
				{
					var pcent = 8000/$("#logo").height();
					$("#logo").height(80);
					$("#logo").width($("#logo").width()*pcent/100);
				}
			});
		}
	});
	switchToDiv("#FicheProduit");
}

function affSoc(id, back)
{
	$.ajax({
		url : "visite/ficheSociete.php",
		type : "POST",
		data : "id="+id+"&back="+back,
		success : function (data)
		{
			$("#FicheSociete").html(data);
			$(".vigLogo").each(function(i)
			{
				//$(this).html(chaine);
				$(this).children("#logo").bind('load', function()
				{
					if ($(this).height() > 80)
					{
						var pcent = 8000 / $(this).height();
						$(this).height(80);
						$(this).width($(this).width() * pcent / 100);
					}
				});
			});
		}
	});
	switchToDiv("#FicheSociete");
}

function affResRecherche(soc_id)
{
	var post = "";
	if(isset(soc_id) && !empty(soc_id))
	{
		post+= "soc_id="+soc_id;
	}
	else
	{
		post+= "valider=recherche";
		if($("#checkSoc").attr('checked') && !empty($("#motclef").val()))
		{
			post+= "&nomSoc="+$("#motclef").val();
			if($("#checkCont").attr('checked')) { post+= "&contact=1"; }
			if($("#checkActiv").attr('checked')) { post+= "&activite=1"; }
		}

		if($("#checkProd").attr('checked') && !empty($("#motclef").val()))
		{
			post+= "&nomProd="+$("#motclef").val();
			post+= "&motcle="+$("#motclef").val();
			if($("#checkMiss").attr('checked')) { post+= "&mission=1"; }
			if($("#checkDesc").attr('checked')) { post+= "&desc=1"; }
		}
	}

	$.ajax({
		url : "visite/affResRecherche.php",
		type : "POST",
		data : post,
		success : function (data) { $("#resultatRecherche").html(data); }
	});
	switchToDiv("#resultatRecherche");
}

function affPhoto(photo, back)
{
	$.ajax({
		url : "visite/photo.php",
		type : "POST",
		data : "back="+back,
		success : function (data)
		{
			$("#Photo").html(data);
			$("#Photo").find("#zoomPhoto").attr("src", $(photo).attr("src"));
			$("#Photo").find("#legende").html($(photo).attr("alt"));
		}
	});
	switchToDiv("#Photo");
}

function affVideo(video, back)
{
	$.ajax({
		url : "visite/video.php",
		type : "POST",
		data : "back="+back+"&video="+video,
		success : function (data) { $("#Video").html(data); }
	});
	switchToDiv("#Video");
}

function switchToDiv(div)
{
	$("#Accueil").hide();
	$("#ListeProduit").hide();
	$("#FicheProduit").hide();
	$("#ListeSociete").hide();
	$("#FicheSociete").hide();
	$("#resultatRecherche").hide();
	$("#Recherche").hide();
	$("#Presse").hide();
	$("#GICAN").hide();
	$("#Photo").hide();
	$("#Video").html("<br />");
	$("#Video").hide();
	$(div).show();
}
