﻿//////最終更新2010/1/5//////
// 新2010/1/5　作成


$(function(){
	var domeMyList = "";
	var domeSelectWord;
	
	$.ajax({
		url: "xml/searchDome.xml",
		type: 'GET',
		dateType: "xml",
		success: function(data){
		
			////デフォルトの状態
			$(data).find('myArea').each(function(){
				domeMyList += '<option>' + $(this).attr('name') + '</option>';
			});
			document.getElementById("domeArea").innerHTML = '<select>' + domeMyList + '</select>';
			domeMyList = "";
			
			$(data).find('myArea:first').children().each(function(){
				domeMyList += '<option value="' + $(this).attr('grp') + '">' + $(this).attr('name') + '</option>';
			});
			document.getElementById("prefecture").innerHTML = '<select>' + domeMyList + '</select>';
			domeMyList = "";
			
			$(data).find('Prefecture:first').children().each(function(){
				domeMyList += '<option value="' + $(this).attr('cat') + '">' + $(this).attr('name') + '</option>';
			});
			document.getElementById("air").innerHTML = '<select>' + domeMyList + '</select>';
			domeMyList = "";
		
		
			//エリア選択時
			$('#domeArea select').change(function(){
				selectWord = "myArea[name=" + $('#domeArea option:selected').text() + "]";	//セレクタ構文
				$(data).find(selectWord).children().each(function(){
					domeMyList += '<option value="' + $(this).attr('grp') + '">' + $(this).attr('name') + '</option>';
				});
				document.getElementById("prefecture").innerHTML = '<select>' + domeMyList + '</select>';
				domeMyList = "";
				
				$(data).find(selectWord).children(':first').children().each(function(){
					domeMyList += '<option value="' + $(this).attr('cat') + '">' + $(this).attr('name') + '</option>';
				});
				document.getElementById("air").innerHTML = '<select>' + domeMyList + '</select>';
				domeMyList = "";
				
				///////////国選択時///////////
				////県のセレクトDomにチェンジがあった場合、選択肢をチェックして、xmlから引っ張る
				$('#prefecture select').change(function(){
					selectWord = "Prefecture[name=" + $('#prefecture option:selected').text() + "]";	//セレクタ構文
					$(data).find(selectWord).children().each(function(){
						domeMyList += '<option value="' + $(this).attr('cat') + '">' + $(this).attr('name') + '</option>';
					});
					document.getElementById("air").innerHTML =  '<select>' + domeMyList + '</select>';		//都市選択なし
					domeMyList = "";
				
				});
				
			});
					
		}
		
	});
	
	////////////出発日の設定////////////
	var calDat = new Date();
	calDat.setDate(calDat.getDate() + 10);		////今の日付の5日後をベースの日とする
	var calYear = calDat.getFullYear();
	var calMonth = calDat.getMonth()+1;
	if(calMonth < 10){calMonth = "0" + calMonth;}
	var calDay;

	var year_month = '<option value="noSelect">指定しない</option><option value="' + calYear + calMonth + '">' +	calYear + '年' + calMonth + '月</option>';
	calDat.setDate(1);
	
	///////半年分の月の選択肢をセレクタオプションに入れる
	for(var i=0; i<5; i++){
		calDat.setMonth(calDat.getMonth() + 1);
		calMonth = calDat.getMonth() + 1;
		if(calMonth < 10) {
			calMonth = "0" + calMonth;
		}
		calYear = calDat.getFullYear();
		year_month += '<option value="' + calYear + calMonth + '">' + calYear + '年' + calMonth + '月</option>';
	}
	document.getElementById("domeSelectDay1").innerHTML = "<select>" + year_month + "</select>";
	var _day = '';
	
	//////日付指定で選択肢を選んだ場合//////
	$('#domeSelectDay1 select').change(function(){
		
		if($('#domeSelectDay1 option:eq(1)').is(':selected')){	//今の月の場合
		
			calDat = new Date();
			calDat.setDate(calDat.getDate() + 10);
			calMonth = calDat.getMonth();
			while(calMonth == calDat.getMonth()) {
				calDay = calDat.getDate();
				if(calDay < 10) {calDay = "0" + calDay;}
				_day += '<option value="' + calDay + '">' + calDay + "日</option>";
				calDat.setDate(calDat.getDate() + 1);
			}
			
		} else if($('#domeSelectDay1 option:first').is(':selected')) {	//日付指定をしない場合
		
			_day = '<option>----</option>';
		
		} else {	//来月以降の指定をした場合
			
			calDat.setFullYear($('#domeSelectDay1 option:selected').attr('value').slice(0,4));
			calDat.setMonth($('#domeSelectDay1 option:selected').attr('value').slice(5,6)-1);
			calDat.setDate(1);
			calMonth = calDat.getMonth();
			while(calMonth == calDat.getMonth()) {
				calDay = calDat.getDate();
				if(calDay < 10) {calDay = "0" + calDay;}
				_day += '<option value="' + calDay + '">' + calDay + "日</option>";
				calDat.setDate(calDat.getDate() + 1);
			}
			
		}
		document.getElementById("domeSelectDay2").innerHTML = "<select>" + _day + "</select>";
		_day = "";
	
	});
	
	////////////検索ボタンを押された際の処理////////////
	$('#domeSearchButtun').click(function(){
		var myGrp = $('#prefecture option:selected').attr('value');
		var myCat = $('#air option:selected').attr('value');
		var myURL = "http://kokunai.tour.ne.jp/tokubrh/search/tour/dtlist.php?grp=" + myGrp + "&cat=" + myCat;
		
		////日付指定がある場合
		if($('#domeSelectDay1 option:first').is(':selected') == false) {
		 var paraYear = $('#domeSelectDay1 option:selected').attr('value').slice(0,4);
		 var paraMonth = $('#domeSelectDay1 option:selected').attr('value').slice(4,6);
		 var paraDay = $('#domeSelectDay2 option:selected').attr('value');
		 myURL += "&sel_year=" + paraYear + "&sel_month=" + paraMonth + "&sel_day=" + paraDay;
		}
		
		//pageTracker._trackPageview('/_CatSearch/' + document.title);	//GAnaにデーター送信
		
		////検索後に戻ってきても選択肢をそのままにするためのクッキー処理
		var searchCat = "dome_" + $('#domeArea option').index($('#domeArea option:selected')) + "_" + $('#prefecture option').index($('#prefecture option:selected')) + "_" + $('#air option').index($('#air option:selected'));
		$.cookie('search', searchCat);
		
		location.href = myURL;
	});
	
	
	////タブ部分の形成
	$('#catSearchContainer > div:last').hide();
	$('#catSearchContainer li a').click(function(){
		$('#catSearchContainer > div').hide();
		$(this.hash).show();
		$('#catSearchContainer li a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	});

	
});
