/* 検索フォーム */

$(document).ready(function() {
	var target = $("#searchFormDate");
	target.datepicker({
// 年末年始の宴会シーズン対応として、選択可能期間を2ヶ月先に変更
//			maxDate: '+1m',			// 1ヶ月後まで選択可能
			maxDate: '+2m',			// 1ヶ月後まで選択可能
			minDate: '0m',			// 昨日以前は選択不可
			showButtonPanel: true,	// 閉じるボタンなどを表示
			numberOfMonths: 2		// ２ヶ月を表示
	});

	var inst = $.datepicker._getInst(target[0]);

	// 「今日」ボタンを押したら閉じるように修正。
	$.datepicker._gotoToday = function(event) {
		var date = new Date();
		var d = date.getDate();
		var month = date.getMonth();
		var year = date.getFullYear();

		var inst = $.datepicker._getInst($(event)[0]);

		var s = $.datepicker._formatDate(inst, d, month, year);

		$.datepicker._selectDate(event, s);
	 };

	// 「閉じる」ボタンの後ろに「日付を指定しない」ボタンを追加する。
	var old = $.datepicker._updateDatepicker;

	$.datepicker._updateDatepickerOrig = old;
	$.datepicker._updateDatepicker = function(inst) {
		this._updateDatepickerOrig(inst);

		var buttonPanel =
			'<button type="button" class="ui-datepicker-noselect ui-state-default ui-priority-secondary ui-corner-all" onclick="$.datepicker.clearSearchFormDatePicker(\'#' + inst.id + '\')"' +
			'>日付を指定しない</button>';

		$(".ui-datepicker-current").after(buttonPanel);
		$(".ui-datepicker-noselect")
				.bind('mouseout', function(){
					$(this).removeClass('ui-state-hover');
				})
				.bind('mouseover', function(){
					$(this).addClass('ui-state-hover');
				});
	}

	// 「日付を指定しない」リンク押下
	$.datepicker.clearSearchFormDatePicker = function(id) {
		var target = $(id);
		target.val("");
		target.datepicker('hide');
	}

	// 「カレンダーから選ぶ」リンク押下
	$.datepicker.openSearchFormDatePicker = function() {
		$("#searchFormDate").datepicker('show');
	}

	var obj = $("#searchFormDateLink");
	obj.bind("click", $.datepicker.openSearchFormDatePicker);
	obj.attr("href", "javascript:void(0)");


	// 検索結果ページの検索フォームではサブフォームの入力もあわせて検索されるようにする。
	$("#searchForm").bind("submit", function() {
			searchSubForm_onSubmit(document.forms["searchSubForm"]);
			return false;
	});

	// エリア検索、駅名検索を切り替える
	var searchForm = document.forms["searchForm"];
	if (searchForm != null) {
		if (searchForm["stationSearchFlag"].value != null) {
			var flag = searchForm["stationSearchFlag"].value;

			if (flag == "true") {
				// エリア検索部分を非表示、エリア検索変更リンクを表示
				document.getElementById('selectPrefAreaBox').style.display = "none";
				document.getElementById('changeAreaSearch').style.display = "";

				// 駅検索部分を表示、駅検索変更リンクを非表示
				document.getElementById('selectStationBox').style.display = "";
				document.getElementById('changeStationSearch').style.display = "none";

				searchForm.action = "/stationSearch";
			}
		}
	}

});

// こだわり検索画面へ遷移
function submitToMoreSearchPage() {
	var searchForm = document.forms["searchForm"];
	searchForm.action = "/moreSearch";
	searchForm.submit();
}

// クーポンを隠す
function toggleHideCoupon() {
	var target = $("#searchResult");

	if (!$.cookie("couponInvisible")) {
		target.addClass('coupon-invisible');
		$.cookie("couponInvisible", "1", {expires: 365});
		$("#toggleHideCouponLink").text("検索結果にクーポンを表示する");
		$("#toggleHideCouponLink").css("background-position", "0 -26px");
	} else {
		target.removeClass('coupon-invisible');
		$.cookie("couponInvisible", "", {expires: -1});
		$("#toggleHideCouponLink").text("検索結果にクーポンを表示しない");
		$("#toggleHideCouponLink").css("background-position", "0 -1px");
	}
}

// こだわり検索 日付or曜日 指定
function dateWeekChange() {
	if (document.getElementById('changeSelect')) {
		id = document.getElementById('changeSelect').value;

		if (id == '1') {
			// 日付
			document.getElementById('moreSearchDate').style.display = "";
			document.getElementById('moreSearchWeek').style.display = "none";
		} else if (id == '2') {
			// 曜日
			document.getElementById('moreSearchDate').style.display = "none";
			document.getElementById('moreSearchWeek').style.display = "";
		} else {
			// 指定なし
			document.getElementById('moreSearchDate').style.display = "none";
			document.getElementById('moreSearchWeek').style.display = "none";
		}
	}
}

// 検索結果画面でページ表示数変更
function onChangePageSize(obj) {
	var value = obj.options[obj.selectedIndex].value;
	$.cookie("pageSize", value, {expires: 365});

	var searchSubForm = document.forms["searchSubForm"];

	searchSubForm_onSubmit(searchSubForm);
}

// サブ検索
function searchSubForm_onSubmit(form) {
//	var date = form["date"].value;
//	var time = form["time"].value;
//	var discountRate = form["discountRate"].value;
	var sortType = form["sort"].value;

	var searchForm = document.forms["searchForm"];
//	var searchForm = document.forms[0];

//	searchForm["date"].value = date;
//	searchForm["time"].value = time;
//	searchForm["discountRate"].value = discountRate;
	searchForm["sort"].value = sortType;

	searchForm.submit();

	return false;
}

// 画像先読み
new Image().src = "/images/lightBox/shadow.png";
new Image().src = "/images/lightBox/close.gif";

// 検索パネルの検索モード切替
function changeSearchMode(mode) {
	var searchForm = document.forms["searchForm"];

	if (mode == 0) {
		// エリア検索
		// 駅検索部分を非表示、駅検索変更リンクを表示
		document.getElementById('selectStationBox').style.display = "none";
		document.getElementById('changeStationSearch').style.display = "";

		// エリア検索部分を表示、エリア検索変更リンクを非表示
		document.getElementById('selectPrefAreaBox').style.display = "";
		document.getElementById('changeAreaSearch').style.display = "none";

		searchForm["stationSearchFlag"].value=false;

		searchForm.action = "/search";

	} else {
		// 駅名検索
		// エリア検索部分を非表示、エリア検索変更リンクを表示
		document.getElementById('selectPrefAreaBox').style.display = "none";
		document.getElementById('changeAreaSearch').style.display = "";

		// 駅検索部分を表示、駅検索変更リンクを非表示
		document.getElementById('selectStationBox').style.display = "";
		document.getElementById('changeStationSearch').style.display = "none";

		searchForm["stationSearchFlag"].value=true;

		searchForm.action = "/stationSearch";

	}
}

