$(document).ready(function() {
	$('#photos').galleryView({
		panel_width: 709,
		panel_height: 230,
		frame_width: 96,
		frame_height: 36
	});
	
	$('.SDF2').css("background","none");

	$('.SUTab1 li.JLiTab').click(function() {
		$('.SUTab1 li').each(function() {
		$(this).find("a").removeClass("ROn");
	});

	$('.SDTab1').each(function() {
		$(this).hide();
	});

	$(this).find("a").addClass("ROn");
		var objName = $(this).attr("id").replace("JLiTab","#JDTab");
		$(objName).show();
	});

	$(".JAZoom1").fancybox({});

	$("#JAModal1").fancybox({
		'autoScale': false,
		'transitionIn': 'none',
		'transitionOut': 'none',
		'type': 'iframe'
	});

	$("#JAModal2").fancybox({
		'autoScale': false,
		'transitionIn': 'none',
		'transitionOut': 'none',
		'type': 'iframe'
	});

	$("#JAModal3").fancybox({
		'autoScale': false,
		'transitionIn': 'none',
		'transitionOut': 'none',
		'type': 'iframe'
	});
});

window.onload = function() {
	var DIVs = document.getElementsByTagName('div');
	var MapCount = 0;
	for (var Count = 0; Count < DIVs.length; Count ++) {
		if (DIVs[Count].id.match(/^aagmap\d*$/)) {
			var Info = document.getElementById(DIVs[Count].id).innerHTML.split("|");
			CreateMap(DIVs[Count].id, Info);
		}
	}
}

function CreateMap(GmapID, Info) {
	/*
	Info
		0:住所（必須）
		1:緯度・経度指定
		2:情報ウィンドウ内テキスト
		3:縮尺レベル
		4:道のりスタート住所指定 or 緯度・経度指定
	*/

	Info[0] = Info[0].replace(/^\s*/, "");

	if (Info[1]) {
		// 座標指定
		var Lat = Info[1].replace(/(\d+\.\d+),\d+\.\d+/, "$1");
		var Lng = Info[1].replace(/\d+\.\d+,(\d+\.\d+)/, "$1");
		ViewMap(GmapID, new google.maps.LatLng(Lat, Lng), Info);
	} else {
		// ジオコーディング
		var Geocoder = new google.maps.Geocoder();
		Geocoder.geocode(
			{
				address: Info[0]
			},
			function(Results, Status) {
				if (Status == google.maps.GeocoderStatus.OK) {
					ViewMap(GmapID, Results[0].geometry.location, Info);
				} else {
					document.getElementById(GmapID).innerHTML = '現在、システムが混みあっています。';
					document.getElementById(GmapID).style.visibility = "visible";
				}
			}
		);
	}
}

// 地図表示処理
function ViewMap(GmapID, TargetLatLng, Info) {
	var ViewInfo = Info[2] || Info[0];
	var ZoomLevel = parseInt(Info[3]);
	ZoomLevel = isNaN(ZoomLevel) ? 16 : ZoomLevel;

	// 緯度
	var Lat = TargetLatLng.lat() + 0.0018;

	// 経度
	var Lng = TargetLatLng.lng();

	var MapLatLng = new google.maps.LatLng(Lat, Lng);

	// 地図作成
	var Map = new google.maps.Map(
		document.getElementById(GmapID),
		{
			zoom: ZoomLevel,
			center: MapLatLng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		}
	);

	// マーカー作成
	var Image = '/img/base/IconMaker.png';
	var Marker = new google.maps.Marker({
		map: Map,
		position: TargetLatLng,
		icon: Image
	});

	// 情報ウィンドウ作成
	var InfoWindow = new google.maps.InfoWindow(
		{
			content: ViewInfo
		}
	);

	// 経路処理
	if (Info[4]) {
		var StartLatLng;
		if (Info[4].match(/\d+\.\d+,\d+\.\d+/)) {
			// 座標指定
			var Lat = Info[4].replace(/(\d+\.\d+),\d+\.\d+/, "$1");
			var Lng = Info[4].replace(/\d+\.\d+,(\d+\.\d+)/, "$1");
			ViewRoute(Map, new google.maps.LatLng(Lat, Lng), TargetLatLng);
		} else {
			// ジオコーディング
			var Geocoder = new google.maps.Geocoder();
			Geocoder.geocode(
				{
					address: Info[4]
				},
				function(Results, Status) {
					if (Status == google.maps.GeocoderStatus.OK) {
						ViewRoute(Map, Results[0].geometry.location, TargetLatLng);
					} else {
						document.getElementById(GmapID).innerHTML = '現在、システムが混みあっています。';
						document.getElementById(GmapID).style.visibility = "visible";
					}
				}
			);
		}
	}

	// 地図表示
	document.getElementById(GmapID).style.visibility = "visible";

	// マーカークリック
	google.maps.event.addListener(Marker, 'click', function() {
		InfoWindow.open(Map, Marker);
	})

	InfoWindow.open(Map, Marker);
}

// 経路表示処理
function ViewRoute(Map, StartLatLng, TargetLatLng) {
	// 経路計算
	var DirectionsService = new google.maps.DirectionsService();
	DirectionsService.route(
		{
			origin: StartLatLng,
			destination: TargetLatLng,
			travelMode: google.maps.DirectionsTravelMode.WALKING
		},
		function(Result, Status) {
			if (Status == google.maps.DirectionsStatus.OK) {
				DirectionsDisplay.setDirections(Result);
			}
		}
	);

	// 経路表示
	var DirectionsDisplay = new google.maps.DirectionsRenderer();
	DirectionsDisplay.setMap(Map);
}


