$(function() {
	
	
	$('.postulate_btn').click(function() {
		$('#main .thanks').remove();
		
		$.facebox({ ajax: '/nartex/form.php?id=' + $(this).attr('rel') });
	});
	
	
	$('form.postulate').live('submit', function(event) { 
		 event.preventDefault();
		// submit the form 
	    $(this).ajaxSubmit({
	    	iframe: true,
	    	dataType: 'json',
	    	
	    	beforeSubmit: function() {
	    		$('p.submit, p.wait').toggle();
	    		$('#errors').hide();
	    	},
	    	
	    	
	    	success: function(json) {
	    		$('p.submit, p.wait').toggle();
	    		//console.log(json);
	    		if(json.success) {
		    		$.facebox.close();
		    		$('#main').prepend("<p class='thanks'>Merci pour votre candidature</p>")
		    		$.scrollTo($('#main'), 200);
	    		}
	    		else {
	    			var $errors = $('#errors');
	    			$errors.html("").show();
					$.each(json.errors, function(i, v) {
						$errors.append('<li>' + v + "</li>");
					});
					
					$.scrollTo($('form.postulate'), 200);
	    		}
	    	}
	    }); 
	    // return false to prevent normal browser submit and page navigation 
	    return false; 
	});
	
	
	
});
