// JavaScript Document

(function($){
	$.fn.kscal=function(config){
        var defaults={
			url:"http://lilting.ch/",
			year:"",
			month:""
        };
        var options=$.extend(defaults, config);
		o=options;
		var toDate = new Date();
		toflag = false;
		if((o.year != "") && (o.month != ""))
		{
			var now = new Date(o.year,o.month - 1,toDate.getDate());
			if((o.year == toDate.getFullYear()) && (o.month == toDate.getMonth()+1))
			{
				toflag = true;
			}
		}
		else
		{
			var now = new Date();
			toflag = true;
		}
		//今日
		today = now.getDate();
		//月末
		var dt = new Date(now.getFullYear(), now.getMonth()+1, 0);
		var enddate = dt.getDate();
		//月初の曜日
		dt = new Date(now.getFullYear(), now.getMonth(), 1);
		var firstday = dt.getDay();
		montharray = new Array();
		for(i = 0;i < 42;i++)
		{
			if((i < firstday) || (i - firstday + 1 > enddate))
			{
				montharray[i] = "&nbsp;";
			}
			else
			{
				montharray[i] = i - firstday + 1;
			}
		}
		var tomonth = now.getMonth()+1;
		if(tomonth < 10){tomonth = "0" + tomonth}
		$("#ksCal th.tmonth").html(now.getFullYear() + "-" + tomonth);
		return $("#ksCal td").each(function(n,ele) {
			if(montharray[n] != "&nbsp;")
			{
				if((toflag == true) && (today == montharray[n]))
				{
					return $(this).addClass("today").html(montharray[n]).css("cursor","pointer");
				}else{
					return $(this).removeClass("today").html(montharray[n]).css("cursor","pointer");
				}
			}
			else
			{
				return $(this).html(montharray[n]).css("cursor","auto");
			}
		});
	};
})(jQuery);