//
// yurichan.js
//
YAHOO.namespace('yurichan');
YAHOO.yurichan.tree = function(id) {

	var use_clipicon = false;
  var keyword = "hogehoge";

//  if (id.match(/^treeDivSideLink/) != null) {
  if (id == "treeDivSideLink") {
		keyword = expandnode;
//  } else if (id.match(/^treeDivSideCA/) != null) {
//		keyword = searchstring;
//	} else {
	}

  this.tree = new YAHOO.widget.TreeView(id);
  
  this.mkTreeByArray = function (treeData,treeNode) {
    if(!treeNode)treeNode = this.tree.getRoot(); 

    for (var i = 0; i < treeData.length; i++) {

      //if(!(treeData[i][0]=="_open"||treeData[i][0]=="_close"||treeData[i][0]=="_load")){
      if(!(treeData[i][0]=="_load")){
        var tmpNode = new YAHOO.widget.TextNode(treeData[i][0], treeNode, false);

        if(typeof treeData[i][1] == "string") {
	  			tmpNode.href= treeData[i][1];
					tmpNode.target= "";

					//if (use_clipicon) {
	  		//		if (treeData[i][0].charAt(0) == "▼") {
  	    //			tmpNode.labelStyle = "icon-clipr";
	  	//			} else {
  	   // 			tmpNode.labelStyle = "icon-clip";
      	//		}
					//}

				}
				if(typeof treeData[i][1] == "object") {
          this.mkTreeByArray(treeData[i][1], tmpNode); 

	  			if (treeData[i][1][0][0]=="_load") {
	    			YAHOO.yurichan.loadTreeData(this, tmpNode, treeData[i]);
	    //if (treeData[i][0].match(keyword) != null) {
	    //   tmpNode.expand();
			//}
	  //} else if ((treeData[i][1][0]=="_open") || (treeData[i][0].match(keyword) != null)) {
	  //} else if ((treeData[i][1][0]=="_open") || (treeData[i][0] == keyword)) {
	  			} else if (treeData[i][0] == keyword) {
	    			tmpNode.expand();
					}
        }
      }
    }
    this.tree.draw();
  }
}

YAHOO.yurichan.loadTreeData = function(oj, tmpNode, treeDataFrg) {

  if(!!YAHOO.util.Connect){
      if(treeDataFrg[1][0][1]){
        tmpNode.method=(treeDataFrg[1][0][1].method)?treeDataFrg[1][0][1].method:"GET";
        tmpNode.url=(treeDataFrg[1][0][1].url)?treeDataFrg[1][0][1].url:"";
      }
      tmpNode.setDynamicLoad(
        function (node,onCompleteCallback ){
          tmpNode =new YAHOO.widget.Node("",tmpNode.parent,false);

          var delay = YAHOO.yurichan.loadTreeData.delay ;
          if(YAHOO.yurichan.loadTreeData.delay>0)setTimeout(onCompleteCallback,delay);
          else onCompleteCallback();
        }
      );
      
      oj.tree.onExpand= function(node) {
				if (node.dynamicLoadComplete) {
          if(node.children.length<=0){
            YAHOO.util.Connect.asyncRequest(node.method, node.url, {
							success: getResponse,
              argument: {'node':node},
							scope: oj
            }, null);
          }
				}
      }
      getResponse = function(oj) {
        data = eval(oj.responseText);
        this.mkTreeByArray (data,oj.argument.node); 
      } 
  }
}

YAHOO.yurichan.loadTreeData.delay = 0;
