function showLibrairieListe()
{
	$('#librairie-liste').show();
	$('#librairie-detail').hide();
	
	$('#librairie-nav').show();
	$('#librairie-detail-retour').hide();
}

function showLibrairieMedia(data)
{
	$('#librairie-liste').hide();
	$('#librairie-detail').show();
	
	$('#librairie-nav').hide();
	$('#librairie-detail-retour').show();
}

jQuery(function($){

	$("#agenda_register_form").bind("submit", function(event) {
		event.preventDefault();
		event.stopPropagation();
		var form = $(this), error;

		form.find(".error").remove();

		var request = $.post(form.attr("action"), form.serialize(), function (data, textStatus, jqXHR)
		{
			$.fancybox('<div class="popup"><div>Merci, nous avons bien enregistré votre demande.</div></div>', {
				showCloseButton  : true,
				scrolling : false
			});

		}, "json");
		request.error(function() {
			var errors = JSON.parse(request.responseText);
			console.log(request, errors);
			for (var index in errors)
			{
				$("#" + errors[index]["id"])
					.before($("<div />", { className: "error" }).html(errors[index]["error"]));
			}

		});
	});

	// API by
	window.APP = {};

	// Menu (scrolling)
	$('#byScrolling li a').each(function(){

		var toId = $(this).attr('rel')
		var dest = $('#'+toId);

		if (dest.length == 0)
			dev.error("La div d'ID #"+ toId +" n'existe pas");
		else {

			$(this).bind('click',function(e){
				e.preventDefault();

				if (window.history) {
					window.history.pushState({ "to_id" : toId }, $(this).text(), "#" + toId);
				}

				$.scrollTo('#'+toId,800, {
					offset: -75, 
					easing: 'easeInOutQuart'
				});
			});
		}
	});
	
	// Popups
	$('.fancy').fancybox();

	// Egalisation des sidebars
	$(window).load(function(){
		$('.sidebar-right').each(function(){
			var currentHeight = $(this).outerHeight();
			var newHeight = $(this).siblings('.sidebar-left:first').outerHeight();
			if (newHeight > currentHeight)
				$(this).height(newHeight - 22);
		});
		
		showLibrairieListe();
		$("#librairie-detail-retour-lien").click(showLibrairieListe);
	});

	$("a#prehome-trigger").fancybox({
		"modal": true,
		"hideOnOverlayClick": true,
		"autoDimensions": false,
		"scrolling": "no",
		"width": 1280,
		"height": 679,
		"onComplete": function() { 
			$("#layout-pre-home")
				.show()
				.find(".close-prehome-link").bind("click", function(event) {
				event.stopPropagation();
				event.preventDefault();

				if ($("#remember-prehome").is(":checked")) {
					console.log("Remember");
					document.cookie = "ignore_prehome=1";
				}

				$.fancybox.close();
			});
		},
		"onCleanup": function() { $("#layout-pre-home").remove(); }
	}).trigger("click").remove();
	
});
