var holyday = new Array();
holyday[2010] = [
					[],
					[],
					[],
					[],
					[],
					[],
					[],
					[],
					[],
					[],
					[],
					[]
			];
holyday[2011] = [
					[],
					[],
					[],
					[3,17,25,29],
					[1,3,4,5,9,15,23,29],
					[5,13,18,19,20,27],
					[3,11,17,18,25,31],
					[7,14,15,16,21,29],
					[4,12,18,19,23,26],
					[2,10,16,24,30],
					[3,6,14,20,23,28],
					[4,12,18,23,26]
			];
holyday[2012] = [
					[1,2,9,15,23,29],
					[5,13,19,27],
					[4,12,15,20,26],
					[4,9,15,23,29,30],
					[3,4,5,6,14,20,28],
					[3,11,17,25],
					[1,9,15,16,23,29],
					[5,3,19,27],
					[2,10,16,17,22,24,30],
					[7,8,15,21,29],
					[4,12,18,23,26],
					[2,10,16,24,30]
			];
//現在の日付取得
var date = new Date;
//年
var now_year = date.getFullYear();
//月
var now_month = date.getMonth() + 1;
//前後6ケ月分の範囲
var month_space = 0;
//13月、0月の回避関数
function vali_month(month){
	if(month == 13){
		month = 1;
		now_year += 1;
	}else if(month==0){
		month = 12;
		now_year -= 1;
	}
	return month;
}
//カレンダー生成開始
$(document).ready(function(){
	set_cal();
	//月画像のはめ込み
	$(".now_month img").attr("src","http://" + location.hostname + "/images/common/c_" + (now_month) + ".jpg"),
	$(".now_month img").attr("alt", (now_month) + "月");
});
//base生成
function set_cal(){
	//1日の曜日をもとめる
	var date_first = new Date(now_year,now_month - 1,1);
	var first_day = date_first.getDay() ;
	//最終日を取る
	var last_date = new Date(now_year, now_month, 0);
	var last_day = last_date.getDate();
	//カレンダー枠生成
	var base;
	var weekday = 0;
	base = "<p id=\"holy\"><img src=\"http://" + location.hostname + "/images/common/holy.gif\" width=\"20\" height=\"18\" alt=\"青枠\"  />は定休日（予定）</p>";
	base += "<div id=\"cal\" style=\"margin:0 0 20px 0;\">\n";
	base += "<div id=\"cal_bottom\">\n";
	base += "<ul>\n";
	if(month_space > -6){
		base += "<li class=\"prev\"><a class=\"btn_cal\">prev</a></li>\n";
	}
	base += "<li class=\"now_month\"><img src=\"\" width=\"36\" height=\"30\" alt=\"\" /></li>";
	if(month_space < 6){
		base += "<li class=\"next\"><a class=\"btn_cal\">next</a></li>\n";
	}
	base += "</ul>\n";
	
	//テーブルの生成
	base += "<table class=\"table_cal\">\n";
	base += "<tr>\n";
	base += "<th scope=\"col\" class=\"sun\">日</th>\n";
	base += "<th scope=\"col\">月</th>\n";
	base += "<th scope=\"col\">火</th>\n";
	base += "<th scope=\"col\">水</th>\n";
	base += "<th scope=\"col\">木</th>\n";
	base += "<th scope=\"col\">金</th>\n";
	base += "<th scope=\"col\" class=\"sat\">土</th>\n";
	base += "</tr>\n";
	base += "<tr class=\"calday\">\n";
	
	//1日開始まで空白を代入
	while(weekday != first_day){
		base += "<td>&nbsp;</td>\n";
		weekday++;
	}
	//日付の代入
	for(var day = 1; day < last_day + 1; day++){
		//土曜日まで書いたら行を変える
		if(weekday > 6){
			weekday = 0;
			base += "</tr>\n<tr>\n";
		}
		//文字の色を決める
		switch(weekday){
			case 0://日曜
				base += "<td class=\"sun\n";
			break;
			case 6://土曜
				base += "<td class=\"sat\n";
			break;
			default://月～金
				base += "<td class=\"\n";
			break;
		}
		//休日にマーク
		if(holyday[now_year]){
			for(var i = 0;i < holyday[now_year][now_month - 1].length; i++){
				if(day == holyday[now_year][now_month - 1][i]){
					base += " holy\n";
				}
			}
		}
		base += "\">\n";
		//マスの表示
		base += day + "</td>\n";
	  	weekday ++;
	  }
	  //テーブルを最後まで空欄でを埋める
	  while(weekday < 6){
		base +="<td>&nbsp;</td>\n";
		weekday++;
	  }

	base += "</tr>\n\n";
	base += "</table>\n";
	base += "</div><!--/#cal_bottom-->\n";
	base += "</div><!--/#cal-->\n";
//HTML書き出し
$("#calendar").html(base);
set_btn();
}
//ボタン関数
function set_btn(){
//PREVクリック関数
	$(".prev").click(function () {
		if(month_space > -6){
			month_space -= 1;
			now_month -= 1;
			now_month = vali_month(now_month);
			set_cal();
			$(".now_month img").attr("src","http://" + location.hostname + "/images/common/c_" + now_month + ".jpg"),
			$(".now_month img").attr("alt", (now_month) + "月");;
		}
	});
//NEXTクリック関数
	$(".next a").click(function () {
		if(month_space < 6){
			month_space += 1;
			now_month += 1;
			now_month = vali_month(now_month);
			set_cal();
			$(".now_month img").attr("src","http://" + location.hostname + "/images/common/c_" + now_month + ".jpg"),
			$(".now_month img").attr("alt", (now_month) + "月");;
		}
	});
}



/* CSSハック用 */
/* ----------------------------------------------------- */
var css_browser_selector = function() {
	var 
		ua=navigator.userAgent.toLowerCase(),
		is=function(t){ return ua.indexOf(t) != -1; },
		h=document.getElementsByTagName('html')[0],
		b=(!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua))?('ie ie'+RegExp.$1):is('gecko/')? 'gecko':is('opera/9')?'opera opera9':/opera (\d)/.test(ua)?'opera opera'+RegExp.$1:is('konqueror')?'konqueror':is('applewebkit/')?'webkit safari':is('mozilla/')?'gecko':'',
		os=(is('x11')||is('linux'))?' linux':is('mac')?' mac':is('win')?' win':'';
	var c=b+os+' js';
	h.className += h.className?' '+c:c;
}();



