/**
 * カテゴリツリーを作成するクラスです。<br />
 * カテゴリツリー一つに対して一つのインスタンスを作成します。<br />
 * 初期化はコンストラクタによって行われますが、描画はdrawメソッドを呼び出すことで行われます。
 * 
 * @namespace com.nec.jp.ngsf.wiki
 * @class SDWikiCategory
 * @uses YAHOO.widget.TreeView
 * @param containerId {string} カテゴリツリーを作成するコンテナ（divエレメント）のID
 * @param cookieId {string} Cookieに値を保存するためのID
 * @constructor
 */
com.nec.jp.ngsf.wiki.SDWikiCategoryTree = function(containerId, cookieId) {
	if (containerId) {
		if (cookieId) {
			this.initialize(containerId, cookieId);
		} else {
			this.initialize(containerId);
		}
	}
};
com.nec.jp.ngsf.wiki.SDWikiCategoryTree.prototype = {
	/**
	 * ツリービューのオブジェクト。
	 * @property tree
	 * @type YAHOO.widget.TreeView
	 */
	tree: null,
	
	/**
	 * カテゴリツリーを作成するコンテナ（divエレメント）のID。
	 * @property containerId
	 * @type string
	 */
	containerId: '',
	
	/**
	 * コンテナIDのプレフィックス。
	 * @property containerIdPrefix
	 * @type string
	 * @default 'gleanfeed-wiki-plugin-category'
	 */
	containerIdPrefix: 'gleanfeed-wiki-plugin-category',
	
	/**
	 * ツリー構造を表現する配列データ。
	 * @property treeData
	 * @type string[]
	 */
	treeData: null,
	
	/**
	 * ルートのカテゴリ。
	 * @property rootCategory
	 * @type string
	 */
	rootCategory: '/',
	
	/**
	 * 表示中のカテゴリ。（ページを表示中の場合、ページが保存されているカテゴリ）
	 * @property dispCategory
	 * @type string
	 */
	dispCategory: '/',
	
	/**
	 * 非同期読み込みフラグ。
	 * @property isDynamic
	 * @type boolean
	 */
	isDynamic: true,
	
	/**
	 * メニュー表示フラグ。
	 * @property menu
	 * @type boolean
	 */
	menu: true,
	
	/**
	 * Cookieに値を保存する場合のID。
	 * @property cookieId
	 * @type string
	 */
	cookieId: '',
	
	/**
	 * カテゴリツリーを初期化します。<br />
	 * 各プロパティの設定を行い、_makeTreeメソッドを呼び出します。
	 * @method initialize
	 * @param containerId {string} カテゴリツリーを作成するコンテナ（divエレメント）のID
	 */
	initialize: function(containerId, cookieId) {
		this.containerId = containerId;
		
		if (cookieId) {
			this.cookieId = cookieId;
		}
		
		var containerElm = document.getElementById(this.containerId);
		
		// 表示中のカテゴリを設定
		if (document.getElementById('gleanfeed-wiki-category')) {
			this.dispCategory = document.getElementById('gleanfeed-wiki-category').value;
		}
		
		// ルートカテゴリを設定
		var rootCategoryElm = document.getElementsByClassName('rootCategory', containerElm);
		if (rootCategoryElm && rootCategoryElm[0]) {
			this.rootCategory = rootCategoryElm[0].value;
		}
		
		// 非同期読み込みフラグを設定
		var isDynamicElm = document.getElementsByClassName('isDynamic', containerElm);
		if (isDynamicElm && isDynamicElm[0]) {
			this.isDynamic = Boolean(eval(isDynamicElm[0].value));
		}
		
		// メニュー表示フラグを設定
		var menuElm = document.getElementsByClassName('menu', containerElm);
		if (menuElm && menuElm[0]) {
			this.menu = Boolean(eval(menuElm[0].value));
		}
		
		// ツリーデータを設定
		var treeDataElm = document.getElementsByClassName('treeData', containerElm);
		if (treeDataElm && treeDataElm[0]) {
			this.treeData = eval(treeDataElm[0].value);
		}
		
		// ツリーの作成
		this._makeTree();
	},

	/**
	 * カテゴリツリーを描画します。
	 * @method draw
	 */
	draw: function() {
		if (this.tree != null) {
			this.tree.draw();
		}
	},
	
	/**
	 * ツリーを作成します。
	 * @method _makeTree
	 * @private
	 */
	_makeTree: function() {
		// ツリーを配置するdivのエレメントを取得
		var containerElm = document.getElementById(this.containerId);
		var treeElm = document.getElementsByClassName(this.containerIdPrefix, containerElm)[0];
		treeElm.id = this.containerId + '-tree';
		
		// ツリーを作成
		this.tree =  new YAHOO.widget.TreeView(treeElm.id);
		this.isMenuDisp = this.menu;
		
		// イベントを設定
		this._setCustomEvent(this.tree);
		
		// 非同期読み込み時の処理を設定
		if (this.isDynamic) {
			this.tree.setDynamicLoad(this._loadNodeData);
		}
		
		// ノードの追加
		var root = this.tree.getRoot();
		this._appendNode(this.treeData[0][4], root);
		
		// ルートが選択されていた場合のハイライト
		if (this.dispCategory == this.rootCategory) {
			var rootElms = document.getElementsByClassName(this.containerIdPrefix + '-top', containerElm);
			if (rootElms[0]) {
				rootElms[0].className =	rootElms[0].className + '-selected';
			}
		}
	},
	
	/**
	 * ツリーにノードを追加します。
	 * @method _appendNode
	 * @param nodeData {string[]} ノードを作成するための配列データ
	 * @param parentNode {Node} 親ノードオブジェクト
	 * @private
	 */
	_appendNode: function(nodeData, parentNode) {
		if (nodeData != undefined && nodeData.length > 0) {
			var ln = nodeData.length;
			for (var i = 0; i < ln; i++) {
				if (nodeData[i] != undefined) {
					// ノードの作成
					var cNode = new YAHOO.widget.TextNode(nodeData[i][1], parentNode, false);
					cNode.href = 'wiki.view_file.form?file=' + nodeData[i][3];
					cNode.fullName = nodeData[i][2];
					cNode._appendNode = this._appendNode; // ノード追加処理を設定 (TODO 継承とかしたい)

					// トグルアイコンの表示制御
					if (nodeData[i][0] == 'leaf') {
						cNode.isLeaf = true;
					}
					
					// 表示中のカテゴリによってツリーを開く
					if ((nodeData[i][0] == 'node_open') && (nodeData[i][4] != undefined)) {
						cNode.expand();
					}
					
					// 表示中のカテゴリをハイライト
					if (cNode.fullName == this.dispCategory) {
						cNode.selected = true;
					}

					// サブカテゴリを再帰的に作成
					if ((nodeData[i][4] != undefined) && (nodeData[i][4].length > 0)){
						this._appendNode(nodeData[i][4], cNode);
						if (this.isDynamic) {
							cNode.dynamicLoadComplete = true;
						}
					}
				}
			}
		}
	},
	
	/**
	 * ノードデータの非同期読み込みを行います。<br />
	 * 読み込み終了後、ノードを子として追加します。
	 * @method _loadNodeData
	 * @param node {YAHOO.widget.Node} 子のデータを読み込むノード。
	 * @param fnLoadComplete {function} 終了時の処理関数。（Yahoo UI TreeViewでセットされます）
	 * @private
	 */
	_loadNodeData: function(node, fnLoadComplete) {
		// DWR非同期処理を呼ぶ
		WikiPluginController.processAction("util_categorytree", new Array("getNodeData", node.fullName), function(oResponse) {
			if (oResponse != null && oResponse.length > 0) {
				var nodeData = eval(oResponse[0]);
				node._appendNode(nodeData[0][4], node);
				fnLoadComplete();
			}
		});
	},
	
	/**
	 * 追加のイベントをカテゴリツリーに設定します。<br />
	 * カテゴリプラグインではCookieによる開閉状態の維持をするためのイベントを設定します。
	 * （Cookieの生存期間はセッション期間となります）
	 * @method _setCustomEvent
	 * @param tree {Yahoo.widget.TreeView} イベントを設定するツリーオブジェクト。
	 * @private
	 */
	_setCustomEvent: function(tree) {
	}
};
