var GoTreeViewer = Class.create();
Object.extend(GoTreeViewer.prototype, TreeViewer.prototype);
Object.extend(GoTreeViewer.prototype, {
	// インスタンス化時の処理を行います。(オーバーライド)
	_processInitialize: function() {
		// システム設定項目
		// 文字の大きさ
		this._fontSize = '1.0em';
		// ツリービューのレベル間の幅
		this._levelDifferenceWidth = '16px';
		// now loading...画像の高さ, 幅 (高さ, 幅には多少のmarginを追加して設定します。)
		this._loadingImgHeight = '20px';
		this._loadingImgWidth = '20px';
		// ツリービューのノードの状態を表す画像の高さ, 幅, マージン
		this._nodeImgHeight = '12px';
		this._nodeImgWidth = '12px';
		this._nodeImgMargin = '1px 2px';
		// ツリービューのノードの高さ,パディング
		this._nodeHeight = '14px';
		this._nodePadding = '2px 0px';
		
		// ツリーにおける操作対象の親オブジェクト
		this._operationalParentObj = null;
		// ツリーにおける操作対象のオブジェクト
		this._operationalObj = null;
		// ツリーにおけるクリック時に強調したオブジェクト
		this._tmpEmphaticObj = null;
		// ノードを展開するために子ノードを一時的に保存するオブジェクト
		this.tmpOperationalChildrenObj = null;
		
		var treeView = $(this.treeId);
		if (treeView) {
			Element.setStyle(treeView, {
					'fontSize': this._fontSize,
					'backgroundColor': '#ffffff',
					'margin': '0px',
					'padding': '0px'});
		} else {
			throw 'Error: Not found the tree view id.'; 
		}
	}, 
	
	// 指定したtermIdによるツリービューを表示します。(新規メソッド)
	displayViewByTermId: function(termId) {
		var param = this.createParameter('createViewByTermId', null, termId);
		this._operationalParentObj = $(this.treeId);
		this._createRequest(param);
	},
	
	// パラメーターを作成します。(オーバーライド)
	createParameter: function(action, nodeId, termId) {
		var param = new String;
		param += "action=" + encodeURIComponent(action);
		if (nodeId) {
			param += "&nodeId=" + encodeURIComponent(nodeId);
		}
		if (termId) {
			param += "&termId=" + encodeURIComponent(termId);
		}
		
		return param;
	},
	
	// ツリーにstyleをセットします。(オーバーライド)
	_setStyleTree: function() {
		var uls = this._operationalParentObj.select('ul');
		uls.each(function(ul) {
			var marginLeft = this._getTreeLevelMarginLeft(ul);
			Element.setStyle(ul, {
				'marginBotton': '0px',
				'marginLeft': marginLeft,
				'marginRight': '0px',
				'marginTop': '0px',
				'padding': '0px'
				});
		}, this);

		var lis = this._operationalParentObj.select('ul li');
		lis.each(function(li) {
			Element.setStyle(li, {
				'listStyle': 'none',
				'margin': '2px 0px',
				'padding': '0px'
				});
		});
		
		var divs = this._operationalParentObj.select('ul li div');
		divs.each(function(div) {
			Element.setStyle(div, {
				'lineHeight': this._nodeHeight,
				'padding': this._nodePadding
				});
		}, this);
		
		var nodeImgs = this._operationalParentObj.select('ul li img');
		nodeImgs.each(function(nodeImg) {
			Element.setStyle(nodeImg, {
				'border': 'none',
				'height': this._nodeImgHeight,
				'margin': this._nodeImgMargin,
				'verticalAlign': 'middle',
				'width': this._nodeImgWidth
				}, this);
				
			if (nodeImg.readAttribute('class').match('open')) {
				this._changeNodeStatusOpenImg(nodeImg);
				Event.observe(nodeImg, 'click', this._processNodeOnClick.bind(this));
				
			} else if (nodeImg.readAttribute('class').match('close')) {
				this._changeNodeStatusCloseImg(nodeImg);
				Event.observe(nodeImg, 'click', this._processNodeOnClick.bind(this));
				
			} else if (nodeImg.readAttribute('class').match('leaf')) {
				nodeImg.writeAttribute('src', this.nodeStatusLeafImg);
			}
		}, this);
		
		var strongs = this._operationalParentObj.select('ul li div > strong');
		strongs.each(function(strong) {
			Element.setStyle(strong, {
				'margin': '0px 0px 0px 1px',
				'verticalAlign': 'middle'
			});
		});
		
		var spans = this._operationalParentObj.select('ul li div > span:first-of-type');
		spans.each(function(span) {
			Element.setStyle(span, {
				'margin': '0px 0px 0px 1px',
				'verticalAlign': 'middle'
				});
		});

		if (this.tmpOperationalChildrenObj) {
			this.tmpOperationalChildrenObj.each(function(altLi) {
				var lis = this._operationalParentObj.select('ul li');
				lis.each(function(li) {
					if (altLi.readAttribute('id') == li.readAttribute('id')) {
						Element.replace(li, altLi);
					}
				});
			}, this);
		}
		this.tmpOperationalChildrenObj = null;
		
		this._operationalParentObj = null;
	},
	
	// ノードの状態を表す画像でのクリック時のノードを展開する処理を行います。(オーバーライド)
	_processExpandNodeOnClick: function(nodeImg) {
		if (nodeImg.readAttribute('class').match('childIsCompleted')) {
			var childUl = nodeImg.up(1).down('ul');
			childUl.show();
			
			Element.removeClassName(nodeImg, 'close');
			Element.addClassName(nodeImg, 'open');
			this._changeNodeStatusOpenImg(nodeImg);
			
			var li = nodeImg.up('li', 0);
			this._accentuateNodeOnClick(li);
			
		} else {
			if (!this._checkRequestNow()) {
				var li = nodeImg.up(1);
				var childUl = li.down('ul', 0);
				
				var childUlIsExisted = false;
				var liChildren = li.childElements();
				liChildren.each(function(childLi) {
					if (childLi.match('ul')) {
						childUlIsExisted = true;
					}
				});
				
				if (childUlIsExisted) {
					this.tmpOperationalChildrenObj = childUl.childElements();
					if (this.tmpOperationalChildrenObj) {
						this.tmpOperationalChildrenObj.each(function(childLi) {
							childLi.remove();
						});	
					}
				}
				this._getChildNode(li);
				
				Element.removeClassName(nodeImg, 'childIsUncompleted');
				Element.addClassName(nodeImg, 'childIsCompleted');
				Element.removeClassName(nodeImg, 'close');
				Element.addClassName(nodeImg, 'open');
				this._changeNodeStatusOpenImg(nodeImg);	

				var li = nodeImg.up('li', 0);
				this._accentuateNodeOnClick(li);
			}
		}
	}
});