$(document).ready(function($) {
	// TODO 日付制御 
	var dayStr = ['日', '月', '火', '水', '木', '金', '土'];
	var holidays = [new Date(2010, 0, 1), new Date(2010, 0, 11), new Date(2010, 1, 11), new Date(2010, 2, 22), new Date (2010, 3, 29), new Date(2010, 4, 3), new Date(2010, 4, 4), new Date(2010, 4, 5), new Date(2010, 6, 19), new Date(2010, 8, 20), new Date(2010, 8, 23), new Date(2010, 9, 11), new Date(2010, 10, 3), new Date(2010, 10, 23), new Date(2010, 11, 23)];
	
	initDateInput();
	initAirportSelect();
	initAreaSelect();
	
	function initDateInput(){
		$('#forth-departure-date-input, #back-departure-date-input').datepicker({
			showOn: 'button',
			buttonImageOnly: true,
			buttonImage: '/images/bt_calendar.png',
			showButtonPanel: true,
			buttonText: '日付を選択',
			closeText: 'Close',
			numberOfMonths: 1,
			dateFormat: 'yy-mm-dd',
			beforeShow: function(input, inst){
        		inst.dpDiv.css({marginLeft: -150 + 'px'});
    		}
		});
		
		$('#forth-departure-date-input').datepicker('option', {
			onSelect: function (dateText, inst) {
				var tmp = dateText.split('-');
				var selDate = new Date(parseInt(tmp[0], 10), parseInt(tmp[1],10) - 1, parseInt(tmp[2],10));
				$(this).siblings('span.departure-date').html(jpDateStr(selDate));
				var minDate = new Date(selDate.getTime() + 24 * 60 * 60 * 1000 * 1);
				$('#back-departure-date-input').datepicker('option', 'minDate', minDate).datepicker('setDate', minDate).siblings('span.departure-date').html(jpDateStr(minDate));
				var maxDate = new Date(minDate.getTime() + 24 * 60 * 60 * 1000 * (calendarParam['daysStayMax']-1));
				$('#back-departure-date-input').datepicker('option', 'maxDate', maxDate);
			}
		});
		
		$('#back-departure-date-input').datepicker('option', {
			onSelect: function (dateText, inst) {
				var tmp = dateText.split('-');
				var selDate = new Date(parseInt(tmp[0],10), parseInt(tmp[1],10) - 1, parseInt(tmp[2],10));
				$(this).siblings('span.departure-date').html(jpDateStr(selDate));
			}
		});
		
		$('#forth-departure-date-input').datepicker('option', {
			minDate: calendarParam['forthStartDate'],
			maxDate: calendarParam['forthEndDate']
		});
		
		$('#back-departure-date-input').datepicker('option', {
			minDate: calendarParam['backStartDate'],
			maxDate: calendarParam['backEndDate']
		});
		
		var date = calendarParam['forthStartDate'];
		$('#forth-departure-date-input').datepicker('setDate', date).siblings('span.departure-date').html(jpDateStr(date));
		date = calendarParam['backStartDate'];
		$('#back-departure-date-input').datepicker('setDate', date).siblings('span.departure-date').html(jpDateStr(date));
		
		$('#ui-datepicker-div').hide();
	}
	
	function initAirportSelect(){
		$('#forth-departure-airport-select').change(function(){
			var selectedOption = $('#forth-departure-airport-select option:selected');
			var val = selectedOption.val();
			var text = selectedOption.text();
			$('#back-arrival-airport-select').attr('value', val);
			$('#back-arrival-airport').text(text);
			
			if(val != ''){
				$('#forth-arrival-airport-select').removeAttr('disabled', 'disabled');
				$('#forth-arrival-airport-select').empty();
				$('#forth-arrival-airport-select').append('<option value="">▼選択して下さい</option>');
				
				var enableAirpots = enableAirpotData[val];
				for (var i = 0; i < enableAirpots.length; i++) {
					var airport = enableAirpots[i];
					$('#forth-arrival-airport-select').append('<option value="' + airport[0] + '">' + airport[1] + '</option>');
				}
			}else{
				$('#forth-arrival-airport-select').empty();
				$('#forth-arrival-airport-select').html('<option value="">▼選択して下さい</option>');
				$('#forth-arrival-airport-select').attr('disabled', 'disabled');
			}
			
			$('#forth-arrival-airport-select').trigger('change');
		});
		
		$('#forth-arrival-airport-select').change(function(){
			var selectedOption = $('#forth-arrival-airport-select option:selected');
			var val = selectedOption.val();
			var text = selectedOption.text();
			$('#back-departure-airport-select').attr('value', val);
			$('#back-departure-airport').text(text);
			
			if(val != ''){
				var prefectures = accessiblePrefectures[val];
				var options = '<option value="0">▼選択して下さい</option>';
				for(var i = 0; i < prefectures.length; i++){
					options += '<option value="'+ prefectures[i][0] +'">' + prefectures[i][1] + '</option>';
				}
				$('#prefecture-select').html(options);
				$('#prefecture-select').removeAttr('disabled', 'disabled');
				$('#search-submit').removeAttr('disabled', 'disabled');
			}else{
				$('#prefecture-select').html('<option value="0">▼選択して下さい</option>');
				$('#prefecture-select').attr('disabled', 'disabled');
				$('#prefecture-select').trigger('change');
				$('#search-submit').attr('disabled', 'disabled');
			}
		});
		
		$('#forth-departure-airport-select option:first').attr('selected', 'selected');
	}
	
	function initAreaSelect(){
		$('#prefecture-select').change(function(){
			var selectedOption = $('#prefecture-select option:selected');
			var val = selectedOption.val();
			var text = selectedOption.text();
			
			if(val != 0){
				var areas = accessibleAreas[val];
				var options = '<option value="0">▼選択して下さい</option>';
				
				// TODO 改修
				var category = $('#product_category_id').val();
				var arrivalAirport = $('#forth-arrival-airport-select option:selected').val();
				for(var i = 0; i < areas.length; i++){
					var isExclude = false;
					if(category == 3){
						if(arrivalAirport == 2 && areas[i][0] == 4){
							isExclude = true;
						}else if(arrivalAirport == 3 && areas[i][0] == 1){
							isExclude = true;
						}
					}else if(category == 2){
						if(arrivalAirport == 2 && (areas[i][0] == 4 || areas[i][0] == 6)){
							isExclude = true;
						}
					}else if(category == 1){
						if(arrivalAirport == 2 && (areas[i][0] == 4 || areas[i][0] == 5)){
							isExclude = true;
						}else if(arrivalAirport == 3 && (areas[i][0] == 1 || areas[i][0] == 2 || areas[i][0] == 3)){
							isExclude = true;
						}
					}
					
					if(!isExclude) options += '<option value="'+ areas[i][0] +'">' + areas[i][1] + '</option>';
					
					// TODO 改修ここまで
				}
				$('#area-select').html(options);
				$('#area-select').removeAttr('disabled', 'disabled');
			}else{
				$('#area-select').html('<option value="0">▼選択して下さい</option>');
				$('#area-select').attr('disabled', 'disabled');
			}
		});
		
		$('#prefecture-select option:selected').removeAttr('selected');
		$('#area-select option:selected').removeAttr('selected');
	}
	
	$('#search-submit').click(function(){
		if('' == $('#forth-departure-airport-select option:selected').val()){
			alert('出発地を選択して下さい。');
			return false;
		}
		
		if('' == $('#forth-arrival-airport-select option:selected').val()){
			alert('到着地を選択して下さい。');
			return false;
		}
		
		var daid = parseInt($('#forth-departure-airport-select option:selected').val(), 10);
		var aaid = parseInt($('#forth-arrival-airport-select option:selected').val(), 10);
		if(daid == 9 || aaid == 9){
			var fddate = $('#forth-departure-date-input').datepicker('getDate');
			if(fddate.getTime() < 1281452400000){
				alert('北九州空港は2010年8月11日からの就航となります。\n出発日を2010年8月11日以降に設定してください。');
				return false;
			}
		}else if(daid == 4 && aaid == 7){
			var fddate = $('#forth-departure-date-input').datepicker('getDate');
			if(fddate.getTime() < 1283266800000){
				alert('神戸空港から鹿児島空港へは2010年9月1日からの就航となります。\n出発日を2010年9月1日以降に設定してください。');
				return false;
			}
		}
	});
	
	function jpDateStr(date){
		return date.getFullYear() + '年' + (date.getMonth() + 1) + '月' + date.getDate() + '日' + '(' + dayStr[date.getDay()] + (holiday_check(date) ? '・祝' : '') + ')';
	}

	function holiday_check(date){
		for(var i = 0; i < holidays.length; i++){
			if(date.getTime() == holidays[i].getTime()){
				return true;
			}
		}
		return false;
	}
});