var gLocalSearch;

gLocalSearch = new GlocalSearch();
gLocalSearch.SMALL_RESULTSET;

var gLocalSearchAddress;

gLocalSearchAddress = new GlocalSearch();
gLocalSearchAddress.SMALL_RESULTSET;

// 検索完了時のコールバック関数をセット
gLocalSearch.setSearchCompleteCallback(null, onLocalSearch);

// 検索完了時のコールバック関数をセット
gLocalSearchAddress.setSearchCompleteCallback(null, onLocalSearchAddress);

function changeLL_In(lat, lng)
{
	var point = new GLatLng(parseFloat(lat), parseFloat(lng));

	if (point) {
		var message = 'お店の場所をココに設定する<img src="/images/koko.gif" width="51" height="42" align="middle" /><br /> 緯度 <strong style="color:#d10000">' + point.y + '</strong><br /> 経度 <strong style="color:#d10000">' + point.x + "</strong>";

		parent.gmap.map.openInfoWindowHtml(point, (message));

		var cond = '';
		cond = cond + '緯度 <input type="text" id="lat" value="';
		cond = cond + point.y;
		cond = cond + '" name="lat" size="21" onchange="changeLL_In(document.getElementById(\'lat\').value, document.getElementById(\'lng\').value)">';
		cond = cond + '&nbsp;&nbsp;&nbsp;';
		cond = cond + '経度 <input type="text" id="lng" value="';
		cond = cond + point.x;
		cond = cond + '" name="lng" size="21" onchange="changeLL_In(document.getElementById(\'lat\').value, document.getElementById(\'lng\').value)">';

		document.getElementById("condition").innerHTML = cond;
	}
}

// qはフォームのテキストエリアの値
function localSearch(q)
{
	if (!q) return false;
	document.getElementById("loader").style.display = 'inline';
	document.getElementById('result_keyword').innerHTML = '';
	gLocalSearch.execute(q);
	return false;
}

// qはフォームのテキストエリアの値
function localSearchAddress(q)
{
	if (!q) return false;
	document.getElementById("loader").style.display = 'inline';
	document.getElementById('result_address').innerHTML = '';
	gLocalSearchAddress.execute(q);
	return false;
}

// 検索完了時に呼ばれるコールバック関数
function onLocalSearch()
{
	document.getElementById("loader").style.display = 'none';
	
	if (!gLocalSearch.results || gLocalSearch.results.length == 0) {
		document.getElementById("result_keyword").style.display = 'block';
		document.getElementById('result_keyword').innerHTML = 'キーワードに該当する検索結果は見つかりませんでした。';
		gLocalSearch.clearResults();
		return;
	}

	document.getElementById("result_keyword").style.display = 'none';

	// 検索結果の1番目を取得
	var first = gLocalSearch.results[0];
	
	// その結果の緯度・経度を地図の中心にセット
	parent.gmap.map.setCenter(new GLatLng(parseFloat(first.lat), parseFloat(first.lng)));
	changeLL_In(parseFloat(first.lat), parseFloat(first.lng));

	gLocalSearch.clearResults();
}

// 検索完了時に呼ばれるコールバック関数
function onLocalSearchAddress()
{
	document.getElementById("loader").style.display = 'none';
	
	if (!gLocalSearchAddress.results || gLocalSearchAddress.results.length == 0) {
		document.getElementById("result_address").style.display = 'block';
		document.getElementById('result_address').innerHTML = 'キーワードに該当する検索結果は見つかりませんでした。';
		gLocalSearchAddress.clearResults();
		return;
	}

	document.getElementById("result_address").style.display = 'none';

	// 検索結果の1番目を取得
	var first = gLocalSearchAddress.results[0];
	
	// その結果の緯度・経度を地図の中心にセット
	parent.gmap.map.setCenter(new GLatLng(parseFloat(first.lat), parseFloat(first.lng)));
	changeLL_In(parseFloat(first.lat), parseFloat(first.lng));

	gLocalSearchAddress.clearResults();
}

function requestXmlAddressId2LL(id)
{
	document.getElementById("loader").style.display = 'inline';
	
	var request = GXmlHttp.create();
	request.open("GET", "index.php?action_xml_address_prf=1&address_prf_id=" + id, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			document.getElementById("loader").style.display = 'none';
			var xml = request.responseXML;
			var item = xml.documentElement.getElementsByTagName('item');

			if (item.length == 0) return;

			changeLL_In(parseFloat(item[0].getAttribute('lat')), parseFloat(item[0].getAttribute('lng')));
		}
	}
	request.send('');
}

