/*** Æ®¸®¸Þ´º ***/
function tree()
{
	//this.dir = "";
	this.iciDiv;
	this.isMoved = 0;

	this.icon = new Array();
}

tree.prototype.img = function(mode,step)
{
	var tmp = "";
	if (mode=="plus" || mode=="plusbottom"){
		tmp = "onclick=\"x.openclose(this," + step + ")\" style='cursor:pointer'";
	}
	return "<img src='" + this.dir + mode + ".gif' align=absmiddle " + tmp + ">";
}

tree.prototype.deco = function(obj,step,pbottom)
{
	for (var i=0;i<obj.childNodes.length;i++){
		var space = "";
		var node = obj.childNodes[i].childNodes[0];
		var parent = node.parentNode;
	
		var isBottom = (parent==parent.parentNode.childNodes[parent.parentNode.childNodes.length-1]) ? "bottom" : "";

		for (var j=0;j<step;j++) space += (pbottom && step==j+1) ? this.img('empty') : this.img('line');
		var img = (node.nextSibling) ? this.img('plus'+isBottom,step) + this.img('folder') : this.img('join'+isBottom) + this.img('page');
		
		/*** ¾ÆÀÌÄÜÀ» Æú´õ·Î °íÁ¤ ***/
		if (this.onlyDir) img = this.img(((node.nextSibling)?'plus':'minus')+isBottom,step) + this.img('folder');

		node.innerHTML = space + img + node.innerHTML;
		if (node.nextSibling) this.deco(node.nextSibling,step+1,isBottom);
	}
}

tree.prototype.init = function(id)
{
	this.icon['line'] = new Image();
	this.icon['line'].src = this.dir + 'line.gif';

	this.icon['empty'] = new Image();
	this.icon['empty'].src = this.dir + 'empty.gif';

	var t = document.getElementById(id);
	this.deco(t,0);
}

tree.prototype.select = function (obj)
{
	//obj.style.fontWeight = "bold";
	obj.style.color = "#ffffff";
	obj.style.background = this.color;
	if (this.iciDiv && this.iciDiv!=obj){
		this.iciDiv.style.color = this.iciDiv.style.fontWeight = this.iciDiv.style.background = "";
		if (this.iciDiv.color=="gray") this.iciDiv.style.color = "#C0C0C0";
	}
	this.iciDiv = obj;
}

tree.prototype.view = function(obj)
{
	_ID('ifrmCategory').src = this.linkurl + "?category=" + obj.getAttribute('v');
}

tree.prototype.move = function (idx)
{	
	if (this.iciDiv==null || this.iciDiv.getAttribute('top')==1) return false;
	
	var obj = this.iciDiv.parentNode.parentNode;
	var tmp = obj.innerHTML;

	var target = (idx==1) ? obj.nextSibling : obj.previousSibling;

	if (!target) return false;

	obj.innerHTML = target.innerHTML;
	target.innerHTML = tmp;

	if (idx==1 && !target.nextSibling){

		var img = target.childNodes[0].getElementsByTagName('img');
		img[img.length-2].src = img[img.length-2].src.replace(".gif","bottom.gif");

		if (target.childNodes[1]){
			var sub = target.childNodes[1];
			for (var i=0;i<sub.childNodes.length;i++){
				sub.childNodes[i].getElementsByTagName('img')[img.length-2].src = this.icon['empty'].src;
			}
		}

		var img = obj.childNodes[0].getElementsByTagName('img');
		img[img.length-2].src = img[img.length-2].src.replace("bottom.gif",".gif");

		if (obj.childNodes[1]){
			var sub = obj.childNodes[1];
			for (var i=0;i<sub.childNodes.length;i++){
				sub.childNodes[i].getElementsByTagName('img')[img.length-2].src = this.icon['line'].src;
			}
		}
		
	}

	if (idx==-1 && !obj.nextSibling){
		var img = target.childNodes[0].getElementsByTagName('img');
		img[img.length-2].src = img[img.length-2].src.replace("bottom.gif",".gif");

		if (target.childNodes[1]){
			var sub = target.childNodes[1];
			for (var i=0;i<sub.childNodes.length;i++){
				sub.childNodes[i].getElementsByTagName('img')[img.length-2].src = this.icon['line'].src;
			}
		}

		var img = obj.childNodes[0].getElementsByTagName('img');
		img[img.length-2].src = img[img.length-2].src.replace(".gif","bottom.gif");
	
		if (obj.childNodes[1]){
			var sub = obj.childNodes[1];
			for (var i=0;i<sub.childNodes.length;i++){
				sub.childNodes[i].getElementsByTagName('img')[img.length-2].src = this.icon['empty'].src;
			}
		}
	}

	this.iciDiv = target.childNodes[0].getElementsByTagName('a')[0];
	this.isMoved = 1;

	return false;
}

tree.prototype.openclose = function (obj,step)
{
	var obj = obj.parentNode;
	var node = obj.nextSibling;
	if (!node) return;
	var bottom = (obj.parentNode.nextSibling) ? "" : "bottom";
	var io = !tree_isOpened(node);
	node.style.display = (io) ? "block" : "none";
	obj.childNodes[step].src = this.dir + ((io) ? "minus" : "plus") + bottom + ".gif";
	if (obj.childNodes[step+1]) obj.childNodes[step+1].src = this.dir + "folder" + ((io) ? "open" : "") + ".gif";
}

function tree_isOpened(obj)
{
	return (obj.style.display=="none") ? false : true;
}


tree.prototype.addChild = function(category,catnm)
{
	var ici = this.iciDiv.parentNode;
	if (!ici.nextSibling){
		var outline = document.createElement('div');
		ici.parentNode.appendChild(outline);
	} else {
		var outline = ici.nextSibling;

		/* ÀÌÀü³ëµå ÀÌ¹ÌÁö ¼öÁ¤ */
		if (outline.childNodes.length){
			var img = outline.childNodes[outline.childNodes.length-1].getElementsByTagName('img');
			img[img.length-2].outerHTML = this.img('join');
		}
	}

	var newDiv = document.createElement('div');
	outline.appendChild(newDiv);

	var subDiv = document.createElement('div');

	for (var i=0;i<ici.getElementsByTagName('img').length-1;i++){
		subDiv.innerHTML += this.img('line');
	}
	subDiv.innerHTML += this.img('joinbottom');
	subDiv.innerHTML += this.img('page');

	subDiv.innerHTML += "<a href='javascript:void(0)' onclick='x.select(this);x.view(this)' onfocus=blur() s=cate" + (category.length%3) + " v='" + category + "'>" + catnm + "</a>";
	newDiv.appendChild(subDiv);
}