// for src-001.html

$(function() {
	$('#sdKeyword').initInput('店名、駅名、業種、カテゴリ名など');
	$('input.imageButton').rollover('_o','_a');

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

	// 日付選択の年以外を disabled に
	$('#sdNrMonth, #sdNrDay, #sdNrTime').attr('disabled', 'disabled');
	// Firefox 対策
	$('#sdNrMonth').val('');
	$('#sdNrDay').val('');

	// 年選択を今年と来年のみに
	var sdNrYear = '<option value="">指定無し</option>';
	sdNrYear += '<option value="' + beautyToday.year + '">' + beautyToday.year + '年</option>';
	sdNrYear += '<option value="' + beautyToday.nextYear + '">' + beautyToday.nextYear + '年</option>';

	// 年を選択したら月を有効にする
	$('#sdNrYear').html(sdNrYear).change(function(){
		if ( $('#sdNrYear').val() == '' ){
			$('#sdNrMonth, #sdNrDay, #sdNrTime').attr('disabled', 'disabled');
			$(this).focus();
		} else {
			$('#sdNrMonth').removeAttr('disabled').focus();
		}
	});


		$('#searchDetailCal')
			.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]);
			}
		);

	// 月を選択したら日を有効にする
	$('#sdNrMonth').change(function(){
		if ( $('#sdNrMonth').val() == '' ){
			$('#sdNrDay, #sdNrTime').attr('disabled', 'disabled');
			$(this).focus();
		} else {
			$('#sdNrDay').removeAttr('disabled').focus();
			var selectDate = {
				year: $('#sdNrYear').val(),
				month: $('#sdNrMonth').val()
			}

			// 曜日つきのセレクトメニューを作成…（暫定対応）
			var sdNrDay = '<option value="">指定無し</option>';
			sdNrDay += '<option value="1">1日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/01').getDay()] + '）</option>';
			sdNrDay += '<option value="2">2日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/02').getDay()] + '）</option>';
			sdNrDay += '<option value="3">3日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/03').getDay()] + '）</option>';
			sdNrDay += '<option value="4">4日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/04').getDay()] + '）</option>';
			sdNrDay += '<option value="5">5日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/05').getDay()] + '）</option>';
			sdNrDay += '<option value="6">6日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/06').getDay()] + '）</option>';
			sdNrDay += '<option value="7">7日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/07').getDay()] + '）</option>';
			sdNrDay += '<option value="8">8日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/08').getDay()] + '）</option>';
			sdNrDay += '<option value="9">9日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/09').getDay()] + '）</option>';
			sdNrDay += '<option value="10">10日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/10').getDay()] + '）</option>';
			sdNrDay += '<option value="11">11日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/11').getDay()] + '）</option>';
			sdNrDay += '<option value="12">12日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/12').getDay()] + '）</option>';
			sdNrDay += '<option value="13">13日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/13').getDay()] + '）</option>';
			sdNrDay += '<option value="14">14日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/14').getDay()] + '）</option>';
			sdNrDay += '<option value="15">15日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/15').getDay()] + '）</option>';
			sdNrDay += '<option value="16">16日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/16').getDay()] + '）</option>';
			sdNrDay += '<option value="17">17日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/17').getDay()] + '）</option>';
			sdNrDay += '<option value="18">18日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/18').getDay()] + '）</option>';
			sdNrDay += '<option value="19">19日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/19').getDay()] + '）</option>';
			sdNrDay += '<option value="20">20日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/20').getDay()] + '）</option>';
			sdNrDay += '<option value="21">21日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/21').getDay()] + '）</option>';
			sdNrDay += '<option value="22">22日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/22').getDay()] + '）</option>';
			sdNrDay += '<option value="23">23日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/23').getDay()] + '）</option>';
			sdNrDay += '<option value="24">24日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/24').getDay()] + '）</option>';
			sdNrDay += '<option value="25">25日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/25').getDay()] + '）</option>';
			sdNrDay += '<option value="26">26日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/26').getDay()] + '）</option>';
			sdNrDay += '<option value="27">27日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/27').getDay()] + '）</option>';
			sdNrDay += '<option value="28">28日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/28').getDay()] + '）</option>';
			sdNrDay += '<option value="29">29日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/29').getDay()] + '）</option>';
			if ( selectDate.month !== '2' ) {
				sdNrDay += '<option value="30">30日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/30').getDay()] + '）</option>';
				if ( selectDate.month == '1' || selectDate.month == '3' || selectDate.month == '5' ||　selectDate.month == '7' ||　selectDate.month == '8' ||　selectDate.month == '10' ||　selectDate.month == '12' ) {
					sdNrDay += '<option value="31">31日（' + dow[new Date(selectDate.year + '/' + selectDate.month + '/31').getDay()] + '）</option>';
				}
			}
			$('#sdNrDay').html(sdNrDay);
			$('#searchDetailCal')
				.datePicker({ // associate the link with a date picker
					createButton:false,
					year:selectDate.year,
					month:selectDate.month-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]);
				}
			);
		}
	});

	$('#sdNrDay').change(function(){
		if ( $('#sdNrDay').val() == '' ){
			$('#sdNrTime').attr('disabled', 'disabled');
			$(this).focus();
		} else {
			$('#sdNrTime').removeAttr('disabled').focus();
		}
	});

	// initialise the "Select date" link


	var updateSelects = function (selectedDate)
	{
		var selectedDate = new Date(selectedDate);
		var sdNrDay = '<option value="">指定無し</option>';
		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>';
			}
		}
		$('#sdNrDay').html(sdNrDay);
		$('#sdNrMonth, #sdNrDay, #sdNrTime').removeAttr('disabled');
		$('#sdNrDay option[value=' + selectedDate.getDate() + ']').attr('selected', 'selected');
		$('#sdNrMonth option[value=' + (selectedDate.getMonth()+1) + ']').attr('selected', 'selected');
		$('#sdNrYear option[value=' + (selectedDate.getFullYear()) + ']').attr('selected', 'selected');
	}

	// listen for when the selects are changed and update the picker
	$('#sdNrDay, #sdNrMonth, #sdNrYear')
		.bind(
			'change',
			function() {
				/* var d = new Date(
					$('#sdNrYear').val(),
					$('#sdNrMonth').val()-1,
					$('#sdNrDay').val()
				);
				$('#searchDetailCal').dpSetSelected(d.asString()); */
			}
		);

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

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

	$('.maleOK, .coupleOK').excludeCheckbox('.femaleOnly');

});

/**
 * @param target {jQuery} target jQuery Object
 * @example $(elem).excludeCheckbox(target);
 * Uncheck elem and target checkbox exclusively.
 */
$.fn.excludeCheckbox = function(_target){
	return this.each(function(){
		var elem = $(this);
		var target = elem.parents('form').find(_target);
		var uncheck = function(e1,e2){
			if(e1.checked){e2.removeAttr('checked')}
		}
		elem.bind('click',function(){uncheck(this,target)})
		target.bind('click',function(){uncheck(this,elem)})
	});
};

