// デスラー暦カレンダー
function Descal() {
    this.date = new Date();
    this.curDate = new Date();
    this.seiYear = this.date.getFullYear();

    this.setUrl = function(url) {
        this.url = url;
        var urlstr = "";
        if (url) {
            urlstr = url + "/";
        }
        this.sundayPicUrl = urlstr + "sunday.gif";
        this.satadayPicUrl = urlstr + "sataday.gif";
        this.weekdayPicUrl = urlstr + "weekday.gif";
        this.mainboardPicUrl = urlstr + "Default.png";
        this.uraboardPicUrl = urlstr + "ura.png";
        this.ibuttonUrl = urlstr + "info.gif";
        this.cbuttonUrl = urlstr + "conv.gif";
        this.nextPicUrl = urlstr + "next.gif";
        this.prevPicUrl = urlstr + "prev.gif";
        this.readmeUrl = urlstr + "readme.html";
        this.xmlutf16Url = urlstr + "descal_utf16.xml";
        this.xmlUrl = urlstr + "descal.xml";
    }

    this.setUrl(null);

    this.load = function() {
        var args = splitUrl();
        var date = args["date"];
        if (date) {
            var y = parseInt(date.substring(0,4));
            var m = parseInt(date.substring(4,6)) - 1;
            var d = parseInt(date.substring(6,8));
            this.curDate = new Date(y, m, d);
            this.date = new Date(y, m, d);
        }

        app.dispCal(true);
    }

    this.dispCal = function(isError) {

        var elm = document.getElementById("mainboard");

        var mainboardStyle = "";
        mainboardStyle += "width: 280px;";
        mainboardStyle += "height: 256px;";
        mainboardStyle += "background-image: url(" + this.mainboardPicUrl + ");";
        mainboardStyle += "margin: 0px;";
        mainboardStyle += "padding: 0px;";

        var tablestyle = "";
        tablestyle += "color : #ffffff;";
        tablestyle += "margin-top: 0px;";
        tablestyle += "margin-left: 8px;";
        tablestyle += "margin-right: 0px;";
        tablestyle += "margin-bottom: 0px;";
        tablestyle += "padding: 0px;";
        tablestyle += "font-size: 12px;";
        tablestyle += "width: 229px;";
        tablestyle += "font-family: sans-serif;";

        var cellstyle = "";
        cellstyle += "text-align: center;";
        cellstyle += "color: #ffffff;";
        cellstyle += "padding: 0px;";
        cellstyle += "margin: 0px;";
        cellstyle += "border: 0px;";
        cellstyle += "width: 32px;";
        cellstyle += "height: 19px;";
        cellstyle += "font-size: 13px;";

        var weekdaystyle = new Object();
        weekdaystyle[1] = cellstyle;
        weekdaystyle[1] += "background-image: url(" + this.sundayPicUrl + ");";
        weekdaystyle[2] = cellstyle;
        weekdaystyle[2] += "background-image: url(" + this.weekdayPicUrl + ");";
        weekdaystyle[3] = cellstyle;
        weekdaystyle[3] += "background-image: url(" + this.weekdayPicUrl + ");";
        weekdaystyle[4] = cellstyle;
        weekdaystyle[4] += "background-image: url(" + this.weekdayPicUrl + ");";
        weekdaystyle[5] = cellstyle;
        weekdaystyle[5] += "background-image: url(" + this.weekdayPicUrl + ");";
        weekdaystyle[6] = cellstyle;
        weekdaystyle[6] += "background-image: url(" + this.weekdayPicUrl + ");";
        weekdaystyle[7] = cellstyle;
        weekdaystyle[7] += "background-image: url(" + this.satadayPicUrl + ");";

        var html = "";
        html += "<table style='" + mainboardStyle + "'><tr><td style='vertical-align: top;'>";
        html += "<table style='" + tablestyle + "'>";
        html += "<tr>";
        html += "<td colspan='5' style='text-align: left; font-weight: bold;'>";
        html += this.getDesMonthString();
        html += "</td>";
        html += "<td style='text-align: right;' colspan='3'>";
        html += "<img src='" + this.prevPicUrl + "'onclick='app.dispPrev()'/>";
        html += "&nbsp;&nbsp;";
        html += "<img src='" + this.nextPicUrl + "'onclick='app.dispNext()'/>";
        html += "&nbsp;&nbsp;";
        html += "<img src='" + this.cbuttonUrl + "' id='cbutton'";
        html += " onclick='app.dispConvert()'/>";
        html += "</td>";
        html += "</tr>";
        html += "<tr>";
        html += "<th>Sun.</th>";
        html += "<th>Mon</th>";
        html += "<th>Tue.</th>";
        html += "<th>Wed.</th>";
        html += "<th>Thu.</th>";
        html += "<th>Fri.</th>";
        html += "<th>Sat.</th>";
        html += "</tr>";
        var wk = 1;
        var wd = 1;
        for (wk = 1; wk <= 6; wk++) {
            html += "<tr>";
            for (wd = 1; wd <= 7; wd++) {
                html += "<td style='padding: 0px; margin: 0px;'>";
                html += "<table style='" + weekdaystyle[wd] + "'><tr><td style='padding: 0px; margin: 0px;'>";
                html += this.getDayOfMonthFromWeek(wk, wd);
                html += "</td></tr></table>";
                html += "</td>";
            }
            html += "</tr>";
        }
        html += "<tr>";
        html += "</tr>";
        html += "<tr>";
        html += "<td colspan='7' style='height: 48px; font-size: 11px;'>";
        html += "デスラーの一言：<br/>";
        html += "<div id='hitokoto'>" + this.getDesMessage(this.curDate, isError) + "</div>";
        html += "</td>";
        html += "</tr>";
        html += "<tr>";
        html += "<td colspan='7' style='width: 229px; text-align: left; font-size: 11px;'>";
        html += this.getMetsuboDay(this.curDate);
        html += "</td>";
        html += "<td>";
        html += "<img src='" + this.ibuttonUrl + "' id='ibutton'";
        html += " onclick='app.dispUra()'/>";
        html += "</td>";
        html += "</tr>";
        html += "</table>";
        html += "</td></tr></table>";
        elm.innerHTML = html;
    }

    this.dispConvert = function() {
        var elm = document.getElementById("mainboard");
        var tablestyle = "";
        tablestyle += "color : #ffffff;";
        tablestyle += "margin-top: 4px;";
        tablestyle += "margin-left: 8px;";
        tablestyle += "width: 252px;";
        tablestyle += "font-size: 13px;";

        var mainboardStyle = "";
        mainboardStyle += "width: 280px;";
        mainboardStyle += "height: 256px;";
        mainboardStyle += "background-image: url(" + this.mainboardPicUrl + ");";
        mainboardStyle += "margin: 0px;";
        mainboardStyle += "padding: 0px;";

        var html = "";
        html += "<table style='" + mainboardStyle + "'><tr><td style='vertical-align: top;'>";
        html += "<table style='" + tablestyle + "'>";
        html += "<tr>";
        html += "<td style='text-align; left;font-weight: bold;' colspan='3'>";
        html += "西暦<=>デスラー暦変換";
        html += "</td>";
        html += "<td style='text-align: right;'>";
        html += "<img src='" + this.cbuttonUrl + "' id='cbutton'";
        html += " onclick='app.dispCal()'/>";
        html += "</td>";
        html += "</tr>";
        html += "<tr>";
        html += "<td>&nbsp;</td>";
        html += "</tr>";
        html += "<tr>";
        html += "<td style='text-align; left;'>";
        html += "西暦";
        html += "</td>";
        html += "<td>";
        html += "<select name='seiKigen'>";
        html += "<option value='1'>紀元前</option>";
        html += "<option value='2'>紀元</option>";
        html += "</select>";
        html += "</td>";
        html += "<td>";
        html += "<input type='text' name='seiYear' value='' style='width: 40px;'/>年";
        html += "</td>";
        html += "<td></td>";
        html += "</tr>";
        html += "<tr>";
        html += "<td>&nbsp;</td>";
        html += "</tr>";
        html += "<tr>";
        html += "<td style='text-align; left;'>";
        html += "デスラー暦";
        html += "</td>";
        html += "<td>";
        html += "<select name='desKigen'>";
        html += "<option value='1'>紀元前</option>";
        html += "<option value='2'>紀元</option>";
        html += "</select>";
        html += "</td>";
        html += "<td>";
        html += "<input type='text' name='desYear' value='' style='width: 40px;'/>年";
        html += "</td>";
        html += "<td></td>";
        html += "</tr>";
        html += "<tr>";
        html += "<td>&nbsp;</td>";
        html += "</tr>";
        html += "<tr>";
        html += "<td colspan='4' style='width: 229px; text-align: center;'>";
        html += "<input type='button' value='西暦→デスラー暦' onclick='app.convStoD();'/>";
        html += "</td>";
        html += "</tr>";
        html += "<tr>";
        html += "<td colspan='4' style='width: 229px; text-align: center;'>";
        html += "<input type='button' value='デスラー暦→西暦' onclick='app.convDtoS();'/>";
        html += "</td>";
        html += "</tr>";
        html += "<tr>";
        html += "<td colspan='4' style='width: 229px; text-align: center;'>";
        html += "<input type='button' value='カレンダーに設定' onclick='app.setDate();'/>";
        html += "</td>";
        html += "</tr>";
        html += "<tr>";
        html += "<td colspan='4' style='width: 256px; text-align: right; height: 20px; vertical-align: bottom;'>";
        html += "<img src='" + this.ibuttonUrl + "' id='ibutton'";
        html += " onclick='app.dispUra()'/>";
        html += "</td>";
        html += "</tr>";
        html += "</table>";
        html += "</td></tr></table>";
        elm.innerHTML = html;
        this.dispConvertedYear();
    }

    this.convStoD = function() {
        this.seiYear = parseInt(document.forms.descalForm.seiYear.value);
        if (document.forms.descalForm.seiKigen.value == "1") {
            this.seiYear *= -1;
        }
        this.dispConvertedYear();
    }

    this.convDtoS = function() {
        var desYear = parseInt(document.forms.descalForm.desYear.value);
        if (document.forms.descalForm.desKigen.value == "1") {
            desYear *= -1;
        }
        this.setDesYear(desYear);
        this.dispConvertedYear();
    }

    this.setDate = function() {
        try {
            var desYear = parseInt(document.forms.descalForm.desYear.value);
            if (document.forms.descalForm.desKigen.value == "1") {
                desYear *= -1;
            }
            this.setDesYear(desYear);
            if (!isNaN(this.seiYear)) {
                this.date.setFullYear(this.seiYear);
                this.dispCal();
            }
        } catch (e) {
        }
    }

    this.dispPrev = function() {
        this.date.setMonth(this.date.getMonth() - 1);
        this.dispCal();
    }

    this.dispNext = function() {
        this.date.setMonth(this.date.getMonth() + 1);
        this.dispCal();
    }

    this.dispConvertedYear = function() {
        if (this.seiYear > 0) {
            document.forms.descalForm.seiKigen.value = 2;
        } else {
            document.forms.descalForm.seiKigen.value = 1;
        }
        document.forms.descalForm.seiYear.value = Math.abs(this.seiYear);

        if (this.getDesYear(this.seiYear) > 0) {
            document.forms.descalForm.desKigen.value = 2;
        } else {
            document.forms.descalForm.desKigen.value = 1;
        }
        document.forms.descalForm.desYear.value = Math.abs(this.getDesYear(this.seiYear));
    }

    this.dispInfo = function() {
        var elm = document.getElementById("mainboard");
        var tablestyle = "";
        tablestyle += "color : #ffffff;";
        tablestyle += "margin-top: 4px;";
        tablestyle += "margin-left: 8px;";
        tablestyle += "width: 248px;";
        tablestyle += "font-size: 13px;";

        var mainboardStyle = "";
        mainboardStyle += "width: 280px;";
        mainboardStyle += "height: 256px;";
        mainboardStyle += "background-image: url(" + this.uraboardPicUrl + ");";
        mainboardStyle += "margin: 0px;";
        mainboardStyle += "padding: 0px;";

        var cellStyle = "";
        cellStyle += "text-align: left;";
        cellStyle += "font-size: 12px;";
        cellStyle += "width: 248px;";

        var html = "";
        html += "<table style='" + mainboardStyle + "'><tr><td style='vertical-align: top;'>";
        html += "<table style='" + tablestyle + "'>";
        html += "<tr>";
        html += "<td style='" + cellStyle + "' colspan='3'>";
        html += "<h3 style='font-size:16px;'>デスラー暦カレンダー ver. 0.10</h3>";
        html += "このプログラムは、「宇宙戦艦ヤマト」に登場する「デスラー暦」に対応したカレンダーです。";
        html += "<br/>";
        html += "このプログラムは<a ";
        html += "style ='color: #aaaaff;'";
        html += " href=\"javascript:openURL('http://homepage.mac.com/s_jima/jsgame.html')\"";
        html += ">Javascript Game Garden</a>で配布しています。";
        html += "<br/>";
        html += "このプログラムの使い方は<a ";
        html += "style ='color: #aaaaff;'";
        html += " href=\"javascript:openURL('" + this.readmeUrl + "')\"";
        html += ">こちらのページ</a>に記載してあります。";
        html += "<br/>";
        html += "このプログラムの使用・配布は<a ";
        html += "style ='color: #aaaaff;'";
        html += " href=\"javascript:openURL('http://www.gnu.org/licenses/gpl.ja.html')\"";
        html += ">GNU General Public License</a>に準拠してください。";
        html += "<br/>";
        html += "</td>";
        html += "</tr>";
        html += "<tr>";
        html += "<td style='width: 229px; text-align: center;'>";
        html += "<br/>";
        html += "<br/>";
        html += "<br/>";
        html += "<input type='button' value='完了' onclick='app.dispOmote();'/>";
        html += "</td>";
        html += "</tr>";
        html += "</table>";
        html += "</td>";
        html += "</tr>";
        html += "</table>";
        elm.innerHTML = html;
    }

    this.getDesYear = function(seiYear) {
        var desYear = "";
        try {
            if (seiYear > 2096) {
                desYear = seiYear - 2096;
            } else {
                desYear = seiYear - 2097;
            }
            if (seiYear < 0) {
                desYear += 1;
            }
        } catch (e) {
        }
        return desYear;
    }

    this.setDesYear = function(desYear) {
        try {
            if (desYear > 0) {
                this.seiYear = desYear + 2096;
            } else {
                this.seiYear = desYear + 2097;
            }
            if (this.seiYear <= 0) {
                this.seiYear -= 1;
            }
        } catch (e) {
        }
    }

    this.getDesMonthString = function() {
        var result = "";

        result = "デスラー";
        var desYear = this.getDesYear(this.date.getFullYear());
        if (desYear <= 0) {
            result += "紀元前";
        } else {
            result += "紀元";
        }

        result += Math.abs(desYear) + "年";
        result += this.date.getMonth() + 1 + "月";

        return result;
    }

    this.getDayOfMonthFromWeek = function(week, day) {
        this.date.setDate(1);
        var firstWeekDay = this.date.getDay() + 1;
        if (week == 1) {
            if (day < firstWeekDay) {
                return "&nbsp;";
            }
        }
        var dom = ( (week - 1) * 7 + day - firstWeekDay + 1);
        var vdate = new Date();
        vdate.setFullYear(this.date.getFullYear());
        vdate.setMonth(this.date.getMonth());
        vdate.setDate(dom);
        if (this.date.getMonth() != vdate.getMonth() ) {
            return "&nbsp;";
        }
        if ( (this.curDate.getFullYear() == this.date.getFullYear() )
            && (this.curDate.getMonth() == this.date.getMonth() )
            &&(this.curDate.getDate() == dom) ) {
            return "<span style='font-weight: bold; color: #ff7700;'>" + dom + "</span>";
        }
        return dom;
    }

    this.getMetsuboDay = function(date) {
        var metsuboDate = new Date(2200, 9, 6);
        var curDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() );

        var result = Math.round((metsuboDate.getTime() - curDate.getTime())
                   / (24 * 3600 * 1000));

        if (result > 0) {
            return "人類滅亡まであと" + result + "日";
        } else {
            return "";
        }
    }

    this.getDesMessage = function(date, isError) {
        if (messagelist) {
            var curDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() );
            var kijunDate = new Date(1970, 0, 1);
            var d = (curDate.getTime() - kijunDate.getTime() );
            d = Math.round(d / (24 * 3600 * 1000)) + 38;
            d = d % messagelist.length;
            return messagelist[d].text + "(" + messagelist[d].from + ")";
        } else {
            return "残念だが君のブラウザーは対応していないようだ。"
                + "別のブラウザを試してもらいたい。";
        }
    }

    // 表面を表示する。
    this.dispOmote = function() {
        if (window.widget) {
            widget.prepareForTransition("ToFront");
        }
		this.dispCal();
        if (window.widget) {
            setTimeout("widget.performTransition();", 0);
        }
    }

    // 裏面を表示する
    this.dispUra = function() {
        if (window.widget) {
            widget.prepareForTransition("ToBack");
        }
		this.dispInfo();
        if (window.widget) {
            setTimeout("widget.performTransition();", 0);
        }
    }

}

var app = new Descal();