var	name_tarray=new Array();
var	name_parray=new Array();
var partgender  = new Object();  // Objectの生成 
var name_imgarray=new Array();
var xmlDoc = null;
var mis_open = false;	//HTTP通信でファイルを取得しようとして失敗したらTrueを挿入する

/*
//##############################################################################
//XMLファイルを読み込む(ブラウザによって、読み方を変更する)
//##############################################################################
function loadFile(func){
	loadXMLFile(func);
}
*/


//##############################################################################
// getPostList.phpからDBにアクセスして検索結果をXMLとして受け取る
// istid(例文ID)、p_row(previous_row:前例文の行数)、n_row(next_row:次の例文の行)
//##############################################################################
function loadFile(func){

	httpObj = createXMLHttpRequest(Ready);
	if (httpObj)
	{
		try{
			httpObj.open("GET",xmlfile,true);
			httpObj.send(null);
		}catch(e){
			//httpObj.openが失敗したらTrueを挿入する
			mis_open = true;
			createXMLDoc(func)
		}
	}
}

//##############################################################################
// HTTP通信用、共通関数
//##############################################################################
function createXMLHttpRequest(cbFunc)
{
	var XMLhttpObject = null;
	try{
		XMLhttpObject = new XMLHttpRequest();
	}catch(e){
		try{
			XMLhttpObject = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				XMLhttpObject = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){
				return null;
			}
		}
	}
	if (XMLhttpObject) {
		XMLhttpObject.onreadystatechange = cbFunc;
	}
	return XMLhttpObject;
}

//##############################################################################
//createXMLHttpRequest()の方法が成功しなかったときは、古い方法で試してみる。
//Localファイルを取得するときに失敗するケースが多い？
//##############################################################################
function createXMLDoc(func){
	xmlflag=false;
	var userAgent = navigator.userAgent;

	if(document.getElementById){
		if (window.ActiveXObject){
			// for FireFox(Local)
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async = false;
			if(xmlDoc.load(xmlfile)){func(xmlDoc)}
		}else if ( document.implementation && document.implementation.createDocument ){
			// for IE(Local)
			xmlDoc = document.implementation.createDocument("","",null);
			xmlDoc.onload = function (){func(xmlDoc)};
			xmlDoc.load(xmlfile);
		} 
		return xmlDoc;
	}

	xmlflag=true;
	return xmlDoc;
}

/**************************************************************
**　取得したデータを、指定された行に表示する
**************************************************************/
function Ready()
{
	var userAgent = navigator.userAgent;
	//HTTP経由でXMLファイルを取得しようとする場合はこのチェックが必要。
	//ローカル経由でXMLファイルを読むときは必要ない
	
	if(!mis_open){
		if(document.getElementsByTagName) {
			//httpObj.readyState == 4  すべてのデータを受信した
			//httpObj.status == 200はHTTP：httpObj.status == 0はLocal
			if(((httpObj.readyState == 4) && (httpObj.status == 200)) || (httpObj.readyState == 4) && (httpObj.status == 0)){
				xmlDoc = httpObj.responseXML;
				put();
			}else{
				//$("result").innerHTML = "<b>Loading...</b>";
			}
		}	
	}else{
		put();
	}

}


/**************************************************************
**　取得したデータを、指定された行に表示する
**************************************************************/
function put()
{
	var part_x		= null;
	var v_partstr	="";

	var checked		= "";
	var part		= "";
	var l_part		= "";
	var gender		= "";
	var Img			= "";
	
	//#####################################################################################################
	xmlflag=true;
	
	//パンくずナビの値をセットする
	mystring=setNavigation();

	//登場人物情報を取得
	part_x = xmlDoc.getElementsByTagName("part");
	v_partstr="";
	for(i=0; i<part_x.length; i++){
		checked	= "";
		part	= "";
		l_part	= "";
		gender	= "";
		Img 	= "";

		part			= trim(part_x[i].attributes.getNamedItem("id").nodeValue);
		l_part			= part.toLowerCase();
		name_parray[i]	= part_x[i].attributes.getNamedItem("name_p").nodeValue;		//名前(日本語)
		name_tarray[i]	= part_x[i].attributes.getNamedItem("name_t").nodeValue;		//名前(原語)
		gender			= trim(part_x[i].attributes.getNamedItem("gender").nodeValue);	//性別取得
		
		if(gender=="男"){
				name_imgarray[i]="img/m_"+l_part+"_on.gif";
				Img = "img/m_" + l_part + "_on.gif";
				//人物画像配置
				rightobj = getObj("right_" + l_part);
				if(rightobj != null) rightobj.src = Img;
		}else{
				name_imgarray[i]="img/f_"+l_part+"_on.gif";
				Img = "img/f_" + l_part + "_on.gif";
				//人物画像配置
				rightobj = getObj("right_" + l_part);
				if(rightobj != null) rightobj.src = Img;
		}
		
		topobj = getObj("part_" + l_part);

		if(topobj){
			topobj.src = Img;
			topnameobj = getObj("part_name_" + l_part);
			topnameobj.innerHTML = part_x[i].attributes.getNamedItem("name_t").nodeValue;
		}

		if(i==0)	checked="checked";
		else		checked="";
	}
	SetAllStr();
}

//##############################################################################
//状況、会話、登場人物名を表示する(表示しなおす)
//一番最初に表示する時
//##############################################################################
function SetAllStr(){
	//状況を表示する
	SetInfo();
	//会話文を表示する
	//ただしHTMLのタイプが[聞く]の時は、Dialogueの中に会話文をセットしない(リアルタイムで一行ずつ表示させるので)
	if(htmltype != 1) SetDialogue();
	//登場人物を表示する
	SetPart();
}
//##############################################################################
/*パンくずナビの値をセットする*/
//##############################################################################
function setNavigation(){
var mystring = "";
	//タイトルを取り出す
	var target_x = xmlDoc.getElementsByTagName("targetfunction");
	for(i=0; i<target_x.length; i++){
		var title_x = target_x[i].getElementsByTagName("title");
		title_str = title_x[0].firstChild.nodeValue;
	}

	//シチュエーション取得
	var setting_x = xmlDoc.getElementsByTagName("setting")
	for(i=0; i<setting_x.length; i++){
		var situation_x = setting_x[i].getElementsByTagName("situation");
		situation_str = situation_x[0].firstChild.nodeValue;
	}

	//タイトルページの中身を書き換える
	mystring="<table id='navi_tbl'><tr><td valign='middle'>";
	mystring+=(id);
	mystring+=" ";
	
	mystring+= title_str;
	mystring+=" </td>";

	mystring+="<td valign='middle'>";
	mystring+= situation_str;
	mystring+="</td></tr></table>";
	try{
		parent.title.situation.innerHTML=mystring;
	}catch(e){
	}
	
	return mystring;
}

function lineVisible(lineid){
	var lineview = getObj("lineview_" + lineid);
	var dlg = getObj("dialoguearea_" + lineid);
	
	if(lineview.checked){
		//dlg.style.display = "";
		dlg.style.color="#000000";
	}else{
		//dlg.style.display = "none";
		dlg.style.color="#faf8f4";
	}
}

//################################################################################
//XMLデータを読みdialogueを表示
//################################################################################
function SetDialogue(){
	//XML読み込みが完了していない場合
	if(!xmlflag){
		alert("NOW LOADING");
		return;
	}
	var dialogueObj = getObj("dialogue");

	//dialogueがあったら会話を表示する
	if(dialogueObj){
		var chtrans = getObj("ch_trans");
		var chtarget = getObj("ch_target");
		var chpron = getObj("ch_pron");
	
		var lineviewcheck = false;
		if(chtrans.checked || chtarget.checked || (chpron && ch_pron.checked)) lineviewcheck = true;
	
		//line
		//sentence
		//をloop
		var html="<table border='0' id='dialoguetbl'>";
		
		//XML内の取り出したいタグの配列を作る
		var line_x = xmlDoc.getElementsByTagName("line");
		i=0;
	
		for(toosibanngou=1;i<line_x.length; i++){
			lineid	= trim(line_x[i].attributes.getNamedItem("id").nodeValue);		//id属性の値取得
			part 	= trim(line_x[i].attributes.getNamedItem("part").nodeValue);		//id属性の値取得
			//登場人物の台詞を非表示選択したときの動き(非表示選択した登場人物は空の行を表示する)
			if(part=="A" && right_a.value==0){
				html+="<TR><TD COLSPAN='3'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_1'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_2'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_3'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_4'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_5'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_6'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_7'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_8'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_9'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_10'>";
				html+="</TD></TR>";
				continue;
			}else if(part=="B" && right_b.value==0){
				html+="<TR><TD COLSPAN='3'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_1'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_2'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_3'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_4'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_5'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_6'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_7'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_8'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_9'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_10'>";
				html+="</TD></TR>";
				continue;
			}else if(part=="C" && right_c.value==0){
				html+="<TR><TD COLSPAN='3'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_1'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_2'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_3'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_4'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_5'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_6'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_7'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_8'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_9'>";
				html+="<DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_10'>";
				html+="</TD></TR>";
				continue;
			}
	
			if(part=="A"){
				img=name_imgarray[0];
			}else if(part=="B"){
				img=name_imgarray[1];
			}else if(part=="C"){
				img=name_imgarray[2];
			}
			sentencenodes = line_x[i].getElementsByTagName("sentence");
			//sentenceの数
			var rowspan=sentencenodes.length;
			
			//原語 or 訳語表示用チェックボックスの片方でもチェックされている時、もしくはHTMLのタイプが練習３の時
			//if(chtrans.checked == true || chtarget.checked == true || (chpron != null && chpron.checked == true) || htmltype == 3) 
				//登場人物パートのアイコンを表示する
				html+="<TR><TD ROWSPAN='"+rowspan+"' WIDTH='30' align='center' valign='middle'>"
					+ "<INPUT TYPE='IMAGE' SRC="+img+" BORDER='0' onClick='PlayLine("+lineid+");this.blur()'></TD>\n";
			
			hihyoujihtml="";
			for(var j=0; j<sentencenodes.length ;j++,toosibanngou++){
	
				sentenceid=trim(sentencenodes[j].attributes.getNamedItem("id").nodeValue);		//id属性の値取得
				if(j!=0){	//一人の人が二言以上話す時はTRで行変えしなければいけない
					html+="<TR>";
				}

				if(lineviewcheck) lineviewcheck = "<INPUT TYPE='checkbox' onClick='lineVisible(\""+lineid + "_" +sentenceid+"\")' name='lineview_" + lineid + "_" + sentenceid 
									+ "' id='lineview_" + lineid + "_" + sentenceid + "' checked>";
				else lineviewcheck="";
	
				//原語 or 訳語表示用チェックボックスの片方でもチェックされている時、もしくはHTMLのタイプが練習１の時
				//if(chtrans.checked == true || chtarget.checked == true || (chpron != null && chpron.checked == true) || htmltype == 3)
					//登場人物の一言再生用スピーカーアイコンを表示する。
					html+="<TD WIDTH='50'>"
									+ "<INPUT TYPE='IMAGE' SRC='img/sentence.gif' BORDER='0' onClick='PlaySentence("+lineid+","+sentenceid+");this.blur()'>"
									+ lineviewcheck
									+ "</TD>\n";
				
				//原語取得
				targetlanguage = "";
				targetlanguage_x = sentencenodes[j].getElementsByTagName("targetlanguage");
				if(targetlanguage_x[0].firstChild != null) targetlanguage = targetlanguage_x[0].firstChild.nodeValue;
	
				/*ラオス語か、カンボジア語の場合は、XMLに文字が入力されていないので、画像ファイル名を取得する*/
				if(lang == "lo" || lang == "km"){
					targetlanguage = "<img src=\"../langimg/" + lang + id + "-" + lineid + "-" + sentenceid + ".gif\" >";
				}
	
				//訳語取得
				translation = "";
				if(lang != "ja"){	//日本語以外だったらtranslationから取ってくる。日本語のときは"pron"から取ってくる
					translation_x=sentencenodes[j].getElementsByTagName("translation");
					if(translation_x[0].firstChild != null) translation=translation_x[0].firstChild.nodeValue;
				}else{
					pron_x=sentencenodes[j].getElementsByTagName("pron");
					if(pron_x[0].firstChild != null) translation=pron_x[0].firstChild.nodeValue;
				}
	
				//発音取得
				pron = "";
				pron_x=sentencenodes[j].getElementsByTagName("pron");
				if(pron_x[0].firstChild != null) pron=pron_x[0].firstChild.nodeValue;
				
	
				html+="<TD><DIV class='dialoguearea' ID='dialoguearea_"+lineid+"_"+sentenceid+"'>";
	
				//訳語表示
				if(chtarget.checked == true) html+="<DIV class='target_" + lang +"'>" + targetlanguage + "</div>";

				//原語表示
				if(chtrans.checked == true) html+="<DIV class='trans_" + lang +"'>" + translation + "</div>";
	
				//発音表示
				if(chpron != null && chpron.checked == true) html+="<DIV class='pron_" + lang +"'>" + pron + "</div>";
	
	
				html+="</DIV></TD>";
	
				html+="</TR>";
			}
			//html+="<TR><TD><BR></TD></TR>";
			html+="<TR><TD class=\"linechange\"></TD></TR>";
		}
		html+="</table>";
		//原語 と 訳語表示用チェックボックスが両方ともチェックされていない(非表示にしたい)とき
		if(chtrans.checked == false && chtarget.checked == false) html+="";
		dialogueObj.innerHTML=html;
	}
}

//################################################################################
//登場人物名を表示する
//################################################################################
function SetPart(){
	var chtrans = getObj("ch_trans");
	var chtarget = getObj("ch_target");

	//ch_transとch_targetがあったら登場人物名を表示する
	if(chtrans && chtarget){
		//登場人物の表示
		//日本語にチェックが入っていて、なおかつフランス語にチェックが入っていないときは、人物名を日本語で表示する。
		if(chtrans.checked == true && chtarget.checked == false){
			for(i=0;i<name_parray.length;i++){
				switch(i){
					case 0:part_name_a.innerHTML = name_parray[i]; break;
					case 1:part_name_b.innerHTML = name_parray[i]; break;
					case 2:part_name_c.innerHTML = name_parray[i]; break;
				}
			}
		}else{
			for(i=0;i<name_tarray.length;i++){
				switch(i){
					case 0:part_name_a.innerHTML = name_tarray[i]; break;
					case 1:part_name_b.innerHTML = name_tarray[i]; break;
					case 2:part_name_c.innerHTML = name_tarray[i]; break;
				}
			}
		}
	}
}

//################################################################################
//状況表示を作成
//################################################################################
function SetInfo(){
	//XML読み込みが完了していない場合
	if(!xmlflag){
		alert("NOW LOADING");
		return;
	}

	var infoplace = getObj("info_place");
	var infosituation = getObj("info_situation");

	//info_placeとinfo_situationがあったら、表示する
	if(infoplace && infosituation){
		//シチュエーション取得
		var setting_x = xmlDoc.getElementsByTagName("setting")
		for(i=0; i<setting_x.length; i++){
			var situation_x = setting_x[i].getElementsByTagName("situation");
			var place_x = setting_x[i].getElementsByTagName("place")
			situation_str = "";
			place_str = "";
			if(situation_x[0].firstChild != null) situation_str = situation_x[0].firstChild.nodeValue;
			if(place_x[0].firstChild != null) place_str = place_x[0].firstChild.nodeValue;
		}
		
		//状況
		infoplace.innerHTML = place_str;
		infosituation.innerHTML = situation_str;
	}
	return;
}

/*
//##############################################################################
//XMLファイルの中身を読み込んで、適切な形で表示する
//##############################################################################
function Ready(xmlDoc){
	
	xmlflag=true;
	
	//パンくずナビの値をセットする
	mystring=setNavigation();

	//登場人物情報を取得
	var part_x = xmlDoc.getElementsByTagName("part");
	var v_partstr="";
	name_imgarray=new Array();
	for(i=0; i<part_x.length; i++){

		var part=trim(part_x[i].attributes.getNamedItem("id").nodeValue);
		var l_part=part.toLowerCase();
		name_parray[i]=part_x[i].attributes.getNamedItem("name_p").nodeValue;		//名前(日本語)
		name_tarray[i]=part_x[i].attributes.getNamedItem("name_t").nodeValue;		//名前(原語)
		var gender=trim(part_x[i].attributes.getNamedItem("gender").nodeValue);	//性別取得

		var Img = "";
		
		if(gender=="男"){
				name_imgarray[i]="img/m_"+l_part+"_on.gif";
				Img = "img/m_" + l_part + "_on.gif";
				//人物画像配置
				rightobj = getObj("right_" + l_part);
				if(rightobj != null) rightobj.src = Img;
		}else{
				name_imgarray[i]="img/f_"+l_part+"_on.gif";
				Img = "img/f_" + l_part + "_on.gif";
				//人物画像配置
				rightobj = getObj("right_" + l_part);
				if(rightobj != null) rightobj.src = Img;
		}
		
		topobj = getObj("part_" + l_part);

		if(topobj){
			topobj.src = Img;
			topnameobj = getObj("part_name_" + l_part);
	
			topnameobj.innerHTML = part_x[i].attributes.getNamedItem("name_t").nodeValue;
		}

		if(i==0){
			var checked="checked";
		}else{
			var checked="";
		}
	}

	SetAllStr();
}
*/