var sHolidays = new Array();
sHolidays[0] = new Date(2012, 2-1, 18);
sHolidays[1] = new Date(2012, 2-1, 25);
sHolidays[2] = new Date(2012, 3-1, 10);
sHolidays[3] = new Date(2012, 3-1, 24);


// 追加の休日
var sHolidaysEx = new Array();
sHolidaysEx[1] = new Date(2011, 12-1, 31);
sHolidaysEx[2] = new Date(2012, 1-1, 1);
sHolidaysEx[3] = new Date(2012, 1-1, 2);
sHolidaysEx[4] = new Date(2012, 1-1, 3);
sHolidaysEx[0] = new Date(2012, 2-1, 11);
sHolidaysEx[1] = new Date(2012, 3-1, 20);



// 追加の午前診療
var sHalf = new Array();
//sHalf[0] = new Date(2011, 12-1, 29);


var sReson = '';


function mail() {
	m1 = "jiwaa"
	m2 = "seagreen.ocn.ne.jp"
	m0 = "mailto:"
	location.href =(m0+m1+"@"+m2);
}



// 月の日数を得る
function daysInMonth(month, year){
  return 32 - new Date(year, month-1, 32).getDate();
}


function calendar() {
	
	// 今月
	var now = new Date();
	
	// 今月初日
	var thisMonth = new Date( now.getFullYear(), now.getMonth(), 1);
	
	// タイトルの部分＋月
	var newH2 = document.createElement('h2');
		newH2.className = 'calendar';
		newH2.appendChild( document.createTextNode( now.getMonth()+1+ '月'));

	// テーブル
	var newTable = document.createElement('table');
		newTable.className = 'calendar';
	var	newTbody = document.createElement('tbody');
	
	// 曜日の書き出し
	var tr = document.createElement("tr");
	var th = document.createElement("th");	th.innerHTML = "日";	tr.appendChild(th);
	var th = document.createElement("th");	th.innerHTML = "月";	tr.appendChild(th);
	var th = document.createElement("th");	th.innerHTML = "火";	tr.appendChild(th);
	var th = document.createElement("th");	th.innerHTML = "水";	tr.appendChild(th);
	var th = document.createElement("th");	th.innerHTML = "木";	tr.appendChild(th);
	var th = document.createElement("th");	th.innerHTML = "金";	tr.appendChild(th);
	var th = document.createElement("th");	th.innerHTML = "土";	tr.appendChild(th);
	newTbody.appendChild(tr);


	/// 準備
	// カレンダーの段数
	// ( 1日の曜日(日曜=0) + 最終日 ) / 7
	var lastDay = daysInMonth( now.getMonth()+1, now.getYear() ); 	// 今月の最終日
	var calRows = Math.ceil(( thisMonth.getDay() + lastDay) / 7) ; 	// 段数
	
	var day = 1　-　thisMonth.getDay();
	
	for(var i = 0 ; i < calRows ; i++) {
	  var tr = document.createElement("tr");
	  for(var j = 0 ; j < 7 ; j++) {
	    var td = document.createElement("td");
	    
	    // 日付と曜日によって処理を変える
	    if ((day <= 0) || (day > lastDay)) {
	    	td.className="blank";
	    } else {
	    	td.innerHTML = day;
	    	if ( (j==3) || (j==6) ) {
	    		td.className="sat";
	    	} else if ( j==0 ) {
	    		td.className="sun";
	    	}
	    	
			// 土曜休診日
			for (var n = 0; n < sHolidays.length; n++ ) {
				
				if ( ( sHolidays[n].getFullYear() ==  now.getFullYear()) &&
				     ( sHolidays[n].getMonth() == now.getMonth() ) &&
				     ( sHolidays[n].getDate() == day ) ) {
					td.className="sun";
				}
			}
			
			// その他の休診日
			for (var n = 0; n < sHolidaysEx.length; n++ ) {
				
				if ( ( sHolidaysEx[n].getFullYear() ==  now.getFullYear()) &&
				     ( sHolidaysEx[n].getMonth() == now.getMonth() ) &&
				     ( sHolidaysEx[n].getDate() == day ) ) {
					td.className="sun";
				}
			}
			
			// その他の午前診療日
			for (var n = 0; n < sHalf.length; n++ ) {
				
				if ( ( sHalf[n].getFullYear() ==  now.getFullYear()) &&
				     ( sHalf[n].getMonth() == now.getMonth() ) &&
				     ( sHalf[n].getDate() == day ) ) {
					td.className="sat";
				}
			}
	    }
	    
	    // 今日
	    if ( now.getDate() == day ) {
	    	td.style.textDecoration ='underline';
	    }
	    
	    day++;
	    
	    tr.appendChild(td);
	  }
	  newTbody.appendChild(tr);
	}
	newTable.appendChild( newTbody );


	document.getElementById('calendar').appendChild( newH2 );
	document.getElementById('calendar').appendChild( newTable );
	
	
	// 休診日の案内
	var infotext = '';
	var nonconsul = new Array();

	for (var n = 0; n < sHolidays.length; n++ ) {
		if ( ( sHolidays[n].getFullYear() ==  now.getFullYear()) &&
		     ( sHolidays[n].getMonth() == now.getMonth() ) && 
		     ( sHolidays[n].getDay() == 6 ) ) {
			nonconsul[nonconsul.length] = sHolidays[n].getDate();
		}
		
		// 今日が休診か
		if ( ( sHolidays[n].getFullYear() ==  now.getFullYear()) &&
		     ( sHolidays[n].getMonth() == now.getMonth() ) && 
		     ( sHolidays[n].getDate() == now.getDate() ) ) {
			var bSatholiday = true; // 今日は休日
		}
	}
	
	// 追加の休日
	for (var n = 0; n < sHolidays.length; n++ ) {
		// 今日が休診か
		if ( ( sHolidaysEx[n].getFullYear() ==  now.getFullYear()) &&
		     ( sHolidaysEx[n].getMonth() == now.getMonth() ) && 
		     ( sHolidaysEx[n].getDate() == now.getDate() ) ) {
			var bSatholiday = true; // 今日は休日
		}
	}
	
	
	// 追加の午前診療日
	for (var n = 0; n < sHalf.length; n++ ) {
		
		// 今日の場合
		if ( ( sHalf[n].getFullYear() ==  now.getFullYear()) &&
		     ( sHalf[n].getMonth() == now.getMonth() ) && 
		     ( sHalf[n].getDate() == now.getDate() )
		    ) {
			var bHalf = true;
		}
	}


	////
	if (nonconsul.length == 2) {
		infotext = now.getMonth()+1 +'月の土曜休診日は'+nonconsul[0]+'日と'+nonconsul[1]+'日です';
	} else if (nonconsul.length == 1) {
		infotext = now.getMonth()+1 +'月の土曜休診日は'+nonconsul[0]+'日です';
	} else 	if (nonconsul.length == 3) {
		infotext = now.getMonth()+1 +'月の土曜休診日は'+nonconsul[0]+'日、'+nonconsul[1]+'日、'+nonconsul[2]+'日です';}
	
	
	// その他の休日ご案内
	var infotext3 = '';
	var nonconsul3 = new Array();
	for ( n = 0; n < sHolidaysEx.length; n++ ) {
		if ( ( sHolidaysEx[n].getFullYear() ==  now.getFullYear() ) &&
		     ( sHolidaysEx[n].getMonth()    ==  now.getMonth()    ) ) {
			nonconsul3[nonconsul3.length] = sHolidaysEx[n].getDate();
		}
		
	}
	
	
//	if ( nonconsul3.length == 1 || sReason=='' ) {
//		infotext3 = (sHolidaysEx[0].getMonth()+1)+ "月"+sHolidaysEx[0].getDate()+"日は"+
//				    sReson+"休診です<br>" ;
//	} 	

	//if (nonconsul3.length == 2) {
//		infotext3 = (sHolidaysEx[1].getMonth()+1)+ "月"+sHolidaysEx[1].getDate()+"日は"+
//				    sReson+"休診です<br>" ;
//	} 


	// 本日のご案内
	// 日曜日
	var infotext2 = '';
	
	datetext = (now.getMonth()+1)+'月'+ now.getDate()+'日';
	if ( (now.getDay() == 0) || ( bSatholiday == true ) ) {
		infotext2 = '：本日は休診日です' ;
	} else 
	// 土曜日・水曜日・追加午前診療日
	if ( (now.getDay() == 6) || (now.getDay() == 3) || (bHalf == true) ) {
		if (now.getHours() >= 12) {
			infotext2 = '：本日の診察は終了しました';
		} else {
			infotext2 = '：本日は午前診療です' ;
		}
		
	} else {
		
		if ((now.getHours() >= 17) && ( now.getMinutes() >= 30 )) {
			infotext2 = '：本日の診察は終了しました';
		} else {
			infotext2 = '：本日は通常診療です' ;
		}
	}
	
	infotext = infotext +'<br>'+ infotext3+  datetext+ infotext2;
	document.getElementById('rest_info').innerHTML = infotext;
}



function backToTop() {
	var x1 = x2 = x3 = 0;
	var y1 = y2 = y3 = 0;
	if (document.documentElement) {
		x1 = document.documentElement.scrollLeft || 0;
		y1 = document.documentElement.scrollTop || 0;
	}
	if (document.body) {
		x2 = document.body.scrollLeft || 0;
		y2 = document.body.scrollTop || 0;
	}
	x3 = window.scrollX || 0;
	y3 = window.scrollY || 0;
	var x = Math.max(x1, Math.max(x2, x3));
	var y = Math.max(y1, Math.max(y2, y3));
	window.scrollTo(Math.floor(x / 2), Math.floor(y / 2));
	if (x > 0 || y > 0) {
		window.setTimeout("backToTop()", 20);
	}
}
