/**
 * JavaScript calendar
 *
 * @package   Calendar
 * @category  Javascript calendar
 * @author    Remigijus Jarmalavicius (ReekenX)
 * @copyright 2008 Remigijus Jarmalavicius
 * @version   2.0.0
 */

/**
 * Main calendar class
 */
function Rx_Calendar()
{
    this.Language                   = "LT";
    if (this.Language == "EN") {
        this.MonthNames             = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
        this.WeekDays               = ["S", "M", "T", "W", "T", "F", "S"];
    } else if (this.Language == "LT") {
        this.MonthNames             = ['Sausis', 'Vasaris', 'Kovas', 'Balandis', 'Gegužė', 'Birželis', 'Liepa', 'Rugpjūtis', 'Rugsėjis', 'Spalis', 'Lapkritis', 'Gruodis'];
        this.WeekDays               = ["S", "P", "A", "T", "K", "Pn", "Š"];    
    }
    this.MonthDays              = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];   
    this.WeekStart              = 1;
    this.AdditionalTableStyle   = "background-color: #00759B; color: white; font-family: Tahoma; font-size: 11px;";
    this.AdditionalWeekStyle    = "background-color: white; color: #00759B; border-bottom: 1px black solid;";
    this.AdditionalTDStyle      = "background-color: white; color: #666666;";
    this.TDPadding              = 3;

    this.SetStorageValue = function (STORAGE_ID, Y, M, D, FORMAT) {
        var obj = document.getElementById(STORAGE_ID);
        //if (M < 10) {M = '0' + (M+1);}
        if (D < 10) {D = '0' + D;}
        obj.value = Y + '-' + (M+1) + '-' + D;
    
    }
    
    this.OutputCalendar = function (HTML_ID, STORAGE_ID, DATE_FORMAT, START_Y, START_M) 
    {
        /**
         * Check if Year or Month values are not set
         */
        var TempDate = new Date();
        if (typeof(START_Y) == "undefined") {        
            START_Y = TempDate.getFullYear();
        }
        if (typeof(START_M) == "undefined") {
            START_M = TempDate.getMonth();
        }
        var Calendar  = new Date(START_Y, START_M, 1);        
    
        /**
         * Detect how much days have February month
         */
        this.MonthDays[1] = (((Calendar.getFullYear() % 100 != 0) && (Calendar.getFullYear() % 4 == 0)) || (Calendar.getFullYear() % 400 == 0)) ? 29:28;
        
        /**
         * Show Calendar code
         */

        var FullCalendar = "<table width='160' border='0' cellpadding='" + this.TDPadding + "' cellspacing='0' style='" + this.BordersStyle + "; " + this.AdditionalTableStyle + "'>";
        FullCalendar += "<tr>";
        if ((START_M != 0)) {
            FullCalendar += "<td align='center'><img src='templates/Default/images/atgal_mygtukas.jpg' style='width: 20px;' value='&lt;' onclick='CALENDAR.OutputCalendar(\"" + HTML_ID + "\", \"" + STORAGE_ID + "\", " + DATE_FORMAT + ", " + START_Y + ", " + (START_M - 1) + ")' /></td>";
        } else if (START_M == 0) {
            FullCalendar += "<td align='center'><img src='templates/Default/images/atgal_mygtukas.jpg' style='width: 20px;' value='&lt;' onclick='CALENDAR.OutputCalendar(\"" + HTML_ID + "\", \"" + STORAGE_ID + "\", " + DATE_FORMAT + ", " + (START_Y - 1) + ", 11)' /></td>";
        }
        
        FullCalendar += "<td colspan='5' align='center'><b>" + START_Y + " m. " + this.MonthNames[START_M] + "</b></td>";
        if ((START_M != 11)) {
            FullCalendar += "<td align='center'><img src='templates/Default/images/pirmyn_mygtukas.jpg' style='width: 20px;' value='&gt;' onclick='CALENDAR.OutputCalendar(\"" + HTML_ID + "\", \"" + STORAGE_ID + "\", " + DATE_FORMAT + ", " + START_Y + ", " + (START_M + 1) + ")' /></td></tr>";
        } else if (START_M == 11) {
            FullCalendar += "<td align='center'><img src='templates/Default/images/pirmyn_mygtukas.jpg' style='width: 20px;' value='&gt;' onclick='CALENDAR.OutputCalendar(\"" + HTML_ID + "\", \"" + STORAGE_ID + "\", " + DATE_FORMAT + ", " + (START_Y + 1) + ", 0)' /></td></tr>";
        }
        FullCalendar += "<tr>";
        for (i = 0; i < 7; i++) {
            FullCalendar += "<td align='center' style='" + this.AdditionalWeekStyle + "'>" + this.WeekDays[(i + this.WeekStart) % 7] + "</td>";
        }
        FullCalendar += "<tr>";
        
        var Day = 1;
        var StartFromDay = Calendar.getDay();
        if (StartFromDay == 0) {
            StartFromDay = 7;
        }
        var after = 0;
        var BEFORE_M = START_M - 1;
        if (BEFORE_M = 0) {
            BEFORE_M = 1;
        }
        var start = false;
        for (i = 0; i < 9; i++) {
            for (j = 0; j <= 6; j++) {
                //if ((Day <= this.MonthDays[Calendar.getMonth()]) && (i > 0 || j >= StartFromDay - this.WeekStart)) {
                if ((Day <= this.MonthDays[START_M]) && (i > 0 || j >= StartFromDay - this.WeekStart)) {
                    FullCalendar += "<td align='center' style='cursor: pointer; " + this.AdditionalTDStyle +"' id='" + HTML_ID + "_day_" + Day + "' onclick='CALENDAR.Unclick(\"" + HTML_ID + "\", this, " + Day + "); CALENDAR.SetStorageValue(\"" + STORAGE_ID + "\", " + START_Y + ", " + START_M + ", " + Day + ", " + DATE_FORMAT+ ")'>" + Day + "</td>";
                    Day++;
                    start = true;
                } else {
                    if (start) {
                        after++;
                        FullCalendar += "<td align='center' style='background-color: white; color: silver;'>" + after + "</td>";
                    } else {
                            FullCalendar += "<td align='center' style='background-color: white; color: silver;'>" + (this.MonthDays[BEFORE_M] + 2 - StartFromDay + j) + "</td>";

                    }
                }
            }  
            FullCalendar += "</td>";
            
            if (Day > this.MonthDays[Calendar.getMonth()]) {
                if (i < 5) {
                     FullCalendar += "<tr>";
                     for (j = 0; j <= 6; j++) {
                        after++;
                        FullCalendar += "<td align='center' style='background-color: white; color: silver;'>" + after + "</td>";
                     }
                     FullCalendar += "</tr>";
                     i++;
                }
                if (i < 5) {
                     FullCalendar += "<tr>";
                     for (j = 0; j <= 6; j++) {
                        after++;
                        FullCalendar += "<td align='center' style='background-color: white; color: silver;'>" + after + "</td>";
                     }
                     FullCalendar += "</tr>";
                }                
                break;
            } else {
                FullCalendar += "</tr><tr>";
            }
        } 
        
        FullCalendar += "</table>";        
        document.getElementById(HTML_ID).innerHTML = FullCalendar + "<input type='hidden' id='" + HTML_ID + "_date_selected' />";
    }

    
    this.Unclick = function(HTML_ID, OBJECT, DAY)
    {
        var obj = document.getElementById(HTML_ID + "_date_selected");
        if (obj.value != "") {
            var date = document.getElementById(HTML_ID + "_day_" + obj.value);
            date.style.backgroundColor = "white";
            date.style.color = "#666666";
        }
        obj.value = DAY;
        OBJECT.style.backgroundColor = "#00759B";
        OBJECT.style.color = "white";
    }
    
    this.ClickDay = function(HTML_ID, DAY)
    {
        var obj = document.getElementById(HTML_ID + "_date_selected");
        var SEL_DAY = document.getElementById(HTML_ID + "_day_" + DAY);
        SEL_DAY.style.backgroundColor = "#00759B";
        SEL_DAY.style.color = "white"; 
        obj.value = DAY;
    }
}

/**
 * Define calendar variable
 */
CALENDAR = new Rx_Calendar();