var blogID;
var placename;

function getCalendar(blogURL, id, path, pname) {
    blogID = id;
//    var cookie = readCookie("AjaxCal" + id);
//    if(cookie != null) {
//        if(blogURL.lastIndexOf("/") != blogURL.length - 1) {
//            path = blogURL + "/calendar/" + cookie + "/";
//        } else {
//            path = blogURL + "calendar/" + cookie + "/";
//        }
//    }
//alert(path);
	placename = pname;

    changeMonth(path, placename);
}

function changeMonth(url, pname) {
    
	placename = pname;
	
	url += "?" + (new Date()).getTime();
	
    new Ajax.Updater({success: placename},
                     url, {
                         method: 'get',
                         onComplete: endProcess,
                         onFailure: errorProcess
                     });
    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() {
	//alert ("endProcess");
    var value = document.getElementById("itemtable").getAttribute('summary');
	//alert (value);
    setWeekendAndHoliday(value.split("/")[0], value.split("/")[1]);
    document.cookie = "AjaxCal" + blogID + "=" + value + "; path=/";
}

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

function setWeekendAndHoliday(y,m) {
    setCurrentDate();
    //var elements = document.getElementById("itemtable");
	var year = y;
	var month = m;
	//alert ("y:"+y);
	//alert ("m:"+m);

    //for (var j = 0; j < elements.length; j++) {
        //var trs = document.getElementsByName("sldayline");
		//alert ("trs.length"+trs.length);
        
		var monthdate = new Date(year, month, 0).getDate();

		alert(monthdate);
		var day;
        for (i = 0; i < monthdate; i++) {
			//var trdate = trs[i].date;
			//alert (i);
            //if (trdate) {
				//alert ('trdate');
                /*
				if(trdate.innerHTML.indexOf("href") != -1){
                    day = trdate.getElementsByTagName("a")[0].innerHTML;
                } else {
                    day = trdate.innerHTML;
                }
				*/
				//alert ("day:");
				day = i + 1;
				var trdayline = document.getElementById("dayline"+day);
				var trdate = document.getElementById("date"+day);
				var tryoubi = document.getElementById("youbi"+day);
				
				//alert ("year:"+year);
				//alert ("month:"+month);
				//alert ("day:"+day);
				
                if(isSaturday(year, month, day)) {
					trdayline.setAttribute('class', 'holiday');
                    trdayline.setAttribute('className', 'holiday');
                    trdate.setAttribute('class', 'saturday');
                    trdate.setAttribute('className', 'saturday');
                    tryoubi.setAttribute('class', 'saturday');
                    tryoubi.setAttribute('className', 'saturday');
                }else if(isSunday(year, month, day)) {
					trdayline.setAttribute('class', 'holiday');
                    trdayline.setAttribute('className', 'holiday');
                    trdate.setAttribute('class', 'sunday');
                    trdate.setAttribute('className', 'sunday');
                    tryoubi.setAttribute('class', 'sunday');
                    tryoubi.setAttribute('className', 'sunday');
                }else if(isHoliday(year, month, day)){
					trdayline.setAttribute('class', 'holiday');
                    trdayline.setAttribute('className', 'holiday');
				}
                if (isToday(year, month, day)) {
                }
            //}
        }
    //}
}

