var herosSearch = function(){
	this.EventSenser = function(catchName,catchValue){
		ohSearch.main();
	}
}

var herosGetData = function(){
	this.primaryOld = '';
	this.juniorHighOld = '';

	this.main = function(){
		var primary = ( document.schoolSearch.primary.value == '') ? undefined : document.schoolSearch.primary.value;
		var juniorHigh = ( document.schoolSearch2.juniorHigh.value == '') ? undefined : document.schoolSearch2.juniorHigh.value;
		var count = 0;

		$("#estatelistarea").replaceWith('<div id="estatelistarea"><p id="result-line" style="width: 400px;"></p><br style="clear: both;"/></div>');
		$.getJSON("hiro-searchdata.json", function(searchData){
			var i = 0;
			for ( i = 0; searchData.items.length > i; i++){

				if( categorySwitch && categorySwitch != searchData.items[i].category ){	
					continue;
				}
				if( primary == undefined && juniorHigh == searchData.items[i].juniorHigh ){
					contentReplacer( searchData.items[i] );
					count++;
				}
				else if( juniorHigh == undefined && primary == searchData.items[i].primary){
					contentReplacer( searchData.items[i] );
					count++;
				}
				else if( primary == searchData.items[i].primary && juniorHigh == searchData.items[i].juniorHigh ){
					contentReplacer( searchData.items[i] );
					count++;
				}
				else if( primary == undefined && juniorHigh == undefined ){
					contentReplacer( searchData.items[i] );
					count++;
				}
			}
			if( count > 0 ){
				$('#result-line').append(  count + '件の物件が見つかりました');
			}
			else{
				/*alert('条件に合う物件がありませんでした。\n前の条件にもどります。');*/
				$('#result-line').append( '条件に合う物件がありませんでした。');
			}
			this.primaryOld = primary;
			this.juniorHighOld = juniorHigh;
		});
	};
}
function contentReplacer( catchData ){

	var line = '';
if(catchData.category != "賃貸"){
	line = line + '		<ul class="list_table">';
	line = line + '			<li>';
	line = line + '				<h3><a href="'+ catchData.link +'">' + catchData.name + '</a></h3>';
	line = line + '				<p><a href="' + catchData.link + '"><img src="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>' + catchData.value + '円</td>';
	line = line + '						<td>' + catchData.primary + ' / ' + catchData.juniorHigh + '</td>';
	line = line + '						<td>' + catchData.town + catchData.address + '</td>';
	line = line + '						<td width="65">' + catchData.tubo + '</td>';
	line = line + '						<td width="65">' + catchData.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);
}
$(document).ready(function(){
	ohSearch = new herosGetData();

	if(document.schoolSearch.primary.value != '' || document.schoolSearch2.juniorHigh.value != '' ){
		heroSearch.EventSenser();
	}
});


