//var buildLeftTreeChilds;
var createLeftTreeNode;
var leftNodeTitleClick;

var PlusImgUrl = '/images/treeplus.png';
var MinusImgUrl = '/images/treeminus.png';
var EmptyNodeImgUrl = '/images/treeempty.gif';
var treeLoadingImgUrl = '/images/treewait.gif';
var treeLoadingTxt = 'Загрузка...';
var treeContLoadingStyle = 'tree_c_loading';
var treeContLoadedStyle = 'tree_c_loaded';
var leftTreeDivId = 'lefttree';
var leftTreeDiv;

var leftTreeAjaxUrl;
//var courseTreeConatainer;
//var courseTree;



function initTreeDiv()
{
	var treediv = document.getElementById(leftTreeDivId);
	if (treediv)
	{
		if (treediv.tagName == 'DIV')
		{
			return treediv;
		}
		else
		{
			treediv.parent.removeChild(treediv);
		}
	}
	treediv = MyCreateElement('div');
	treediv.id = leftTreeDivId;
	var leftcont = document.getElementById('leftdv');
	if (!leftcont) return null;
	leftcont.appendChild(treediv);
	return treediv;
}



function CourseTreeBuildRoot(rootid, container)
{
	ProcessAjax({
				method: 'POST',
				data: 'parid=' + parid,
				url: courseTreeAjaxUrl,
				callback: function(respXML){
					buildTreeChilds(respXML, parid, container);
					nimg.src = MinusImgUrl;
				},
				errback: alertAjaxError
			});
}





function buildTreeChilds(xml, parId, ParConteiner)
{
	var cbState = false;
	var usePredifinedCont = (ParConteiner != null);
	if (!usePredifinedCont)
	{
		ParConteiner = document.getElementById('c' + parId);
		document.getElementById('i' + parId).src = MinusImgUrl;
		var cbimg = document.getElementById('b' + parId);
		cbState = (cbimg.src == leftCBImages.all.src || cbimg.src == leftCBImages.children.src);
	}
	while (ParConteiner.hasChildNodes())
	{
		ParConteiner.removeChild(ParConteiner.lastChild);
	}
	if (!usePredifinedCont)
	{
		ParConteiner.className = treeContLoadedStyle;
	}
	var elems = xml.getElementsByTagName('o');
	for (var i = 0; i < elems.length; i++)
	{
		ParConteiner.appendChild(createLeftCourseNode(elems[i], cbState));
	}
}


function ndCToggle(nimg)
{
	var imgid = new String(nimg.id);
	var parid = imgid.substr(1);
	var nblock =  document.getElementById('c'+parid);
	if (nblock.style.display == 'none')
	{ 
		if (nblock.className == '')
		{
			nblock.className = treeContLoadingStyle;
			nimg.src = treeLoadingImgUrl;
			nblock.innerHTML = treeLoadingTxt;
			ProcessAjax({
				method: 'POST',
				data: 'parid=' + parid,
				url: leftTreeAjaxUrl,
				callback: function(respXML){
					buildTreeChilds(respXML, parid);
					nimg.src = MinusImgUrl;
				},
				errback: alertAjaxError
			});
		}
		else
		{
			nimg.src = MinusImgUrl;
		}
		nblock.style.display = 'block';
	}
	else
	{
		if (nblock.className != treeContLoadingStyle)
		{
			nblock.style.display = 'none';
			nimg.src = PlusImgUrl;
		}
	}
}


function MyCreateElement(tgname)
{
	var element = null;
	// Попытка создать элемент в стиле IE. Она будет неудачной в большинстве броузеров
	try 
	{
		element = document.createElement('<' + tgname + '>');
	} 
	catch (e)
	{
	}
	if (!element || element.nodeName != tgname)
	{
		// Для не IE; использование стандартного метода создания элемента
		element = document.createElement(tgname);
	}
	return element;
}



function alertAjaxError(status, message)
{
	alert("ERROR " + status + " (" + message + ")");
}


function hideLeftTree()
{
	leftTreeDiv = document.getElementById(leftTreeDivId);
	if (leftTreeDiv)
	{
		leftTreeDiv.style.display = 'none';
		document.getElementById('footer').appendChild(leftTreeDiv);
	}
}

function restoreLeftTree()
{
	if (leftTreeDiv)
	{
		document.getElementById('leftdv').appendChild(leftTreeDiv);
		leftTreeDiv.style.display = 'block';
		return true;
	}
	return false;
}
