var currentjsonStreamgraph = "";
var currentjsonTreemap = "";
var currentText = "";
var typeTri = "treemap";
var currentfromdate = "";
var currenttodate = "";			
var currentcategories = [];		
var firsttimefiltre = true;	
var firsttimerequest = true;	
var currentqueryidtreemap= ""; 
var currentqueryidstreamgraph= ""; 
var currentuser = "";


	/*function launchpage(json)
	{
		curentjson = json;
		if (typeTri == "treemap")
		{
			var k =1;
			content = "<div class=\"actus\">";
			for (i = 0; i < json.clusters.length && i<4; i++)
			{
				content += "<a id=\""+json.clusters[i].id+"\" href=\"http://localhost:8000/documents_cluster/"+json.query.query_id+"/"+json.clusters[i].id+"/0/10\"><div  class=\"actu\"><img src=\"/static/img/home-visuel-"+k+".jpg\" alt=\"\" /><div class=\"inner-actu\">";
				content += "<h2> ";
				content += json.clusters[i].title;
				content += "</h2></div></div></a>";
				k++;
			}
			content +="</div>";
		}
		else
		{
			content = "<div class=\"articles\">";
			for (i = 0; i < json.clusters.length && i<4; i++)
			{
				content += "<div name=\"cluster_id\" class=\"article\" id=\""+json.clusters[i].id+"\"><div class=\"inner-article clearfix\"><h2><a title=\"Lire l\'article\" id=\""+json.clusters[i].id+"\" href=\"http://localhost:8000/documents_cluster/"+json.query.query_id+"/"+json.clusters[i].id+"/0/10\">";
				content += json.clusters[i].title;
				content += "</a></h2><p class=\"number-article\"><a href=\"#\">";
				content += json.clusters[i].documents_number;
				content +=" articles | ";
				content += json.clusters[i].annotations.length;
				content += " annotations </a><a title=\"ajouter une annotation\" class=\"add-annotation\" href=\"#\"></a></p> <div class=\"article-annotations\"><a title=\"title\" class=\"blue\" href=\"#\" style=\"width:20%;\"></a><a title=\"title\" class=\"green\" href=\"#\" style=\"width:20%;\"></a><a title=\"title\" class=\"red\" href=\"#\" style=\"width:40%;\"></a><a title=\"title\" class=\"empty\" href=\"#\" style=\"width:20%;\"></a></div><div class=\"article-content\">";
				content +="<div class=\"article-visuel-wrap\"><img class=\"article-visuel\" src=\"/static/img/visuel-article.jpg\" alt=\"\" /></div>";	 
				content +="<p class=\"resume\">";
				content += json.clusters[i].abstract;
				content += "</p><ul class=\"articlelinks\"><ul>";
				
				for (j = 0; j < json.clusters[i].documents.length; j++)
				{
					content += "<li><a href=\"\" id=\""+json.clusters[i].documents[j].id+"\">";
					content += json.clusters[i].documents[j].title;
					content += "</a></li>";
				}
				content +="</ul></ul>";
				content +="</div></div></div>";
			}
			content +="</div>";
		}
		$("#right").empty();
		$(content).appendTo("#right");		
	}

	
	function refreshview(type)
	{
		typeTri = type; 
		if(type == 'treemap')
		{
			typeTri = "treemap";
			var k =1;
		content = "<div class=\"actus\">";
		for (i = 0; i < curentjson.clusters.length && i<4; i++)
		{
			content += "<a id=\""+curentjson.clusters[i].id+"\" href=\"http://localhost:8000/documents_cluster/"+curentjson.query.query_id+"/"+curentjson.clusters[i].id+"/0/10\"><div  class=\"actu\"><img src=\"/static/img/home-visuel-"+k+".jpg\" alt=\"\" /><div class=\"inner-actu\">";
			content += "<h2> ";
			content += curentjson.clusters[i].title;
			content += "</h2></div></div></a>";
			k++;
		}
		content +="</div>";
		$("#right").empty();
		$(content).appendTo("#right");		
		}
		else
		{
			typeTri = "liste";
			content = "<div class=\"articles\">";
		for (i = 0; i < curentjson.clusters.length && i<4; i++)
		{
			content += "<div name=\"cluster_id\" class=\"article\" id=\""+curentjson.clusters[i].id+"\"><div class=\"inner-article clearfix\"><h2><a title=\"Lire l\'article\" id=\""+curentjson.clusters[i].id+"\" href=\"http://localhost:8000/documents_cluster?query_id="+curentjson.query.query_id+"&cluster_id="+curentjson.clusters[i].id+"&offset=0&count=10\">";
			content += curentjson.clusters[i].title;
			content += "</a></h2><p class=\"number-article\"><a href=\"#\">";
			content += curentjson.clusters[i].documents_number;
			content +=" articles | ";
			content += curentjson.clusters[i].annotations.length;
			content += " annotations </a><a title=\"ajouter une annotation\" class=\"add-annotation\" href=\"#\"></a></p> <div class=\"article-annotations\"><a title=\"title\" class=\"blue\" href=\"#\" style=\"width:20%;\"></a><a title=\"title\" class=\"green\" href=\"#\" style=\"width:20%;\"></a><a title=\"title\" class=\"red\" href=\"#\" style=\"width:40%;\"></a><a title=\"title\" class=\"empty\" href=\"#\" style=\"width:20%;\"></a></div><div class=\"article-content\">";
			content +="<div class=\"article-visuel-wrap\"><img class=\"article-visuel\" src=\"/static/img/visuel-article.jpg\" alt=\"\" /></div>";	 
			content +="<p class=\"resume\">";
			content += curentjson.clusters[i].abstract;
			content += "</p><ul class=\"articlelinks\"><ul>";
			
			for (j = 0; j < curentjson.clusters[i].documents.length; j++)
			{
				content += "<li><a href=\"\" id=\""+curentjson.clusters[i].documents[j].id+"\">";
				content += curentjson.clusters[i].documents[j].title;
				content += "</a></li>";
			}
			content +="</ul></ul>";
			content +="</div></div></div>";
		}
		content +="</div>";
		$("#right").empty();
		$(content).appendTo("#right");
		}
	}
	*/
	/*function setAffichage(typeTri)
	{
		alert(typeTri);
		 $('<input />').attr('type', 'hidden')
							.attr('name', 'typeTri')
							.attr('value', typeTri)
							.appendTo('#setAffichage');
			
			$("#setAffichage").submit();
	}*/
	
	function setCategories(json)					
	{	
	
		$("#pickeddatebegin").val(json.query.from_date);
		$("#pickeddateend").val(json.query.to_date);
		$("#recherche_id").val(json.query.text);
				
		$('input[name=categories1]').attr('checked', false);
		$('input[name=categories2]').attr('checked', false);
		$('input[name=categories3]').attr('checked', false);
		$('input[name=categories4]').attr('checked', false);
		
		if(json.query.typeTri == "treemap")
		{
			$('#treemapaff').click();
		}
	
		if(json.query.typeTri == "liste")
		{
			$('#listehref').click();
		}
		
		for (j = 0; j < json.query.categories.length; j++)
		{
			if(json.query.categories[j] == "International")
			{
				$('input[name=categories1]').attr('checked', true);
			}
			else if(json.query.categories[j] == "France")
			{
				$('input[name=categories2]').attr('checked', true);
			}
			else if(json.query.categories[j] == "Economie")
			{
				$('input[name=categories3]').attr('checked', true);
			}
			else if(json.query.categories[j] == "Culture")
			{
				$('input[name=categories4]').attr('checked', true);
			}
		}
	}
	
	function updatedossierdoc(json)
	{
			//alert('updatedossierdoc'+json.dossierDocs);
			content="";
		    for (i = 0; i < json.dossierDocs.length ; i++)
			{
				content += "<li class=\"clearfix\"> <a href=\"/documentary_files/"
				content += json.user;
				content += "/";
				content += json.dossierDocs[i].id;
				content += "\"><img src=\"";
				content += json.dossierDocs[i].url;
				content += "\" alt=\"\" width=\"50px\" height=\"29px\"/><span class=\"documentaire-texte\"><h3>";
				content += json.dossierDocs[i].title;
				content += "</h3><p>";
				content += json.dossierDocs[i].description;
				content += "</p></span></a></li>";				
			}
		//alert(content);
		$("#mesdossiers").empty();
		$(content).appendTo("#mesdossiers");	
	}
	
	function getDossierDocs(user)
	{
		
			if(user != "")
			{
				window.location.href = "http://localhost:8000/documentary_files/"+user;
			}
			else
			{
				alert('Vous devez vous connecter.');
			}
	
	}
	
	function launchsearch()
	{

		    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('');
				});
			
			if ($("#ajaxtreemap input[name=from_date]").val()	!= undefined)	
			{
				 $("#ajaxtreemap input[name=from_date]").val(currentfromdate);
			}
			else
			{
				 $('<input />').attr('type', 'hidden')
							.attr('name', 'from_date')
							.attr('value', currentfromdate)
							.appendTo('#ajaxtreemap');
			}
			
			if ($("#ajaxtreemap input[name=to_date]").val()	!= undefined)	
			{
				 $("#ajaxtreemap input[name=to_date]").val(currenttodate);
			}
			else
			{
				$('<input />').attr('type', 'hidden')
					.attr('name', 'to_date')
					.attr('value', currenttodate)
					.appendTo('#ajaxtreemap');
			}
			
			var i=1;
			$.each(currentcategories, function() 
			 {
				if(this != undefined)
				{
					if (firsttimerequest == false)	
					{
						$("#ajaxtreemap input[name=categories"+i+"]").val(this);
						i++;
					}
					else
					{
						
						$('<input />').attr('type', 'hidden')
							.attr('name', 'categories'+i)
							.attr('value', this)
							.appendTo('#ajaxtreemap');
							i++;
					}
				}
				
			 });			
				
		
			
			if ($("#ajaxstreamgraph input[name=from_date]").val()	!= undefined)	
			{
				 $("#ajaxstreamgraph input[name=from_date]").val(currentfromdate);
			}
			else
			{
				 $('<input />').attr('type', 'hidden')
							.attr('name', 'from_date')
							.attr('value', currentfromdate)
							.appendTo('#ajaxstreamgraph');
			}
			
			if ($("#ajaxstreamgraph input[name=to_date]").val()	!= undefined)	
			{
				 $("#ajaxstreamgraph input[name=to_date]").val(currenttodate);
			}
			else
			{
				$('<input />').attr('type', 'hidden')
					.attr('name', 'to_date')
					.attr('value', currenttodate)
					.appendTo('#ajaxstreamgraph');
			}
			
			if ($("#ajaxstreamgraph input[name=recherche]").val()	!= undefined)	
			{
				 $("#ajaxstreamgraph input[name=recherche]").val(currentText);
			}
			else
			{
				$('<input />').attr('type', 'hidden')
					.attr('name', 'recherche')
					.attr('value', currentText)
					.appendTo('#ajaxstreamgraph');
			}
		
								
			var j=1;
			$.each(currentcategories, function() 
			 {
				if(this != undefined)
				{
					if (firsttimerequest == false)	
					{
						$("#ajaxstreamgraph input[name=categories"+j+"]").val(this);
						j++;
					}
					else
					{
						$('<input />').attr('type', 'hidden')
							.attr('name', 'categories'+j)
							.attr('value', this)
							.appendTo('#ajaxstreamgraph');
							j++;
					}
				}
				
			 });			

		 firsttimerequest = false;	
		 
		$("#ajaxstreamgraph").submit();
		$('#ajaxtreemap').submit();
	}
	$(function(){
		
	$('#login_button').click(function() {
		
		$('#ajaxlogin').submit();
		});
		
	$('#treemapaff').click(function() 
	{
		$.ajax(
			{  
				type: "POST",
				url: "/",
				data      : "typeTri=treemap&requestType=setAffichage",
				success: function(data) {
					
				}
			});
			
	});
		
	$('#listehref').click(function() 
	{
		$.ajax(
			{  
				type: "POST",
				url: "/",
				data      : "typeTri=liste&requestType=setAffichage",
				success: function(data) {
					
				}
			});
	});
		
	$("#ajaxlogin").submit( function() 
		{
			var urlSubmit = $(this).attr('action');
						
			$.ajax(
			{  
				type: "POST",
				url: urlSubmit,
				data      : $(this).serializeArray(),
				success: function(data) {
					var json = jQuery.parseJSON(data);
					
					showResults(json);
					curentjson = json;
				}
			});
			return false;
			
			});
			
		
		
		$('#recherche_button').click(function() {
			launchsearch();
		
	});
			
		// Formulaire POST AJAX
		$("#ajaxtreemap").submit( function() 
		{
			var urlSubmit = $(this).attr('action');
			$.ajax(
			{  
				type: "POST",
				url: urlSubmit,
				data      : $(this).serializeArray(),
				success: function(data) {
					currentjsonTreemap = data;
					var json = jQuery.parseJSON(data);
					currentqueryidtreemap = json.query.query_id;
					showResults(json);
				}
			});
			return false;
		});
		
		// Formulaire POST AJAX
		$("#ajaxstreamgraph").submit( function() 
		{
			
			var urlSubmit = $(this).attr('action');
			
			$.ajax(
			{  
				type: "POST",
				url: urlSubmit,
				data      : $(this).serializeArray(),
				success: function(data) {
					currentjsonStreamgraph = data;
					var json = jQuery.parseJSON(data);
					loadStreamgraph(json);
					//currentqueryidstreamgraph = json.query.query_id;
					
				}
			});
			return false;
		});
		
		// Formulaire POST AJAX
		$("#ajaxfiltretreemap").submit(function()
		{
			var urlSubmit = $(this).attr('action');
			
			if (($(this).attr('action')).indexOf("next") !== -1)
			{
				var myurl = ($(this).attr('action')).split('/');
				
				
				
				if ($("#ajaxfiltretreemap input[name=docId]").val()	!= undefined)	
				{
					 $("#ajaxfiltretreemap input[name=docId]").val(myurl[4]);
				}
				else
				{
					$('<input />').attr('type', 'hidden')
								.attr('name', 'docId')
								.attr('value', myurl[4])
								.appendTo('#ajaxfiltretreemap');
				}				
			}
			
			
			
			if ($("#ajaxfiltretreemap input[name=recherche]").val()	!= undefined)	
			{
				 $("#ajaxfiltretreemap input[name=recherche]").val($("#recherche_id").val());
			}
			else
			{
				$('<input />').attr('type', 'hidden')
								.attr('name', 'recherche')
								.attr('value', $("#recherche_id").val())
								.appendTo('#ajaxfiltretreemap');
			}

					
			$('#ajaxfiltrestreamgraph').submit();
			
			$.ajax(
			{  
				type: "POST",
				url: urlSubmit,
				data      : $(this).serializeArray(),
				success: function(data) {
					currentjsonTreemap = data;
					var json = jQuery.parseJSON(data);
					showResults(json);
					//curentjson = json;
					document.getElementById('titleleft').innerHTML = "";
				}
			});
			return false;
		});
		
		// Formulaire POST AJAX
		$("#ajaxfiltrestreamgraph").submit( function() 
		{
			
			if (($(this).attr('action')).indexOf("next") !== -1)
			{
				var myurl = ($(this).attr('action')).split('/');
				$('<input />').attr('type', 'hidden')
								.attr('name', 'docId')
								.attr('value', myurl[4])
								.appendTo('#ajaxfiltrestreamgraph');
			}
			
			currentfromdate = $("#pickeddatebegin").val();
			currenttodate = $("#pickeddateend").val();
								
			if ($("#ajaxfiltrestreamgraph input[name=from_date]").val()	!= undefined)	
			{
				 $("#ajaxfiltrestreamgraph input[name=from_date]").val(currentfromdate);
			}
			else
			{
				 $('<input />').attr('type', 'hidden')
							.attr('name', 'from_date')
							.attr('value', currentfromdate)
							.appendTo('#ajaxfiltrestreamgraph');
			}
			
			if ($("#ajaxfiltrestreamgraph input[name=to_date]").val()	!= undefined)	
			{
				 $("#ajaxfiltrestreamgraph input[name=to_date]").val(currenttodate);
			}
			else
			{
				$('<input />').attr('type', 'hidden')
						.attr('name', 'to_date')
						.attr('value', currenttodate)
						.appendTo('#ajaxfiltrestreamgraph');	
			}
				
				currentcategories =[];
				
				$("input:checked").each(function() {
						currentcategories.push($(this).val());
				});
				
				$("input:checkbox:not(:checked)").each(function() {
					    currentcategories.push('');
				});
				 var i=1;
				$.each(currentcategories, function() 
				 {
					if(this != undefined)
					{
						if (firsttimefiltre == false)	
						{
							$("#ajaxfiltrestreamgraph input[name=categories"+i+"]").val(this);
							i++;
						}
						else
						{
							$('<input />').attr('type', 'hidden')
								.attr('name', 'categories'+i+'')
								.attr('value', this)
								.appendTo('#ajaxfiltrestreamgraph');
								i++;
						}
					}
					
				 });
				 
				firsttimefiltre = false;
				var urlSubmit = $(this).attr('action');
	
				$.ajax(
				{  
					type: "POST",
					url: urlSubmit,
					data      : $(this).serializeArray(),
					success: function(data) {
						currentjsonStreamgraph = data;
						var json = jQuery.parseJSON(data);
						loadStreamgraph(json);
					}
				});
				return false;
		});
		
		 $('.checkbox_category').click(function()
		 {
			// $("#categories1").removeAttr('checked');
			 //$('input[name=categories1]').removeAttr('checked');
			$('#ajaxfiltretreemap').submit();
  		});
		
		
		// Formulaire POST AJAX
		$("#ajaxdocumentaryfile").submit(function()
		{
			currentuser = $("#user_connected").text();
			
			$('<input />').attr('type', 'hidden')
						.attr('name', 'user')
						.attr('value', currentuser)
						.appendTo('#ajaxdocumentaryfile');
			
			$('<input />').attr('type', 'hidden')
						.attr('name', 'jsontreemap')
						.attr('value', currentjsonTreemap)
						.appendTo('#ajaxdocumentaryfile');

			$('<input />').attr('type', 'hidden')
						.attr('name', 'jsonstreamgraph')
						.attr('value', currentjsonStreamgraph)
						.appendTo('#ajaxdocumentaryfile');
			
			$('<input />').attr('type', 'hidden')
						.attr('name', 'query_id')
						.attr('value', currentqueryidtreemap)
						.appendTo('#ajaxdocumentaryfile');

			$('<input />').attr('type', 'hidden')
						.attr('name', 'visibility')
						.attr('value', 'private')
						.appendTo('#ajaxdocumentaryfile');			

			var urlSubmit = $(this).attr('action');			
			$.ajax(
			{  
				type: "POST",
				url: urlSubmit,
				data      : $(this).serializeArray(),
				success: function(data) {
					$('#ajaxupdatedossierdoc').submit();
				}
			});
			return false;
		});
		
		// Formulaire POST AJAX
		$("#ajaxupdatedossierdoc").submit(function()
		{
			currentuser = $("#user_connected").text();
			$('<input />').attr('type', 'hidden')
						.attr('name', 'user')
						.attr('value', currentuser)
						.appendTo('#ajaxupdatedossierdoc');
				
			var urlSubmit = $(this).attr('action');
			$.ajax(
			{  
				type: "POST",
				url: urlSubmit,
				data      : $(this).serializeArray(),
				success: function(data) 
				{
					//alert(data);
					var json = jQuery.parseJSON(data);
					updatedossierdoc(json);
				}
			});
			return false;
		});
		
		
		$( "#dialog-form" ).dialog({
		autoOpen: false,
		height: 250,
		width: 350,
		modal: false,
		buttons: {
		"Créer dossier": function() {
			$('#ajaxdocumentaryfile').submit();
			$( this ).dialog( "close" );
		},
		"Annuler": function() {
		
		$( this ).dialog( "close" );
		}
		},
		close: function() {
			allFields.val( "" ).removeClass( "ui-state-error" );
		}
		});
		$( "#create-dossierDoc" ).click(function() {
			
			currentuser = $("#user_connected").text();
						
			if(currentuser != "")
			{
				$( "#dialog-form" ).dialog( "open" );
			}
			else
			{
				alert('Vous devez vous connecter.');
			}
			
		
		
});


	
});//jQuery