$(document).ready(function(){
	//charcounter des commentaires des fiches (250 caractères max)
	$("#description").charCounter(250, {
		container: "#charcounter_comm"
	});
						   
	//gestion des textes par défaut dans le commentaire et le permalien dailymotion
	$("#description").focus(function(){
		if($(this).val() == "commentaire")
			$(this).val('');
	})
	.blur(function(){
		if($(this).val() == "")
			$(this).val('commentaire');
	});
											
	$("#video_url").focus(function(){
		if($(this).val() == "http://")
			$(this).val('');
		$("#typeVideoDM").attr("checked", true);
	})
	.blur(function(){
	   	if($(this).val() == "")
		{
			$(this).val('http://');
			$("#typeVideoDM").attr("checked", false);
		}
	});
			
	$("#btn_valider_comm").live("click", function(e){
		e.preventDefault();

		s = $("#form_comm").serialize();
		$.ajax({
                        type: "POST",
                        data: s,
                        url: "/ajax/graph/check_graph.php",
                        dataType : "json",
                        error: function(msg, string){
                                return false;
                        },
                        success: function(data, status){
                                if(typeof(data.retour) != 'undefined')
                                {
                                        if (data.retour == "0")
                                        {
						if($(":radio:checked").length)
                				{
                        				if($(":radio:checked").attr("id") == "typeVideoDM")
                        				{
                                				uploadDone(0);
                        				}
                        				else
                        				{
                                				swfUploadLoaded();
                        				}
                				}
                				else
                				{
                        				uploadDone(0);
                				}

						hideerr();
                                        }
                                        else
                                        {
						if (data.retour == "2")
						{
							window.location = "/login.php";
						}
						else
							afferr(data.error);
                                        }
                                }
                        }
                });

		return false;
	});

	//gestion des pieces jointes	   
	$(".contenuDuCommPosteRight").live("click",function(e){
		e.preventDefault();

		tpj = $(this).children("p").children("a").attr("val");

		//si la piece jointe est cachée, on la montre, sinon on la cache (+ modif css pour afficher les img en bloc)
		if($(this).siblings(".pj_comm").css("display") == "none")
		{
			$(this).siblings(".pj_comm").show();

			if(tpj == "2")
			{
				$(this).siblings(".pj_comm").css("display", "block");
				$(this).children("p").children("a").html("Masquer la photo");
			}
			else
				$(this).children("p").children("a").html("Masquer la vidéo");
		}
		else
		{
			$(this).siblings(".pj_comm").hide();

			if(tpj == "2")
				$(this).children("p").children("a").html("Voir la photo");
			else
				$(this).children("p").children("a").html("Voir la vidéo");
		}

		//on cache toutes les autres pieces jointes déployées
		$(".contenuDuCommPosteRight").not(this).siblings(".pj_comm").hide();

		$(".contenuDuCommPosteRight").not(this).children("p").children("a").each(function(){
			if($(this).attr("val") == "1")
				$(this).html("Voir la vidéo");
			else
				$(this).html("Voir la photo");
		});

		return false;
	});

	$(".supprimer_comm").live("click",function(e){
		$.ajax({
                        type: "POST",
                        data: "graph_id=" + escape($(this).attr("val")),
                        url: "/ajax/graph/del_graph.php",
                        dataType : "json",
                        error: function(msg, string){
                                return false;
                        },
                        success: function(data, status){
                                if(typeof(data.retour) != 'undefined')
                                {
                                        if (data.retour == "0")
                                        {
						hideerr();
						c = data.current - 1;

						$("#commentairesPostes").load("/graph/lc.php", {'graph_entite': $("#graph_entite").val(), 'graph_entite_id': $("#graph_entite_id").val(), 'page': data.current});
                                                $("#GraphPagination").pagination('renumber', {pages: data.pages, current: c});

						if (data.pages <= 1)
							$("#GraphPagination").hide();
                                        }
                                        else
						afferr(data.error);
                                }
                        }
                });

		return false;
	});

});

