/**
 * ファイル選択ダイアログのカテゴリツリーを表すクラスです。
 * 
 * @namespace com.nec.jp.ngsf.wiki
 * @class SDWikiFileSelect
 * @extends com.nec.jp.ngsf.wiki.SDWikiCategory
 * @uses YAHOO.widget.TreeView
 * @param containerId {string} カテゴリツリーを作成するコンテナ（divエレメント）のID
 * @constructor
 */
com.nec.jp.ngsf.wiki.SDWikiFileSelect = function(containerId, tabs) {
	if (containerId) {
		this.initialize(containerId, tabs);
	}
};
com.nec.jp.ngsf.wiki.SDWikiFileSelect.prototype =
	Object.extend(new com.nec.jp.ngsf.wiki.SDWikiCategoryTree, {
	
	/**
	 * コンテナIDのプレフィックス。
	 * @property containerIdPrefix
	 * @type string
	 * @default 'gleanfeed-wiki-fileselect-category'
	 */
	containerIdPrefix: 'gleanfeed-wiki-fileselect-category',
	
	/**
	 * 非同期読み込みフラグ。
	 * @property isDynamic
	 * @type boolean
	 */
	isDynamic: true,
	
	/**
	 * メニュー表示フラグ。
	 * @property menu
	 * @type boolean
	 */
	menu: false,
	
	/**
	 * 表示タブのリストを保持する配列。
	 * @property tabs
	 * @type string[]
	 * @default ['page']
	 */
	tabs: ['page'],
	
	/**
	 * 画面が閉じられた場合のコールバック処理
	 * @property closeCallback
	 * @type function
	 * @default null
	 */
	closeCallback: null,

	/**
	 * カテゴリツリーを初期化します。<br />
	 * 各プロパティの設定を行い、_makeTreeメソッドを呼び出します。
	 * @method initialize
	 * @param containerId {string} カテゴリツリーを作成するコンテナ（divエレメント）のID
	 * @param tabs {string[]} 表示するタブのリスト
	 */
	initialize: function(containerId, tabs) {
		this.tabs = tabs;
		this.containerId = containerId;
		
		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);
		}
		
		// ルートカテゴリのイベントをセット
		var topElm = document.getElementsByClassName(this.containerIdPrefix + '-top', containerElm);
		if (topElm && topElm[0]) {
			var anchorElm =	topElm[0].getElementsByTagName('a');
			if (anchorElm && anchorElm[0]) {
				var href = 'javascript:WikiEditPageController.getFileListForm(\'/\', [';
				for (var j = 0; j < this.tabs.length; j++) {
					href = href + '\'' + this.tabs[j] + '\',';
				} 
				href = href + '], com.nec.jp.ngsf.wiki.SDWikiFileSelect.selectGetPageListCallback);';
				
				anchorElm[0].href = href;
			}
		}

		// ツリーの作成
		this._makeTree();
	},
	
	/**
	 * カテゴリツリーの選択されているカテゴリーをハイライトします。
	 * @method hilightNode
	 * @param categoryTitle {string} 選択されたカテゴリのタイトル
	 */
	hilightNode: function(categoryTitle) {
		var treeElm = this.tree.getEl();
		
		// 選択されていたノードのスタイルを初期化
		var selectedNodeElms =
			document.getElementsByClassName('gleanfeed-wiki-plugin-category-ygtvline-selected', treeElm);
		if (selectedNodeElms && selectedNodeElms[0]) {
			selectedNodeElms[0].className = 'gleanfeed-wiki-plugin-category-ygtvline';
		}
		var containerElm = document.getElementById(this.containerId);
		var topElms = document.getElementsByClassName(this.containerIdPrefix + '-top-selected', containerElm);
		if (topElms && topElms[0]) {
			topElms[0].className = this.containerIdPrefix + '-top';
		}
		
		// 選択したノードのスタイルを変更
		if (categoryTitle == '/') {
			// ルート
			var topElms = document.getElementsByClassName(this.containerIdPrefix + '-top', containerElm);
			if (topElms && topElms[0]) {
				topElms[0].className = this.containerIdPrefix + '-top-selected';
			}
		} else {
			// ルート以外
			var nodes = this.tree._nodes;
			if (nodes) {
				for (var i = 0; i < nodes.length; i++) {
					if (nodes[i] && (nodes[i] != null) && (nodes[i].fullName == categoryTitle)) {
						var nodeElm = nodes[i].getEl();
						var tableElms =
							document.getElementsByClassName('gleanfeed-wiki-plugin-category-ygtvline', nodeElm);
						tableElms[0].className =
							'gleanfeed-wiki-plugin-category-ygtvline-selected';
						break;
					}
				}
			}
		}
	},
	
	/**
	 * ツリーにノードを追加します。
	 * @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 = 'javascript:WikiEditPageController.getFileListForm(com.nec.jp.ngsf.wiki.escapeForJSArg(\'' +
							nodeData[i][2] + '\'), [';
					for (var j = 0; j < this.tabs.length; j++) {
						cNode.href = cNode.href + '\'' + this.tabs[j] + '\',';
					} 
					cNode.href = cNode.href + '], com.nec.jp.ngsf.wiki.SDWikiFileSelect.selectGetPageListCallback);';
					cNode.fullName = nodeData[i][2];
					cNode._appendNode = this._appendNode; // ノード追加処理を設定 (TODO 継承とかしたい)
					cNode.tabs = this.tabs;
					
					// トグルアイコンの表示制御
					if (nodeData[i][0] == 'leaf') {
						cNode.isLeaf = true;
					}
					
					// 表示中のカテゴリによってツリーを開く
					if ((nodeData[i][0] == 'node') && (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);
						cNode.dynamicLoadComplete = true;
					}
				}
			}
		}
	}
	
});

/**
 * ファイル選択ダイアログのカテゴリツリーを保持します。
 * @property com.nec.jp.ngsf.wiki.SDWikiFileSelect.treeView
 * @type com.nec.jp.ngsf.wiki.SDWikiFileSelect
 * @default null
 * @static
 */
com.nec.jp.ngsf.wiki.SDWikiFileSelect.treeView = null;

/**
 * ファイル選択ダイアログで選択されたファイルのタイトルを保持します。<br />
 * ファイル選択ダイアログを表示する際に初期化され、ファイルを選択すると値が設定されます。<br />
 * ファイル選択ダイアログを呼び出した処理中で、ダイアログを閉じた後にこのプロパティを参照し、
 * ファイル選択以降の処理を行います。
 * @property com.nec.jp.ngsf.wiki.SDWikiFileSelect.selectedFileTitle
 * @type string
 * @default null
 * @static
 */
com.nec.jp.ngsf.wiki.SDWikiFileSelect.selectedFileTitle = null;

/**
 * ファイル選択ダイアログで選択されたファイルの種類を保持します。
 * @property com.nec.jp.ngsf.wiki.SDWikiFileSelect.selectedFileType
 * @type string
 * @default null
 * @static
 */
com.nec.jp.ngsf.wiki.SDWikiFileSelect.selectedFileType = null;

/**
 * com.nec.jp.ngsf.wiki.SDWikiFileSelect.selectedFileTypeに設定される値「ページ」
 * @property com.nec.jp.ngsf.wiki.SDWikiFileSelect.FILETYPE_PAGE
 * @type string
 * @static
 * @constant
 */
com.nec.jp.ngsf.wiki.SDWikiFileSelect.FILETYPE_PAGE = "page";

/**
 * com.nec.jp.ngsf.wiki.SDWikiFileSelect.selectedFileTypeに設定される値「テンプレート」
 * @property com.nec.jp.ngsf.wiki.SDWikiFileSelect.FILETYPE_TEMPLATE
 * @type string
 * @static
 * @constant
 */
com.nec.jp.ngsf.wiki.SDWikiFileSelect.FILETYPE_TEMPLATE = "template";

/**
 * com.nec.jp.ngsf.wiki.SDWikiFileSelect.selectedFileTypeに設定される値「画像」
 * @property com.nec.jp.ngsf.wiki.SDWikiFileSelect.FILETYPE_IMAGE
 * @type string
 * @static
 * @constant
 */
com.nec.jp.ngsf.wiki.SDWikiFileSelect.FILETYPE_IMAGE = "image";

/**
 * com.nec.jp.ngsf.wiki.SDWikiFileSelect.selectedFileTypeに設定される値「アップロードファイル」
 * @property com.nec.jp.ngsf.wiki.SDWikiFileSelect.FILETYPE_UPLOADFILE
 * @type string
 * @static
 * @constant
 */
com.nec.jp.ngsf.wiki.SDWikiFileSelect.FILETYPE_UPLOADFILE = "uploadfile";

/**
 * ファイル選択ダイアログを表示します。
 * @method com.nec.jp.ngsf.wiki.SDWikiFileSelect.open
 * @param categoryName {string} 
 * @param tabList {string[]} 
 * @param closeCallback {function} 
 * @static
 */
com.nec.jp.ngsf.wiki.SDWikiFileSelect.open = function(categoryName, tabs, closeCallback) {
	com.nec.jp.ngsf.wiki.SDWikiFileSelect.selectedFileTitle = null;
	com.nec.jp.ngsf.wiki.SDWikiFileSelect.selectedFileType = null;
	
	try {
		WikiEditPageController.getPopupForm("select-file", categoryName, function(oResponse) {
				if (oResponse != undefined && oResponse.length > 0) {
					// ポップアップウィンドウを表示する
					popupWindow.open(oResponse, "popup", "gleanfeed-wiki-popup-title", 300, 150, true, closeCallback);
					
					// ポップアップウィンドウのカテゴリツリー領域を初期化する
					if (document.getElementById('gleanfeed-wiki-fileselect-category') != undefined) {
						com.nec.jp.ngsf.wiki.SDWikiFileSelect.treeView =
							new com.nec.jp.ngsf.wiki.SDWikiFileSelect('gleanfeed-wiki-fileselect-category', tabs);
						com.nec.jp.ngsf.wiki.SDWikiFileSelect.treeView.draw();
					}
					
					// ページ/テンプレートのリストを表示する。
					WikiEditPageController.getFileListForm(
						com.nec.jp.ngsf.wiki.escapeForJSArg(categoryName),
						tabs,
						com.nec.jp.ngsf.wiki.SDWikiFileSelect.selectGetPageListCallback);
				}
				com.nec.jp.ngsf.wiki.close_popup(); // TODO;may be bug, should call here to close existing popup.
			});
	} catch (e) {
		window.alert(e.description);
	}
};

/*
 * カテゴリが選択された際のページ・テンプレート表示処理。
 * @method com.nec.jp.ngsf.wiki.SDWikiFileSelect.selectGetPageListCallback
 * @param oResponse {string[]} 非同期処理のレスポンス。要素0にHTML断片が格納されている。
 * @static
 */
com.nec.jp.ngsf.wiki.SDWikiFileSelect.selectGetPageListCallback = function(oResponse) {
	if (oResponse && oResponse[0]) {
		document.getElementById('gleanfeed-wiki-popup-form-contentslist').innerHTML = oResponse[0];
	}

	// 表示中のカテゴリを選択
	if ((com.nec.jp.ngsf.wiki.SDWikiFileSelect.treeView != null) && oResponse[1]) {
		com.nec.jp.ngsf.wiki.SDWikiFileSelect.treeView.hilightNode(oResponse[1]);
	}
};
/*
 * 選択された引用先ページ名、ファイルのタイプを設定しウィンドウを閉じます。
 * @method com.nec.jp.ngsf.wiki.SDWikiFileSelect.selectFile
 * @param fileTitle {string}
 * @param fileType {string}
 * @static
 */
com.nec.jp.ngsf.wiki.SDWikiFileSelect.selectFile = function(fileTitle, fileType) {
	// 選択されたファイルの情報をstaticプロパティに保持
	com.nec.jp.ngsf.wiki.SDWikiFileSelect.selectedFileTitle = fileTitle;
	com.nec.jp.ngsf.wiki.SDWikiFileSelect.selectedFileType = fileType;

	popupWindow.close(0);
};
