$(document).ready(function(){
	
	// ポップウインドウで開くようにする
	$('#areamap a, #arealist a').click(function(){
		var href = this.href;
		var opts = 'width=597, height=500, scrollbars=yes';
		window.open(href, 'housePopup', opts);
		return false;
	});
	
	////////////////////////////////////////////////////////
	
	// TOOOOOOOOOOOOOOOOOOOOLTIIIIIIIIIIIIIIIIIIIIIIIIIIP
	
	//Select all anchor tag with rel set to tooltip
	$('a[rel=tooltip]').mouseover(function(e) {
		
		//Grab the title attribute's value and assign it to a variable
		var tip = $(this).attr('title');	
		
		//Remove the title attribute's to avoid the native tooltip from the browser
		$(this).attr('title','');
		
		//Append the tooltip template and its value
		$(document.body).append('<div id="tooltip"><div class="tipBody">' + tip + '</div></div>');	
				
	}).mousemove(function(e) {
	
		//Keep changing the X and Y axis for the tooltip, thus, the tooltip move along with the mouse
		$('#tooltip').css('top', e.pageY + 10 );
		$('#tooltip').css('left', e.pageX + 20 );
		
	}).mouseout(function() {
	
		//Put back the title attribute's value
		$(this).attr('title',$('.tipBody').html());
	
		//Remove the appended tooltip template
		$(document.body).children('div#tooltip').remove();
	});
	
	////////////////////////////////////////////////////////
	
	// 住所から検索
	var listTokyo = $('#listTokyo');
	var listSaitama = $('#listSaitama');
	var listKanagawa = $('#listKanagawa');
	var listChiba = $('#listChiba');
	var lists = [listTokyo, listSaitama, listKanagawa, listChiba];
	
	// 全て非表示の状態に移行
	hideLists();
	
	// セレクトボックスの値が変更された
	$('#prefecture').bind('change', switchAreaList);
	
	////////////////////////////////////////////////////////
	
	// エリアリストを入れ替える
	function switchAreaList() {
		hideLists();
		var selectedVal = this.value;
		var targetList = getTargetList(selectedVal);
		if(targetList) targetList.css('display', 'block');
	}
	
	// セレクトボックスで選択された県に対応したリストを表示
	function getTargetList(val) {
		var targetList = null;
		switch(val) {
			case 'tokyo':
				targetList = listTokyo;
				break;
			case 'saitama':
				targetList = listSaitama;
				break;
			case 'kanagawa':
				targetList = listKanagawa;
				break;
			case 'chiba':
				targetList = listChiba;
				break;
			default:
				targetList = null;
				break;
		}
		return targetList;
	}
	
	// リストを全て非表示の状態へ移行
	function hideLists() {
		jQuery.each(lists, function(i, el){
			el.css('display', 'none');
		});
	}
	
});

