var week_table       = new Array( "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
var month_table      = new Array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var month_name_table = new Array( "January", "February", "March", "April", "May", "June",
							"July", "August", "September", "October", "November", "December");
var national_holiday = {
	data : [
		{ month : 1,  day : 1,  name : "元日"        },
		{ month : 1,  day : 12, name : "成人の日"    },
		{ month : 2,  day : 11, name : "建国記念の日"},
		{ month : 3,  day : 20, name : "春分の日"    },
		{ month : 4,  day : 29, name : "昭和の日"    },
		{ month : 5,  day : 3,  name : "憲法記念日"  },
		{ month : 5,  day : 4,  name : "みどりの日"  },
		{ month : 5,  day : 5,  name : "こどもの日"  },
		{ month : 5,  day : 6,  name : "振替休日"    },
		{ month : 7,  day : 20, name : "海の日"      },
		{ month : 9,  day : 21, name : "敬老の日"    },
		{ month : 9,  day : 22, name : "国民の休日"  },
		{ month : 9,  day : 23, name : "秋分の日"    },
		{ month : 10, day : 23, name : "体育の日"    },
		{ month : 11, day : 3,  name : "文化の日"    },
		{ month : 11, day : 23, name : "勤労感謝の日"},
		{ month : 12, day : 23, name : "天皇誕生日"  }
	]
}

Calendar.prototype = {
	initialize : function(){
		this.month_table = month_table;
		this.today();
	},
	
	today : function(){
		this.date   = new Date();
		
		this.today = this.date.getDate();
		this.year  = this.date.getYear();
		this.month = this.date.getMonth();
		this.week_today = this.date.getDay();
		this.date.setDate(1);
		this.week_firstday = this.date.getDay();
		
		this.leap_year();
	},
	
	next : function(){
		if(this.month == 11){
			this.year += 1;
			this.month = 0;
		}else{
			this.month++;
		}
		
		this.leap_year();
		
		this.today = -1;
		this.date.setDate(1);
		this.date.setYear(this.year);
		this.date.setMonth(this.month);
		this.week_firstday = this.date.getDay();
	},
	
	leap_year : function(){
		this.year = ( this.year < 2000) ? ( this.year + 1900) : this.year;
		
		if((( this.year % 4 == 0) && ( this.year % 100 != 0)) || ( this.year % 400 == 0)){
			this.month_table[1] = 29;
		}
	},
	
	get_firstday : function(){
		return this.week_firstday;
	},
	
	get_shop_holiday : function(){
		this.shop_holiday = this.week_firstday ? (29 - this.week_firstday) : 22;
		
		return this.shop_holiday
	},
	
	get_days : function(){
		return this.month_table[this.month];
	}
}

CalendarTable.prototype = {
	initialize : function(calendar){
		this.calendar = calendar;
		
		this.set_up();
	},
	
	set_up : function(){
		this.month = this.calendar.month;
		this.days = this.calendar.get_days();
		this.week = this.calendar.get_firstday();
		this.shop_holiday = this.calendar.get_shop_holiday();
		
		this.table_line = Math.ceil(( this.week + this.days) / 7);
		this.cells = this.table_line * 7;
		
		this.set_table();
		this.set_holiday_table();
		this.make_table();
	},
	
	set_table : function(){
		this.date_table = new Array(this.cells);
		
		for( var i = 0; i < this.cells; i++){
			this.date_table[i] = "　";
		}
		for(var i = 0; i < this.days; i++){
			this.date_table[i + this.week] = i + 1;
		}
	},
	
	set_holiday_table : function(){
		this.holiday_table = new Array(this.cells);
		for( var i = 0; i < this.cells; i++){
			this.holiday_table[i] = "　";
		}
		
		for(var i = 0; i < national_holiday.data.length; i++){
			if(national_holiday.data[i].month == this.month + 1){
				this.holiday_table[national_holiday.data[i].day + this.week - 1] = national_holiday.data[i].name;
			}
		}
		
		this.holiday_table[this.shop_holiday + this.week - 1] = "定休日";
	},
	
	make_table : function(){
		this.table = new Array(this.cells);
		for(var i = 0; i < this.cells; i++){
			this.table[i] = this.date_table[i] + " " + this.holiday_table[i];
		}	
	},
	
	get_table : function(){
		return this.table;
	},
	
	get_line : function(){
		return this.table_line;
	}
}

CalendarView.prototype = {
	initialize : function(elm_calendar, calendar_table){
		this.element = elm_calendar;
		this.calendar_table = calendar_table;
		
		this.create_html();
		this.print_html();
	},
	
	create_html : function(){
		this.inner_html = "<table class='calendar'>";
		this.create_head_line();
		this.create_table();
	},
	
	create_head_line : function(){
		this.inner_html += "<tr>";
		
		for( var i = 0; i < 7; i++){
			this.inner_html += "<th";
			
			if( i == 0){
				this.inner_html += " class='js_header js_sunday'>";
			}else if( i == 6){
				this.inner_html += " class='js_header js_saturday'>";
			}else{
				this.inner_html += " class='js_header js_weekday'>";
			}
			
			this.inner_html += week_table[i];
			this.inner_html += "</th>";
		}
		
		this.inner_html += "</tr>";
	},
	
	create_table : function(){
		var today = this.calendar_table.calendar.today;
		var week  = this.calendar_table.calendar.week_firstday
		var table = this.calendar_table.get_table();
		var line  = this.calendar_table.get_line();
		
		for( var i = 0; i < line; i++){
			this.inner_html += "<tr class='tr_week'>";
			
			for( var j = 0; j < 7; j++){
				var day = j + i * 7 - week + 1;
				var con = table[j + (i * 7)];
				
				this.inner_html += "<td " ;
				if(con != "　 　"){
					this.inner_html += "id='menu" + day + "' " ;
					if(day == today){
						this.inner_html += "class='js_cell js_today'>";
					}else if( j == 0){
						this.inner_html += "class='js_cell js_sunday'>";
					}else if( j == 6){
						this.inner_html += "class='js_cell js_saturday'>";
					}else{
						this.inner_html += "class='js_cell js_weekday'>";
					}
				}else{
					this.inner_html += "class='js_cell2'>";
				}
				this.inner_html += con;
				this.inner_html += "</td>";
			}
			this.inner_html += "</tr>";
		}
	},
	
	print_html : function(){
		this.element.innerHTML = this.inner_html;
	}
	
}

function Calendar(){
	this.initialize();
}

function CalendarTable(calendar){
	this.initialize(calendar);
}

function CalendarView(elm_calender, calendar_table){
	this.initialize(elm_calender, calendar_table);
}

