// ===========================================================================
// 合成音声データ取得用 JS ファイル
//
// auther kawahara@ferix.jp
// Copyright (C) 2007 by Ferix Inc.                    ALL RIGHTS RESERVED.
// ===========================================================================

// ===========================================================================
// グローバル変数宣言
// ===========================================================================
var selected_id4AITalk	= 0;
var h3_objs         = new Array();
var content_objs    = new Array();


/*****************************************************************************
 * Blog 解析用 Script
 ****************************************************************************/
// ===========================================================================
// Blog コンテンツを選択条件に設定
// ===========================================================================
function getBlogInfo(){

    // タイトル情報収集
    var h3_nodes        = document.getElementsByTagName("h3");
    var ary_length      = 0;

    for( iCnt = 0; iCnt < h3_nodes.length; iCnt ++ ){
        if( h3_nodes[iCnt].getAttribute("class") == "storytitle"
            || h3_nodes[iCnt].getAttribute("className") == "storytitle" ){
            h3_objs[ary_length++]   = h3_nodes[iCnt];
        }
    }

    // コンテンツ情報収集
    var content_nodes   = document.getElementsByTagName("div");
    ary_length          = 0;
    for( iCnt = 0; iCnt < content_nodes.length; iCnt ++ ){
        if( content_nodes[iCnt].getAttribute("class") == "storycontent"
            || content_nodes[iCnt].getAttribute("className") == "storycontent" ){
            content_objs[ary_length++]  = content_nodes[iCnt];
        }
    }

    var select_node     = document.getElementById("blog_content4ai");

    for( iCnt = 0; iCnt < h3_objs.length; iCnt ++ ){
        h3_obj          = h3_objs[iCnt];
        a_obj           = h3_obj.lastChild;
        title_obj       = a_obj.firstChild;
        title_text      = _getContentText(a_obj);
        var option_node = document.createElement("option");
        option_node.setAttribute("label", title_obj.nodeValue);

        option_node.setAttribute("value", h3_obj.getAttribute("id"));
//        option_node.setAttribute("value", h3_obj.getAttribute("name"));

        var text_node   = document.createTextNode(title_text.substring(0, 10));
        option_node.appendChild(text_node);

        select_node.appendChild(option_node);
    }
}

// ===========================================================================
// ブラウザを判定してテキストコンテンツを取得
// ===========================================================================
function _getContentText(aObj){
    //Win ie用
    if(window.ActiveXObject){
        return aObj.innerText;
    } else {
        return aObj.textContent;
    }
}


/*****************************************************************************
 * Ajax 用 Script
 ****************************************************************************/
// ===========================================================================
// Ajax 用コールバック関数
// ===========================================================================
function handleHttpEvent(){
alert(xmlhttp.readyState);
	if (xmlhttp.readyState == 4) {
		replace_node				= document.getElementById("wave_place");
		if (xmlhttp.status == 200) {
			replace_node.innerHTML	= "<embed src=\"http://www.ferix.jp/blog/kw/wav_files/"+xmlhttp.responseText+"\" type=\"audio/wav\" console=\"s1\" height=\"20\" width=\"170\" autostart=\"true\">";
			id_node				= document.getElementById("selected_id4AITalk");
			window.location		="#"+selected_id4AITalk;
		} else {
			window.alert("通信エラーが発生しました。");
		}
	}
}

// ===========================================================================
// AITalk 用データ送信関数
// ===========================================================================
function _sendData4AITalk(){
	replace_node				= document.getElementById("wave_place");
	replace_node.innerHTML	= "合成音声生成中・・・";
	setTimeout( _sendData4AITalkInner, 10);
}

function _sendData4AITalkInner(){
	blog_content				= document.getElementById("blog_content4ai");
	selected_id4AITalk			= blog_content.value;
	xmlhttp						= new createHttpRequest();
	xmlhttp.onreadystatechange 	= handleHttpEvent;
	xmlhttp.open("POST", "http://www.ferix.jp/blog/kw/FRXGetWave.php", false);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

	for( iCnt = 0; iCnt < h3_objs.length; iCnt ++ ){
		h3_obj          = h3_objs[iCnt];
		if( h3_obj.getAttribute("id") == selected_id4AITalk ){
//		if( h3_obj.getAttribute("name") == selected_id4AITalk ){

			content_obj     = content_objs[iCnt];
			content_text    = _getContentText(content_objs[iCnt]);
			xmlhttp.send("blog_content4ai="+content_text);
		}
	}

	handleHttpEvent();
}

// ===========================================================================
//XMLHttpRequestオブジェクト生成
// ===========================================================================
function createHttpRequest(){
	//Win ie用
	if(window.ActiveXObject){
		try {
			//MSXML2以降用
			return new ActiveXObject("Msxml2.XMLHTTP") //[1]'
		} catch (e) {
			try {
				//旧MSXML用
				return new ActiveXObject("Microsoft.XMLHTTP") //[1]'
			} catch (e2) {
				return null
			}
		}
	} else if(window.XMLHttpRequest){
		//Win ie以外のXMLHttpRequestオブジェクト実装ブラウザ用
		return new XMLHttpRequest() //[1]'
	} else {
		return null
	}
}

