// layer : 1 ... 1st, 2 ... 2nd
// calendarSize : "l" ... large, "m" ... medium
function setupCalendar(layer, calendarSize) {
    var url     = "cms/web.php?menu=calendar&year=";
    var path    = ""; // 遷移先のパス
    var imgPath = ""; // カレンダーの星の画像パス

    if (layer == "0") { // 市と同じ階層
        url     = "../" + url;
        imgPath = "../";
    } else if (layer == "1") { // トップの階層
        path = "ichi/";
    } else if (layer == "2") { // ２階層
        path    = "../ichi/";
        url     = "../" + url;
        imgPath = "../";
    }

    var mDate = new Date();
    var bDate = new Date(mDate.getFullYear(), mDate.getMonth(), "29");
    var currYear  = mDate.getFullYear();
    var currMonth = mDate.getMonth() + 1;
    if (bDate <= mDate) {
        currMonth++;
    }

    var currUrl = url + currYear + "&month=" + currMonth;
    $.getJSON(currUrl, function(data) { createCalendar(data, currYear, currMonth, "curr", calendarSize, path, imgPath); });

    var thisMonth = mDate.getMonth();
    mDate.setMonth(currMonth);
    var nextYear  = mDate.getFullYear();
    var nextMonth = mDate.getMonth() + 1;

    var nextUrl = url + nextYear + "&month=" + nextMonth;
    $.getJSON(nextUrl, function(data) { createCalendar(data, nextYear, nextMonth, "next", calendarSize, path, imgPath); });
}

function createCalendar(data, year, month, whichCal, calendarSize, path, imgPath) {
    var starOther     = "ico_st_other";
    var starRecycle   = "ico_st_recycle";
    var starKotto     = "ico_st_kotto";
    var starZakka     = "ico_st_zakka";
    var starKikinzoku = "ico_st_kikinzoku";
    var starKimono    = "ico_st_kimono";
    var width         = "12";
    var height        = "12";

    if (calendarSize == "m" || calendarSize == "s") {
        starOther     = "ico_st_other"     + "_" + calendarSize + ".gif";
        starRecycle   = "ico_st_recycle"   + "_" + calendarSize + ".gif";
        starKotto     = "ico_st_kotto"     + "_" + calendarSize + ".gif";
        starZakka     = "ico_st_zakka"     + "_" + calendarSize + ".gif";
        starKikinzoku = "ico_st_kikinzoku" + "_" + calendarSize + ".gif";
        starKimono    = "ico_st_kimono"    + "_" + calendarSize + ".gif";
    } else {
        starOther     = "ico_st_other.gif";
        starRecycle   = "ico_st_recycle.gif";
        starKotto     = "ico_st_kotto.gif";
        starZakka     = "ico_st_zakka.gif";
        starKikinzoku = "ico_st_kikinzoku.gif";
        starKimono    = "ico_st_kimono.gif";
    }

    if (calendarSize == "m") {
        width  = "10";
        height = "10";
    } else if (calendarSize == "s") {
        width  = "9";
        height = "9";
    }

    var tableObj = $(".calendar table#" + whichCal + "_calendar_body");
    $.each(data, function(i, week) {
        var trObj = $("<tr>");
        $.each(week, function(j, day) {
            var tdObj = $("<td>");
            if (day.DAY == null) {
                tdObj.append("&nbsp;");
            }
            if (day.DAY != null) {
                tdObj.append("<em>" + day.DAY + "</em>");

                $.each(day.INFO, function(j, info) {
                    var aObj = $("<a>");
                    aObj.attr("class", "tip tip" + info.TYPE);
                    var msg = "|" + info.HOUR + ":" + info.MINUTE + "<em>" + info.TYPE_NAME + "</em>" ;
                    aObj.attr("title", msg);
                    aObj.hover(
                        function() { $("#cluetip-inner").attr("class", "tip" + info.TYPE); },
                        function() { $("#cluetip-inner").attr("class", "tip"); }
                    );

                    if (info.TYPE == 0) {
                        aObj.click(function() { return false; });
                    }

                    var href = "#";
                    var imgStr = "";

                    switch (info.TYPE) {
                    case 0: imgStr = '<img src="' + imgPath + 'images/calender/star/' + starOther     + '" width="' + width + '" height="' + height + '" alt="その他" />'; break;
                    case 1: imgStr = '<img src="' + imgPath + 'images/calender/star/' + starRecycle   + '" width="' + width + '" height="' + height + '" alt="リサイクル市" />';       href = path + 'recycle.html';   break;
                    case 2: imgStr = '<img src="' + imgPath + 'images/calender/star/' + starKotto     + '" width="' + width + '" height="' + height + '" alt="骨董市" />';             href = path + 'kotto.html';     break;
                    case 3: imgStr = '<img src="' + imgPath + 'images/calender/star/' + starZakka     + '" width="' + width + '" height="' + height + '" alt="美術・工芸品" />';       href = path + 'koge.html';      break;
                    case 4: imgStr = '<img src="' + imgPath + 'images/calender/star/' + starKikinzoku + '" width="' + width + '" height="' + height + '" alt="貴金属・ブランド品" />'; href = path + 'kikinzoku.html'; break;
                    case 5: imgStr = '<img src="' + imgPath + 'images/calender/star/' + starKimono    + '" width="' + width + '" height="' + height + '" alt="着物市" />';             href = path + 'kimono.html';    break;
                    }

                    aObj.html(imgStr);
                    aObj.attr("href", href);
                    tdObj.append(aObj);
                });

            } else {
                tdObj.append("");
            }

            trObj.append(tdObj);
        });
        tableObj.append(trObj);
    });
    month = (month > 9) ? month : "0" + month;
    $("#" + whichCal + "_month_title").html(year + ".<span>" + month + "</span>");

    $("a.tip").cluetip({
        width: 170,
        splitTitle: "|",
        showTitle: false,
        clickThrough: true
    });
}
