// Copyleft 2006, Kotatuinu.
//
// title   : kotatuinu Tree Object
// author  : 炬燵犬
// version : 0.1
// mail    : kotatuinu@nifty.com
// Website : http://homepage2.nifty.com/kotatuinu/
// Released: 2006/05/01
// Update  : 2006/05/06 [mswitchDYN : sendRequest()の非同期を同期に修正]
// Update  : 2006/05/12 [createTree : sendRequest()の非同期を同期に修正]
// NOTICE  : This Program Needs jslb_ajax.js and kotatucommon1.js
// 本プログラムは、高橋登史朗さま作成の jslb_ajax.js と拙作のkotatucommon1.jsが必要です。
// jslb_ajax.jsについては、高橋登史朗さまのホームページ http://jsgt.org/mt/01/ を参照してください。
//
// 本プログラムは、商用利用および改造を自由に行ってくださってもかまいません。
// ただし、Ajaxの更なる発展のため、改造したソースは公開してください。
// 利用・改造の連絡は不要です。
// なお、本プログラムの利用によりあなた、またはあなたの周囲に損害が発生しても、
// 当方は一切関知しません。
//  - USE THIS PROGRAM AT YOUR OWN RISK -

var exMenu=function(){};
exMenu.prototype={
	arry:[],
	add:function(obj){
		this.arry[obj.top]=obj;
	},
	del:function(topid){
		delete this.arry[topid];
	},
	alldel:function(){
		var i;
		var cnt=this.arry.length;
		for(i=0; cnt>i; i++) {
			delete this.arry[i];
		}
	},
	mswitch:function(topid,id){
		var mark;
		var obj=this.arry[topid];
		if(typeof(obj)!='object') return;
		var tMenu = getDiv(id).style;
		if(obj.imgflag){
			mark=(tMenu.display!='block') ? obj.showimg : obj.hiddenimg;
			mark='<img src="'+mark+'">';
		} else {
			mark=(tMenu.display!='block') ? obj.showmark : obj.hiddenmark;
		}
		tMenu.display = (tMenu.display!='block') ? 'block' : 'none';
		this.changeDiv(id + '_', mark);
	},
	mswitchDYN:function(topid,param){
		var data='';
		var type='GET';

		var tree=this.arry[topid];
		if(typeof(tree)!='object') return;
		var func=tree.on_loaded1; // 苦肉の策
		if(typeof(tree.script.progress)=='object' &&
		   typeof(tree.script.progress.prog_start)=='function') {
			tree.script.progress.prog_start();
		}
		if(typeof(param.data)=='string') data=param.data;
		if(typeof(param.type)=='string') type=param.type;

		var i;
		var contentsName;
		var urlList=param.url;
		var len=urlList.length;

		var P2=new Array();
		P2.tree=tree;
		P2.edge=param.edge;
		P2.level=param.level;
		P2.amode=true;
		P2.id=param.id;

		for(i=0; len>i;i++) {
			P2.edge=param.edge;
			if(len-1==i) {
				P2.edge[P2.level]=true;
			}
			P2.contentsID=i;
			func(null,P2);
			tree.sendRequest(func,data,type,urlList[i],false,true);
		}
		if(typeof(tree.script.progress)=='object' &&
		   typeof(tree.script.progress.prog_start)=='function') {
			tree.script.progress.prog_stop();
		}

		var id=param.id;
		var obj1=getDiv(param.pid).childNodes.item(0);
		var obj2=obj1.getElementsByTagName('a').item(0);
		var val="exMenu.mswitch('"+tree.top+"','"+id+'\')';
		changeAttribute(obj2, [{id:"onclick",value:val}]);
		exMenu.mswitch(topid,id);

		var mark;
		var tMenu = getDiv(param.pid).style;
		if(tree.imgflag){
			mark=(tMenu.display=='block') ? tree.showimg : tree.hiddenimg;
			mark='<img src="'+mark+'">';
		} else {
			mark=(tMenu.display=='block') ? tree.showmark : tree.hiddenmark;
		}
		this.changeDiv(id + '_', mark);

	},
	changeDiv:function(tName,tMsg){
		var objDiv;
		if(document.all || document.getElementById) {
			if(typeof(tName)=='string') {
				if(document.all) {
					objDiv=document.all(tName);
				} else {
					objDiv=document.getElementById(tName);
				}
			} else {
				objDiv=tName;
			}
			objDiv.innerHTML=tMsg;
		} else {
			if(document.layers) {
				if(typeof(tName)=='string') {
					objDiv=document.layers(tName)
				} else {
					objDiv=tName;
				}
				with(objDiv) {
					document.open();
					document.write(tMsg);
					document.close();
				}
			}
		}
	},
	addDiv:function(tName,tMsg){
		var objDiv;
		if(document.all || document.getElementById) {
			if(typeof(tName)=='string') {
				if(document.all) {
					objDiv=document.all(tName);
				} else {
					objDiv=document.getElementById(tName);
				}
			} else {
				objDiv=tName;
			}
			objDiv.innerHTML+=tMsg;
		} else {
			if(document.layers) {
				if(typeof(tName)=='string') {
					objDiv=document.layers(tName)
				} else {
					objDiv=tName;
				}
				with(objDiv) {
					document.open();
					document.write(tMsg);
					document.close();
				}
			}
		}
	}
}
exMenu=new exMenu;
var kotatutree=function(){};
kotatutree.prototype = {
	treename:'',
	showmark:'－',
	hiddenmark:'＋',
	showimg:'',
	hiddenimg:'',
	imgflag:false,
	top:null,
	idtype:0,
	showflag:true,
	treedata:null,
	createTreeData:"",
	sendRequest:null,
	edgeList:null,
	Browser:function(){
		this.Browser=new chkAjaBrowser();
	},
	script:function(){
		this.prototype={
			link:undefined,
			progress:undefined
		};
	},
	init:function(){
		var objTop;
		if(typeof(this.top)=='object'){
			objTop=tName;
		} else {
			objTop=getDiv(this.top);
		}
		removeChildlen(objTop);
		exMenu.del(this.top)
		this.clear();
		this.top=null;
	},
	clear:function(){
		this.treename='';
		this.showmark='－';
		this.hiddenmark='＋';
		this.showimg='';
		this.hiddenimg='';
		this.imgflag=false;
		this.idtype=0;
		this.showflag=true;
		this.script.link=undefined;
		this.script.progress=undefined;
	},
	show:function(){
		this.disp('block');
	},
	hide:function(){
		this.disp('none');
	},
	disp:function(param){
		var objTop
		if(typeof(this.top)=='object'){
			objTop=this.top;
		} else {
			objTop=getDiv(this.top);
		}
		if(objTop!=null) {
			objTop.style.display=param;
		}
	},
	setLinkScript:function(scr){
		if(typeof(scr)=='string') this.script.link=scr;
	},
	setProgress:function(scr){
		if(typeof(scr)=='object') this.script.progress=scr;
	},
	setParam:function(param){
		var objP;
		if(typeof(param)=='string') {
			eval('var objPrm='+param);
			objP=objPrm;
		} else if(typeof(param)=='object') {
			objP=param;
		} else {
			return;
		}

		if(objP.treename!=undefined) this.treename=objP.treename;
		if(objP.showmark!=undefined) this.showmark=objP.showmark;
		if(objP.hiddenmark!=undefined) this.hiddenmark=objP.hiddenmark;
		if(objP.showimg!=undefined) this.showimg=objP.showimg;
		if(objP.hiddenimg!=undefined) this.hiddenimg=objP.hiddenimg;
		if(objP.imgflag!=undefined) this.imgflag=objP.imgflag;

		if(objP.idtype!=undefined) this.idtype=objP.idtype;
		if(objP.showflag!=undefined) this.showflag=objP.showflag;
	},
	setTreeData:function(data){
		this.treedata=data;
	},
	writeContent:function(objContents,level,parentName,contentsID,bIsEdge){
		var i;
		var str="";
		var len=0;
		var contentsName='';
		var objChildList=null;
		var objChildURL=null;
		var showflag;
		var id;

		str='<p class="nowrap">';
		for(i=1;level>i;i++){
			if(!bIsEdge[i]){
				str+='│';
			}else{
				str+='　';
			}
		}
		if(level>0){
			if(!bIsEdge[level]){
				str+='├';
			}else{
				str+='└';
			}
		}

		if(this.idtype==0){
			if(typeof(objContents.text)=='string') contentsName=objContents.text;
		} else {
			contentsName=contentsID;
		}
		id=parentName+'_'+contentsID;
		showflag=true;
		if(typeof(objContents.showflag)=='boolean') {
			showflag=objContents.showflag;
		} else if(typeof(this.showflag)=='boolean') {
			showflag=this.showflag;
		}

		if(objContents.child!=null) {
			objChildList=objContents.child;

			str+='<a class="brunch" onclick="exMenu.mswitch(\''+this.top+"','"+id+'\')"><span class="brunch" id="'+id+'_'+'">';
			if(showflag) {
				str+=(this.imgflag) ? '<img src=\''+this.showimg+'\'>' : this.showmark;
			} else {
				str+=(this.imgflag) ? '<img src=\''+this.hiddenimg+'\'>' : this.hiddenmark;
			}
			str+='</span></a>';
		} else if(objContents.childurl!=null) {
			objChildURL=objContents.childurl;

			this.setEdge(id,bIsEdge);
			str+='<a class="brunch" onclick="exMenu.mswitchDYN(\''+
				this.top+"',{pid:'"+parentName+'\',id:\''+id+'\',level:'+(1+level)+',url:['+ArraytoString(objChildURL)+'],edge:['+this.edgeList[id]+']})"><span class="brunch" id="'+id+'_'+'">';
			showflag=false;
			str+=(this.imgflag) ? '<img src=\''+this.hiddenimg+'\'>' : this.hiddenmark;
			str+='</span></a>';

		} else {
			str+='<span class="nobrunch">・</span>';
		}
		if(typeof(objContents.url)=='string') {
			if(typeof(this.script.link)=='string'){
				str+='&nbsp;<a class="nowrap" href="javascript:void(0);" onclick="' 
					+ this.script.link + '('+"'"+objContents.url+"'"+')">'+contentsName+'</a><br>';
			} else {
				str+='&nbsp;<a class="nowrap" href="'+objContents.url+'")">'+contentsName+'</a><br>';
			}
		} else {
			str+='&nbsp;'+contentsName+'<br>';
		}
		str+='</p>';

		if(objChildList!=null || objChildURL!=null){
			str+='<div class="nowrap" id="'+id+'" ';
			if(showflag) {
				str+='style="DISPLAY:block">';
			} else {
				str+='style="DISPLAY:none">';
			}
			if(objChildList!=null) {
				len=objChildList.length;
				var childID;
				for(i=0;len>i;i++){
					bIsEdge[level+1]=(len-1==i);
					if(this.idtype==0){
						childID=i;
					} else {
						childID=objChildList[i].text;
					}
					str+=this.writeContent(objChildList[i],level+1,id,childID,bIsEdge);
				}
			}
			str+='</div>';
		}
		return str;
	},
	setEdge:function(id,edge){
		if(this.edgeList==null) this.edgeList=new Object();
		this.edgeList[id]=edge;
	},
	createTree:function(top){
		this.sendRequest=sendRequest;
		this.top=top;
		var bIsEdge;
		
		if(typeof(this.treedata)=='string') {
			if(typeof(this.script.progress)=='object' &&
			   typeof(this.script.progress.prog_start)=='function') {
				this.script.progress.prog_start();
			}
			var bIsEdge=new Array();
			bIsEdge[0]=true;

			var id;
			if(typeof(this.top)=='string'){
				id=this.top;
			}else {
				id = getDiv(this.top);
			}
			var treename;
			if(this.treename!='') treename=this.treename;

			var func=this.on_loaded1; // 苦肉の策
			func(null,{tree:this,treename:treename,id:id,level:0,edge:bIsEdge});
			this.sendRequest(func,'','GET',this.treedata,false,true);

			if(typeof(this.script.progress)=='object' &&
			   typeof(this.script.progress.prog_start)=='function') {
				this.script.progress.prog_stop();
			}

		} else if(typeof(this.treedata)=='object') {
			var bIsEdge=new Array();
			bIsEdge[0]=true;
			var id;
			var childID;
			if(typeof(this.top)=='string'){
				if(this.idtype==0){
					id = this.top;
					childID = 0;
				} else {
					id = this.top;
					childID=this.treedata.text;
				}
			}else {
				id = getDiv(this.top);
				childID=this.treedata.text;
			}
			if(this.treename!='') id=id+'_'+this.treename;

			var str = this.writeContent(this.treedata,0,id,childID,bIsEdge);
			exMenu.changeDiv(this.top,str);
			this.createTreeData=str;
		}
		exMenu.add(this);
	},
	on_loaded1:function(oj,param){
		var P;
		if(typeof(param)!='undefined') {
			var obj;
			this.P=param;
			return;
		}
		var bIsEdge;
		if(typeof(this.P.edge)!='undefined') {
			bIsEdge = this.P.edge;
		} else {
			bIsEdge = new Array();
			bIsEdge[0] = true;
		}
		var id=this.P.id;
		var topname;
		if(typeof(this.P.treename)!='undefined') {
			topname=id+'_'+this.P.treename;
		} else {
			topname=id;
		}

		eval('var res='+oj.responseText);

		var objTree = this.P.tree;
		var childID;
		if(objTree.idtype==0){
			if(typeof(this.P.contentsID)=='undefined') {
				childID=0;
			} else {
				childID=this.P.contentsID;
			}
		} else {
			childID=res.text;
		}
		var level=this.P.level;
		var str = objTree.writeContent(res,level,topname,childID,bIsEdge);
		if(this.P.amode) {
			exMenu.addDiv(id,str);
		} else {
			exMenu.changeDiv(id,str);
		}
		objTree.createTreeData=str;
	}
}
