/*
 * YahooUI TreeView関連処理
 */

	var tree;		//ツリービューオブジェクト


	/*
	 * タブ情報取得
	 */
	function getContent() {

		var callback = {success:makeTree, failure:getContentErr};
		var url;
		var flg = document.getElementById("select_no").value;
		url = "./vpimap/VpImap_kt.php"

		try {
			YAHOO.util.Connect.setForm("view")
			//YAHOO.util.Connect.asyncRequest('GET', url, callback);
			YAHOO.util.Connect.asyncRequest('POST', url, callback);
		}
		catch (e) {
		}
	}

	/*
	 * 検索結果情報取得 ADD 2008/03/14 MIMURA
	 */
	function getContent2() {

		var callback = {success:makeTree2, failure:getContentErr};
		var url;
		var flg = document.getElementById("select_no").value;
		var url = './vpimap/VpImap_ktdata.php';

		try {
			YAHOO.util.Connect.setForm("view")
			//YAHOO.util.Connect.asyncRequest('GET', url, callback);
			YAHOO.util.Connect.asyncRequest('POST', url, callback);
		}
		catch (e) {
		}
		
	}


	/*
	 * タブ情報取得通信エラー
	 */
	function getContentErr () { }


	/*
	 * 子ノードの作成
	 * 親ノードを初めてクリックされたときに実行される関数
	 */
	function addnode(node, completeCallBack) {
		//var url = './data' + node.c_id + '.xml';
		var url = './vpimap/VpImap_ktdata.php';

		var callback = {success:makeNodes, failure:getNodeDataErr, argument:{node:node, callbackfunc:completeCallBack}};

		try {
			//カテゴリNOをhiddenにセット
			document.getElementById("select_ct").value = node.c_id;
			YAHOO.util.Connect.setForm("view")
			//YAHOO.util.Connect.asyncRequest('GET', url, callback);
			YAHOO.util.Connect.asyncRequest('POST', url, callback);
		}
		catch (e) {
			completeCallBack();
		}
	}


	/*
	 * 子ノードデータ取得通信エラー
	 */
	function getNodeDataErr (obj) {	
		obj.argument.callbackfunc();		
	}

	/*
	 * 地域情報取得
	 */
	function getArea() {

		var callback = {success:editListArea, failure:GetAreaErr};
		var url;

		//url = "./areal.xml"
		url = "./vpimap/VpImap_dt.php"

		try {
			YAHOO.util.Connect.setForm("view")
			//YAHOO.util.Connect.asyncRequest('GET', url, callback);
			YAHOO.util.Connect.asyncRequest('POST', url, callback);
		}
		catch (e) {
		}
	}


	/*
	 * 地域情報取得エラー
	 */
	function GetAreaErr () { }


	/*
	 * タブ選択
	 */
	function selectTab (flg) {

		var tabsArea = document.getElementById("tabs");
		var tabs = tabsArea.getElementsByTagName("li");
		var seltab = parseInt(flg);

		//アクティブタブの設定
		for (i=0; i < tabs.length; i++ ) {
	
			if ( i + 1 == seltab ) {
				tabs[i].id = "current";
			}
			else {
				tabs[i].id = "";
			}
		}

		//区分NOをhiddenにセット
		document.getElementById("select_no").value = flg;
		//プルダウン編集
		editList(flg);

	}


	/*
	 * プルダウン編集
	 */
	function editList (flg) {
		//ADD 2008/02/29 MIMURA プルダウンをJavaScriptで制御
		var element = document.getElementById('div01');
    	element.innerHTML = "<BR><select id=\"select_item\" name=\"select_item\" style=\"width:128px\" onchange=\"getContent()\"></select>"

		var sel = document.getElementById("select_item");
		//プルダウン初期化
		eraseList(sel);

		switch (flg) {
			case "1":		//月別
				//月別でプルダウン編集
				editListMonth(sel);
				//プルダウンの初期選択値でコンテンツ取得
				getContent();
				break;
			case "2":		//撮影月別
				editListEvent(sel);
				getContent();
				//ADD 2008/02/29 MIMURA　プルダウン不要
				element.innerHTML = "<BR><font size=2>撮影日別の表示</font>";
				break;
			case "3":		//地域
				//地域情報取得
				getArea();
				break;
			case "4":		//イベント タグ移動 2008/02/08 mimura
				//イベントでプルダウン編集
				editListEvent(sel);
				//プルダウンの初期選択値でコンテンツ取得
				getContent();
				break;
			case "5":		//検索 タグ追加 ADD 2008/03/14 MIMURA
				//検索編集
				getContent();
				//ADD 2008/02/29 MIMURA　プルダウン不要
				element.innerHTML = "<font size=2>例：「桜」や「谷川岳」（30字まで）</font><br><input name=\"select_item\" id=\"select_item\" type=text maxlength=30 size=29><input type=button value=\"検索\" onClick=\"sel5tag();\">";

				break;
		}		
	}

	function sel5tag() {
		var kword = document.getElementById('select_item').value;
		var check = true;
		if (kword.match("'") ){
			check = false;
		}
		if (kword.match(";") ){
			check = false;
		}
		if (kword.match("<") ){
			check = false;
		}
		if (kword.match(">") ){
			check = false;
		}
		if (kword.match(",") ){
			check = false;
		}

		if(check == false){
		document.getElementById('select_item_h').value = "";
		document.getElementById('select_item').value = "不正な文字です。";
		}

		document.getElementById('select_item_h').value = document.getElementById('select_item').value;
		document.getElementById('select_no').value="5";
		document.getElementById('select_ct').value="1";
		document.getElementById('select_dt').value="1";
		getContent2();
	}




	/*
	 * プルダウン編集 月別用
	 */
	function editListMonth (obj) {

		//システム日付取得
		var now = new Date();
		var yyyy = now.getYear();
		var nowYear = 2007;

		//IE以外用
		if (yyyy < 2000) {
			yyyy += 1900;			
		}

		//2007～システム年までを編集
		while ( nowYear <= yyyy ) {
			obj.length++;
			obj.options[obj.length - 1].text = nowYear.toString() + "年";
			obj.options[obj.length - 1].value = nowYear.toString();
			nowYear++;
		}

		//システム年をデフォルト選択
		obj.selectedIndex = obj.length - 1;
		
	}


	/*
	 * プルダウン編集 地域用
	 */
	function editListArea (obj) {

		var res = obj.responseXML;
		var xmlDoc = res.documentElement;
		var errCode = xmlDoc.getElementsByTagName("error_code");

		//サーバー処理結果判定
		if (errCode[0].firstChild.nodeValue != CODE_SUCCESS ) {
			return;
		}

		var details = xmlDoc.getElementsByTagName("detail");
		var ids = xmlDoc.getElementsByTagName("id");
		var names = xmlDoc.getElementsByTagName("name");
		var sel = document.getElementById("select_item");

		for (i = 0;i < details.length; i++) {
			sel.length++;
			sel.options[sel.length - 1].text = names[i].firstChild.nodeValue;
			sel.options[sel.length - 1].value = ids[i].firstChild.nodeValue;
		}

		//プルダウンの初期選択値でコンテンツ取得
		getContent();

	}


	/*
	 * プルダウン編集イベント用
	 */
	function editListEvent (obj) {

		var now = new Date();
		var startYear = now.getYear();

		//IE以外用
		if (startYear < 2000) {
			startYear += 1900;			
		}

		//システム年～システム年+2までを編集
		var EndYear = startYear + 2;
		while ( startYear <= EndYear ) {
			obj.length++;
			obj.options[obj.length - 1].text = startYear.toString() + "年";
			obj.options[obj.length - 1].value = startYear.toString();
			startYear++;
		}
		
	}


	/*
	 * 年、地域プルダウン全消去
	 */
	function eraseList(obj) {
		//プルダウン全消去
		while (obj.hasChildNodes()) {
			obj.removeChild(obj.lastChild);
		}
	}


	/*
	 * 子ノード作成
	 */
	function makeNodes(obj) {

		var childnode;	
		var res = obj.responseXML;
		var nodetitle;
		var xmlDoc = res.documentElement;
		var errCode = xmlDoc.getElementsByTagName("error_code");

		//サーバー処理結果判定
		if (errCode[0].firstChild.nodeValue != CODE_SUCCESS ) {
			obj.argument.callbackfunc();
			return;
		}

		var datas = xmlDoc.getElementsByTagName("data");
		var ids = xmlDoc.getElementsByTagName("id");
		var titles = xmlDoc.getElementsByTagName("title");
		var dates = xmlDoc.getElementsByTagName("date");

		for (i = 0;i < datas.length; i++) {
			nodetitle = titles[i].firstChild.nodeValue + "(" + dates[i].firstChild.nodeValue + ")";
			childnode = new YAHOO.widget.MenuNode(nodetitle, obj.argument.node, false);
			childnode.d_id = ids[i].firstChild.nodeValue;
			childnode.href = "javascript:showDetail('" + childnode.d_id + "')";			
		}
		obj.argument.callbackfunc();
	} 

	
	
function newNode(){
				node.setDynamicLoad(addnode);
				tree.draw();
}
	/*	
	 * タブ情報取得後に実行される関数
	 */
	function makeTree(obj) {

		//mimura 2008/03/11
		var flg = document.getElementById("select_no").value;

		//ツリー生成
		tree = null;
		tree = new YAHOO.widget.TreeView("treeDiv1");

		var res = obj.responseXML;
		var xmlDoc = res.documentElement;
		var errCode = xmlDoc.getElementsByTagName("error_code");

		//サーバー処理結果判定
		if (errCode[0].firstChild.nodeValue != CODE_SUCCESS ) {
			tree.draw();
			return;
		}

		//取得カテゴリ数チェック
		var count = xmlDoc.getElementsByTagName("ct_cnt");
		if (count[0].firstChild.nodeValue == "0") {
			tree.draw();
			return;
		}

		var categorys = xmlDoc.getElementsByTagName("category");
		var ids = xmlDoc.getElementsByTagName("id");
		var names = xmlDoc.getElementsByTagName("name");
		var counts = xmlDoc.getElementsByTagName("data_cnt");

		for (i = 0;i < categorys.length; i++) {
			var thisLabel;
			var node;

			thisLabel = names[i].firstChild.nodeValue + "(" + counts[i].firstChild.nodeValue + ")";
			node = new YAHOO.widget.MenuNode(thisLabel, tree.getRoot(), false);

			if(flg==5){
				/*node.setDynamicLoad(addnode);*/
				node.setDynamicLoad( addnode );
			}else{
				//子ノードを動的に作成するようにする
				node.setDynamicLoad( addnode );
			}
				node.c_id = ids[i].firstChild.nodeValue;
		}

		tree.draw();

	//add start 2007-08-02 A.hyodo---------------------------------------------------
		//画面の渡された座標にマップの中央をあわせる
		var mapx ="";
		var mapy ="";
		var objMapx = xmlDoc.getElementsByTagName("mapx")[0].firstChild;
		var objMapy = xmlDoc.getElementsByTagName("mapy")[0].firstChild;
		
		//データがなかったら抜ける
		if(objMapx==null || objMapy==null ){ return; }
		
		//object 内の値を取得
		var mapx = objMapx.nodeValue;
		var mapy = objMapy.nodeValue;

		//データがなかったら抜ける
		if( mapx=="" || mapy=="" ){ return; }

		//拡大率取得
		var nowZoom = map.getZoom();
		//マップの中央を移動
		map.setCenter(new GLatLng(mapy , mapx ), nowZoom);
		
		//メモリ開放
		objMapx = null;
		objMapy = null;
	//add end   2007-08-02 A.hyodo---------------------------------------------------

	}

	/*	
	 * タブ情報取得後に実行される関数
	 */
	function makeTree2(obj) {

		//mimura 2008/03/11
		var flg = document.getElementById("select_no").value;

		//ツリー生成
		tree = null;
		tree = new YAHOO.widget.TreeView("treeDiv1");

		var res = obj.responseXML;
		var xmlDoc = res.documentElement;
		var errCode = xmlDoc.getElementsByTagName("error_code");
/*
		//サーバー処理結果判定
		if (errCode[0].firstChild.nodeValue != CODE_SUCCESS ) {
			tree.draw();
			return;
		}
*/
		//取得カテゴリ数チェック
		if (document.getElementById('select_item_h').value == "") {
			tree.draw();
			return;
		}

		var datas = xmlDoc.getElementsByTagName("data");
		var ids = xmlDoc.getElementsByTagName("id");
		var titles = xmlDoc.getElementsByTagName("title");
		var dates = xmlDoc.getElementsByTagName("date");

		node = new YAHOO.widget.HTMLNode("検索数 "+datas.length+"件", tree.getRoot(), false);

		for (i = 0;i < datas.length; i++) {

			nodetitle = titles[i].firstChild.nodeValue + "(" + dates[i].firstChild.nodeValue + ")";
			node = new YAHOO.widget.MenuNode(nodetitle, tree.getRoot(), false);
			node.d_id = ids[i].firstChild.nodeValue;
			node.href = "javascript:showDetail('" + node.d_id + "')";			
		}
		tree.draw();

	}
