function nl2br (str, is_xhtml) {
	if(is_xhtml="undefined")is_xhtml=false;
    var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '' : '<br>';
    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
}
String.prototype.br2nl =
  function() {
    return this.replace(/<br\s*\/?>/mg,"\n");
  };
function logged_as_admin(){
	if($("#content").find(".logged_as_admin").text()=="true")return true;
	else return false;
}
$(document).ready(function() {
	
	$('#contenu_actu').markItUp(mySettings);
	
	function defiler(){
		var first_div = $('.dernieres_photos').find('div:first');
		var first_div_html = first_div.html();
		
		var scroll_value = -$('.dernieres_photos').find('div:first').outerHeight(true);

		$('.dernieres_photos').animate({
			top: scroll_value
			}, 6000, 'linear', function() {
				$('.dernieres_photos').append('<div>'+first_div_html+'</div>');
				$('.dernieres_photos').css('top','0px');
				first_div.remove();
				defiler();
		});
		$('.dernieres_photos').children('div:eq(3)').css('opacity','0');
		$('.dernieres_photos').children('div:eq(2)').animate({
			opacity: 1
			}, 1500, function() {
		});
	}
	defiler();
	
	$('#messages').delay(2000).animate({
		opacity: 0
		}, 1500, function() {
		$(this).remove();
	});
	if(!logged_as_admin())
	{
		$('.actualite p').truncatable({	limit: 300, more: '<br/><br/>[Afficher la suite]', less: true, hideText: '<br/><br/>[Cacher]' }); 
	}
	$('.partie_livre h3 img').click(
		function(){
			var commentaire_id = $(this).parent().children('.hidden').text();
			var image = $(this);
			
			if(image.attr('src')=='/assets/images/valid.png')
			{
				$.ajax({
				   url: "livre/valider/"+commentaire_id,
				   success: function(){
				   		image.attr('src','/assets/images/delete.png');
				   }
				});
			}
			else
			{
				$.ajax({
				   url: "livre/supprimer/"+commentaire_id,
				   success: function(){
				   		image.parent().parent().remove();
				   }
				});
			}
		}
	);
	
    $(".dialog, .formulaire_mail").dialog({ 
		autoOpen: false,
		resizable: false,
		bgiframe: true,
		autoOpen: false,
		height: 'auto',
		width: 'auto',
		modal: true
	});
	$('.fb-comments').hide();
	$('.openFb-comments').click(function(){
		$(this).hide();
		if($(this).data('openedonce')=='true')
		{
			$(this).next('.fb-comments').show();
		}
		else
		{
			$(this).next('.fb-comments').prepend('Commentez sur facebook : <div title="Fermer" class="closeFb-comments"></div>').show();
			$(this).data('openedonce','true');
		}
	});
	$(".openDialog").click(function(){
		$('.dialog').dialog( "option", "title", 'Actualité' );
		$('.dialog').find('input[type=text], textarea').val('');
		$('.dialog').find('#image').replaceWith('<input name="image" id="image" type="file">');
		$('.dialog').find('option:nth-child(7)').attr('selected', 'selected').parent('select');
		$(".dialog").dialog('open');
	});
	$(".openFormulaire_mail").click(function(){$(".formulaire_mail").dialog('open');});
	
	$('.modifier').click(function(){
		var id_actu;
		
		id_actu = $(this).text();
		$('#idactu').val(id_actu);
		
		$.get("recuperer/"+id_actu, 
			function(data)
			{				
				$('.dialog').dialog( "option", "title", 'modifier : '+data.titre );
		
				$('#titre').val(data.titre);
				$('#type').val(data.type);
				$('#contenu_actu').val(data.contenu);
				
				$('.dialog').dialog('open');
		    },
		"json").error(function() { alert("erreur"); });

	})
	$(".supprimer").click(
		function(){
			i_actu = $(this).text();
			actu = $(this).parent().parent().parent();
			
			$.ajax({
			   	url: "supprimer/"+id_actu,
			   	success: function(){
					actu.animate({
						height: 0,
						opacity: 0
					  }, {
						duration: 1000,
						complete: function() {
						  actu.remove();
						}
					  });
			   },
				error:function (xhr, ajaxOptions, thrownError){
	             	//alert(xhr.status + ' - ' + thrownError);
	           	}
			 });
 
		}
	);
	
	$(".liste_albums, .liste_photos, .liste_videos").find("img").hover(
		function(){
			$(this).stop();
			$(this).animate({opacity: 1}, {duration: 300});
		}
	);
	$(".liste_albums, .liste_photos, .liste_videos").find("img").mouseout(
		function(){
			$(this).stop();
			$(this).animate({opacity: 0.6}, {duration: 300});
		}
	);
});

//LIVE (gère le contenu ajouté à la volée) 

$('.closeFb-comments').live("click",function(){
		$(this).parent().hide();
		$(this).parent().prev('.openFb-comments').show();
});

