﻿// add marker(サブカテゴリ）------------------------------------------------------------
function addCategoriesSub(mainCatName,subCatName){
	var thisScName;
	map.clearOverlays();
	var thisMainCat = xml.getElementsByTagName('category');
	for(var i=0;i<thisMainCat.length;i++){
		if(mainCatName==thisMainCat[i].getAttribute('id')){
			var thisSubCat = thisMainCat[i].getElementsByTagName('subCat');
			for(var il=0;il<thisSubCat.length;il++){
				if(thisSubCat[il].getAttribute('id')==subCatName){
					thisScName = thisSubCat[il].getAttribute('name');
					var currentMarkers = thisSubCat[il].getElementsByTagName('marker');
					for(var im=0;im<currentMarkers.length;im++){
						
						// infoHTMLがある場合
						var thisInfoHtml = currentMarkers[im].getElementsByTagName('infoHtml');
						if(thisInfoHtml.length){
							var markerHTML = thisInfoHtml[0].firstChild.nodeValue;
							while(markerHTML.indexOf('\\n') != -1){markerHTML = markerHTML.replace('\\n','');}
						} else {
							var markerHTML = "";
						}
						
						// 記事ページがある場合
						var specialNodes = currentMarkers[im].getElementsByTagName('special')
						if(specialNodes.length){
							var thisURL = specialNodes[0].getAttribute('href');
							if(articleLinkHTML==""){articleLinkHTML = thisURL;}
							
							thisURL = '<div class="'+map_info_link+'"><a href="'+thisURL+'" title="'+articleLinkTitle+'">'+articleLinkHTML+'</a></div>';
							var iconName = "special";
						}else{
							var thisURL = "";
							var iconName = mainCatName;
						}
						// マーカー名
						thisMarkerName = currentMarkers[im].getAttribute('name');
						
						// 座標
						var markerY = currentMarkers[im].getAttribute('lat');
						var markerX = currentMarkers[im].getAttribute('lng');
						
						// 印刷ボタン
						if(showPrintButton!=""){var printButton = '<div class="map_info_print"><a href="#" onClick="openPrintWindow(\''+escape(thisMarkerName)+'\',\''+escape(thisScName)+'\',\''+markerX+'\',\''+markerY+'\',\''+escape(markerHTML)+'\');return false">'+printButtonHTML+'</a></div>';}
						else{var printButton ="";}
						
						var markerName = '<div class="'+map_info_name+' map_info_'+mainCatName+'">'+thisMarkerName+'</div><div class="'+map_info_html+'">'+markerHTML+'</div>'+thisURL+printButton;

						
						addMarker(markerX,markerY,markerName,iconName);
					}
				}
			}
		}
	}
	if(isUseJSdefault){
		permalink(mainCatName,subCatName,thisScName);
	}
}
// add marker(メインカテゴリ）------------------------------------------------------------
function addCategoriesMain(mainCatName){
	map.clearOverlays();
	var thisMainCat = xml.getElementsByTagName('category');
	for(var i=0;i<thisMainCat.length;i++){
		if(mainCatName==thisMainCat[i].getAttribute('id')){
			var thisSubCat = thisMainCat[i].getElementsByTagName('subCat');
			for(var il=0;il<thisSubCat.length;il++){
				var currentMarkers = thisSubCat[il].getElementsByTagName('marker');
				for(var im=0;im<currentMarkers.length;im++){
					var markerName = currentMarkers[im].getAttribute('name');
					var markerY = currentMarkers[im].getAttribute('lat');
					var markerX = currentMarkers[im].getAttribute('lng');
					addMarker(markerX,markerY,markerName,mainCatName);
				}
			}
		}
	}
}
// special --------------------------------------------------------------------------------
function spMarker(){
	map.clearOverlays();
	var special = xml.getElementsByTagName('special');
	for(i=0;i<special.length;i++){
		var thisURL = special[i].parentNode.parentNode.getElementsByTagName('special')[0].getAttribute('href');
		
						// マーカー名
						thisMarkerName = special[i].parentNode.getAttribute('name');
		
		var markerName = '<div class="'+map_info_name+' map_info_'+special[i].parentNode.parentNode.parentNode.getAttribute('id')+'">'+special[i].parentNode.getAttribute('name')+'</div>';
		var markerHTML = '<div class="'+map_info_html+'">'+special[i].parentNode.getElementsByTagName('infoHtml')[0].firstChild.nodeValue+'</div>';
		while(markerHTML.indexOf('\\n') != -1){markerHTML = markerHTML.replace('\\n','');}
		
		if(articleLinkHTML!=""){var linkedHTML = articleLinkHTML;}else{linkedHTML = thisURL;}
		var markerURL = '<div class="'+map_info_link+'"><a href="'+thisURL+'" title="'+articleLinkTitle+'">'+linkedHTML+'</a></div>';
		
		var thisScName = special[i].parentNode.parentNode.getAttribute('name')
		
						// 座標
						var markerY = special[i].parentNode.getAttribute('lat');
						var markerX = special[i].parentNode.getAttribute('lng');
		
						// 印刷ボタン
						if(showPrintButton!=""){var printButton = '<div class="map_info_print"><a href="#" onClick="openPrintWindow(\''+escape(thisMarkerName)+'\',\''+escape(thisScName)+'\',\''+markerX+'\',\''+markerY+'\',\''+escape(markerHTML)+'\');return false">'+printButtonHTML+'</a></div>';}
						else{var printButton ="";}
		
		addMarker(markerX, markerY,markerName+markerHTML+markerURL+printButton,"special");
	}
	
	if(isUseJSdefault){permalink();}
}

///////////////////////////////////////////////////////////////////////////////////////////

// マーカー追加----------------------------------------------------------------------------
function addMarker(makerX,makerY,makerHTML,markerIcon){
	//console.log(makerHTML);
	var point = new GPoint(makerX,makerY);
	
	var ico = new GIcon();
	ico.image = iconDir+markerIcon+".png";
	ico.printImage = iconDir+markerIcon+".gif";
	ico.mozPrintImage = iconDir+markerIcon+".gif";
	
	// スペシャルカテゴリ用アイコン
	if(markerIcon == "special"){
		icoWidth = markerImgSizeSpecial[0];
		icoHeight = markerImgSizeSpecial[1]
		ico.iconSize = new GSize(icoWidth,icoHeight);
		ico.iconAnchor = new GPoint(Math.round(icoWidth/2),icoHeight);
		ico.infoWindowAnchor = new GPoint(Math.round(icoWidth/2),0);
		ico.shadow = iconDir+"shadow_sp.png"
		ico.shadowSize = new GSize(40,32);
	
	// 通常カテゴリ用アイコン
	} else {
		icoWidth = markerImgSizeNormal[0];
		icoHeight = markerImgSizeNormal[1]
		ico.iconSize = new GSize(icoWidth,icoHeight);
		ico.iconAnchor = new GPoint(Math.round(icoWidth/2),icoHeight);
		ico.infoWindowAnchor = new GPoint(Math.round(icoWidth/2),0);
		ico.shadow = iconDir+"shadow.png"
		ico.shadowSize = new GSize(37,32);
	}
	var marker = new GMarker(point, ico);
	
	
	GEvent.addListener(marker, "click", function(overlay, point) {
		marker.openInfoWindowHtml(makerHTML);
		}
	);
	

	
	map.addOverlay(marker);
}
// Icon -----------------------------------------------------------------------------------
function icon(iconCat){
	iconObj = new GIcon();
	iconObj.image = iconDir + iconCat+".png";
	iconObj.iconSize = new GSize(21,32);
	iconObj.iconAnchor = new GPoint(0,0);
	return iconObj
}
function escapeEntity(text){
	text = text.replace(/&/g,"&amp;");
	text = text.replace(/\"/g,"&quot;");
	text = text.replace(/</g,"&lt;");
	text = text.replace(/>/g,"&gt;");
	return text;
}
function openPrintWindow(name,category,y,x,html){
	
	window.open('http://www.tsunashima.info/mapmt/map/print.html?'+name+'&'+category+'&'+y+'&'+x+'&'+html,'printWin','width=800,height=750,location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=yes');
}
