var blogID;

function getCalendar_top(blogURL, id, path) {
    blogID = id;
    var cookie = readCookie("AjaxCal" + id);
    if(cookie != null) {
        if(blogURL.lastIndexOf("/") != blogURL.length - 1) {
            path = blogURL + "/calendar_top/" + cookie + "/";
        } else {
            path = blogURL + "calendar_top/" + cookie + "/";
        }
    }
    changeMonth(path);
}

function getCalendar_header(blogURL, id, path) {
    blogID = id;
    var cookie = readCookie("AjaxCal" + id);
    if(cookie != null) {
        if(blogURL.lastIndexOf("/") != blogURL.length - 1) {
            path = blogURL + "/calendar_header/" + cookie + "/";
        } else {
            path = blogURL + "calendar_header/" + cookie + "/";
        }
    }
    changeMonth_header(path);
}

function getCalendar_body(blogURL, id, path) {
    blogID = id;
    var cookie = readCookie("AjaxCal" + id);
    if(cookie != null) {
        if(blogURL.lastIndexOf("/") != blogURL.length - 1) {
            path = blogURL + "/calendar_body/" + cookie + "/";
        } else {
            path = blogURL + "calendar_body/" + cookie + "/";
        }
    }
    changeMonth(path);
}

function changeMonth(url) {
    url += "?" + (new Date()).getTime();
    new Ajax.Updater({success: 'calendar'},
                     url, {
                         method: 'get',
                         onComplete: endProcess,
                         onFailure: errorProcess
                     });
    return false;
}

function changeMonth_header(url) {
    url += "?" + (new Date()).getTime();
    new Ajax.Updater({success: 'calendar_header'},
                     url, {
                         method: 'get',
                         onComplete: endProcess_header,
                         onFailure: errorProcess_header
                     });
    return false;
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0, len = ca.length; i < len; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function endProcess() {
    var value = $('calendar').getElementsByTagName('table')[0].getAttribute('summary');
    setWeekendAndHoliday(value.split("/")[0], value.split("/")[1]);
    document.cookie = "AjaxCal" + blogID + "=" + value + "; path=/";
}

function endProcess_header() {
    var value = $('calendar_header').getElementsByTagName('div')[0].getAttribute('id');
    setWeekendAndHoliday(value.split("/")[0], value.split("/")[1]);
    document.cookie = "AjaxCal" + blogID + "=" + value + "; path=/";
}

function errorProcess() {
    $('calendar').innerHTML = 'File Not Found';
}

function errorProcess_header() {
    $('calendar_header').innerHTML = 'File Not Found';
}

function setWeekendAndHoliday(y,m) {
    setCurrentDate();
    var elements = $('calendar').getElementsByTagName("table");
    for (var j = 0, len = elements.length; j < len; j++) {
        var element = elements[j].getAttribute("summary");
        if(element == null){
            return;
        }
        var year = element.split("/")[0];
        var month = element.split("/")[1];
        if(!(year == y && month == m)){
            return;
        }
        var tds = elements[j].getElementsByTagName("td");
        var day;
        for (i = 0; i < tds.length; i++) {
            if (tds[i].parentNode.nodeName == "TR") {
                if(tds[i].innerHTML.indexOf("href") != -1){
                    day = tds[i].getElementsByTagName("a")[0].innerHTML;
                } else if (tds[i].innerHTML.indexOf("div") != -1) {
                    day = tds[i].getElementsByTagName("div")[0].innerHTML;
                } else {
                    day = tds[i].innerHTML;
                }
                if (isHoliday(year, month, day)) {
									if (tds[i].getAttribute('class')) {
                    tds[i].setAttribute('class', 'cal-sun ' + tds[i].getAttribute('class'));
									} else if (tds[i].getAttribute('className')) {
                    tds[i].setAttribute('className', 'cal-sun ' + tds[i].getAttribute('className'));
									} else {
                    tds[i].setAttribute('class', 'cal-sun');
                    tds[i].setAttribute('className', 'cal-sun');
                  }
                } else if(isSaturday(year, month, day)) {
									if (tds[i].getAttribute('class')) {
                    tds[i].setAttribute('class', 'cal-sat ' + tds[i].getAttribute('class'));
									} else if (tds[i].getAttribute('className')) {
                    tds[i].setAttribute('className', 'cal-sat ' + tds[i].getAttribute('className'));
                  }else {
                    tds[i].setAttribute('class', 'cal-sat');
                    tds[i].setAttribute('className', 'cal-sat');
                  }
                }
                if (isToday(year, month, day)) {
                    tds[i].parentNode.setAttribute('class', 'today');
                    tds[i].parentNode.setAttribute('className', 'today');
                }
            }
        }
    }
}

function setWeekendAndHoliday2(y,m) {
    setCurrentDate();
    var elements = document.getElementsByTagName("table");
    for (var j = 0, len = elements.length; j < len; j++) {
        var element = elements[j].getAttribute("summary");
        if(element == null){
            return;
        }
        var year = element.split("/")[0];
        var month = element.split("/")[1];
        if(!(year == y && month == m)){
            return;
        }
        var tds = elements[j].getElementsByTagName("td");
        var day;
        for (i = 0; i < tds.length; i++) {
            if (tds[i].parentNode.nodeName == "TR") {
                if(tds[i].innerHTML.indexOf("href") != -1){
                    day = tds[i].getElementsByTagName("a")[0].innerHTML;
                } else if (tds[i].innerHTML.indexOf("div") != -1) {
                    day = tds[i].getElementsByTagName("div")[0].innerHTML;
                } else {
                    day = tds[i].innerHTML;
                }
                if (day.match(/[0-9]+/)) {
                  day = day.match(/[0-9]+/);
                } else {
                  day = "-";
                }
                if (isHoliday(year, month, day)) {
									if (tds[i].getAttribute('class')) {
                    tds[i].setAttribute('class', 'cal-sun ' + tds[i].getAttribute('class'));
									} else if (tds[i].getAttribute('className')) {
                    tds[i].setAttribute('className', 'cal-sun ' + tds[i].getAttribute('className'));
									} else {
                    tds[i].setAttribute('class', 'cal-sun');
                    tds[i].setAttribute('className', 'cal-sun');
                  }
                } else if(isSaturday(year, month, day)) {
									if (tds[i].getAttribute('class')) {
                    tds[i].setAttribute('class', 'cal-sat ' + tds[i].getAttribute('class'));
									} else if (tds[i].getAttribute('className')) {
                    tds[i].setAttribute('className', 'cal-sat ' + tds[i].getAttribute('className'));
                  }else {
                    tds[i].setAttribute('class', 'cal-sat');
                    tds[i].setAttribute('className', 'cal-sat');
                  }
                }
                if (isToday(year, month, day)) {
                    tds[i].parentNode.setAttribute('class', 'today');
                    tds[i].parentNode.setAttribute('className', 'today');
                }
            }
        }
    }
}
