function download(content, contentMap, contentPie, formato, button) {
	
	// recupero i dati per il filtro 
	var area = $("#area option:selected").text();
	var sottoarea = $("#sottoarea:visible option:selected").text();
	var annoScolastico = $("#year option:selected").text();
	var dataRif = $(".dataRif option:selected").text();
	
	// recupero il contenuto zippato di tabella e grafici
	var zippedContent = getZippedContent(content);
	var zippedContentMap = getZippedContent(contentMap);
	var zippedContentPie = getZippedContent(contentPie);
	
	var flags = new Array();
	$.each($('input[type="checkbox"]:checked'), function() {
		flags.push($(this).attr('value'));
	});
	
	var msg = "";
	$.fileDownload('export.' + formato, {
		data : {
			zippedContent : zippedContent,
			zippedContentMap : zippedContentMap,
			zippedContentPie : zippedContentPie,
			area : area,
			'flags[]' : flags,
			areaTerritoriale : espscuVar.areaTerritoriale,
			filename : button.attr('data-filename') + " " + area,
			title : button.attr('data-title'),
			columnSeparator : button.attr('data-columnSeparator'),
			rotate : button.attr('data-rotate'),
			logo : button.attr('data-logo'),  
			livello : espscuVar.livelloTerritoriale,
			regione : espscuVar.regione,
			filtro : "Area=" + area + "|Sottoarea=" + sottoarea + "|Anno Scolastico=" + annoScolastico + "|Data Aggiornamento=" + $(".dataRif").val(),
			mostraNotaInfo201819 : $("#hiddenNotaInfo201819").val()
		},
		httpMethod : 'POST',
		successCallback : function() {
			console.log("download completato");
		},
		failCallback : function() {
			msg.close();
			showErrorMessage('errore nel download');
		}
	});

}

/*
 * funzione che restituisce un contet zippato a partire dalla stringa
 */
function getZippedContent(content){
	if (content != null) {
		var zip = new JSZip();
		zip.file("content.html", content);
		var zippedContent = zip.generate({type:"base64"});
		console.log("ZIP SIZE : " + zippedContent.length);
		return zippedContent;
	} else {
		return null;
	}
}


function abilitaDownload(container) {
	
	// download pdf con tabella, grafico mappa e grafico torta
	$('button.export-dati',container).click(function() {
		
		// content tabella
		var exportContainer = $('#' + getDataRef(this));
		var content = exportContainer.html();
		
		// content mappa
		var contentMap = $('.jvectormap-container').html();
		
		// content torta
		var contentPie = $('.highcharts-container').html();
		
		download(content, contentMap, contentPie, 'pdf', $(this));
		return false;
	});

	// download pdf con grafico mappa e grafico torta
	$('button.export-graph',container).click(function() {
		
		// content mappa
		var contentMap = $('.jvectormap-container').html();
		
		// content torta
		var contentPie = $('.highcharts-container').html();
		
		download(null, contentMap, contentPie, 'graph', $(this));
		return false;
	});
	
	// download tabella formato excel
	$('button.export-excel',container).click(function() {
		var exportContainer = $('#' + getDataRef(this));
		download(exportContainer.html(), null, null, 'xls', $(this));
		return false;
	});
	
	// download tabella formato pdf
	$('button.export-pdf',container).click(function() {
		var exportContainer = $('#' + getDataRef(this));
		download(exportContainer.html(), null, null, 'pdf', $(this));
		return false;
	});
}


function getDataRef(el){
	var dataRef= $(el).attr('data-ref');
	if(!dataRef){
		dataRef = $(el).parents().prevAll().find('.display').last().attr('id')+'-exportContainer';
	}
	
	if($('#'+dataRef).length>0){
		return dataRef;
	}else{
		return 'undefined';
	}
}

