//ぐるなび
var ioffsetpage = 1;
var callback_gnavi = {};
callback_gnavi.onload = function(res){
	var oshopname;
	var oshop;
	var shtml = '';
	var shtmlimg = '';
	var shtmldescription = '';
	var latlng = null;
	var idx = 1;

	document.getElementById('gnavi').innerHTML = '';

	if(res.error){
		//res.error.code
		//600:情報が見つからない
		//601:認証エラー
		//602:不正なぐるなび店舗ID
		//603:不正な取得種別
		//604:処理中にエラーが発生
		shtml += 'グルメ情報がありませんでした。(err:'+res.error.code+')';

		document.getElementById('gnavi').style.display = 'none';
		document.getElementById('ads1').style.display = 'block';
	}else if(!res.rest){
		shtml = 'グルメ情報がありませんでした。';
		shtmlimg = '';
		shtmldescription = '';

		document.getElementById('gnavi').style.display = 'none';
		document.getElementById('ads1').style.display = 'block';
	} else {
		for(var oshopname in res.rest) {
			oshop = eval('res[\'rest\'][\'' + oshopname + '\']');
			shtmlimg = '';
			shtmldescription = '';

			//img
			if(oshop.image_url){
				if(oshop.image_url.shop_image1){
					shtmlimg += '<a href="' + oshop.url + '" target="_blank">';

					if(!String(oshop.image_url.shop_image1).match(/object/i)){
						shtmlimg += '<img src="' + oshop.image_url.shop_image1 + '" alt="' + oshopname + '" width="90" height="90" border="1" style="border:1px solid #cccccc;"/>';
					}else{
						shtmlimg += '<img src="' + oshop.image_url.shop_image2 + '" alt="' + oshopname + '" width="90" height="90" border="1" style="border:1px solid #cccccc;"/>';
					}

					shtmlimg += '</a>';
					shtmlimg += '<p style="font-size:9px; line-height:9px;">提供：ぐるなび</p>';
				}
			}
			//description
			shtmldescription += '<div style="margin-bottom:5px;">';
			shtmldescription += '<img src="/img/mapicon/' + idx + '.png" align="left" />';
			shtmldescription += '<h2 style="margin-bottom:5px;font-weight:bold;">' + oshopname.replace('<BR>','&nbsp;') + '</h2>';
			if(oshop.category)shtmldescription += '<p>[種類］&nbsp;' + oshop.category + '</p>';
			shtmldescription += '<div style="clear:both;"></div>';
			shtmldescription += '</div>';
			if(oshop.budget)shtmldescription += '<p>[平均予算]&nbsp;' + oshop.budget + '円</p>';
			if(oshop.pr)shtmldescription += '<p style="margin-bottom:5px;">[ＰＲ]&nbsp;' + oshop.pr.pr_short + '</p>';
			if(oshop.url)shtmldescription += '<p style="margin-bottom:5px;"><a href="/gnavilink.asp?url=' + escape(oshop.url) + '" target="_blank">詳しくはこちら</a></p>';

			if(shtmlimg.length > 0){
				shtml += '<div style="margin-bottom:5px; border:1px solid #cccccc;">';
				shtml += '<div style="padding:3px;">';

				shtml += '<div style="float:left; width:88px;">';
				shtml += shtmlimg;
				shtml += '</div>'

				shtml += '<div style="float:right; width:375px;">';
				shtml += shtmldescription;
				shtml += '</div>';

				shtml += '<div style="clear:both;"></div>';
				shtml += '</div>';
				shtml += '</div>';
			}else{
				shtml += '<div style="margin-bottom:5px; border:1px solid #cccccc;">';
				shtml += '<div style="padding:3px;">';
				shtml += shtmldescription;
				shtml += '</div>';
				shtml += '</div>';
			}
			latlng = new GPoint(oshop.longitude, oshop.latitude);

			omap.addOverlay(createMarker(latlng, '/img/mapicon/' + idx + '.png', ''));
	
			idx++;
		}

		//page
		shtml += '<div style="margin-bottom:5px;text-align:center;">';
		for(var ipage = 1; ipage <= Math.floor(res.total_hit_count / res.hit_per_page); ipage++){
			if(ipage == ioffsetpage){
				shtml += '［' + ipage + '］';
			}else{
				shtml += '［<span style="color:#0045f9;cursor:pointer;" onclick="ioffsetpage=' + ipage + ';gnavi_read();">' + ipage + '</span>］';
			}
			shtml += '';
		}
		shtml += '<span style="""">';
		shtml += '</div>';

		shtml += '<div style="text-align:center;">Powered by <a href="http://www.gnavi.co.jp/">ぐるなび</a></div>'
		document.getElementById('gnavi').innerHTML = shtml;

		document.getElementById('gnavi').style.display = 'block';
		document.getElementById('ads1').style.display = 'none';
	}

	res = null;
}

function gnavi_read(){
	omap.clearOverlays();

	var gnavi_url = 'http://api.gnavi.co.jp/ver1/RestSearchAPI/?keyid=1815c9a7e8d515c4bfef092209b8e13e&longitude='+omap.getCenter().x+'&input_coordinates_mode=2&coordinates_mode=2&latitude='+omap.getCenter().y+'&range=1&hit_per_page=5';
	if(ioffsetpage)gnavi_url += '&offset_page='+ioffsetpage;

	xml2Json(gnavi_url, 'callback_gnavi');
}
