/*-------------------------------------------------------
treeObject.js
depends on prototype.js(http://prototype.conio.net/)
@version  V1.0
@author   youngjin.shin (2007/04/20)
Copyright (c) 2007 Rakuten Travel, Inc. All Rights Reserved.
--------------------------------------------------------*/
Util.createTreeObject = function  ( id ) {
	   if(this.instances == null) this.instances = new Array(); 	   
	   var instance = null;
  	   for (var i = 0; i < this.instances.length ; i++) {
  		    if( this.instances[i].id == id ){
  			return this.instances[i];
  			}
  		}
  		instance = new Util.TreeObject(id);
  		this.instances.push(instance);	 
    },

Util.TreeObject.prototype = {
		id 	   : null ,
  		Parent : null,
   		Code   : null, 
   		Name   : null,
   		Depth  : null, 
		Children  : null,
		pIndex : 0, 
		
   		initialize:function(code ,name ,parent ) {
   		   this.Code   = code;
   		   this.Name   = name;
   		   this.Children  = new Array();
   		   if(parent){
   		   	parent.addChild(this);
   		   	this.Depth = parent.Depth + 1; 
   		   } else {
   		    this.Depth = 0;
   		   }
   		},

		addChild:function(child){
			if(child){
				child.setParent(this);
				this.Children.push(child);
			}
		},

		findChild:function(code){
			if(!code) {
				return null;
			}
			for(var i = 0 ;i < this.Children.length ; i++){
				if( this.Children[i].getCode() == code ){
					return this.Children[i];
				}
			}
			return null;
		},
/*
		findChildAll:function(code){
		    // initiate
			var ret   = null;
			var index = 0;
			
			// When the code is empty then return value of null;  
			if(!code) {
				return ret;
			}
			
			for(var i=this.pIndex ; i <this.pIndex+this.Children.length ;i++ ){
			    var index = i;
			    if( i >= this.Children.length){
			    	index = i - this.Children.length;
			    }
			    
				if(this.Children[index].getCode() == code ){
				  	ret = this.Children[index];
				  	this.pIndex = index;
				  	break;
				}
				if(this.Children[index].hasChild()){
					ret = this.Children[index].findChildAll(code);
					if(ret){
				  		this.pIndex = index;
						break;
					}
				}
			}
			return ret;
		},
*/		
		findChildAll:function(code,depth){
		    // initiate
			var ret   = null;
			var index = 0;
			// When the code is empty then return value of null;  
			if(!code) {
				return ret;
			}
			if( depth && this.Depth > depth - 1){
				return ret;
			}
			for(var i=this.pIndex ; i <this.pIndex+this.Children.length ;i++ ){
			    var index = i;
			    if( i >= this.Children.length){
			    	index = i - this.Children.length;
			    }
			    
				if(this.Children[index].getCode() == code && (!depth || this.Depth == (depth - 1))){
				  	ret = this.Children[index];
				  	this.pIndex = index;
				  	break;
				}
				if(this.Children[index].hasChild()){
					ret = this.Children[index].findChildAll(code,depth);
					if(ret){
				  		this.pIndex = index;
						break;
					}
				}
			}
			return ret;
		},
				
		hasChild:function(){
			if(this.Children.length > 0){
				return true;
			}
			return false;
		},

		hasParent:function(){
			if(this.Parent){
				return true;
			}
			return false;
		},

		setParent:function(parent){
   			this.Parent = parent;
   		},

		getParent:function(){
   			return this.Parent;
   		},
		
		getChildren:function(){
			return this.Children;
		},

   		getCode :function (){
   			return this.Code;
   		},

   		getName: function (){
   			return this.Name;
   		},
   		
   		getOptionalList:function(){
   			var ret = new Array();
   			for(var i=0 ; i< this.Children.length; i++){
   				if(this.Children[i]){
   					ret.push(this.Children[i].getCode());
   					ret.push(this.Children[i].getName());
   				}
   			}
   			return ret;
   		}
}