data = new Array();

var pending = 0;

function load_external_data(fname){
	if( document.createElement && document.childNodes){
		var scriptTag = document.getElementById('loadScript');
		var scriptElem = document.createElement('script');
		var head = document.getElementsByTagName('head').item(0);
		if(scriptTag) head.removeChild(scriptTag);
		re = new RegExp ("([a-z]+)([0-9]*)");
		if (fname.match (re)) {
			var m = re.exec (fname);
		}
		else {
			return;
		}
		var status = 1;
		var lang_id = 1;
		var obj = document.getElementById("lang_id");
		if (obj) lang_id = obj.value;
		var obj = document.getElementById("status");
		if (obj) status = obj.value;
		scriptElem.src = "/menu/"+m[1]+"/" + m[2] + "?status=" + status + "&lang_id=" + lang_id;
//		alert(fname);
		scriptElem.type = 'text/javascript';
		data[fname] = new Array();
		document.getElementsByTagName('head')[0].appendChild(scriptElem);
	}
}

function load_external_data2(fname){
	load_external_data(fname);
	if (data[fname]["children"]){
		for (i in data[fname]["children"]){
			load_external_data(data[fname]["children"][i]);
		}
	}
}


function Control(obj){
	if ((obj) && (obj.tagName == "IMG") && (obj.parentNode.tagName == "DIV")){
		if (!pending){
			pending = 1;
			div = obj.parentNode;

			load_external_data(div.id);
			if (obj.id == "closed"){
				load_div = document.createElement("DIV");
				load_div.className = "tree_dir_closed";
				load_div.id = "loading";
				load_div.innerHTML = "<img src='/images/tree/load4.gif' width=10 height=10><b>...Загрузка</b>";
				div.appendChild(load_div);

				setTimeout("ShowChilds(div)", 500);
			}else
				if (obj.id == "opened")
					setTimeout("HideChilds(div)", 500);
		}
	};
}

function ShowChilds(obj){
	if (data[obj.id]["class_name"]!=null){
		for (i=0;i<obj.childNodes.length;i++){
			if (obj.childNodes.item(i).tagName == "IMG"){
				obj.childNodes.item(i).id = "opened";
				obj.childNodes.item(i).src = "/images/tree/" + data[obj.id]["class_name"] + "_opened.gif";
			};
		};
		obj.className = "tree_" + data[obj.id]["class_name"] + "_opened";
		if ((obj) && (data[obj.id]["children"])){
			for (i in data[obj.id]["children"]){
				//load_external_data(data[obj.id]["children"][i]);
				new_div = document.createElement("DIV");
				var postfix = "";
				if (!data[data[obj.id]["children"][i]]["class_name"].indexOf("dir"))
					postfix = "_closed";
				if (!data[data[obj.id]["children"][i]]["class_name"].indexOf("dir"))
					new_div.innerHTML = "<img src = \"" + "/images/tree/" + data[data[obj.id]["children"][i]]["class_name"] + postfix + ".gif\"" + " onClick=\"Control(this)\" id = \"closed\" onMouseOver = \"load_external_data2(this.parentNode.id);\" > ";
				new_div.className = "tree_" + data[data[obj.id]["children"][i]]["class_name"] + postfix;
				new_div.id = data[obj.id]["children"][i];
				new_a = document.createElement("A");
				new_a.innerHTML = data[data[obj.id]["children"][i]]["title"];
				if (data[data[obj.id]["children"][i]]["class_name"].indexOf("dir"))
					new_a.innerHTML = "&raquo;"+new_a.innerHTML;
				new_a.href = data[data[obj.id]["children"][i]]["href"];
				new_div.appendChild(new_a);
				obj.appendChild(new_div);
			}
		};
	}//else
//		setTimeout("ShowChilds(obj)", 500);
		
		pending = 0;
		for (i = obj.childNodes.length-1;i>0;i--){
			if (obj.childNodes[i].id == "loading"){
				obj.removeChild(obj.childNodes[i]);
			}
		}
}

function HideChilds(obj){
	if (data[obj.id]["class_name"]!=null){
		for (i = obj.childNodes.length-1;i>0;i--){
			if (obj.childNodes[i].tagName == "DIV"){
				obj.removeChild(obj.childNodes[i]);
			}
		}
		for (i=0;i<obj.childNodes.length;i++){
			if (obj.childNodes.item(i).tagName == "IMG"){
				obj.childNodes.item(i).id = "closed";
				obj.childNodes.item(i).src = "/images/tree/" + data[obj.id]["class_name"] + "_closed.gif";
			};
		}
		obj.className = "tree_" + data[obj.id]["class_name"] + "_closed";
		pending = 0;
	}//else
//		setTimeout("HideChilds(obj)", 500);
}



