document.write('<script language="JavaScript" src="/common/prototype.js"></script>');

var map;
var maps;
var gMarkers = [];

var gCategory = [];
var gArea = '';
var gMarkCnt = 0;

////////// イベント //////////
window.onresize=_resize;

function initialize() {

	maps = google.maps
	
	if (maps.BrowserIsCompatible()) {
	} else {
		return;
	}
		
	_resize();

	// map内 検索バー(adsense付き)
	var wOption =	{googleBarOptions : 	{	style 		: 'new',
												adsOptions	: 	{
																	client	: 'ca-pub-1143705882337292',
																	adsafe	: 'high',
																	channel	: '5888252724',
																	language: 'ja'
																}      
											}
					}

	map = new maps.Map2(document.getElementById('map'), wOption);

	// 中心
	zoom_area();

	// ズームと地図タイプ変更のコントロールを追加する
	map.addControl(new maps.LargeMapControl3D());

	map.addControl(new maps.OverviewMapControl());

	// マウスホール拡大縮小 有効
	map.enableScrollWheelZoom();
		
	// 地図種別 (航空写真など)
	map.addControl(new maps.MapTypeControl());

	// map内検索バーを有効に
	map.enableGoogleBar();

	// map内 Google Adsense 表示
	var wAdOptions = {
		maxAdsOnMap : 3,
		style: 'adunit',
		channel: '5888252724'
	};
	
	wAdmanager = new maps.AdsManager(map, 'ca-pub-1143705882337292', wAdOptions);
	wAdmanager.enable();

	// スポット表示
	display_spot();
	
	// 中心点表示
	_display_center();
}

function _display_center() {

	var wIcon = new google.maps.Icon();
	wIcon.image = "http://www.mantentosa.com/images/icn_map_center.png";
	wIcon.iconSize = new google.maps.Size(30, 30);
	wIcon.iconAnchor = new google.maps.Point(15, 15);
	
	// 中心マーク表示
	var wMarker = new maps.Marker(map.getCenter(), {zIndexProcess	: function(){return gMarkCnt + 1},
													icon			: wIcon,
													clickable 		: false}
								);
								
	map.addOverlay(wMarker);
	
	// 地図を移動　中心点
	maps.Event.addListener(map, "move", function(){
											wMarker.setPoint(map.getCenter());
											}
							);
	

}

function display_spot() {

	var wMenu_html = "";
	
	for (var i = 0; i < gMarkers.length; i++) {
		gMarkers[i].hide();
	}

	gArea = document.getElementById("combo_area").value;
	gCategory = document.getElementsByName("category");

	new Ajax.Request('spot.xml', 
		{
			method: "get", 
			asynchronous:false,
			onComplete: function(wObj)
			{
				var wXml = wObj.responseXML;
				var wItems = wXml.getElementsByTagName('item');

				for (var i = 0; i < wItems.length; i++) {
					
					var wCategorys;
					var wCategory  = [];
					var wArea;
					
					try {wCategorys = wXml.getElementsByTagName("category")[i].firstChild.nodeValue;} catch (e) {wCategory = '';}
					
					wCategory = wCategorys.split('/');
					
					var wSpotF = false;
					for (var j = 0; j < gCategory.length; j++) {
						if (gCategory[j].checked) {
							for (var k = 0; k < wCategory.length; k++) {
								if (gCategory[j].value == wCategory[k]) {
									wSpotF = true;
									break;
								}
							}
							if (wSpotF) {
								break;
							}
						}
					}
					if (wSpotF) {
					} else {
						continue;
					}

					try {wArea = wXml.getElementsByTagName("area")[i].firstChild.nodeValue;} catch (e) {wArea = '';}
					
					if (gArea != '') {
						if (gArea != wArea) {
							continue;
						}
					}

					var wInfos = new Array();
					// データが空文字の場合、エラーとなるのでtry-catch
					try {wInfos['id'] = wXml.getElementsByTagName("id")[i].firstChild.nodeValue;} catch (e) {wInfos['id'] = 0;}
					try {wInfos['name'] = wXml.getElementsByTagName("name")[i].firstChild.nodeValue;} catch (e) {wInfos['name'] = '';}
					try {wInfos['kana'] = wXml.getElementsByTagName("kana")[i].firstChild.nodeValue;} catch (e) {wInfos['kana'] = '';}
					try {wInfos['post'] = wXml.getElementsByTagName("post")[i].firstChild.nodeValue;} catch (e) {wInfos['post'] = '';}
					try {wInfos['address'] = wXml.getElementsByTagName("address")[i].firstChild.nodeValue;} catch (e) {wInfos['address'] = '';}
					try {wInfos['tel'] = wXml.getElementsByTagName("tel")[i].firstChild.nodeValue;} catch (e) {wInfos['tel'] = '';}
					try {wInfos['fax'] = wXml.getElementsByTagName("fax")[i].firstChild.nodeValue;} catch (e) {wInfos['fax'] = '';}
					try {wInfos['homepage'] = wXml.getElementsByTagName("homepage")[i].firstChild.nodeValue;} catch (e) {wInfos['homepage'] = '';}
					try {wInfos['link'] = wXml.getElementsByTagName("link")[i].firstChild.nodeValue;} catch (e) {wInfos['link'] = '';}
					try {wInfos['email'] = wXml.getElementsByTagName("email")[i].firstChild.nodeValue;} catch (e) {wInfos['email'] = '';}
					try {wInfos['latitude'] = wXml.getElementsByTagName("latitude")[i].firstChild.nodeValue;} catch (e) {wInfos['latitude'] = 0;}
					try {wInfos['longitude'] = wXml.getElementsByTagName("longitude")[i].firstChild.nodeValue;} catch (e) {wInfos['longitude'] = 0;}
					try {wInfos['opentime'] = wXml.getElementsByTagName("opentime")[i].firstChild.nodeValue;} catch (e) {wInfos['opentime'] = '';}
					try {wInfos['holiday'] = wXml.getElementsByTagName("holiday")[i].firstChild.nodeValue;} catch (e) {wInfos['holiday'] = '';}
					try {wInfos['icon'] = wXml.getElementsByTagName("icon")[i].firstChild.nodeValue;} catch (e) {wInfos['icon'] = 'http://labs.google.com/ridefinder/images/mm_20_red.png';}
					try {wInfos['memo'] = wXml.getElementsByTagName("memo")[i].firstChild.nodeValue;} catch (e) {wInfos['memo'] = '';}

					map.addOverlay(CreateMarker(wInfos));
				}

				document.getElementById("menu").innerHTML = wMenu_html;
				
			}
		}
		);

	function CreateMarker(pInfos) {
	
		var wImg = new Image();
		wImg.src = pInfos['icon'];	// アイコンの幅・高さを取得するため
		
		var wIcon = new maps.Icon();
		wIcon.image = pInfos['icon'];
		wIcon.shadow="http://labs.google.com/ridefinder/images/mm_20_shadow.png";
		wIcon.iconSize=new maps.Size(wImg.width, wImg.height);
		wIcon.shadowSize=new maps.Size(22,20);
		wIcon.iconAnchor=new maps.Point(6,20);
		wIcon.infoWindowAnchor=new maps.Point(6,1);
		wIcon.infoShadowAnchor=new maps.Point(13,13);

		var wOption = {icon:wIcon};

//		var wOption = {};
		var wLatLng = new maps.LatLng(pInfos['latitude'], pInfos['longitude']);

		var marker = new maps.Marker(wLatLng, wOption);

		maps.Event.addListener(marker, "click", function() {
													//map.setCenter(wLatLng);
													marker.openInfoWindowHtml(_set_infowindow_html(pInfos));
												}
								);
		gMarkers[gMarkCnt] = marker;

		wMenu_html += '<img src="' + wImg.src + '" width="' + wImg.width + '" height="' + wImg.height + '">'
					+ '<a href="javascript:_menu_click(' + gMarkCnt + ')">&nbsp' + pInfos['name'] + '</a><br><br>';

		gMarkCnt++;
		return marker;
	}


	function _set_infowindow_html(pInfos) {

		var wName = '<b>'
				  + (pInfos['link'] != '' ? '<a href="' + pInfos['link'] + '">' : '') 
				  + pInfos['name'] + ' (' + pInfos['kana'] + ')'
				  + (pInfos['link'] != '' ? '</a>' : '')
				  + '</b>'
				  + '<br><br>';
		var wPost = (pInfos['post'] != '') ? '〒 ' + pInfos['post'] + '<br>' : '';
		var wAddress = (pInfos['address'] != '') ? pInfos['address'] + '<br>' : '';
		var wTel =  (pInfos['tel'] != '') ? 'TEL：　' + pInfos['tel'] + '<br>' : '';
		var wFax =  (pInfos['fax'] != '') ? 'FAX：　' + pInfos['fax'] + '<br>' : '';
		var wUrl =  (pInfos['homepage'] != '') ? 'URL：　<a href="' + pInfos['homepage'] + '" target="_blank">' + pInfos['homepage'] + '</a>' + '<br>' : '';
		var wEmail = (pInfos['email'] != '') ? 'E-mail： <a href="mailto:' + pInfos['email'] + '">' + pInfos['email'] + '</a>' + '<br>' : '';
		var wOpentime = (pInfos['opentime'] != '') ? '営業時間：　' + pInfos['opentime'] + '<br>' : '';
		var wHoliday = (pInfos['holiday'] != '') ? '定休日：　' + pInfos['holiday'] + '<br>' : '';
		var wMemo = (pInfos['memo'] != '') ? '備考：　' + pInfos['memo'] + '<br>' : '';
		var wNavi = '<a href="http://www.mantentosa.com/common/YSGoogleNavi.php?to=' + pInfos['latitude'] + ',' + pInfos['longitude'] + '" target="_blank">'
				  + '<img src="http://www.mantentosa.com/images/icn_nav.png" alt="ナビ" border="0">'
				  + '</a>';
		
		return wName
				+ wPost
				+ wAddress
				+ wTel
				+ wFax
				+ wUrl
				+ wEmail
				+ wOpentime
				+ wHoliday
				+ wMemo
				+ wNavi;
	}
}

function zoom_area() {

	gArea = document.getElementById("combo_area").value;

	new Ajax.Request('area.xml', 
		{
			method: "get", 
			asynchronous:false,
			onComplete: function(wObj)
			{
				var wXml = wObj.responseXML;
				var wItems = wXml.getElementsByTagName('item');

				for (var i = 0; i < wItems.length; i++) {
					
					var wInfos = new Array();

					try {wInfos['area'] = wXml.getElementsByTagName("area")[i].firstChild.nodeValue;} catch (e) {wInfos['area'] = '';}
					
					if (gArea == wInfos['area']) {
						// データが空文字の場合、エラーとなるのでtry-catch
						try {wInfos['latitude'] = wXml.getElementsByTagName("latitude")[i].firstChild.nodeValue;} catch (e) {wInfos['latitude'] = 0;}
						try {wInfos['longitude'] = wXml.getElementsByTagName("longitude")[i].firstChild.nodeValue;} catch (e) {wInfos['longitude'] = 0;}
						try {wInfos['zoom'] = wXml.getElementsByTagName("zoom")[i].firstChild.nodeValue;} catch (e) {wInfos['zoom'] = 14;}
						
						var wLatLng = new maps.LatLng(wInfos['latitude'], wInfos['longitude']);
						var wZoom = parseInt(wInfos['zoom']);

						map.setCenter(wLatLng, wZoom);

						break;
					}

				}
			}
		}
		);
}

function _menu_click(i) {
	maps.Event.trigger(gMarkers[i], "click");
}

function _resize() {
	
	var wMenu = document.getElementById('menu');
	var wMap = document.getElementById('map');
	var wWidth;
	var wHeight;

	if (window.innerWidth) {
		wWidth = window.innerWidth;
		wHeight=window.innerHeight;
	} else if (document.documentElement && document.documentElement.clientWidth) {
		wWidth=document.documentElement.clientWidth;
		wHeight=document.documentElement.clientHeight;
	} else if (document.body && document.body.clientWidth) {
		wWidth=document.body.clientWidth;
		wHeight=document.body.clientHeight;
	}
	
	wWidth -= 250; // 左メニュー分
	wWidth -= 155; // 右天気分
	if (wWidth < 0) {
		wWidth = 0;
	}
	wMap.style.width = wWidth + "px";

	wHeight -= 120; // ヘッダー分小さく
	if (wHeight < 0) {
		wHeight = 0;
	}
	wMenu.style.height = wHeight + "px";
	
	wHeight -= 200; // 広告分小さく
	wMap.style.height = wHeight + "px";

}
