/******************************************************************************************************************/
/*
/* OverView:
/*   Main Constructor
/*   Calendar( controller )
/*   CalendarLayer
/*   CalendarWeek
/*   CalendarDate
/*   CalendarYearMonth
/*
/* NEEDS CHARSET!! utf8
/******************************************************************************************************************/


/******************************************************************************************************************/
/*
/* Main Constructor
/*
/******************************************************************************************************************/

$(function(){
	var c=$('.jq-calendar').calendar()

	if($.fn.alphanumeric){// load alphanumeric
		c.css({imeMode:'disabled'}).numeric({allow:'/'});
	}
});

(function($){
	$.fn.calendar=function(){
		return this.each(function(){
			var c;
			$(this).click(function(){
				c=new Calendar(this);
				$(this).change(function(){
					var d=c.toDate($(this).attr('value'));
					if(d){
						$(this).attr('value',c.dateFormat('yyyy/mm/dd',d));
					}else{
						$(this).attr('value','');
					}
				});
				return false;
			});
			$(this).keypress(function(e){
				if(e.keyCode==9){// TAB
					c.Layer.hide();
				}
			});
		});
	}
})(jQuery);


/******************************************************************************************************************/
/*
/* Part of CalendarLayer
/*
/******************************************************************************************************************/

CalendarLayer.prototype.mainBox=$(document.createElement('DIV'))
	.attr({
		id:'calendar-base'
	})
	.css({
		margin: 0,
		padding: 0,
		letterSpacing: '1px',
		border: '2px solid #336699',
		backgroundColor: '#F9FFFF',
		textAlign: 'left',
		zIndex: 9999,
		display: 'block',
		position: 'absolute',
		width: '214px',// MonthBorder(1*2)
		fontFamily: '"ＭＳ ゴシック", "Osaka－等幅\", "monospace"',
		overflow:'hidden',
		
		left: '-1000px',
		top: '-1000px'
	}).hide();

CalendarLayer.prototype.buttonCss={
		margin:0,
		textAlign:'center',
		border:'1px solid #ccccff',
		height:'20px',
		padding:'2px',
		fontSize:'12px',
		fontWeight:'bold',
		backgroundColor:'#336699',
		color:'#feffff',
		cursor:'pointer'
	};
CalendarLayer.prototype.toolBox=$(document.createElement('TABLE'))
	.attr({
		width:210,
		border:0,
		cellspacing:0,
		cellpadding:0,
		marginLeft:'auto',
		marginRight:'auto'
	})
	.css({
		fontSize: '14px',
		fontFamily: '"ＭＳ ゴシック", "Osaka－等幅\", "monospace"',
		backgroundColor: '#f9ffff',
		cursor: 'default',
		width:'210px',
		marginLeft:'auto',
		marginRight:'auto',
		marginTop:'2px',
		borderCollapse: 'collapse',
		borderSpacing: 0
	}).append(
		$(document.createElement('TR'))
			.append(
				$(document.createElement('TD')).addClass('calendar-btn')
				.attr({id:'calendar-prev-year-btn',title:'前年'}).css('width','20px').text('<<')
			)
			.append(
				$(document.createElement('TD')).addClass('calendar-btn')
				.attr({id:'calendar-prev-month-btn',title:'前月'}).css('width','40px').text('<')
			)
			.append(
				$(document.createElement('TD')).addClass('calendar-btn')
				.attr({id:'calendar-cur-month-btn',title:'今月'}).text('今月')
			)
			.append(
				$(document.createElement('TD')).addClass('calendar-btn')
				.attr({id:'calendar-next-month-btn',title:'翌月'}).css('width','40px').text('>')
			)
			.append(
				$(document.createElement('TD')).addClass('calendar-btn')
				.attr({id:'calendar-next-year-btn',title:'翌年'}).css('width','20px').text('>>')
			)
	);

CalendarLayer.prototype.titleBox=$(document.createElement('DIV'))
	.attr({
		id: 'calendar-title-box'
	})
	.css({
		borderTop: '1px solid #006699',
		borderBottom: '1px solid #006699',
		marginLeft:'auto',
		marginRight:'auto',
		width:'210px'
	}).append(
		$(document.createElement('DIV'))
		.attr({
			id: 'calendar-title-month'
		})
		.css({
			display:'block',
			float: 'left',
			margin:0,
			padding:'0 0 0 10px',
			textAlign: 'left',
			fontSize: '18px',
			fontWeight: 'bold',
			color:'#006699'
		})
	)
	.append(
		$(document.createElement('DIV')).attr({
			id: 'calendar-description'
		}).css({
			display:'block',
			float: 'right',
			margin:0,
			padding:'3px',
			backgroundColor: 'transparent',
			border:'0 none',
			width:'86px',
			color: '#ff0000',
			fontSize: '12px',
			textAlign: 'right'
		})
	)
	.append( $(document.createElement('BR')).css('clear','both') );

CalendarLayer.prototype.monthBox=$(document.createElement('TABLE'))
	.attr({
		id:'calendar-month-box'
	})
	.css({
//		fontSize:'14px',
		marginLeft:0,
		marginRight:'auto',
		textAlign:'center'
	});

CalendarLayer.prototype.footToolBox=$(document.createElement('TABLE'))
	.attr({
		width:210,
		border:0,
		cellspacing:0,
		cellpadding:0
	})
	.css({
		fontSize: '12px',
		fontFamily: '"ＭＳ ゴシック", "Osaka－等幅\", "monospace"',
		backgroundColor: '#f9ffff',
		cursor: 'default',
		width:'210px',
		marginLeft:'auto',
		marginRight:'auto',
		marginTop:'2px',
		borderCollapse: 'collapse',
		borderSpacing: 0
		
	})
	.append(
		$(document.createElement('TR'))
			.append(
				$(document.createElement('TD')).addClass('calendar-btn')
				.attr({id:'calendar-close-btn',title:'閉じる'}).text('閉じる')
/*				.mouseover(function(){
					$('.calendar-date-cell').css('fontSize','10px')
				})*/
			)
		
	);



/******************************************************************************************************************/
/*
/* Main Layer of Calendar
/*
/******************************************************************************************************************/
CalendarLayer.prototype.CalendarMonth=new Object();
CalendarLayer.Holidays=new Object();
CalendarLayer.prototype.getHolidays=function(_year){
	if(!CalendarLayer.Holidays[_year]){
		CalendarLayer.Holidays[_year]=getHolidays(_year);
	}
	return CalendarLayer.Holidays[_year];
};
function CalendarLayer(){
	this.controller=null;
	
	this.Controll=function(_c){
		this.controller=_c;
	}

	// alert('new CalendarLayer()');
	this.showWeeks=new Array();
	this.yearmonth=null;
	
	this.showFirstWeek=null;
	this.showLastWeek=null;
	
	this.monthBox=this.monthBox.clone();
	var layer=this.mainBox.clone()
		.append( this.toolBox.clone() )
		.append( this.titleBox.clone() )
		.append( this.monthBox )
		.append( this.footToolBox.clone() )

		.click(function(){return false;})

		.find('.calendar-btn').css( this.buttonCss )
		.hover(
				function(){
					$(this).css({backgroundColor:'#99aae0'});
				},
				function(){ $(this).css({backgroundColor:'#336699'}) }
		)
		.end()
		.find('#calendar-close-btn').hide().click(function(){layer.hide(); }).end();
	
	this.setMarkUp=function(){
		// マークアップの初期化
		$('.calendar-date-markup').each(function(){
			var d=$(this).text();
			$(this).parent().text(d).end().remove();
		});

		// マークアップの設定
		var wrapCss=this.controller.markUp;
		for(var cssclass in wrapCss){
			$(cssclass).each(function(){
				var d=$(this).text();
				$(this).text('')
				.append(
					$(document.createElement('div'))
					.css(wrapCss[cssclass])
					.addClass('calendar-date-markup')
					.text(d)
				);
			});
		}
	}
	
	var hideFunc=function(){
		layer.hide();
		$('html').unbind('click',hideFunc);
		// IE6のSELECT消し
		if( $.browser.msie && Number($.browser.version) < 7 ){
			$('select').css({visibility:'visible'});
		}
	};
	this.show=function(){
		layer.css({ left: parseInt(this.controller.showPosition.x), top: parseInt(this.controller.showPosition.y) });
		layer.show();
		this.setMarkUp();
		$('html').click(hideFunc);
		// スクローラ
		if(this.useScroll) this.resetScroll();
		// IE6のSELECT消し
		if( $.browser.msie && Number($.browser.version) < 7 ){
			$('select').css({visibility:'hidden'});
		}
	}
	this.hide=function(){
		hideFunc();
	};
	
	this.setTitle=function(_ym){
		this.yearmonth=_ym || this.yearmonth;
		var _y=this.yearmonth.year;
		var _m=this.yearmonth.month;
		layer.find('#calendar-title-month').text( _y+'年'+_m+'月' );

		// 初期化
		layer.find('.calendar-date-cell').css({
			fontWeight: 'normal',
			fontStyle: 'normal'
		});
		// 太字設定
		var mclass='.calendar-date-month-'+_m;
		var yclass='.calendar-date-year-'+_y;
		layer.find(mclass).filter(yclass).css({
			fontWeight: 'bold',
			fontStyle: 'normal' || 'italic'
		});

		var c=this;
		layer.find('#calendar-prev-year-btn').unbind('click').bind('click',function(){
			c.showMonth(new CalendarYearMonth(_y-1,_m))
		});
		layer.find('#calendar-prev-month-btn').unbind('click').bind('click',function(){
			c.showMonth(new CalendarYearMonth(_y,_m-1))
		});
		layer.find('#calendar-cur-month-btn').unbind('click').bind('click',function(){
			c.showMonth(new CalendarYearMonth())
		});
		layer.find('#calendar-next-month-btn').unbind('click').bind('click',function(){
			c.showMonth(new CalendarYearMonth(_y,_m+1))
		});
		layer.find('#calendar-next-year-btn').unbind('click').bind('click',function(){
			c.showMonth(new CalendarYearMonth(_y+1,_m))
		});
	}
	
	this.addMonth=function(_ym){
		if(this.CalendarMonth[_ym.toString()]){
			return this.CalendarMonth[_ym.toString()];
		}
		var m= new Object();
		this.CalendarMonth[_ym.toString()]=m;

		{// m.head
			var _pm=this.CalendarMonth[_ym.prevMonth().toString()];
			if(_pm && _pm.foot && _pm.foot.inmonth(_ym)){// 先月が存在し、合同の週であれば参照取得
//				alert("参照取得");
				m.head=_pm.foot;
			}else if(_pm && _pm.foot){// 先月が存在し、合同の週ではない場合は新規作成して結合
//				alert("結合");
				m.head=new CalendarWeek( this, new Date(_ym.year,_ym.month-1,1) );
				_pm.foot.chainAfter(m.head);
			}else{// 先月が存在しなければ新規作成
//				alert("新規作成");
				m.head=new CalendarWeek( this, new Date(_ym.year,_ym.month-1,1) );
			}
		}
		var week=m.head;
		while( week.inmonth(_ym) ){
			week=week.nextWeek();
		}
		if(!m.foot){
			// あぶれているので、１つ戻す
			m.foot=week.prevWeek();
			week.remove();
		}
		{// m.foot
			var _nm=this.CalendarMonth[_ym.nextMonth().toString()];
			if(_nm && _nm.head && _nm.head.inmonth(_ym)){// 来月が存在し、合同の週であれば連鎖の修正
//				alert('参照置換');
				m.foot.remove();
				m.foot=m.foot.prevWeek();
				m.foot.chainAfter(_nm.head);
				m.foot=_nm.head;
			}else if(_nm && _nm.head){// 来月が存在し、合同の週でなければ結合
				m.foot.chainAfter(_nm.head);
//				alert("結合");
			}else{// 存在しない場合は何もしない
				// 
			}
		}
		
		return m;
	}
	this.showMonth=function(_ym){
		var m=this.addMonth(_ym);
		this.setTitle(_ym);
		$('.calendar-week').hide();
		$('.calendar-week-of-'+_ym.toString()).show();
		this.showFirstWeek=m.head;
		this.showLastWeek=m.foot;
		/*
		var w=m.top;
		while(w!=m.foot){
			w.show();
			w=w.nextWeek();
		}
		*/

		// スクローラ
		if(this.useScroll) this.resetScroll();
	
	}
	
	this.showNextWeek=function(){
		if(!this.showLastWeek.after){// 翌月を作成！
			this.addMonth( this.showLastWeek.days[0].getYearMonth().nextMonth() );
		}

		this.showLastWeek.after.div.show();
		this.showLastWeek=this.showLastWeek.after;
		
		this.showFirstWeek.div.hide();
		this.showFirstWeek=this.showFirstWeek.after;

		// 第１週最後の曜日にてタイトル設定
		var nym=this.showFirstWeek.days[6].getYearMonth();
		if(nym.month!=this.yearmonth.month) this.setTitle( nym );
	}
	
	this.showPrevWeek=function(){
		if(!this.showFirstWeek.before){// 先月を作成！
			this.addMonth( this.showFirstWeek.days[6].getYearMonth().prevMonth() );
		}
		this.showFirstWeek.before.div.show();
		this.showFirstWeek=this.showFirstWeek.before;
		
		this.showLastWeek.div.hide();
		this.showLastWeek=this.showLastWeek.before;

		// 第１週最後の曜日にてタイトル設定
		var nym=this.showFirstWeek.days[6].getYearMonth();
		if(nym.month!=this.yearmonth.month) this.setTitle( nym );
	}
	
	
	/* コンストラクション */
	this.layer=layer;
	
	if($('body').length){
		$('body').append( layer );
	}else{
		var e=this;
		$(function(){
			$('body').append( layer );
			if($.browser.mozilla || $.browser.msie){
				e.insertScrollBox(layer)
			}
		});
	}
}

/***** ScrollExtend *****/
CalendarLayer.prototype.insertScrollBox=function(_layer){
	var _this=this;
	var btnCss={
		textAlign:'right',
		border:'1px solid #ccccff',
		height:'15px',
		padding:0,
		fontSize:'10px',
		fontWeight:'bold',
		backgroundColor:'#336699',
		color:'#feffff',
		cursor:'pointer'
	}
	var sc=$(document.createElement('TABLE'))
	.attr({
		width:15,
		border:0,
		cellspacing:0,
		cellpadding:0
	})
	.css({
		fontSize: '10px',
		fontFamily: '"ＭＳ ゴシック", "Osaka－等幅\", "monospace"',
		backgroundColor: '#f9ffff',
		cursor: 'default',
		width:'15px',
//		marginLeft:'auto',
//		marginRight:0,
//		marginTop:'-15px',
		borderCollapse: 'collapse',
		borderSpacing: 0,
		float:'right'
	})
	.append(
		$(document.createElement('TR'))
			.append(
				$(document.createElement('TD'))
				.css(btnCss)
				.click(function(){
					_this.showPrevWeek();
				})
				.hover(
					function(){ $(this).css({backgroundColor:'#99aae0'}) },
					function(){ $(this).css({backgroundColor:'#336699'}) }
				)
				.text('▲')
			)
	)
	.append(
		$(document.createElement('TR'))
			.append(
				$(document.createElement('TD'))
			)
	)
	.append(
		$(document.createElement('TR'))
			.append(
				$(document.createElement('TD'))
				.css(btnCss)
				.click(function(){
					_this.showNextWeek();
				})
				.hover(
					function(){ $(this).css({backgroundColor:'#99aae0'}) },
					function(){ $(this).css({backgroundColor:'#336699'}) }
				)
				.text('▼')
			)
	);

	// レイヤー表示後に呼ばれる 
	this.resetScroll=function(){
		var height=this.monthBox.height();
		sc.height( height );
		sc.css({marginTop: -1*height});
	}
	this.useScroll=true;
	
	_layer.append(sc);

}


/******************************************************************************************************************/
/*
/* Contoroller of Calendar
/*
/******************************************************************************************************************/

Calendar.prototype.Layer=new CalendarLayer();
function Calendar(_em){
	this.target=_em;
	

	// setValue;
	var _this=this;
	this.setValue=function(_d){
		if(_d){
			$(_em).attr( 'value', _this.dateFormat('yyyy/mm/dd',_d) );
		}
		$(_em).focus();
		this.Layer.hide();
	}
	
	// showPosition
	var of=$(_em).offset();
	this.showPosition=new Object();
	this.showPosition.x=parseInt(of.left) || 0;
	this.showPosition.y=parseInt(of.top)
						+$(_em).height()
						+( parseInt($.curCSS(_em,'padding')) * 2 || 0)
						+( parseInt($.curCSS(_em,'borderBottomWidth')) || 0);
	
	// markUp
	this.markUp=new Object();
	// 当日
	var cur=new Date();
	var ckey='.calendar-date-'+cur.getFullYear()+'-'+(cur.getMonth()+1)+'-'+cur.getDate();
	this.markUp[ckey]={borderBottom:'2px solid #009966',backgroundColor:'#87f1cf'};
	// 選択日
	var selected=this.toDate($(_em).attr('value'));
	if(selected){
		var selkey='.calendar-date-'+selected.getFullYear()+'-'+(selected.getMonth()+1)+'-'+selected.getDate();
		if(selkey==ckey){
			this.markUp[ckey].backgroundColor='#ff9966';
		}else{
			this.markUp[selkey]={backgroundColor:'#ff9966'};
		}
	}

	// レイヤコントローラの設定
	this.Layer.Controll(this);

	// 初回表示
	var _year,_month; // _emから取得する
	if(selected){
		_year=selected.getFullYear();
		_month=selected.getMonth()+1;
	}
	var ym=new CalendarYearMonth(_year,_month);
	this.Layer.showMonth( ym );
	this.Layer.show();
}

Calendar.prototype.toDate=function(_str){
	_str=_str.toString();
	var y; var m; var d;
	if(_str.match(/([1-2][0-9][0-9][0-9])[-\/\.]?([0-1][0-9])[-\/\.]?([0-3][0-9])/) ){
		// 区切りなし? 8桁
		y=RegExp.$1; m=RegExp.$2; d=RegExp.$3;
	}else if(_str.match(/([0-9][0-9])[-\/\.]?([0-1][0-9])[-\/\.]?([0-3][0-9])/)){
		// 区切りなし? 6桁
		y='20'+RegExp.$1; m=RegExp.$2; d=RegExp.$3;
	}else if(_str.match(/([0-1][0-9])[-\/\.]?([0-3][0-9])/)){
		// 区切りなし? 4桁
		y=(new Date()).getFullYear(); m=RegExp.$1; d=RegExp.$2;
	}else if(_str.match(/(\d+)[-\/\.](\d+)[-\/\.](\d+)/)){
		// 区切りあり 年月日
		y=RegExp.$1.length < 4 ? 2000+parseInt(RegExp.$1) : RegExp.$1; m=RegExp.$2; d=RegExp.$3;
	}else if(_str.match(/(\d+)[-\/\.](\d+)/)){
		// 区切りあり 月日
		y=(new Date()).getFullYear(); m=RegExp.$1; d=RegExp.$2;
	}else if(_str.match(/^(\d+)/) ){
		// 区切りなし 日
		var _cd=new Date();
		y=_cd.getFullYear(); m=_cd.getMonth()+1; d=RegExp.$1;
	}else if(_str.match(/^\+(\d+)/)){
		// 何日後
		var _cd=new Date();
		y=_cd.getFullYear(); m=_cd.getMonth()+1; d=parseInt(_cd.getDate())+ (parseInt(RegExp.$1) ? parseInt(RegExp.$1) : 0);
	}
	if( y && m && d){
		return new Date(y+'/'+m+'/'+d);
	}else{
		return null;
	}
}
Calendar.prototype.dateFormat=function(_fmt,_date){
	if(_date){
		var _y=_date.getFullYear();
		var _m=_date.getMonth()+1;
		var _d=_date.getDate();
		if(_m.toString().match(/^[0-9]$/)){_m='0'+_m;}
		if(_d.toString().match(/^[0-9]$/)){_d='0'+_d;}
		
		return _fmt.replace(/yyyy/,_y)
			.replace(/yy/,_y.toString().substr(2,2))
			.replace(/mm/,_m)
			.replace(/dd/,_d);
	}else{
		return '';
	}
}


/******************************************************************************************************************/
/*
/* Week of Calendar
/*
/******************************************************************************************************************/
function CalendarWeek(_super,_date){
	/* コンストラクタ */
	this.days=new Array(7);
	
	/* DIVの作成 */
	this.div=$(document.createElement('TR'))
		.css({
			marginLeft:'auto',
			marginRight:'auto'
		})
		.addClass('calendar-week');
	
	// 各日付の作成
	_date=new Date( _date.getFullYear(), _date.getMonth(), _date.getDate()-_date.getDay() );

	var sun=_date.getDate();
	var dy=_date.getFullYear();
	var dm=_date.getMonth();
	var inMonth=new Object();
	for(var i=0; i<7; i++){
		var d=new CalendarDate( new Date( dy, dm, sun+i ),_super );
		this.div.append( d.div );

		var m=new CalendarYearMonth(d.getFullYear(),d.getMonth()+1)
		inMonth[m.toString()]=inMonth[m.toString()] ? inMonth[m.toString()]+1 : 1;
		this.days[i]=d;
	}
	
	 // 結合する
	 _super.monthBox.append( this.div.hide() );
	 
	/* メジャー月 */
	var priYM; var max=0;
	for(var ym in inMonth){
		// クラス指定
		this.div.addClass('calendar-week-of-'+ym);
		if(inMonth[ym] > max){
			max=inMonth[ym]; priYM=ym.split('-');
		}
	}

	this.inmonth=function(_ym){
		return !!(inMonth[_ym.toString()]);
	}
	this.month=function(){
		return new CalendarYearMonth(priYM[0],priYM[1]);
	};
	
	this.nextWeek=function(){
		if(!this.after){
			this.after=new CalendarWeek(_super, new Date(this.days[6].getFullYear(), this.days[6].getMonth(), this.days[6].getDate()+1) );
			this.after.before=this;
		}
		
		return this.after
	}
	
	this.prevWeek=function(){
		if(!this.before){
			this.before=new CalendarWeek(_super, new Date(this.days[0].getFullYear(), this.days[0].getMonth(), this.days[0].getDate()-1) );
			this.before.after=this;
		}

		return this.before
	}
	
	this.chainAfter=function(_w){
		if(this.after) this.after.before=null;
		this.after=_w;
		_w.before=this;
		
		var pos=this;
		while(pos.after){
			pos.div.after( pos.after.div );
			pos=pos.after;
		}
		
		this.div.after(_w.div);
	}
	this.chainBefore=function(_w){
		if(this.before) this.before.after=null;
		this.before=_w;
		this.div.before(_w.div)
		_w.after=this;
	}
	this.remove=function(){
		this.div.remove();
		if(this.before && this.after){
			this.before.after=this.after;
			this.after.before=this.before;
		}else if(this.before){
			this.before.after=null;
		}else if(this.after){
			this.after.before=null;
		}
	}
}
/******************************************************************************************************************/
/*
/* Date of Calendar
/*
/******************************************************************************************************************/
function CalendarDate(_date,_super){
	this.date=_date;

	var _y=_date.getFullYear();
	var _m=_date.getMonth()+1;
	var _d=_date.getDate();
	var _w=_date.getDay();

	/***** div の作成 *****/
	var _name=( _super.getHolidays(_y) )[_m+'-'+_d] || '';
	
	var _color=(_w==6) ? '#0000ff' :
				((_w==0 || _name) ? '#ff0000' : '#000000');
	
	var defaultCss={
		backgroundColor:'transparent',
		color:_color
	};
	var pointCss={
		color:'#ffffff',
		backgroundColor:'#ff6600'
	};
	

	this.div=$(document.createElement('TD'))
		.addClass('calendar-date-cell')
		.addClass('calendar-date-year-'+_y)
		.addClass('calendar-date-month-'+_m)
		.addClass('calendar-date-'+_y+'-'+_m+'-'+_d)
//		.attr('id','calendar-date-'+_y+'-'+_m+'-'+_d)
		.text( _d )
		.css({
			width:'24px',
			height:'24px',
			fontSize:'14px',
//			lineHeight:'14px',
			textAlign:'center',
			cursor:'pointer',
			border:'3px solid #f9ffff'
		})
		.css(defaultCss);
		
		
	this.div.mouseover(function(){
			$('#calendar-description').text(_name);
			//changeTitle();
			// 選択色
			$(this).css(pointCss);
		})
		.mouseout(function(){
			$(this).css(defaultCss);
			$('#calendar-description').text('');
		})
		.click(function(){_super.controller.setValue(_date)});
}
CalendarDate.prototype={
	date: new Date(),
	getYearMonth: function(){ return new CalendarYearMonth(this.date.getFullYear(),this.date.getMonth()+1) },
	getFullYear: function(){return this.date.getFullYear()},
	getMonth: function(){return this.date.getMonth()},
	getDate: function(){return this.date.getDate()},
	getDay: function(){return this.date.getDay()},
	
	defaultCss: {color: '#000000',backgroundColor:'transparent'}

};

function CalendarYearMonth(_year,_month){
	_year= (_year==undefined ? (new Date()).getFullYear() : Number(_year));
	_month= (_month==undefined ? (new Date()).getMonth()+1 : Number(_month));
	_year=parseInt( (_year*12+_month-1) / 12 );
	_month=(_year*12+_month) % 12 || 12 ;
	
	this.year=_year;
	this.month=_month;
}
CalendarYearMonth.prototype.toString=function(){
	return this.year.toString()+'-'+this.month.toString();
}
CalendarYearMonth.prototype.nextMonth=function(){
	return new CalendarYearMonth(this.year,this.month+1);
}
CalendarYearMonth.prototype.prevMonth=function(){
	return new CalendarYearMonth(this.year,this.month-1);
}


/***** 祝日の設定 *****/
function getHolidays(_year){
	_year=parseInt(_year);

	var keypack=function(_m,_d){
		return ''+_m+'-'+_d;
	};
	
	var keyByWeek=function(_m,_n,_w){
		var fd=(new Date(_year,_m-1,1)).getDay();// １日の曜日
		var fsun=(1-fd);// 第一月曜もどき
		var fw=(fsun+_w) + (fsun+_w < 1 ? 7 : 0);// 第一 W曜日
		var d=fw+(_n-1)*7;// 第N W曜日
		return keypack(_m,d);
	};
	

	var holidays=new Object();
	/* 元日 */
	if(_year > 1948){
		holidays[keypack(1,1)]='元日';
	}
	/* 成人の日 */
	if(_year >= 2000){
		// １月(1)第２(2)月曜(1)
		holidays[keyByWeek(1,2,1)]='成人の日';
	}else if(_year > 1948){
		holidays[keypack(1,15)]='成人の日';
	}
	/* 建国記念日 */
	if(_year > 1948){
		holidays[keypack(2,11)]='建国記念日';
	}
	/* 春分の日 3月 (1900年～2099年のみ対応)*/
	
	if(1948 < _year && _year <= 2099){
		var syunbun;
		switch( _year % 4 ){
			case 0:
				if(_year<=1956) syunbun=21;
				else if(_year<=2088) syunbun=20;
				else syunbun=19;
				break;
			case 1:
				if(_year<=1989) syunbun=21;
				else syunbun=20;
				break;
			case 2:
				if(_year<=2022) syunbun=21;
				else syunbun=20;
				break;
			case 3:
				if(_year<=1923) syunbun=22;
				else if(_year<=2055) syunbun=21;
				else syunbun=20;
				break;
		}
		holidays[keypack(3,syunbun)]='春分日';
	}
	/* 天皇誕生日 */
	if(_year >= 2007){
		holidays[keypack(4,29)]='昭和の日';
	}else if(_year >=1989 ){
		holidays[keypack(4,29)]='みどりの日';
	}else if(_year > 1948){
		holidays[keypack(4,29)]='天皇誕生日';
	}
	/* 憲法記念日 */
	var hol0506=false;
	if(_year > 1948){
		holidays[keypack(5,3)]='憲法記念日';
	}
	/* みどりの日 ５月４日*/ 
	if(_year >= 2007){
		holidays[keypack(5,4)]='みどりの日';
	}else if(_year > 1985){
		var tmpDate=new Date(_year,4,3);
		if(tmpDate.getDay() == 0){
			// ただの日曜日
		}else if(tmpDate.getDay() == 1){
			holidays[keypack(5,4)]='振り替え休日';
		}else{
			holidays[keypack(5,4)]='国民の休日';
		}
	}
	/* こどもの日 */
	if(_year > 1948){
		holidays[keypack(5,5)]='こどもの日';
	}
	/* 海の日 */
	if(_year >= 2003){
		// ７月(1)第３(3)月曜(1)
		holidays[keyByWeek(7,3,1)]='海の日';
	}else if(_year >= 1996){
		holidays[keypack(7,20)]='海の日';
	}
	/* 敬老の日*/
	var keirou;
	if(_year >= 2003){
		// ９月(1)第３(3)月曜(1)
		holidays[keyByWeek(9,3,1)]='敬老の日';
	}else if(_year >= 1966){
		holidays[keypack(9,15)]='敬老の日';
	}
	/* 秋分の日 9月 (1900年～2099年のみ対応) */
	if(1948 <= _year && _year <= 2099){
		var syubun;
		switch( _year % 4 ){
			case 0:
				if(_year<=2008) syubun=23;
				else syubun=22;
				break;
			case 1:
				if(_year<=1917) syubun=24;
				else if(_year<=2041) syubun=23;
				else syubun=22;
				break;
			case 2:
				if(_year<=1946) syubun=24;
				else if(_year<=2074) syubun=23;
				else syubun=22;
				break;
			case 3:
				if(_year<=1979) syubun=24;
				else syubun=23;
				break;
		}
		holidays[keypack(9,syubun)]='秋分日';
	}
	/* 体育の日 */
	if(_year >=2000 ){
		// 10月(10)第２(2)月曜(1)
		holidays[keyByWeek(10,2,1)]='体育の日';
	}else if(_year >=1966 ){
		holidays[keypack(10,10)]='体育の日';
	}
	/* 文化の日 */
	if(_year >= 1948){
		holidays[keypack(11,3)]='文化の日';
	}
	/* 勤労感謝の日 */
	if(_year >= 1948){
		holidays[keypack(11,23)]='勤労感謝の日';
	}
	/* 天皇誕生日 */
	if(_year >= 1989){
		holidays[keypack(12,23)]='天皇誕生日';
	}
	

	/* 国民の休日 2007年～ */
	if(_year >= 2007){
		var kokumin=new Array();
		for(var i in holidays){
			var md=i.split('-');
			if(holidays[ keypack(md[0],Number(md[1])+2) ]){
				kokumin.push(keypack(md[0],Number(md[1])+1));
			}
		}
		for(var i=0; i<kokumin.length; i++){
			holidays[kokumin[i]]='国民の休日';
		}
	}
	/* 振り替え休日 1973年～ */
	if(_year >= 1973){
		var hurikaeDate=function(_m,_d){
			if( holidays[keypack(_m,1+Number(_d))] ){
				hurikaeDate(_m,1+Number(_d));
			}else{
				hurikae.push(keypack(_m,1+Number(_d)));
			}
		}
		var hurikae=new Array();
		for(var i in holidays){
			var md=i.split('-');
			if( (new Date(_year,md[0]-1,md[1])).getDay() == 0 ){
				hurikaeDate(md[0],md[1]);
			}
		}
		for(var i=0; i<hurikae.length; i++){
			holidays[hurikae[i]]='振り替え休日';
		}
	}

	return holidays;

}
function testHolidays(_year){
	var hdiv=$( document.createElement('DIV') ).append('<h2>'+_year+'年<\/h2');
	var testHolidays=getHolidays(_year);
	for(var i in testHolidays){
		hdiv.append(
			$(document.createElement('div')).css({width:'100px',float:'left'}).text(i)
		).append(
			$(document.createElement('div')).css({width:'100px',float:'left'}).text(testHolidays[i])
		).append(
			$(document.createElement('br')).css({clear:'both'})
		);
	}
	$('body').append(hdiv);
}


/**** 問題が多くて利用できない・・・


CalendarLayer.prototype.insertScrollBox=function(_layer){
	var _this=this;
	// スクロールの作成 
	var frm=$(document.createElement('TEXTAREA'))
		.attr('value',//10行固定
			"\n\n\n\n\n\n\n\n\n\n\n"
		)
		.attr({
			id:'calendar-scroll-box',
			name:'calendar-scroll-box',
			readonly: true
		})
		.css({
			display:'block',
			position: 'absolute',
			top:'52px',// 適当
			left:'200px',
			width:'27px',
			height:'100px',
			lineHeight: '15px',
			fontSize: '1px',
			backgroundColor:'#336699',
			color:'#ffffff',
			border:'1px solid #ccccff',
			padding:0,
			cursor:'n-resize'
		})
		.focus(function(){return false;})
		.mouseover(function(){$(this).css({backgroundColor:'#99aae0'}); frm.focus()})
		.mouseout(function(){$(this).css({backgroundColor:'#336699'})})
		.select(function(){return false;})
		.mousedown(function(){return false;})
		// タイマーモード
		.scroll(function(){
			if(frm.queue().length > 0) return;
			var b=frm.scrollTop();
			if(b < _this.initTop){
				// $('body').append("UP!! ("+b+")<br />")
				_this.showPrevWeek();
			}else if(b > _this.initTop){
				// $('body').append("DOWN!!("+b+")<br />")
				_this.showNextWeek();
			}
			frm.animate({scrollTop:_this.initTop},10,function(){}) // 50: 受付単位ms
		});

		// 連続モード
		.scroll(function(){
			var b=frm.scrollTop();
			if(b==_this.initTop){
				_this.scrolling=false;
			}else if(b < _this.initTop){
				$('body').append("UP!! ("+b+")<br />")
				_this.showPrevWeek();
			}else if(b > _this.initTop){
				$('body').append("DOWN!!("+b+")<br />")
				_this.showNextWeek();
			}
			frm.scrollTop(_this.initTop);
			_this.resetScroll();
		});

	// レイヤー表示後に呼ばれる 
	this.resetScroll=function(){
		var height=this.monthBox.height()-4;
		frm.height( height );

		var scHeight=10*parseInt( frm.css('lineHeight') );
		this.initTop=parseInt( (scHeight-height) / 2 );

		frm.scrollTop(this.initTop);
	}
	_layer.append( frm );
	this.useScroll=true;
	
}

*/
