﻿var myMonth;

function LoadCalendar() {
    LoadingCalendar();
    calendar.CalendarHTML(idClendar, OnCompleteCalendar, LoadCalendarError);
}

function LoadMonth(Month) {
    myMonth = Month
    LoadingCalendar();
    calendar.CalendarHTMLMonth(idClendar, Month, OnCompleteCalendar, LoadCalendarError);
}

function OnCompleteCalendar(result) {
    $get(CalendarElement).innerHTML = result;
    defaultTooltips = new tooltip("defaultTooltips", "", "tooltips", "", 0, 0, 85);
}

function LoadCalendarError() {
    LoadingCalendar(true);
}

function LoadingCalendar(Error) {
    var myCalendarElement = $get(CalendarElement)
    while(myCalendarElement.hasChildNodes()) {
        myCalendarElement.removeChild(myCalendarElement.childNodes[0]);
    };
    var table = document.createElement("table");
    table.setAttribute("border", "0");
    table.setAttribute("cellspacing", "0");
    table.setAttribute("cellpadding", "0");
    table.setAttribute("class", "calendar");
    var trHeader = document.createElement("tr");
    var tdHeader = document.createElement("td");
    tdHeader.setAttribute("height", "25");
    tdHeader.setAttribute("class", "month");
    tdHeader.appendChild(document.createTextNode("Kalendář"));
    trHeader.appendChild(tdHeader);
    table.appendChild(trHeader);
    var tr = document.createElement("tr");
    var td = document.createElement("td");
    if(Error) {
        td.setAttribute("class", "error");
        td.appendChild(document.createTextNode("Chyba při načítání."));
        td.appendChild(document.createElement("br"));
        var a = document.createElement("a");
        if(myMonth) {
            a.tdHeader.setAttribute("href", "javascript:LoadMonth(" + myMonth + ");");
        } else {
            a.tdHeader.setAttribute("href", "javascript:LoadCalendar();");
        }
        a.appendChild(document.createTextNode("Zkusit znovu."));
        td.appendChild(a);
    } else {
        td.setAttribute("class", "weekend");
        td.appendChild(document.createTextNode("Načítám,"));
        td.appendChild(document.createElement("br"));
        td.appendChild(document.createTextNode("čekejte prosím."));
    }
    tr.appendChild(td);
    table.appendChild(tr);
    myCalendarElement.appendChild(table);
}