(function($){if(typeof $.TreeNode=="undefined"){
	var lineColumn={};
	$(['<style>table.tsn-collapse-table{border-collapse:collapse;',
		'border:none;padding:0px;margin:0px;}',
		'td.tsn-blank-square{width:10px;height:10px;',
		'padding:0px;margin:0px;}',
		'span.tsn-content{font:13px}',
		'td.tsn-L-square{width:10px;height:10px;width:10px;',
		'height:10px;border-left:1px dashed #666;',
		'border-bottom:1px dashed #666;padding:0px;margin:0px;}',
		'td.tsn-square-leftLine{width:10px;height:10px;',
		'border-left:1px dashed #666;padding:0px;margin:0px;}',
		'td.tsn-rect-leftline{width:10px;height:20px;',
		'border-left:1px dashed #666;padding:0px;}',
		'td.tsn-rect-blank{width:10px;height:20px;',
		'padding:0px;margin:0px;}',
		'.tsn-pure-node{width:20px;height:20px;float:left;',
		'background:url(../images/options.gif) no-repeat 2px 2px;}',
		'.tsn-expandable-node{width:40px;height:20px;float:left;}',
		'.tsn-expanded-node{background:url(../images/pkg-open.gif) no-repeat 2px 1px;}',
		'.tsn-collapsed-node{background:url(../images/pkg-closed.gif) no-repeat 2px 1px;}',
		'</style>'].join('')).appendTo("head");
	$.TreeNode=function (value,layer,parent){
		this.layer=layer||0;
		this.value=value||"";
		this.parent=parent||null;
		if(typeof parent!="undefined") parent.appendChild(this);
	};
	$.TreeNode.prototype.createChild=function(value){
		var childNode;
		if(typeof value!="undefined"){
			if(typeof this.childList=="undefined") {
				this.childList=[];
			}
			if(typeof value=="string"){
				childNode=new $.TreeNode(value);
			} else if(typeof value=="object"){
				childNode=value
			}
			childNode.parent=this;
			childNode.layer=this.layer+1;
			this.childList.push(childNode);
		}
		return childNode
	};
	$.TreeNode.prototype.addRow=function(hasChildNodes){
		if(hasChildNodes)
			lineColumn["p"+this.layer]=true;
		if (this.parent==null) return ((hasChildNodes)?'<div class="tsn-expandable-node tsn-expanded-node"></div>'
			:'<div class="tsn-pure-node"></div>')+'<span>'+this.value+'</span>';
		var s=[];
		s.push('<table class="tsn-collapse-table"><tr>');
		for(var i=0;i<this.layer-1;i++) s.push(this.getVertLine(i));
		s.push('<td><table class="tsn-collapse-table">');
		s.push('<tr><td class="tsn-blank-square"></td><td class="tsn-L-square"></td></tr>');
		if(this.parent.childList[this.parent.childList.length-1]==this) {
			s.push('<tr><td class="tsn-blank-square"></td><td class="tsn-blank-square"></td></tr>');
			lineColumn["p"+(this.layer-1)]=false;
		}else 	
			s.push('<tr><td class="tsn-blank-square"></td><td class="tsn-square-leftLine"></td></tr>');
		s.push('</table></td><td valign="middle">');
		if(hasChildNodes) s.push('<div class="tsn-expandable-node tsn-expanded-node"></div>');
		else s.push('<div class="tsn-pure-node"></div>');
		s.push('<span class="tsn-content">'+this.value+'</span></td></tr></table>');
		return s.join('')
	};
	$.TreeNode.prototype.appendTo=function(parent){
		var tbody=($(parent)[0].nodeName.toLowerCase()=="tbody")?$(parent):$(parent).find("tbody");
		this.target=$('<tr><td style="padding:0px;margin:0px;"></td></tr>')
			.appendTo(tbody).data("TreeSet",this);
		if(this.childList && this.childList.length>0){
			this.target.find("td").append($(this.addRow(true)));
			for(var i=0;i<this.childList.length;i++){
				this.childList[i].appendTo(tbody);
			}
		} else {
			this.target.find("td").append($(this.addRow()));
		}
		this.target.find("div.tsn-expandable-node").data("TreeSet",this).toggle(function(){
			var TreeNode=$(this).removeClass("tsn-expanded-node").addClass("tsn-collapsed-node")
				.data("TreeSet");
			TreeNode.isCollapsed=true;
			TreeNode.collapse();
		},function(){
			var TreeNode=$(this).removeClass("tsn-collapsed-node").addClass("tsn-expanded-node")
				.data("TreeSet")
			TreeNode.isCollapsed=false;
			TreeNode.expand();
		});
		return this.target
	};
	$.TreeNode.prototype.expand=function(){
		if(!this.isCollapsed && this.childList && this.childList.length>0)
			for(var i=0;i<this.childList.length;i++)
				this.childList[i].show().expand();
		return this
	};
	$.TreeNode.prototype.show=function(){
		this.target.show();
		return this
	};
	$.TreeNode.prototype.collapse=function(){
		if(this.childList && this.childList.length>0)
			for(var i=0;i<this.childList.length;i++)
				this.childList[i].hide().collapse();
		return this
	};
	$.TreeNode.prototype.hide=function(){
		this.target.hide();
		return this
	};
	$.TreeNode.prototype.getVertLine=function(column){
		if(typeof lineColumn["p"+column]=="undefined" || lineColumn["p"+column]) return '<td><table class="tsn-collapse-table">'
			+'<tr><td class="tsn-rect-blank"></td><td class="tsn-rect-leftline"></td></tr>'
			+'</table></td>';
		else
			return '<td><table class="tsn-collapse-table">'
			+'<tr><td class="tsn-rect-blank"></td><td class="tsn-rect-blank"></td></tr>'
			+'</table></td>';
	};
}})(jQuery);
