
(function($){

	$(document).ready(function(){
		// for ie...
		if ($.browser.msie) {
			// because of onchange occurs when loses its focus.
			$('#search')
				.find(':checkbox')
					.click(function(){
						this.blur();
					})
				.end()
				.find('label img')
					.click(function(){
						$(this).prev().click().change();
					})
				.end()
			;
		}

		// setup filtering search.
		$('#search form')
			.submit(function(){
				return false;
			})
			.find(':checkbox')
				.change(searchItems)
			.end()
		;
		initSearch();
	});

	function initSearch()
	{
		var restaurantData = {
			'RH': [ 'hiramatsu', 'tokyo1', 'french', 'private', 'wedding' ],
			'FR': [ 'hiramatsu', 'hakata', 'french', 'private', 'wedding' ],
			'LB': [ 'hiramatsu', 'sapporo', 'french', 'private', 'wedding' ],
			'LR': [ 'hiramatsu', 'tokyo1', 'french', 'private', 'wedding' ],
			'RHLC': [ 'hiramatsu', 'paris', 'french', 'private' ],

			'RA': [ 'aso', 'tokyo3', 'italian', 'private', 'wedding' ],
			'AA': [ 'aso', 'tokyo2', 'italian', 'private', 'wedding' ],
			'RAT': [ 'aso', 'hakata', 'italian', 'private', 'wedding' ],
			'AC': [ 'aso', 'tokyo3', 'italian', ],
			'AC2': [ 'aso', 'tokyo2', 'italian', 'private' ],
			'CM': [ 'aso', 'tokyo3', 'cafe', ],

			'PBD': [ 'paulbocuse', 'tokyo3', 'french', 'private', 'wedding' ],
			'PBK': [ 'paulbocuse', 'kanazawa', 'french', 'private', 'wedding' ],
			'PBG': [ 'paulbocuse', 'tokyo2', 'french', 'private' ],
			'PBT': [ 'paulbocuse', 'tokyo2', 'french', ],
			'PBM': [ 'paulbocuse', 'tokyo1', 'french', ],
			'PBC': [ 'paulbocuse', 'tokyo1', 'french', ],
			'PBKC': [ 'paulbocuse', 'kanazawa', 'french', 'cafe', ],
			'PBH': [ 'paulbocuse', 'hakata', 'french', 'wedding', ],

			'PBN': [ 'paulbocuse', 'nagoya', 'french', 'private' ],
			'AN': [ 'haeberlin', 'nagoya', 'french', 'private', 'wedding' ],
			'AT': [ 'haeberlin', 'tokyo1', 'french', 'private', 'wedding' ],
			'SS': [ 'pourcel', 'tokyo2', 'french', 'private', 'wedding' ],
			'BO': [ 'ddlondon', 'tokyo1', 'italian', 'private' ],

			'IC': [ 'ddlondon', 'tokyo2', 'italian', 'private', 'wedding' ],
			'DP': [ 'tokyo1', 'cafe', ],
			'NACC': [ 'tokyo1', 'cafe', ]
		};

		// setup restaurant list.
		$('#restaurant li').each(function(){
			// set data.
			var name = $(this).attr('class').split(' ')[0];
			$(this).data('cond', (restaurantData[name] || []).join());
		});

		searchItems();
	}

	function searchItems()
	{
		// hide all.
		$('#restaurant li').hide();

		var target = $('#restaurant li');

		if ($('#search :checked').length > 0) {
			// filter items with each condition.
			$('#search div.findto').each(function(){
				if ($(':checked', this).length > 0) {
					target = filterItems({
						items: target,
						filter: this
					});
				}
			});
		}

		// show matched.
		$(target).fadeIn();
	}

	/*
	 * returns filtered elements.
	 *
	 * @param {Object} option parameters.
	 */
	function filterItems(params)
	{
		var option = $.extend({
			items: [],
			filter: []
		}, params);

		// collect checked params.
		var param = $.param($(option.filter).find(':checked')).split('&');

		// generate regexp.
		var cond = $.map(param, function(n, i){
			return n.split('=')[0];
		});
		var re = new RegExp('('+ cond.join('|') +')');

		// filter items.
		return $(option.items).filter(function(){
			return re.test($(this).data('cond'));
		});
	}

})(jQuery);

