var now=new Date();
var month=['Jan', 'Fev', 'Mar', 'Avr', 'Mai', 'Juin', 'Jul', 'Août', 'Sept', 'Oct', 'Nov', 'Dec'];

	$(document).ready(function(){
		entete();
		affiche();
	});
function entete()
{
	$('.liste').append('<table class="bordered centered"><tr class="center"><th class="center">Nom du cycle</th><th class="center">Date</th><th colspan="2" class="center">Option</th></tr></table>');
}

function affiche()
{
		for (i=0; i<=localStorage.getItem('last_cycle'); i++)  
			{  
				val = localStorage.getItem('cycle'+i);
				$('table').append('<tr><td>'+val+'</td><td>'+get_interval(val)+'</td><td><a href="" data-cycle='+val+' class="graph">Voir la courbe</a></td><td><a href="" data-cycle='+val+' class="erase">Effacer le cycle</a></td></tr>');
			};
}


function readAllData()
{
	for (i=0; i<=localStorage.length-1; i++)  
			{  
				key = localStorage.key(i);  
				val = localStorage.getItem(key);
				alert(key+'='+val);
			};
}


function get_interval(cycle)
{
	//Début
	var f=cycle.split('*');
	var debut=localStorage.getItem(f[0]+'*'+'0');
	var splitter=debut.split('*');
	var daty=splitter[0];
	var daty2=daty.split('/');
	var jour_debut=daty2[0];
	var mois_debut=month[daty2[1]];

	//Fin
	var fin=localStorage.getItem(cycle+'*'+localStorage.getItem(f[0]+'*length'));
	var splitter=fin.split('*');
	var daty=splitter[0];
	var daty2=daty.split('/');
	var jour_fin=daty2[0];
	var mois_fin=month[daty2[1]];

	if (mois_debut==mois_fin)
	{
		var affiche=jour_debut+' - '+jour_fin+' '+mois_fin;
	}

	else
	{
		var affiche=jour_debut+mois_debut+' - '+jour_fin+mois_fin;
	}

	return affiche;
}

function erase_cycle(cycle)
{
	for (var i=0; i<=localStorage.getItem(cycle+'*length'); i++)
		{
			localStorage.removeItem(cycle+'*'+i);
		}

	localStorage.removeItem(cycle+'*length');

	localStorage.removeItem(cycle);

	if (localStorage.getItem('cycle_actif')==cycle)
	{
		localStorage.removeItem('cycle_actif');
	}

	Materialize.toast('Cycle effacé.', 3000);
	$('table').remove();
	entete();
	affiche();
}


	$('.liste').on('click', '.graph', function(e){
		e.preventDefault();
		localStorage.setItem('courbe', $(this).data('cycle'));
		window.location.replace('graph.html');
	});

	$('.liste').on('click', '.erase', function(e){
		e.preventDefault();
		erase_cycle($(this).data('cycle'));
	});
