function showObligate(){
		$(".textObligate").hide();
		$(".textObligate").text("Merci de renseigner tous les champs obligatoires");
		$(".textObligate").css('display', 'inline');
		$(".textObligate").fadeIn(500);
}


function onReady(){
	
	$('.aCommander').click( function(){
		var srcIMG = $(this).parent().prev().prev().children().attr('src');
		var typeCD = srcIMG.substr('-7', '3');
		var refCD = $(this).parent().parent().attr('id');
		$(this).attr('href', 'commande.php?src=' + srcIMG + '&type=' + typeCD + '&ref=' + refCD);
		//window.location.replace("http://localhost/dites-le/toto/" + page);
	});
	
	
	//Page voeux.php, click sur les onglets pour changer l'image des types de cd
		$('.bnt span').click( function(){
			//alert($(this).attr('class'));
			$(this).parent().parent().attr('id') + $(this).attr('class');
		
			var holdsrc = $(this).parent().prev().children().attr('src'); //Ancien path
			var path = holdsrc.split('/'); // decoupe du path
			var rep = $(this).parent().parent().attr('id'); // Repertoir 
			var type = $(this).attr('class'); // Type (CDS, CDR ou SCD)
		
			$(this).parent().prev().children().hide();
			$(this).parent().prev().children().attr('src', path[0]+'/'+path[1]+'/'+rep+type+'.jpg')
			$(this).parent().prev().children().fadeIn(1000);
		
			$(this).parent().css('background', 'url(img/bnt_voeux_'+ type +'.png)');
		});

	$('#bouche').Draggable();
	
	
	//Page index.php, popup sur les images
	$('.nyroModal').nyroModal({});
	$.nyroModalSettings({
		galleryLinks: '<a href="#" class="nyroModalPrev">Prev</a><a href="#" class="nyroModalNext">Next</a>',
		addImageDivTitle: true,
		bgColor: '#6b6c55',
		galleryLinks: '<a href="#" class="nyroModalPrev">Prev</a><a href="#"  class="nyroModalNext">Next</a>', 
		closeButton: '<a href="#" class="nyroModalClose" id="closeBut" title="fermer">Fermer</a>', 
	});
	//*
	
	//Page contact.php et voeux.php, class uploadify
	$("#fileToUpload").uploadify({ 
			'width'			 : '120',
			'uploader'       : 'lib/uploadify.swf',
			'script'         : 'sendmail.php',
			'cancelImg'      : 'ico/cancel.png',
			'folder'         : '../fic',
			'queueID'        : 'fileQueue',
			'buttonText'     : 'Parcourir',
			'buttonImg'	     : 'img/parcourir.jpg',
			'fileDesc'		 : '',
			'fileExt'		 : '*.*',
			'sizeLimit'		 : '3000000',
			'auto'           : false,
			'multi'          : false,
	});
	//*/
	
	//Page contact, envoi de la candidature.
	$("#sendCandidature").click( function(){
		$(".textObligate").hide();
		$(".obligate").css('color', '#6b6c55');
		var error = 0;
		
		var civilite = document.getElementById("civilite").value;
		var nom = document.getElementById("nom").value;
		var prenom = document.getElementById("prenom").value;
		var mail = document.getElementById("mail").value;
		var filename = $("#fileQueue .fileName").text();
	
		if (nom == ""){error++; $(".nom").css('color', '#ff0000');showObligate()}
		if (prenom == ""){error++; $(".prenom").css('color', '#ff0000');showObligate()}
		if (mail == ""){error++; $(".mail").css('color', '#ff0000');showObligate()}
		if (filename == ""){error++; $(".cv").css('color', '#ff0000');showObligate()}
		
		if (error == 0){
			var newSetting = "sendmail.php?civilite=" + civilite + "&nom=" + nom + "&prenom=" + prenom + "&mail=" + mail;
			jQuery('#fileToUpload').uploadifySettings('script', newSetting);
			jQuery('#fileToUpload').uploadifyUpload()
		}
	});
	
	//Page commande.php, envoi de la candidature.
	
	function showFin(){
		$('#choixCD').hide();
		$('#formulaire').hide();
		$('#fin').fadeIn(750);
	}
	
	$("#sendCommande").click( function(){
		$(".textObligate").hide();
		$(".obligate").css('color', '#6b6c55');
		var error = 0;

		var nom = document.getElementById("nom").value;
		var prenom = document.getElementById("prenom").value;
		var rsocial = document.getElementById("rsocial").value;
		var adresse = document.getElementById("adress").value;
		var cpville = document.getElementById("cpville").value;
		var phone = document.getElementById("phone").value;
		var mail = document.getElementById("mail").value;
		var contactto = $("input[name=contactto]:checked").attr('id')
		
		var filename = $("#fileQueue .fileName").text();
		
		var ref = $("#modele option:selected").text();
		var typeCD = $("#forme option:selected").attr('value');
		var qte = document.getElementById("qte").value;
	
		if (nom == ""){error++; $(".nom").css('color', '#8c0741');}
		if (prenom == ""){error++; $(".prenom").css('color', '#8c0741');}
		if (cpville == ""){error++; $(".cpville").css('color', '#8c0741');}
		if (phone == ""){error++; $(".phone").css('color', '#8c0741');}
		if (mail == ""){error++; $(".mail").css('color', '#8c0741');}
		
		if (error != 0){showObligate()}
		
		if (error == 0){
			var script = "sendCommande.php"
			var data = "nom=" + nom + "&prenom=" + prenom + "&rsocial=" + rsocial + "&adresse=" + adresse + "&cpville=" + cpville + "&phone=" + phone + "&mail=" + mail + '&qte=' + qte + '&contactto=' + contactto + '&ref=' + ref + '&typeCD=' + typeCD;
			
			//Si aucun fichier n'est joint, on envoi le formulaire en ajax
			if (filename == "")
			{
				$.ajax({
					type: 		"GET",
					url: 		script+"?"+data+"&havefile=false",
					complete : function(){
						$('#choixCD').hide();
						$('#formulaire').hide();
						$('#fin').fadeIn(750);
					}
				});
			}else{
			//Sinon via Uploadify.js
			jQuery('#fileToUpload').uploadifySettings('script', script+"?"+data+"&havefile=true");
			jQuery('#fileToUpload').uploadifyUpload()
			}
		}
	});
	
	$("#modele").change(function(){
		var rep = $("#modele option:selected").attr('name');
		var img = $("#modele option:selected").text();
		var forme = $("#forme option:selected").attr('value');
		//alert('img/' + rep + '/' + img + '/' + img +''+ forme +'.jpg');
		$('.thumb').hide();
		$('.thumb').attr('src', 'voeux/' + rep + '/' + img + forme +'.jpg');
		$('.thumb').fadeIn(1000);
	});
	
	$("#forme").change(function(){
		var rep = $("#modele option:selected").attr('name');
		var img = $("#modele option:selected").text();
		var forme = $("#forme option:selected").attr('value');
		//alert('img/' + rep + '/' + img + '/' + img +''+ forme +'.jpg');
		$('.thumb').hide();
		$('.thumb').attr('src', 'voeux/' + rep + '/' + img + forme +'.jpg');
		$('.thumb').fadeIn(1000);
	});
	
}