// calendar for index.html, top-00x.html

$(function() {

	// 現在の日付を取得
	var today = new Date();
	beautyToday = {
		day: today.getDate(),
		month: today.getMonth() + 1,
		year: today.getFullYear(),
		nextYear: today.getFullYear() + 1
	}
	var dow = new Array('日','月','火','水','木','金','土');

	$('#searchCal')
		.datePicker({ // associate the link with a date picker
			createButton:false,
			endDate:'31/12/' + beautyToday.nextYear,
			showYearNavigation:false,
			displayClose:true
		}).bind( // when the link is clicked display the date picker
			'click', function() {
			// updateSelects($(this).dpGetSelected()[0]);
			$(this).dpDisplay();
			return false;
		}).bind( // when a date is selected update the SELECTs
			'dateSelected',
			function(e, selectedDate, $td, state) {
			updateSelects(selectedDate);
		}).bind(
			'dpClosed',
			function(e, selected) {
			// updateSelects(selected[0]);
		}
	);

  $('input').live('click', function(){
    // 行きたい日からさがす
    if (this.name == "calendarsearch") {
      $('#rsvdate').val($('#gMonth').val()+$('#gDay').val());
    }
  });

	// 月を選択したら日を有効にする
	$('#gMonth').change(function(){
		if ( $('#gMonth').val() == '' ){
		} else {
			var selectDate = {
				month: $('#gMonth').val()
			}
			// 曜日つきのセレクトメニューを作成…（暫定対応）
			var sdNrDay = '<option value="1">1日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/01').getDay()] + '）</option>';
			sdNrDay += '<option value="2">2日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/02').getDay()] + '）</option>';
			sdNrDay += '<option value="3">3日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/03').getDay()] + '）</option>';
			sdNrDay += '<option value="4">4日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/04').getDay()] + '）</option>';
			sdNrDay += '<option value="5">5日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/05').getDay()] + '）</option>';
			sdNrDay += '<option value="6">6日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/06').getDay()] + '）</option>';
			sdNrDay += '<option value="7">7日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/07').getDay()] + '）</option>';
			sdNrDay += '<option value="8">8日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/08').getDay()] + '）</option>';
			sdNrDay += '<option value="9">9日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/09').getDay()] + '）</option>';
			sdNrDay += '<option value="10">10日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/10').getDay()] + '）</option>';
			sdNrDay += '<option value="11">11日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/11').getDay()] + '）</option>';
			sdNrDay += '<option value="12">12日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/12').getDay()] + '）</option>';
			sdNrDay += '<option value="13">13日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/13').getDay()] + '）</option>';
			sdNrDay += '<option value="14">14日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/14').getDay()] + '）</option>';
			sdNrDay += '<option value="15">15日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/15').getDay()] + '）</option>';
			sdNrDay += '<option value="16">16日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/16').getDay()] + '）</option>';
			sdNrDay += '<option value="17">17日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/17').getDay()] + '）</option>';
			sdNrDay += '<option value="18">18日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/18').getDay()] + '）</option>';
			sdNrDay += '<option value="19">19日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/19').getDay()] + '）</option>';
			sdNrDay += '<option value="20">20日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/20').getDay()] + '）</option>';
			sdNrDay += '<option value="21">21日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/21').getDay()] + '）</option>';
			sdNrDay += '<option value="22">22日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/22').getDay()] + '）</option>';
			sdNrDay += '<option value="23">23日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/23').getDay()] + '）</option>';
			sdNrDay += '<option value="24">24日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/24').getDay()] + '）</option>';
			sdNrDay += '<option value="25">25日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/25').getDay()] + '）</option>';
			sdNrDay += '<option value="26">26日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/26').getDay()] + '）</option>';
			sdNrDay += '<option value="27">27日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/27').getDay()] + '）</option>';
			sdNrDay += '<option value="28">28日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/28').getDay()] + '）</option>';
			sdNrDay += '<option value="29">29日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/29').getDay()] + '）</option>';
			if ( selectDate.month.substring(4,6) !== '02' ) {
				sdNrDay += '<option value="30">30日（' + dow[new Date(selectDate.month.substring(0,4) + '/' + selectDate.month.substring(4,6) + '/30').getDay()] + '）</option>';
				if ( selectDate.month.substring(4,6) == '01' || selectDate.month.substring(4,6) == '03' || selectDate.month.substring(4,6) == '05' ||　selectDate.month.substring(4,6) == '07' ||　selectDate.month.substring(4,6) == '08' ||　selectDate.month.substring(4,6) == '10' ||　selectDate.month.substring(4,6) == '12' ) {
					sdNrDay += '<option value="31">31日（' + dow[new Date(selectDate.month + '/31').getDay()] + '）</option>';
				}
			}
			$('#gDay').html(sdNrDay);
			$('#searchCal')
				.datePicker({ // associate the link with a date picker
					createButton:false,
					year:selectDate.month.substring(0,4),
					month:selectDate.month.substring(4,6)-1,
					endDate:'31/12/' + beautyToday.nextYear,
					showYearNavigation:false,
					displayClose:true
				}).bind( // when the link is clicked display the date picker
					'click', function() {
					// updateSelects($(this).dpGetSelected()[0]);
					$(this).dpDisplay();
					return false;
				}).bind( // when a date is selected update the SELECTs
					'dateSelected',
					function(e, selectedDate, $td, state) {
					updateSelects(selectedDate);
				}).bind(
					'dpClosed',
					function(e, selected) {
					// updateSelects(selected[0]);
				}
			);
		}
	});



	var updateSelects = function (selectedDate)
	{
		var selectedDate = new Date(selectedDate);
		var sdNrDay = '<option value="1">1日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/01').getDay()] + '）</option>';
		sdNrDay += '<option value="2">2日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/02').getDay()] + '）</option>';
		sdNrDay += '<option value="3">3日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/03').getDay()] + '）</option>';
		sdNrDay += '<option value="4">4日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/04').getDay()] + '）</option>';
		sdNrDay += '<option value="5">5日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/05').getDay()] + '）</option>';
		sdNrDay += '<option value="6">6日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/06').getDay()] + '）</option>';
		sdNrDay += '<option value="7">7日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/07').getDay()] + '）</option>';
		sdNrDay += '<option value="8">8日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/08').getDay()] + '）</option>';
		sdNrDay += '<option value="9">9日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/09').getDay()] + '）</option>';
		sdNrDay += '<option value="10">10日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/10').getDay()] + '）</option>';
		sdNrDay += '<option value="11">11日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/11').getDay()] + '）</option>';
		sdNrDay += '<option value="12">12日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/12').getDay()] + '）</option>';
		sdNrDay += '<option value="13">13日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/13').getDay()] + '）</option>';
		sdNrDay += '<option value="14">14日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/14').getDay()] + '）</option>';
		sdNrDay += '<option value="15">15日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/15').getDay()] + '）</option>';
		sdNrDay += '<option value="16">16日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/16').getDay()] + '）</option>';
		sdNrDay += '<option value="17">17日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/17').getDay()] + '）</option>';
		sdNrDay += '<option value="18">18日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/18').getDay()] + '）</option>';
		sdNrDay += '<option value="19">19日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/19').getDay()] + '）</option>';
		sdNrDay += '<option value="20">20日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/20').getDay()] + '）</option>';
		sdNrDay += '<option value="21">21日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/21').getDay()] + '）</option>';
		sdNrDay += '<option value="22">22日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/22').getDay()] + '）</option>';
		sdNrDay += '<option value="23">23日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/23').getDay()] + '）</option>';
		sdNrDay += '<option value="24">24日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/24').getDay()] + '）</option>';
		sdNrDay += '<option value="25">25日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/25').getDay()] + '）</option>';
		sdNrDay += '<option value="26">26日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/26').getDay()] + '）</option>';
		sdNrDay += '<option value="27">27日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/27').getDay()] + '）</option>';
		sdNrDay += '<option value="28">28日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/28').getDay()] + '）</option>';
		sdNrDay += '<option value="29">29日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/29').getDay()] + '）</option>';
		if ( (selectedDate.getMonth()+1) !== '2' ) {
			sdNrDay += '<option value="30">30日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/30').getDay()] + '）</option>';
			if ( (selectedDate.getMonth()+1) == '1' || (selectedDate.getMonth()+1) == '3' || (selectedDate.getMonth()+1) == '5' ||　(selectedDate.getMonth()+1) == '7' ||　(selectedDate.getMonth()+1) == '8' ||　(selectedDate.getMonth()+1) == '10' ||　(selectedDate.getMonth()+1) == '12' ) {
				sdNrDay += '<option value="31">31日（' + dow[new Date((selectedDate.getFullYear()) + '/' + (selectedDate.getMonth()+1) + '/31').getDay()] + '）</option>';
			}
		}
		$('#gDay').html(sdNrDay);
		$('#gDay option[value=' + selectedDate.getDate() + ']').attr('selected', 'selected');
		$('#gMonth option[value=' + (selectedDate.getFullYear() + ("0"+(selectedDate.getMonth()+1)).slice(-2)) + ']').attr('selected', 'selected');
	}


	// default the position of the selects to today
	updateSelects(today.getTime());

	// and update the datePicker to reflect it...
	$('#gDay').trigger('change');

});
