$(document).ready(function() 
{
	$("img[rel='#lightBox']").overlay();

	$("a[rel='useFul']").click(function() 
	{
		var objet = this;
		var info = objet.id;
		var tabHref = $(objet).html().split(" ");
		
		$.ajax({
   		type: "GET",
   		url: info,
   		error:function(msg){
     		alert( "Error !: " + msg );
   		},
   		success:function(data)
		{
   			//affiche le contenu du fichier dans le conteneur dédié
			$(objet).text(tabHref[0]+" "+data);
		}});
	});

	$("a[rel='modifCaddie']").click(function()
        {
                var objet = this;
                var info = objet.id;

                $.ajax({
                type: "GET",
                url: info,
		dataType: "xml",
                error:function(msg){
                alert( "Error !: " + msg );
                },
                success:function(xml)
                {
			$(xml).find('reponse').each(function()
			{
				objet.id = $(xml).find('id').text();
                        	//affiche le contenu du fichier dans le conteneur dédié
                        	$(objet).text($(xml).find('data').text());
			});
                }});
        });


	$("a[rel='modifFollowed']").click(function()
        {
		var objet = this;
		var info = objet.id;
		var typeModif = "";
		var li;
		var move;
		var parentThis;
		
		parentThis = $(this).parent().parent();
		
		if(parentThis.attr("rel") == "follower")
                {
                        li = parentThis;
                        move = true;
		}

                if(parentThis.attr("rel") == "followed")
		{
                        li = parentThis;
                        move = false;
		}
	
		$.ajax({
                type: "GET",
                url: info,
                dataType: "xml",
                error:function(msg){
                alert( "Error !: " + msg );
                },
                success:function(xml)
                {
                        $(xml).find('reponse').each(function()
                        {
                                objet.id = $(xml).find('id').text();
                                //affiche le contenu du fichier dans le conteneur dédié
                                $(objet).text($(xml).find('data').text());
				typeModif = $(xml).find('type').text();
			});
		},
		complete :function(originalRequest)
		{
			if($("ul[rel='followed']").html() != null && $("ul[rel='follower']").html() != null && $(objet).attr("rev") != "false")
			{
				if(move)
				{
					if(typeModif != 0)
					{
						intNewFollowed = parseInt($("#nbFollowed").html()) + 1;
						$("#nbFollowed").html(intNewFollowed);
						$("ul[rel='followed']").append('<li rel="followed" id="'+li.attr('id')+'">'+li.html()+'</li>');
					}
					else
					{
						intNewFollowed = parseInt($("#nbFollowed").html()) - 1;
						$("#nbFollowed").html(intNewFollowed);
						$("ul[rel='followed'] li[id='"+li.attr('id')+"']").remove();	
					}
				}
				else
				{
					intNewFollowed = parseInt($("#nbFollowed").html()) - 1;
                                        $("#nbFollowed").html(intNewFollowed);
					$("ul[rel='follower'] li[id='"+li.attr("id")+"']").html(li.html());
					li.remove();
				}
			}
		}
		});
	});

	$("a[id=actionNotLogged],area[id=actionNotLogged]").click(function()
	{
		
		$("#formNotLogged #notLoggedAction").attr("value",$(this).attr("rel"));
		$("#formNotLogged #notLoggedMessage").attr("value",$(this).attr("alt"));
		$("#formNotLogged #notLoggedRedirection").attr("value",$(this).attr("rev"));
		$("#formNotLogged").submit();
	});

	$("select[name=triResult]").change(function()
	{
		$("#triResult").submit();
	});

	$("a[name=pagination]").click(function()
        {
		$("#triResult").attr("action",$(this).attr("rel"));
		$("#triResult").submit();
	});	

	$("a[rel=suppTn]").click(function()
	{
		$tabInfo = $(this).attr("alt").split("_");
		url = $tabInfo["0"];
		$tn_id = $tabInfo["1"];
		data = "tn_id="+$tn_id;
		
		$.ajax({
                type: "POST",
                url: url,
		data: data,
                success:function(data)
                {
			alert(data);
		}});
	});

	$("input[name=validerFormSn]").click(function()
	{
		var data = "";
		var and = "";
		var field = $("#socialNetworkAccess").serializeArray();
		var url = $("#socialNetworkAccess").attr("url");
		jQuery.each(field, function(i, field)
                {
	                if(field.value != "")
                        {
        	                data += and+field.name+"="+field.value;
                                and ="&";
                        }
                });
		
		$.ajax({
                type: "POST",
                url: url,
                data: data,
                success:function(data)
                {
			$("#resultValid").html(data);
			//$("#resultValid").html("<img src='http://img444.imageshack.us/img444/5623/yes6gi.png' />");
                },
		error:function (xhr, ajaxOptions, thrownError){
			$("#resultValid").html("<img src='http://img444.imageshack.us/img444/7303/no5fg.png' />");
		}});
	});

});


