var menu = "";

var menuName = null;

function init(_menuName) {

	menuName = _menuName;
  
		$.ajax({
	url: '../pdf/fi/tree.xml?t='+(new Date()).getTime(),
	dataType: 'xml',
	success: function(xml) {
		RenderTreeXml(xml);
		fogli_informativi();
	},
	error: function(par1, par2, par3) {  
		alert('error '+par1+' , '+par2+' , '+par3);
		}
	});

}
function fogli_informativi(){
	$('.fogli_informativi').click(function() {		
		$(this).next('.toggle').toggle("slow");
	});}

function RenderTreeXml(xml) {
  $(xml).find('menu').each(function(){
 
 	if (menuName) {
		if ($(this).find('label').first().text() != menuName){
			return;
		}
	}

    // per ogni menù creo un div
	var box = $('<div id="container_'+$(this).find('label').first().text().split(' ').join('')+'" class="fogli_informativi"><a class="fogli_informativi" href="#"><p class="foglit">'+$(this).find('label').first().text()+'</p></a></div>');
    box.appendTo($('#treeContainer'));
    RenderMenuItems($(this), box);
    
  });
}


function RenderMenuItems(context, box) {

  $(context).children('menuItem').each(
    function () {
      var menuBox = $('<a id="content_'+$(this).find('label').first().text().split(' ').join('')+'" class="fogli fogli_informativi" href="javascript:void(0);"><p class="foglisub"><strong class="fogli">'+$(this).find('label').first().text()+'</strong></p></a>');     	  
	  var contentBox = $('<div id="'+$(this).find('label').first().text().split(' ').join('')+'" class="toggle"></div>');      
	  var contentBoxP =  $('<p id="p_content_'+$(this).find('label').first().text().split(' ').join('')+'" class="foglip"></p>');      
	  
	  menuBox.appendTo(box);
	  contentBox.appendTo(box);
	  contentBoxP.appendTo(contentBox);	  
	  RenderMenuItems($(this), contentBoxP);
	}
  );

  	$(context).children('doc').each(
			function () {
				//var title = $(this).find('title').first().text();
				//var url = '../pdf/fi/' + $(this).find('url').first().text();
				//var doc = $('<a class="pdf fogli" target="_blank" >').attr('href', url).append(title).append('</a><br />');
				var doc = $('<a class="pdf fogli" target="_blank" href="../pdf/fi/'+ $(this).find('url').first().text() +'">'+$(this).find('title').first().text()+'</a><br />');
				box.append(doc);
			}
		)	
  
 }

