/*
 * カレンダー生成
 *
 */

function createCalender(id,c,y,m){
	
	//年の繰越
	y = m>=13?++y:y;
	m = m>=13?m-12:m;
	
	//カレンダーのid/classをセット
	var
		cldr		= '',
		idName		= id,
		className	= c,	
		month 		= m,
		year 		= y,
		day 		= new Date(),
		ed			= 0.242194 * (y - 1980) - Math.floor((y - 1980)/4),
		numdays		= [31,28,31,30,31,30,31,31,30,31,30,31],
		sunday,
		holiday,
		thisday,
		eigyo;
	
	if(m == day.getMonth()+1){
		thisday = day.getDate(); 	//今日の日付 - 背景色を変更するため
		day.setDate(1);       		//今月最初の日にセット
	}else{
		thisday = 0;
		day = new Date(y,m-1);
	}
		
	// うるう年
	if((year%4 == 0) && (year%100 != 0) || (year%400 == 0)) numdays[1] = 29;
	
	// 休日・営業の定義
	// 休みにしたい日を配列に加える
	holiday =
		[
		/*  1月 */ [2,3,4,9], 
		/*  2月 */ [],
		/*  3月 */ [19,20], 
		/*  4月 */ [30], 
		/*  5月 */ [1,2,3,4], 
		/*  6月 */ [], 
		/*  7月 */ [16], 
		/*  8月 */ [13,14,15,16,17], 
		/*  9月 */ [17], 
		/* 10月 */ [8], 
		/* 11月 */ [23], 
		/* 12月 */ [29,30]
	   ];
	// 営業にしたい日を配列に加える
	eigyo =
		[
		/*  1月 */ [], 
		/*  2月 */ [],
		/*  3月 */ [], 
		/*  4月 */ [], 
		/*  5月 */ [], 
		/*  6月 */ [], 
		/*  7月 */ [], 
		/*  8月 */ [], 
		/*  9月 */ [], 
		/* 10月 */ [], 
		/* 11月 */ [], 
		/* 12月 */ []
	   ];
		
	holiday = holiday[month-1]
	eigyo = eigyo[month-1]	
	
	// 変動制祝日の自動生成	
	// 必要のない場合はコメントアウト
	switch(month){
		case 1:
			//成人の日
			holiday.push(14-(day.getDay()+5)%7);
		break;
		case 3:
			//春分の日
			holiday.push(Math.floor(20.8431 + ed));
		break;
		case 7:
			//海の日
			holiday.push(21-(day.getDay()+5)%7);
		break;
		case 9:
			//敬老の日
			holiday.push(21-(day.getDay()+5)%7);
			//秋分の日
			holiday.push(Math.floor(23.2488 + ed));
		break;
		case 10:
			//体育の日
			holiday.push(14-(day.getDay()+5)%7);
		break;
	}
	
	// カレンダー本体
	cldr += "<table id='"+idName+"' class='"+className+"'>";
	cldr += "<caption>営業カレンダー "+year+"年"+month+"月</caption>";
	cldr += "<tr><th class='sunday week'>日</th><th class='weekday week'>月</th><th class='weekday week'>火</th><th class='weekday week'>水</th><th class='weekday week'>木</th><th class='weekday week'>金</th><th class='saturday week'>土</th></tr>";
	
	// 日付の書き込み
	for(sunday = 1-day.getDay(); sunday <= 31; sunday +=7){
		cldr += "<tr>";
		for(i = sunday; i < sunday + 7; i++){
			if((i > 0) && (i <= numdays[month-1])){
				
				var dayclass;
				
				day.setDate(i);
				today = day.getDay();
				
				//今日の判定
				if(thisday == i) cldr += "<td class='thisday";    
				else cldr += "<td class='day";
				
				//土日の判定
				switch(today){
					case 0:dayclass = "sunday";break;
					case 6:dayclass = "saturday";break;
					default:dayclass = "normal";break;
				}
				
				//祝日の判定
				j = 0;
				while(holiday[j]){
					if((i==holiday[j])||(today == 1 && i-1==holiday[j])){
						dayclass = "holiday";
						j=holiday[j].length;
					}
					j++;
				}
				//営業日の判定
				j = 0;
				while(eigyo[j]){
					if(i==eigyo[j]){
						dayclass = "normal";
						j=eigyo[j].length;
					}
					j++;
				}
				
				cldr += " "+dayclass+"'>"+ i +"</td>";
				
			}else{
				//空セル
				cldr += "<td class='notthismonth'>&nbsp;</td>";
			}
		}
		cldr += "</tr>";
	}
	cldr += "</table>";
	return cldr;
}

//実行
//カレンダー書き出し createCalender(id,class,year,month)
var day = new Date();
var y = day.getFullYear(), m = day.getMonth() + 1;
//当月
document.write(createCalender('c1','calender',y,m));
//翌月
document.write(createCalender('c2','calender',y,++m)); 

//jQueryで挿入なら
//$("#cal").html(createCalender('c2','calender',y,++m));

