﻿$(function() {
		$("#formupload").attr('style','display: block;');
		$("#js_inactif").attr('style','display: none;');
});

$(function() {
	$(".o_creative").click(function(){
		$("#licence").attr('style','display: none;');
		$(".r_creative").attr('style','display: block;');
		$(".o_creative").attr('style','display: none;');
		$('#licence').slideDown(1000, "easeOutQuint");
	});
});

$(function() {
	$(".r_creative").click(function(){
		$("#licence").attr('style','display: block;');
		$(".o_creative").attr('style','display: block;');
		$(".r_creative").attr('style','display: none;');	
		$('#licence').slideUp(1000, "easeOutQuint");
	});
});

$(function() {
	$("#envoyer").click(function(){
		valid = true;
		$("#erreur").attr('style','display: none;');
		if(($("#id_user").val() == "") || ($("#titre").val() == "") || ($("#description").val() == "") || ($("#region").val() == "") || ($("#choix_licence").val() == "")){
		$("#erreur").attr('style','display: block;');
		valid = false;
		}
		else if(!$("#titre").val().match(/^[a-z0-9-_àáâãäåçèéêëìíîïðòóôõöùúûüýÿ\ ]{1,}$/i)){
		$("#verification").attr('style','display: block;');
		valid = false;
		}
		return valid;
		});
});


$(function() {
	$("#envoyer").click(function(){
		if (valid){
			$.ajax({
			   cache: false,
			   url: "../wp-content/themes/objectif-terre/fonction.php",
			   type: "POST",
			   data: ({
					action		  : 'createVar',
					id_user		  : $("#id_user").val(),
					titre		  : $("#titre").val(),
					description   : $("#description").val(),
					id_region     : $("#id_region").val(),
					choix_licence : $('input[type=radio][name=choix_licence]:checked').attr('value')
				}),
				  success: function(response){
						$("#message").html(response);
						$("#message").attr('style','display: block;');
						$("#uploadphoto").attr('style','display: block;');
						$("#formupload").attr('style','display: none;');
						$("#titre").val("");
						$("#description").val("");
						$("#region").val("");
						$("#choix_licence").val("");
					}
			});
		}
	});		
});

$(document).ready(function() {
	$("#photo").uploadify({
		'uploader'       : '../wp-content/themes/objectif-terre/js/uploadify.swf',
		'script'         : '../wp-content/themes/objectif-terre/fonction.php',
		'cancelImg'      : '../wp-content/themes/objectif-terre/images/cancel.png',
		'folder'         : '../wp-content/upload',
		'queueID'        : 'fileAttente',
		'auto'           : false,
		'multi'          : false,
		'buttonText'     : 'Parcourir',
		'fileDesc'       : 'Fichiers au format .jpg',
		'fileExt'        : '*.jpg',
		'sizeLimit'      : 1024000,
		'wmode'          : 'transparent',
		'onComplete'     : function(event, queueID, fileObj, response, data) {
							$("#image").html(response);
							$("#image").fadeIn(1000);
							$("#uploadphoto").attr('style','display: none;');
							$("#upload_sauv").attr('style','display: block;');
							}
	});
});

$(function() {
	$("#upload_valider").click(function(){
		$('#photo').uploadifySettings('scriptData', {
		'action': 'insertPhoto',
		'titre': $('#titre_hidden').val(),
		'description': $('#description_hidden').val(),
		'id_region': $('#id_region_hidden').val(),
		'id_user': $('#id_user_hidden').val(),
		'id_licence': $('#id_licence_hidden').val()
		});
		$('#photo').uploadifyUpload();
		});
});


$(function() {
	$("#upload_supp").click(function(){
		$('#photo').uploadifyClearQueue();
		});
});

$(function() {
	$("#supprimer").click(function(){
		$.ajax({
			   cache: false,
			   url: "../wp-content/themes/objectif-terre/fonction.php",
			   type: "POST",
			   data: ({
					action  : 'supprimerPhoto',
					titre : $("#titre_hid").val()
				}),
				  success: function(response){
						$("#image").fadeOut(1000);
						$("#erreur_form_bis").attr('style','display: none;');
						$("#upload_sauv").attr('style','display: none;');
						$("#uploadphoto").attr('style','display: block;');
					}
			});
		});
});


$(function() {
	$("#sauvegarder").click(function(){
	$.ajax({
			   cache: false,
			   url: "../wp-content/themes/objectif-terre/fonction.php",
			   type: "POST",
			   data: ({
					action  : 'enregistrer',
					id_user		  : $("#id_user_hid").val(),
					titre		  : $("#titre_hid").val(),
					description   : $("#description_hid").val(),
					id_region     : $("#id_region_hid").val(),
					id_licence    : $("#id_licence_hid").val(),
					temps         : $("#temps").val(),
				}),
				  success: function(response){
						$("#message_fin").html(response);
						$("#message_fin").attr('style','display: block;');
						$("#image").attr('style','display: none;');
						$("#supprimer").attr('style','display: none;');
						$("#remarque").attr('style','display: none;');
						$("#sauvegarder").attr('style','display: none;');
						$("#annuler").attr('style','display: none;');
					}
			});
	});
});
