var currentid=0;

function addslashes(ch) {
ch = ch.replace(/\\/g,"\\\\")
ch = ch.replace(/\'/g,"\\'")
ch = ch.replace(/\"/g,"\\\"")
return ch
}

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 title=\"Supprimer le dossier\"><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>";
				
				content += "<li><a class=\"favorite\" href=\"#\"></a></li><li title=\"Modifier le dossier\"><a class=\"edit\" onclick=\"modifyDossierDocHome('";
				content += addslashes(json.documentary_files[i].title);	
				content += "','";
				content += json.user;	
				content += "','";
				content += addslashes(json.documentary_files[i].description);	
				content +="',";
				content += json.documentary_files[i].id
				content += "); return false;\"></a>";							
				content += "</li>";

				//content += "<li title=\"Modifier le dossier\"><a class=\"edit\" href=\"#\"></a></li>";
				
				content += "</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");
		}

/*<div id=\"modify-form\" title=\"Modifier le dossier documentaire\"><form id=\"ajaxdocumentaryfilemodify\" method=\"post\" action=\"/documentary_file_modify/\">{% csrf_token %}<fieldset><label for=\"title\">Titre du dossier</label><input type=\"text\" name=\"title\" id=\"title\" class=\"text ui-widget-content ui-corner-all\" value=\"";
			content += json.documentary_files[i].title;
			content += "\" /><label for=\"description\">Description</label><input type=\"text\" name=\"description\" id=\"description\" value=\"";
			content += json.documentary_files[i].description;
			content += "\" class=\"text ui-widget-content ui-corner-all\" /></fieldset></form></div>*/
			
			
/*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');
	currentid = id;
	$('#ajaxdossierdochome'+id).submit();	
}

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



function modifyDossierDocHome(title,user,description,id)
{
	currentid = id;
	var elem = document.getElementById("descriptionmodif");
	elem.value = description;
	var elem = document.getElementById("titlemodif");
	elem.value = title;
	var elem = document.getElementById("documentary_file_id");
	elem.value = id;
	var elem = document.getElementById("user");
	elem.value = user;
	
	$( "#dialog-form2" ).dialog("open");
	
	//$('#ajaxmodifydossierdoc'+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'+currentid).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
});


$('#ajaxmodifydossierdoc'+currentid).submit(function() 
	{
		    alert('ajaxmodifydossierdoc');
			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;
		});
		
$('#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;
		});

$( "#dialog-form2" ).dialog({
		autoOpen: false,
		height: 280,
		width: 350,
		modal: false,
		buttons: {
		"Modifier dossier": function() {
			$('#ajaxdocumentaryfilemodify').submit();
			$( this ).dialog( "close" );
		},
		"Annuler": function() {
		
		$( this ).dialog( "close" );
		}
		},
		close: function() {
			allFields.val( "" ).removeClass( "ui-state-error" );
		}
		});
		

	
});//jQuery