
function XmlReader() {
    this.xml = null;
    this.handler = null;
    try {
        if (isSafari()) {
            this.xml = new XMLHttpRequest();
            this.xml.onload = handleXmlEvent;
        } else if( document.implementation && document.implementation.createDocument ) {
            this.xml = document.implementation.createDocument("", "", null);
            this.xml.onload = handleXmlEvent;
        } else {
            this.xml = new ActiveXObject("Microsoft.XMLDOM");
            this.xml.onreadystatechange = handleXmlEvent;
        }
    } catch (e) {
        alert("XMLに未対応:" + e);
    }

    this.readXml = function(filePath, handler){
        this.handler = handler;
        if (this.xml == null) {
            this.handler(null);
        }
        if (isSafari() ) {
            try{
                this.xml.open("GET", filePath, true);
                this.xml.send(null);
            } catch (e) {
                handler(null);
            }
         } else {
            this.xml.load(filePath);
        }
    }
}

xmlReader = new XmlReader();
subtitleList = new Array();

function handleXmlEvent() {
    if (xmlReader.xml.readyState) {
        if (xmlReader.xml.readyState == 4) {
            if(xmlReader.xml.parseError && xmlReader.xml.parseError.status) {
                if (xmlReader.xml.parseError.status == 200) {
                    xmlReader.handler(xmlReader.xml);
                } else {
                    xmlReader.handler(null);
                }
            } else {
                xmlReader.handler(xmlReader.xml);
            }
        }
     } else {
        xmlReader.handler(xmlReader.xml);
     }
}

function readTextListXml(filePath) {
    var e = document.getElementById("textListArea");
    e.innerHTML="読み込んでいます。しばらくおまちください・・・";
    xmlReader.readXml(filePath, dispTextListHtml);
}

selectedFilePath = "";
function dispTextListHtml(xml) {
    try {
        makeSubtitleList(xml);
        dispSubtitleList();
    } catch(ex) {
        dump(xml);
    }
}

function dispSubtitleList() {
    var e = document.getElementById("textListArea");
    if (subtitleList.length == 0) {
        e.innerHTML = "読みとりエラー";
        return;
    }

    var msg = "<table class='menu_list'>";
    for (var i = 0; i < subtitleList.length; i++) {
        msg += "<tr><td nowrap='true'>";
        var title = subtitleList[i].title;
        var filePath = subtitleList[i].filePath;
        if (selectedFilePath == filePath) {
            msg += '<div class="selected_subtitle">';
            msg += title;
            msg += '</div>';
        } else {
            msg += '<div class="subtitle">';
            msg += '<a href="javascript:readTextXml(\'' + filePath + '\')">';
            msg += title;
            msg += '</a></div>';
        }
        msg += "</td></tr>";
    }
    msg += "<tr><td nowrap='true'>";
    msg += "<div class='subtitle'><a href='../../jsgame.html'>HOME</a></div>";
    msg += "</td></tr>";
    msg += "</table>";
    e.innerHTML = msg;
}

function makeSubtitleList(xml) {
    subtitleList = new Array();
    if (xml == null) {
        return;
    }

    if (isSafari() ) {
        xml = xml.responseXML;
    }
    var rootElement = xml.documentElement;
    var textDataList = rootElement.getElementsByTagName("TextData");
    for (var i = 0; i < textDataList.length; i++) {
        var textData = textDataList[i];
        var title = textData.getAttribute("title");
        var filePath = textData.getAttribute("filepath");
        var subtitleObj = new Object();
        subtitleObj.title = title;
        subtitleObj.filePath = filePath;
        subtitleList.push(subtitleObj);
    }
}

function readTextXml(filePath, filePath2) {
    selectedFilePath = filePath;
    if (isKonqueror() && (filePath2) ) {
        selectedFilePath = filePath2;
    }
    dispSubtitleList();
    var e = document.getElementById("textArea");
    e.innerHTML="読み込んでいます。しばらくおまちください・・・";
    xmlReader.readXml(filePath, dispTextHtml);
}

function dispTextHtml(xml) {
    var e = document.getElementById("textArea");
    if (xml == null) {
        e.innerHTML = "読みとりエラー";
        return;
    }
    dispPage(0);
}

function dispPage(pageno) {
    var xml = xmlReader.xml;
    if (isSafari() ) {
        xml = xmlReader.xml.responseXML;
    }
    var e = document.getElementById("textArea");
    if (xml == null) {
        e.innerHTML = "読みとりエラー";
        return;
    }
    var textDataElement = xml.documentElement;
    var msg = "";
    msg += "<table class='textAreaTable'><tr><td class='titleArea'><h4>";
    msg += textDataElement.getAttribute("title");
    msg += "</h4></td></tr>";
    var pageList = textDataElement.getElementsByTagName("Page");
    if (pageList.length > 1) {
        msg += "<tr><td align='center'>";
        for (var pno = 0; pno < pageList.length; pno++) {
            if (pno != pageno) {
            msg += '<a href="javascript:dispPage(' + pno + ')" class="page">';
                msg += '&nbsp;' + (pno + 1) + '&nbsp;';
            msg += '</a>'
            } else {
                msg += '<span class="page">&nbsp;' + (pno + 1) + '&nbsp;</span>';
            }
        }
        msg += "</td></tr>";
    }
    msg += "<tr><td><table class='textTable'>";
    var list = pageList[pageno].getElementsByTagName("Sentence");
    for (var i = 0; i < list.length; i++) {
        var str = list[i].firstChild.nodeValue;
        var org = "";
        while (org != str) {
            org = str;
            str = str.replace("\}", ">");
            str = str.replace("\{", "<");
        }
        msg += "<tr><td id='textPage'>&nbsp;&nbsp;" + str + "</td></tr>";
    }
    msg += "</table></td></tr>";

    msg += "</table>";

    e.innerHTML = msg;
    msg = "";
    if (pageno > 0) {
        msg += '<span class="button">';
        msg += '<a href="javascript:dispPage(' + (pageno - 1) + ')"';
        msg += '>←前へ</a></span>';
    }
    e = document.getElementById("prevarea");
    e.innerHTML = msg;
    
    msg = "";
    if (pageno < (pageList.length - 1) ) {
        msg += '<span class="button">';
        msg += '<a href="javascript:dispPage(' + (pageno + 1) + ')"';
        msg += '>次へ→</a></span>';
    }
    e = document.getElementById("nextarea");
    e.innerHTML = msg;
}

function dump(obj) {
    var msg = "";
    for (var elm in obj) {
        msg += elm + ":" + obj[elm] + "\n";
    }
    alert(msg);
}

function isKonqueror() {
    var ua = navigator.userAgent;
    return (ua.indexOf("Konqueror") != -1);
}

function isSafari() {
    var ua = navigator.userAgent;
    return (ua.indexOf("Safari") != -1);
}

function readText() {
    if (isKonqueror() ) {
        readTextListXml('textlist_utf16.xml');
    } else {
        readTextListXml('textlist.xml');
    }
}

