// load json resource per XMLHTTP and display result

var jsonObj = null;
var insertJSON = function(node,uri) {
	//uri = uri+"?nc="+Math.random(); // prevent caching
	var req = getXMLHTTPObject();
	req.open('POST',uri,true);
	req.onreadystatechange =
	function() {
		if(this.readyState == 4) {
			
			jsonObj = this.responseText;
			jsonObj = eval("("+jsonObj+")");

			aa = JSONtoHTML(jsonObj.children);
			aa = 
				'<a onclick="invViz(document.getElementById(\'jTree\').firstChild,this)" '+
				'title="Kursbaum vollstaendig erweitern/schliessen" '+
				'style="font-size: 85%; cursor: pointer;">'+
				'erweitern</a>'+
				'<div id="jTree" class="lmTree">'+aa+'</div>';
			node.innerHTML = aa;
		};
	};
	req.send(null);
};

var lmsLink = "https://www.lms.saarlernnetz.de/goto.php?target=cat_$$$&amp;client_id=SaarLernNetz";
var JSONtoHTML = function(el) {
	var ret = "";
	if(el instanceof Array) {
		ret += "<ul>";
		for(var i = 0; i<el.length; i++) {
			ret += JSONtoHTML(el[i]);
		}
		ret += "</ul>";
	}
	else if(el instanceof Object) {
		if(el.type == "cat") {
			ret += 
			  "<li class='cat'>"+
				"<a name='"+el.ref_id+"' class='cat' onclick='switchCat(this)'>"+
				"<img src='http://www.saarlernnetz.de/ilias-meta-hub/gfx/orange-plus.png' /> "+
				el.title+
				"</a>"+
				JSONtoHTML(el.children);
			"</li>";
		}
		else if(el.type == "lm") {
			//console.debug(el);
			ret += 
			"<li class='lm'>"+
			"<img src='http://www.saarlernnetz.de/ilias-meta-hub/gfx/blue-doc.png' /> "+
			"<a href='"+lmsLink.replace("$$$",el.parent)+"'>"+
			el.title+
			"</a>"+
			"<span class='lm-desc'>"+
			(el.description == "" ? "keine Beschreibung" : el.description)+
			"</span>"+
			(el.loc ?  el.loc : '')+
			"</li>";
		}
	}
	return ret;
};

//var dbg = function(e) { console.log(e); };	
var switchCat = function(node) {
	// get image
	var img = node.firstChild;
	while(img.nodeName != 'IMG') { img = img.nextSibling; }

	var pnode = node.parentNode;

	var kids = pnode.childNodes;
	if(!pnode.showKids) { // show child uls / change image
		pnode.showKids = true;
		switchCat2(node,true);
	}
	else { // hide child uls / change image
		pnode.showKids = false;
		switchCat2(node,false);
	}
};
var switchCat2 = function(node,state) {
	var img = node.firstChild;
	while(img.nodeName != 'IMG') { img = img.nextSibling; }
	
	var kids = node.parentNode.childNodes;
	if(state) {
		img.src = 'http://www.saarlernnetz.de/ilias-meta-hub/gfx/orange-minus.png';
		for(var i=0; i<kids.length; i++ ) {
			if(kids[i].nodeName == 'UL') 
				kids[i].style.display = 'block';
		}		
	}
	else {
		img.src = 'http://www.saarlernnetz.de/ilias-meta-hub/gfx/orange-plus.png';
		for(var i=0; i<kids.length; i++ ) {
			if(kids[i].nodeName == 'UL') 
				kids[i].style.display = 'none';
		}
	}
}

var invViz = function(el,src) {
	//console.log(src);
	var state = ( typeof(el.invViz) == "undefined" ? false : el.invViz );
	state = !state;
	el.invViz = state;
	
	src.innerHTML = ( state ? "schlie&szlig;en" : "erweitern" );

	elems = el.getElementsByTagName('A');
	for(var i=0; i<elems.length; i++) {
		if(elems[i].className == 'cat') {
			//switchCat(elems[i]);
			switchCat2(elems[i],state);
		}
		/*
		e = elems[i];
		if(e.viz) {
			e.viz = false;
			e.style.display = 'none';
		}
		else {
			e.viz = true;
			e.style.display = 'block';
		}
*/
	}
};


