var map;
var data;
var addhtml;
var mlarea = '';
var mlspottj = '';
var mlspottakt = '';
var mglid = '';
var mgmid = '';
var plural_mgmid = '';
var mmid = '';
var offset = 0;
var limit = 10; // const:表示件数
var defltlat = '36.687137'; // const:デフォ緯度
var defltlng = '137.219804'; // const:デフォ経度
var defltzoom = parseInt(13); // const:デフォズームLv
var zoomlv = new Object();
zoomlv["01"] = parseInt(10); // const: ズームレベル(県内)
zoomlv["02"] = parseInt(8); // const: ズームレベル(県外)

/* initialize */
function load() {
	if (GBrowserIsCompatible()) {
		map = new GMap2($('mapArea'));
		map.setCenter(new GLatLng(defltlat, defltlng), defltzoom);
		map.addControl(new GLargeMapControl());
		map.addControl(new GOverviewMapControl());
	}
	plural_mgmid = $('plural_mgmid').value;
}

/* JSON受信 */
function receiveJson(){
	$('recordinfo').innerHTML = '';
	$('datalist').innerHTML = '';
	$('pg_prev').innerHTML = '';
	$('pg_next').innerHTML = '';
	if($('mmid').innerHTML){ mmid = '1'; }else{ mmid = ''; }
	var url = "./googlemap.php?output=json";
	url += "&mlspottj="+mlspottj+"&mlspottakt="+mlspottakt;
	url += "&mglid="+mglid+"&mgmid="+mgmid+"&plural_mgmid="+plural_mgmid;
	url += "&mmid="+mmid;
	new Ajax.Request(url, {method: 'get', onComplete: receiveJsonComplete});
}

/* JSON受信完了したとき */
function receiveJsonComplete(httpObj){
	data = eval(httpObj.responseText);
	dispRecordInfo();
	addhtml = '<ul class="resultList">';
	if(data[3].length > offset + limit){
		var listend = offset + limit;
	}else{
		var listend = data[3].length;
	}
	for(var i = offset; i < listend; i++){ dispDataList(i); }
	addhtml += '</ul>';
	$('datalist').innerHTML = addhtml;
	for(var i = 0; i < data[3].length; i++){ addMarker(i); }
	dispNavi();
}

/* レコード情報表示 */
function dispRecordInfo(){
	var alltotal = data[0];
	var alltotal2 = data[1];
	var total = data[2];
	var dispfrom = offset + 1;
	var dispto = offset + limit;
	if(dispto > total){
		dispto = total;
	}
	if(total == 0){
		$('recordinfo').innerHTML = '該当する店舗情報はありません。';
	}else{
		$('recordinfo').innerHTML = total + ' 件中 ' + dispfrom + ' - ' + dispto + ' 件を表示';
	}
	$('totalnum').innerHTML = alltotal;
	$('totalnum2').innerHTML = alltotal2;
}

/* ページ処理・表示分岐 */
function dispNavi(){
	if(offset != 0){
		$('pg_prev').innerHTML = '<a href="javascript:void(0)" onClick="dispPrevPage()">前の' + limit + '件</a>';
	}else{
		$('pg_prev').innerHTML = '　';
	}
	if(data[2] > offset + limit){
		if(data[2] - (offset + limit) < limit){
			var nextpg = data[2] - (offset + limit);
		}else{
			var nextpg = limit;
		}
		$('pg_next').innerHTML = '<a href="javascript:void(0)" onClick="dispNextPage()">次の' + nextpg + '件</a>';
	}else{
		$('pg_next').innerHTML = '　';
	}
}

/* ページ処理・prev */
function dispPrevPage(){
	map.clearOverlays();
	offset = offset - limit;
	receiveJson();
}

/* ページ処理・next */
function dispNextPage(){
	map.clearOverlays();
	offset = offset + limit;
	receiveJson();
}

/* datalistの表示 */
function dispDataList(i){
	addhtml += '<li id="link_marker' + i + '">';
	addhtml += '<a href="javascript:void(0)" onclick="this.blur()">';
	addhtml += data[3][i].name + "</a></li>\n";
}

/* マーカーの追加 */
function addMarker(i){
	var point = new GPoint(data[3][i].longitude, data[3][i].latitude);
	var icon = new GIcon();
	icon.image = './images/pin.png';
	icon.shadow = './images/shadow.png';
	icon.iconSize = new GSize(40, 42);
	icon.shadowSize = new GSize(40, 42);
	icon.iconAnchor = new GPoint(20, 42);
	icon.infoWindowAnchor = new GPoint(18, 1);
	var marker = new GMarker(point, icon);
	var msg = '<div class="infowindow">';
	msg += '<span class="shopname">' + data[3][i].name + '</span><br />';
	msg += '<span class="shopadd">' + data[3][i].address + '</span><br />';
	msg += '<span class="shoptel">' + data[3][i].tel + '</span><br />';
	msg += '<span class="genre">' + data[3][i].mgmname_disp + '</span><br />';
	if(data[3][i].mlmemo != null){
		msg += '<span class="memo">' + data[3][i].mlmemo + '</span><br />';
	}
	if(data[3][i].imagepath != null){
		msg += '<img src="' + data[3][i].imagepath + '" ';
		if(data[3][i].imgwidth != null){
			msg += 'width="' + data[3][i].imgwidth + '" ';
		}
		if(data[3][i].imgheight != null){
			msg += 'height="' + data[3][i].imgheight + '" ';
		}
		msg += '/>';
	}
	msg += '<br />';
	if(data[3][i].mlurltj != null){
		msg += '<a href="' + data[3][i].mlurltj + '" target="_blank"><img src="./images/a_webtj.gif" width="70" height="20" /></a>';
	}
	if(data[3][i].mlurltakt != null){
		msg += '<a href="' + data[3][i].mlurltakt + '" target="_blank"><img src="./images/a_takt.gif" width="70" height="20" /></a>';
	}
	if(data[3][i].gourmeturl != null){
		msg += '<a href="' + data[3][i].gourmeturl + '" target="_blank"><img src="./images/a_gourmet.gif" width="70" height="20" /></a>';
	}
	if(data[3][i].chargeurl != null){
		msg += '<a href="' + data[3][i].chargeurl + '" target="_blank"><img src="./images/a_charge.gif" width="70" height="20" /></a>';
	}
	msg += '<br class="clear" /></div>';
	var window_msg = function(){
		marker.openInfoWindowHtml(msg);
	}
	GEvent.addListener(marker, "click", window_msg);
	var marker_link = $('link_marker' + i);
	if(marker_link){
		marker_link.onmousedown = window_msg;
	}
	map.addOverlay(marker);
}

/* 検索処理・TJ記事 */
function flgMlspottj(){
	$('mlspottakt').value = '';
	mlspottakt = '';
	$('mglid').value = '';
	mglid = '';
	$('mgmid').value = '';
	mgmid = '';
	$('mlarea').value = '';
	mlarea = '';
	plural_mgmid = '';
	$('terms').innerHTML = '';
	map.clearOverlays();
	mlspottj = $('mlspottj').value;
	var prefix = mlspottj.substring(0,2);
	if(zoomlv[prefix] != ''){
		map.setCenter(new GLatLng(defltlat, defltlng), zoomlv[prefix]);
	}
	offset = 0;
	receiveJson();
}

/* 検索処理・Takt記事 */
function flgMlspottakt(){
	$('mlspottj').value = '';
	mlspottj = '';
	$('mglid').value = '';
	mglid = '';
	$('mgmid').value = '';
	mgmid = '';
	$('mlarea').value = '';
	mlarea = '';
	plural_mgmid = '';
	$('terms').innerHTML = '';
	map.clearOverlays();
	mlspottakt = $('mlspottakt').value;
	var prefix = mlspottakt.substring(0,2);
	if(zoomlv[prefix] != ''){
		map.setCenter(new GLatLng(defltlat, defltlng), zoomlv[prefix]);
	}
	offset = 0;
	receiveJson();
}

/* 表示切換処理・地図エリア */
function flgMlarea(){
	$('mlspottakt').value = '';
	mlspottakt = '';
	$('mlspottj').value = '';
	mlspottj = '';
	mlarea = $('mlarea').value;
	sparea = mlarea.split('^');
	map.setCenter(new GLatLng(sparea[0], sparea[1]), parseInt(sparea[2]));
}

/* 検索処理・大ジャンル */
function flgMglid(){
	$('mgmid').value = '';
	mgmid = '';
	$('mlspottakt').value = '';
	mlspottakt = '';
	$('mlspottj').value = '';
	mlspottj = '';
	plural_mgmid = '';
	$('terms').innerHTML = '';
	map.clearOverlays();
	mglid = $('mglid').value;
	offset = 0;
	receiveJson();
}

/* 検索処理・中ジャンル */
function flgMgmid(){
	$('mglid').value = '';
	$('mlspottakt').value = '';
	mlspottakt = '';
	$('mlspottj').value = '';
	mlspottj = '';
	mglid = '';
	plural_mgmid = '';
	$('terms').innerHTML = '';
	map.clearOverlays();
	mgmid = $('mgmid').value;
	offset = 0;
	receiveJson();
}

/* 初期状態に戻す */
function setDefault(){
	$('mglid').value = '';
	$('mgmid').value = '';
	$('mlspottakt').value = '';
	mlspottakt = '';
	$('mlspottj').value = '';
	mlspottj = '';
	mglid = '';
	plural_mgmid = '';
	$('terms').innerHTML = '';
	map.clearOverlays();
	offset = 0;
	receiveJson();
}

/* ajax.responders */
var myGlobalHandlers = {
	onCreate: function(){
		Element.show('nowloading');
	},

	onComplete: function() {
		if(Ajax.activeRequestCount == 0){
			Element.hide('nowloading');
		}
	}
};
Ajax.Responders.register(myGlobalHandlers);

/* デバッグ用 */
function trace(str){
	$('debug').innerHTML = str +"<br />"+ $('debug').innerHTML;
}