function serchcityMain(){
	mode = "catch";
	type = "";
	for (var i = 0; i < document.serchcityform.type.length; i++){
		if(document.serchcityform.type[i].checked == true){
			type = document.serchcityform.type[i].value;
		}
	}
	serchcity = document.serchcityform.searchcity.value;
	
	modeSelecter();
}
function modeSelecter(){
	if(mode == "catch"){
		if( type == "土地" ){
			location.href="http://www.net-hiro.co.jp/land/?serchcity=" + encodeURI(serchcity) + "&type=" +encodeURI(type) + "&mode=view";
		}
		else if( type == "売家" ){
			location.href="http://www.net-hiro.co.jp/house-for-sale/?serchcity=" + encodeURI(serchcity) + "&type=" + encodeURI(type) + "&mode=view";
		}
		else if( type == "賃貸" ){
			location.href="http://www.net-hiro.co.jp/lease/?serchcity=" + encodeURI(serchcity) + "&type=" + encodeURI(type) + "&mode=view";
		}
		else{
			alert("検索する内容を選択してください");
		}
	}
	else{
		var herosStartRewrite = new herosGetData2();
		herosStartRewrite.main();
	}
}

var herosGetData2 = function(){
	this.main = function(catchName,catchValue){
		$("#estatelistarea").replaceWith('<div id="estatelistarea"><p id="result-line" style="width: 400px;"></p><br style="clear: both;"/></div>');
		$.getJSON("http://www.net-hiro.co.jp/hiro-searchdata.json", function(searchData){
			var i = 0;
			var count = 0;
			for ( i = 0; searchData.items.length > i; i++){
				if( searchData.items[i].category == type ){
					if(searchData.items[i].town == serchcity){
						contentReplacer2( searchData.items[i] );
						count++;
					}
					else if( serchcity == 'その他'
							 && searchData.items[i].town != '水戸市'
							 && searchData.items[i].town != 'ひたちなか市'
							 && searchData.items[i].town != '那珂市'
							 && searchData.items[i].town != '常陸大宮市'
							 && searchData.items[i].town != '城里町' ){
						contentReplacer2( searchData.items[i] );
						count++;
					}
				}
			}
			if( count > 0 ){
				$('#result-line').append( count + '件の物件が見つかりました');
			}
			else{
				/*alert('条件に合う物件がありませんでした。\n前の条件にもどります。');*/
				$('#result-line').append( '条件に合う物件がありませんでした。');
				this.primary = this.primaryOld;
				this.juniorHigh = this.juniorHighOld;
			}
		});
	};
}
function contentReplacer2( catchData2 ){
	var line = '';

	if( type != "賃貸" ){
		line = line + '		<ul class="list_table">';
		line = line + '			<li>';
		line = line + '				<h3><a href="'+ catchData2.link +'">' + catchData2.name + '</a></h3>';
		line = line + '				<p><a href="' + catchData2.link + '"><img src="http://www.net-hiro.co.jp/images/list/detail_btn.jpg" alt="詳細を見る" title="詳細を見る"></a></p>';
		line = line + '				<table>';
		line = line + '					<tbody><tr>';
		line = line + '						<th width="90">価格</th>';
		line = line + '						<th width="140">学区</th>';
		line = line + '						<th width="180">所在地</th>';
		line = line + '						<th colspan="2">土地面積</th>';
		line = line + '					</tr>';
		line = line + '					<tr>';
		line = line + '						<td>' + catchData2.value + '円</td>';
		line = line + '						<td>' + catchData2.primary + ' / ' + catchData2.juniorHigh + '</td>';
		line = line + '						<td>' + catchData2.town + catchData2.address + '</td>';
		line = line + '						<td width="65">' + catchData2.tubo + '</td>';
		line = line + '						<td width="65">' + catchData2.m2 + '</td>';
		line = line + '					</tr>';
		line = line + '				</tbody></table>';
		line = line + '			</li>';
		line = line + '		</ul>';
	}
	else{
		line = line + '		<ul class="list_table">';
		line = line + '			<li>';
		line = line + '				<h3><a href="'+ catchData2.link +'">' + catchData2.name + '</a></h3>';
		line = line + '				<p><a href="' + catchData2.link + '"><img src="http://www.net-hiro.co.jp/images/list/detail_btn.jpg" alt="詳細を見る" title="詳細を見る"></a></p>';
		line = line + '				<table>';
		line = line + '					<tbody><tr>';
		line = line + '						<th width="90">価格</th>';
		line = line + '						<th width="140">学区</th>';
		line = line + '						<th width="180">所在地</th>';
		line = line + '						<th colspan="2">号室・間取り</th>';
		line = line + '					</tr>';
		line = line + '					<tr>';
		line = line + '						<td>' + catchData2.value + '円</td>';
		line = line + '						<td>' + catchData2.primary + ' / ' + catchData2.juniorHigh + '</td>';
		line = line + '						<td>' + catchData2.town + catchData2.address + '</td>';
		line = line + '						<td width="65">' + catchData2.gositu + '</td>';
		line = line + '						<td width="65">' + catchData2.madori + '</td>';
		line = line + '					</tr>';
		line = line + '				</tbody></table>';
		line = line + '			</li>';
		line = line + '		</ul>';
	}

	$("#estatelistarea").append(line);
}

/*■■■■■　汎用系　■■■■■*/

function searchCatcher(){
	//条件をキャッチするための関数
	//URI問い合わせ部分を切り取って取得
	//serchcity = 水戸市|ひたちなか市|那珂市|常陸大宮市|城里町|その他
	//type = 土地|売買|賃貸
	//mode = catch|view
	var uriString = location.href;
	if( uriString.match(/\?/) ){
		uriSearch = uriString.split("?");
		uriString = uriSearch[1];		/*?以下がuriStringに入る*/

		uriSearch = uriString.split("&");

		var i = 0;
		while( i < uriSearch.length ){
			oneList = uriSearch[i].split("=");
			eval(oneList[0] + " = \"" + decodeURI(oneList[1]) +"\";");
			
			i++;
		}
	}
}
/*■■■■■　スタート　■■■■■*/
$(document).ready(function(){
	if(document.schoolSearch.primary.value == '' && document.schoolSearch2.juniorHigh.value == '' ){
		mode = "";
		type = "";
		serchcity = "";
		searchCatcher();
		if( mode == "view"){
			modeSelecter();
		}
	}
});

