﻿PublicoScripts.calendar =
{
    cssName: "",
    selMonth: "",
    selYear: "",
    selDay: "",
    arrM: new Array("Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"),
    arrD: new Array("S", "T", "Q", "Q", "S", "S", "D"),
    maxDaysCount: 34,
    rwAdded: false,

    maxDays: function (mm, yyyy) {
        var mDay;
        if ((mm == 3) || (mm == 5) || (mm == 8) || (mm == 10)) {
            mDay = 30;
        }
        else {
            mDay = 31
            if (mm == 1) {
                if (yyyy / 4 - parseInt(yyyy / 4) != 0) {
                    mDay = 28
                }
                else {
                    mDay = 29
                }
            }
        }
        return mDay;
    },
    gotoDate: function (d) {
        if (this.cssName)
            document.location = "/hemeroteca/?d=" + d;
        else
            document.location = "/?d=" + d;
    },
    getMonth: function (n, d) {
        switch (selMonth + n) {
            case -1:
                selMonth = 11;
                selYear -= 1;
                break;
            case 12, 13:
                selMonth = 1;
                selYear += 1;
                break;
            default:
                selMonth += n;
        }
        if (this.cssName) {
            document.location = '/hemeroteca/?d=' + d + "-" + selMonth + "-" + selYear;
        } else {
            $get("mDate").innerHTML = this.arrM[selMonth] + " " + selYear;
            this.changeCal();
        }
    },
    writeCalendar: function (css, m, d, y, hideNext, hidePrevious, setDate) {
        if (y)
            var now = new Date(y, m, d);
        else
            var now = new Date;

        var dd = now.getDate();
        var mm = now.getMonth();
        var dow = now.getDay();
        var yyyy = now.getFullYear();
        var text = "";
        var iCount = 0;
        var nRow;
        var alignment = css ? "left" : "";
        var jump = css ? 2 : 1;
        this.cssName = css;
        selMonth = mm;
        selYear = yyyy;

        text = "<table class=\"" + this.cssName + "\" align=\"" + alignment + "\" width=\"140\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" id=\"calendarTable" + this.cssName + "\">";
        text += "<tr><td><a style=\"visibility:" + hidePrevious + ";\" class=\"link\" onclick=\"PublicoScripts.calendar.getMonth(-" + jump + ", '" + dd + "');\" title=\"\">&nbsp;«&nbsp;</a></td>";
        text += "<td colspan=\"5\" id=\"mDate\">" + this.arrM[mm] + " " + selYear + "</a></td>";
        text += "<td><a class=\"link\" style=\"visibility:" + hideNext + ";\" onclick=\"PublicoScripts.calendar.getMonth(" + jump + ", '" + dd + "');\" title=\"\">&nbsp;»&nbsp;</a></td></tr>";

        text += "<tr class=\"weekDay\">";
        for (ii = 0; ii <= 6; ii++) {
            text += "<td>" + this.arrD[ii] + "</td>";
        }
        text += "</tr>"

        for (nRow = 0; nRow <= 4; nRow++) {
            //document.write(iCount + "##<br/>");
            text += "<tr>"
            for (nCell = 0; nCell <= 6; nCell++) {
                text += "<td id=\"sp" + this.cssName + iCount + "\"></td>"
                iCount += 1
            }
            text += "</tr>"
        }
        text += "</table>"
        document.write(text)
        this.changeCal(m, d, y, setDate)
    },

    changeCal: function (m, d, y, setDate) {
        if (y) {
            var now = new Date(y, m, d);
            var mmyyyy = now;
        } else {
            var now = new Date;
            var mmyyyy = new Date();
        }

        if (String(setDate) == "undefined")
            setDate = true;

        var dd = now.getDate();
        var mm = now.getMonth();
        var dow = now.getDay();
        var yyyy = now.getFullYear();
        var currM = parseInt(selMonth);
        var currY = parseInt(selYear);
        var arrN = new Array(this.maxDaysCount);
        var iCount;
        var dCount = 0;
        var ii = 0;

        mmyyyy.setFullYear(currY);
        mmyyyy.setMonth(currM);
        mmyyyy.setDate(1);

        var day1 = mmyyyy.getDay();
        var mDays = this.maxDays(currM, currY);

        if (day1 == 0) {
            if (mDays + 6 > 35) {
                this.addRowToTable();
            }
        } else {
            if (mDays + (day1 - 1) > 35) {
                this.addRowToTable();
            } else {
                if (mDays + (day1 - 1) < 28) {
                    this.removeRow();
                }
            }
        }

/*        if ((mDays == 31 && (day1 == 6 || day1 == 0)) || (mDays == 30 && day1 == 0)) {
            this.addRowToTable();
        } else {
            this.removeRow();
        }
*/

        if (day1 == 0) day1 = 7;

        for (ii = 0; ii < (day1 - 1); ii++)
            arrN[ii] = "";

        iCount = 1;
        day1 -= 1;
        for (ii = day1; ii <= day1 + mDays - 1; ii++) {
            arrN[ii] = iCount;
            iCount += 1
        }

        iCount = 1;
        for (ii = day1 + mDays; ii <= this.maxDaysCount; ii++) {
            arrN[ii] = "";
            iCount += 1;
        }

        for (ii = 0; ii <= this.maxDaysCount; ii++) {
            try {
                $get("sp" + this.cssName + ii).innerHTML = arrN[ii] != "" ? "<a class=\"link\" onclick=\"PublicoScripts.calendar.gotoDate('" + arrN[ii] + "-" + (selMonth + 1) + "-" + yyyy + "');\">" + PublicoScripts.Methods.Right("0" + arrN[ii], 2) + "</a>" : "";
                if (dCount == 6) {
                    $get("sp" + this.cssName + ii).className = "white";
                } else if (dCount == 5)
                    $get("sp" + this.cssName + ii).className = "gray";
                else
                    $get("sp" + this.cssName + ii).className = "";
                if ((arrN[ii] == dd) && (mm == currM) && (yyyy == currY) && setDate) {
                    $get("sp" + this.cssName + ii).className = "selected";
                }
                dCount += 1;
                if (dCount > 6) dCount = 0;
            } catch (e) { }
        }
    },
    addRowToTable: function () {
        var tb = $get("calendarTable" + this.cssName);
        //alert(tb.rows.length);
        var rw = document.createElement("tr");
        var iCount = 35;
        for (nCell = 0; nCell <= 6; nCell++) {
            var td1 = document.createElement("td");
            td1.id = "sp" + this.cssName + iCount;
            rw.appendChild(td1);
            iCount += 1
        }
        tb.appendChild(rw);
        //alert(tb.rows.length);        
        this.maxDaysCount = 41;
        this.rwAdded = true;
    },
    removeRow: function () {
        if (this.rwAdded) {
            var tb = $get("calendarTable" + this.cssName);
            var lastRow = tb.rows.length;
            tb.deleteRow(lastRow - 1);
            this.rwAdded = false;
            this.maxDaysCount = 35;
        }
    }
}
