var currentid=0;

function dossiers(json)
		{

			content = "<div class=\"dossiers\"><ul>";
			for (i = 0; i < json.documentary_files.length; i++)
			{
				content +=  "<li class=\"item-dossier clearfix\"><div class=\"dossier-col-left\"><div class=\"header-dossier\"><h2> <form id=\"ajaxdossierdochome"+json.documentary_files[i].id+"\" method=\"post\" action=\"/documentary_files/"+json.user+"/"+json.documentary_files[i].id+"/\" ><input type=\"hidden\" id=\"requestType\" name=\"requestType\"  value=\"treemap\"><a   onclick=\"dossierDocHome("+json.documentary_files[i].id+"); return false;\" style=\"cursor:pointer;\">";
				content += json.documentary_files[i].title;
				content += " »</a></form></h2></div><div class=\"dossier-content\"><img src=\"";
				content += json.documentary_files[i].url_image;
				content += "\" alt=\"\" height=\"50px\" width=\"50px\" ><p>";
				content += json.documentary_files[i].description;
				content += " </p></div></div><div class=\"dossier-col-right\"><ul class=\"annotations\"><li><a class=\"share\" href=\"#\"></a></li><li><a class=\"favorite\" href=\"#\"></a></li><li><form id=\"ajaxdeletedossierdoc";
				content += json.documentary_files[i].id;	
				content += "\" action=\"/documentary_file_delete/";
				content += json.documentary_files[i].id;	
				content += "/?next=http://localhost:8000/documentary_files/";
				content += json.user;
				content += "/\" method=\"post\"><a class=\"trash\" onclick=\"deleteDossierDocHome(";
				content += json.documentary_files[i].id;	
				content += "); return false;\"></a></form>";
							
				content += "</li><li><a class=\"edit\" href=\"#\"></a></li></ul><h3>";
				content += json.documentary_files[i].nb_articles;
				content += " articles </h3><ul class=\"links\">";
				
				for (j = 0; j < json.documentary_files[i].articles.length; j++)
				{
					content += "<li><a href=\"";
					content += json.documentary_files[i].articles[j].url_document;
					content += "\">";
					content +=	json.documentary_files[i].articles[j].title;
					content += "</a></li>";		
				}
				content += "</ul></div></li>";					
			}
			content += " </ul></div><div class=\"dossier-similaire\"><div class=\"widget\"><h3><a href=\"#\" class=\"open\">TOUTES MES ANNOTATIONS</a></h3><div class=\"widget-inner\"><ul>";
			for (i = 0; i < json.documentary_files.length; i++)
			{
				for (j = 0; j < json.documentary_files[i].annotations.length; j++)
				{
					content += "<li><a href=\"#\">« ";
					content +=	json.documentary_files[i].annotations[j].text;
					content += "»</a> par cobled</li>";		
				}
			}
			content += "</ul></div></div></div></div></div>";
			$("#right").empty();
			$(content).appendTo("#right");
		}

/*function dossiers(json)
		{
			content = "<div class=\"dossiers\"><ul>";
			for (i = 0; i < json.documentary_files.length; i++)
			{
				content +=  "<li class=\"item-dossier clearfix\"><div class=\"dossier-col-left\"><div class=\"header-dossier\"><h2> <form id=\"ajaxdossierdochome"+json.documentary_files[i].id+"\" method=\"post\" action=\"/documentary_files/"+json.user+"/"+json.documentary_files[i].id+"/\" ><input type=\"hidden\" id=\"requestType\" name=\"requestType\"  value=\"treemap\"><a   onclick=\"dossierDocHome("+json.documentary_files[i].id+"); return false;\" style=\"cursor:pointer;\">";
				content += json.documentary_files[i].title;
				content += " »</a></form></h2></div><div class=\"dossier-content\"><img src=\"";
				content += json.documentary_files[i].url_image;
				content += "\" alt=\"\" height=\"50px\" width=\"50px\" ><p>";
				content += json.documentary_files[i].description;
				content += " </p></div></div><div class=\"dossier-col-right\"><ul class=\"annotations\"><li><a class=\"share\" href=\"#\"></a></li><li><a class=\"favorite\" href=\"#\"></a></li><li> <form id=\"delete-dossierDoc\" method=\"post\" action=\"/documentary_file_delete/";
				content += json.documentary_files[i].id;	
				content += "?next={{request.get_full_path}}\"><input class=\"trash\" type=\"button\"  title=\"Supprimer le dossier documentaire\" value=\"\"></form></li><li><a class=\"edit\" href=\"#\"></a></li></ul><h3>";
				content += json.documentary_files[i].nb_articles;
				content += " articles </h3><ul class=\"links\">";
				
				for (j = 0; j < json.documentary_files[i].articles.length; j++)
				{
					content += "<li><a href=\"";
					content += json.documentary_files[i].articles[j].url_document;
					content += "\">";
					content +=	json.documentary_files[i].articles[j].title;
					content += "</a></li>";		
				}
				content += "</ul></div></li>";					
			}
			content += " </ul></div><div class=\"dossier-similaire\"><div class=\"widget\"><h3><a href=\"#\" class=\"open\">TOUTES MES ANNOTATIONS</a></h3><div class=\"widget-inner\"><ul>";
			for (i = 0; i < json.documentary_files.length; i++)
			{
				for (j = 0; j < json.documentary_files[i].annotations.length; j++)
				{
					content += "<li><a href=\"#\">« ";
					content +=	json.documentary_files[i].annotations[j].text;
					content += "»</a> par cobled</li>";		
				}
			}
			content += "</ul></div></div></div></div></div>";
			$("#right").empty();
			$(content).appendTo("#right");
		}*/

/*<h3><a href=\"#\">DOSSIER SIMILAIRE</a></h3><div class=\"widget-inner\"><ul><li><a href=\"#\">« Election 2012 »</a> par TomeJ</li><li><a href=\"#\">« Election 2012 »</a> par TomeJ</li><li><a href=\"#\">« Election 2012 »</a> par TomeJ</li></ul></div></div><div class=\"widget\">*/

function dossierDocHome(id)
{
	//alert('dossierDocHome');
	alert('dossierDocHome');
	$('#ajaxdossierdochome'+id).submit();	
}

function deleteDossierDocHome(id)
{
	//alert('dossierDocHome');
	currentid = id;
	$('#ajaxdeletedossierdoc'+id).submit();	
}


$(function()
{
	$('#recherche_button_doc').click(function() 
	{
		    currentfromdate = $("#pickeddatebegin").val();
			currenttodate = $("#pickeddateend").val();
			currentText = $("#recherche_id").val();
			currentcategories =[];
			
			$("input:checked").each(function() {
					currentcategories.push($(this).val());
			});
			
			$("input:checkbox:not(:checked)").each(function() {
					    currentcategories.push('');
				});
				alert(currentcategories);
				
				$('<input />').attr('type', 'hidden')
							.attr('name', 'from_date')
							.attr('value', $("#pickeddatebegin").val())
							.appendTo('#ajaxtreemapdoc');
							
			$('<input />').attr('type', 'hidden')
					.attr('name', 'to_date')
					.attr('value', $("#pickeddateend").val())
					.appendTo('#ajaxtreemapdoc');
		
		var i=1;
			$.each(currentcategories, function() 
			 {
				if(this != undefined)
				{
					$('<input />').attr('type', 'hidden')
							.attr('name', 'categories'+i)
							.attr('value', this)
							.appendTo('#ajaxtreemapdoc');
							i++;
				}
				
			 });			
			 
					
		$('#ajaxtreemapdoc').submit();	
		$('#create-dossierDoc').show();
	});
			
	$('#ajaxdossierdochome').submit(function() 
	{
		alert('ajaxdossierdochome10');
		//var urlSubmit = $(this).attr('action');		
	});
	
	$('.trash').click(function (event)
	{ 
	alert('enter');
	var urlSubmit = $(this).attr('href');
     
     $.ajax({
        url: urlSubmit,
		success: function(data) {
					
				alert('ok');	
				}
			});
     return false; //for good measure
});


$('#ajaxdeletedossierdoc'+currentid).submit(function() 
	{
		alert('ajaxdeletedossierdoc'+currentid);
			var urlSubmit = $(this).attr('action');
			$.ajax(
			{  
				type: "POST",
				url: urlSubmit,
				data      : $(this).serializeArray(),
				success: function(data) 
				{
					alert('SUCCESS');
					var json = jQuery.parseJSON(data);
					alert('SUCCESS'+json);
					updatedossierdoc(json);
				}
			});
			return false;
		});
	
	
});//jQuery