﻿var http_request = false;
var page=0;
var chpages=false;
var url;
var totals;
var tolearn = false;
var setHeight="50%";
comicArray = new Array();
topstyle = "position:absolute";
liststyle = "";
listlinkstyle = "display: block;padding: 6px;text-decoration: none;";

function chpics(url,num) {
    document.getElementById("frr").src = url;
    page = num;
    document.getElementById("number").innerHTML = "您正在閱覽：" + comicArray[page].ctitle;
    document.getElementById("comicRightZone").scrollTop = 0;
    document.getElementById("comicRightZone").scrollLeft = 0;
    setCookie();
    alphaStart = 0;
    document.getElementById("frr").style.filter = "alpha(opacity=" + alphaStart + ")";
    document.getElementById("frr").style.opacity = (alphaStart * 0.01);
}
function nextpage() {
    page += 1;
    if (page < comicArray.length) {
        geturl = comicArray[page].cimg;
        chpics(geturl, page);
    } else {
        alert("漫畫結束，留下您的感想");
        window.open(comicMessage);
        page -= 1;
    }
}

function setCookie() {
    now = new Date()
    now.setTime(now.getTime() + 1000 * 60 * 60 * 24 * 7 * 5);
    document.cookie = "page=" + page + "; expires=" + now.toGMTString();
}

alphaStart = 0;
function alphaACT(who) {
    if (alphaStart < 100) {
        alphaStart += 20;
        document.getElementById(who).style.filter = "alpha(opacity=" + alphaStart + ")";
        document.getElementById(who).style.opacity = (alphaStart * 0.01);
        fj = setTimeout("alphaACT('" + who + "')", 1);
    } else {
        clearTimeout(fj);
    }
}


function makeRequest(url, theheight) {
    setHeight = theheight;
	url=""+url;
	page=page;
    http_request = false;

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    http_request.onreadystatechange = alertContents;
    http_request.open('GET', url, true);
    http_request.send(null);

}

function alertContents() {

    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
            var xmldoc = http_request.responseXML;
            //編譯漫畫標題區
            var root_node = xmldoc.getElementsByTagName("漫畫標題區")[0];
            vj = 0
            g1 = new Array();
            for (var run = 0; run < root_node.childNodes.length; run++) {
                if ((root_node.childNodes[run].nodeType != 3) &&
					  (root_node.childNodes[run].nodeType != 8)) {
                    g1[vj] = root_node.childNodes[run];
                    vj += 1;
                }
            }
            var g2 = g1.length;
            var rj = 0;
            /*comicName = g1[0].text;
            comicArt = g1[1].text;
            comicPart = g1[2].text;*/
            comicMessage = g1[0].getAttribute("留言位置");
            //編譯漫畫內容區
            var root_node2 = xmldoc.getElementsByTagName("漫畫內容區")[0];
            vj = 0
            g4 = new Array();
            for (var run4 = 0; run4 < root_node2.childNodes.length; run4++) {
                if ((root_node2.childNodes[run4].nodeType != 3) &&
					  (root_node2.childNodes[run4].nodeType != 8)) {
                    g4[vj] = root_node2.childNodes[run4];
                    vj += 1;
                }
            }
            for (caseTitle = 0; caseTitle < g4.length; caseTitle++) {
                ej = new Object();
                ej.ctitle = g4[caseTitle].getAttribute("名稱");
                ej.cimg = g4[caseTitle].getAttribute("圖片");
                ej.cwidth = g4[caseTitle].getAttribute("寬度");
                ej.cheight = g4[caseTitle].getAttribute("高度");
                ej.clink = g4[caseTitle].getAttribute("超連結");
                comicArray.push(ej);
            }
            pagego();
        } 
    }
}
function pagego() {
   /* document.write("<div id=\"comicTopZone\"></div>");
    document.write("<div id=\"comicLeftZone\"></div>");
    document.write("<div id=\"comicRightZone\"></div>");*/
    theHtml = "";
    //theHtml += "<div id=\"comicTopZone\" style=\"position:absolute;\"><h4>漫畫標題：" + comicName + "　集數：" + comicPart + "　作者：" + comicArt + "</h4></div>";
    theHtml += "<ul id=\"comicLeftZone\" style=\"height:" + setHeight + ";\">";
    for (uui = 0; uui < comicArray.length; uui++) {
        thetitle = "" + comicArray[uui].ctitle;
        theimg = "" + comicArray[uui].cimg;
        thewidth = "" + comicArray[uui].cwidth;
        theheight = "" + comicArray[uui].cheight;
        theclink = "" + comicArray[uui].clink;
        if (comicArray[uui].cimg != "") {
            theonclick = "onClick=\"chpics('" + comicArray[uui].cimg + "'," + uui + ")\"";
            thetarget = "";
            theclink = "#";
        } else {
        theonclick = "";
        thetarget = "target=\"_blank\"";
        comicArray.splice(uui, 1);
        }
        theHtml += "<li><a href=\"" + theclink + "\" " + theonclick + " " + thetarget + ">" + thetitle + "</a></li>";
    }
    theHtml += "<li><a href=\"" + comicMessage + "\" target=\"_blank\">留下感想</a></li>";
    theHtml += "</ul>";
    thecookie = "" + document.cookie;
    /*if(thecookie!=""){
        thecookieArray = thecookie.split("=");
        getpage = Number(thecookieArray[1]);
        usepage = confirm("您上次閱覽到" + comicArray[getpage].ctitle + "，您要繼續從這頁開始看嗎?");
        if (usepage) {
            page = getpage;
        }
    }*/
    theHtml += "<div id=\"comicRightZone\" style=\"height:" + setHeight + ";\"><img title=\"點擊漫畫可觀看下一頁\" id=\"frr\" src=\"" + comicArray[page].cimg + "\" onclick=\"nextpage()\" onload=\"alphaACT('frr')\"></div>";
    theHtml += "<div style=\"position:absolute;right:40px;margin-top:-1.5em;\" id=\"number\">您正在閱覽：" + comicArray[page].ctitle + "</div>";
    document.getElementById("comicZone").innerHTML = theHtml;
}
