/*
======== table of content. =================================

summary: show GoogleMap

============================================================
*/

new function() {
	
	var map = null;
	var target = null;
	var guideMarker = null;
	var guideLine = null;
	var parkingMarkers = [];
	
	var areaID = 'gmap';
	var defaultCenter = new google.maps.LatLng( 34.680676647278, 135.51425725221634 );
	var defaultZoom = 17;
	var mapIcon = './images/gmap_icon_jeia.png';
	var mapShadow = './images/gmap_icon_jeia_s.png';
	var iconTitle = '(株)日本電子工藝社';
	var iconLatLng = new google.maps.LatLng( 34.680676647278, 135.51425725221634 );
	var iconSize = new google.maps.Size( 55, 75 );
	var iconOrigin = new google.maps.Point( 0, 0 );
	var iconAnchor = new google.maps.Point( 28, 74 );
	var shadowSize = new google.maps.Size( 92, 75 );
	
	var gIcon = new google.maps.MarkerImage(
		'http://maps.google.co.jp/mapfiles/ms/icons/red.png',
		new google.maps.Size( 32, 32 )
	);
	var gShadow = new google.maps.MarkerImage(
		'http://maps.google.co.jp/mapfiles/ms/icons/msmarker.shadow.png',
		new google.maps.Size( 32, 32 )
	);
	
	var pIcon = new google.maps.MarkerImage(
		'./images/gmap_icon_parking.png',
		new google.maps.Size( 36, 51 ), // size
		new google.maps.Point( 0, 0 ), // origin
		new google.maps.Point( 25, 50 ) // anchor
	);
	
	var guideClass = 'map_guide';
	var lineColor = '#FF0000';
	var lineOpacity = 0.7;
	var lineWidth = 3;
	
	if ( window.addEventListener ) {
		window.addEventListener( 'load', init, false );
	}
	else {
		window.attachEvent( 'onload', init );
	}
	
	/**
	 * 初期化
	 */
	function init() {
		
		// マップ生成
		var mapOption = {
			zoom: defaultZoom,
			center: defaultCenter,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			mapTypeControl: false
		}
		
		map = new google.maps.Map( document.getElementById( areaID ), mapOption );
		
		// マーカー生成
		var markerOption = {
			map: map,
			title:iconTitle,
			position: iconLatLng,
			icon: new google.maps.MarkerImage( mapIcon, iconSize, iconOrigin, iconAnchor ),
			shadow: new google.maps.MarkerImage( mapShadow, shadowSize, iconOrigin, iconAnchor )
		}
		
		var marker = new google.maps.Marker( markerOption );
		
		// クリックイベント
		$( '.' + guideClass ).
			click( onClickGuide );
	}
	
	/**
	 * ガイド表示
	 */
	function onClickGuide( e ) {
		
		// リセット
		if ( guideMarker != null ) {
			guideMarker.setMap( null );
			guideMarker = null;
		}
		
		if ( guideLine != null ) {
			guideLine.setMap( null );
			guideLine = null;
		}
		
		resetParking();
		
		// 選択状態
		$( '#access_route .route' ).removeClass( 'selected' );
		$(this).parent().parent().find( '.route' ).addClass( 'selected' );
		
		// ガイド振り分け
		var key = $(this).attr( 'href' ).substr( 1 );
		
		switch ( key ) {
			case 'view-guide-1':
				showGuide1();
				break;
			case 'view-guide-2':
				showGuide2();
				break;
			case 'view-guide-3':
				showGuide3();
				break;
			case 'view-guide-4':
				showGuide4();
				break;
			default:
				break;
		}
		
		return false;
	}
	
	/**
	 * ガイド表示１
	 * 電車でお越しの方(地下鉄谷町線 谷町4丁目駅下車 8番出口 徒歩5分)
	 */
	function showGuide1() {
		
		// 画面
		var center = new google.maps.LatLng( 34.68109307802122 , 135.51575124263763 );
		map.setCenter( center );
		map.setZoom( 18 );
		
		// ガイド
		var title = '谷町4丁目駅 8番出口';
		var start = new google.maps.LatLng( 34.681170717451636 , 135.51676511764526 );
		var route = [
			start,
			new google.maps.LatLng( 34.68127438358711 , 135.51435381174088 ),
			new google.maps.LatLng( 34.68067885295496 , 135.51430016756058 ),
			iconLatLng
		];
		
		// マーカー生成
		var markerOption = {
			map: map,
			title: title,
			position: start,
			icon: gIcon,
			shadow: gShadow
		};
		guideMarker = new google.maps.Marker( markerOption );
		
		// ルート表示
		var lineOption = {
			map: map,
			path: route,
			strokeColor: lineColor,
			strokeOpacity: lineOpacity,
			strokeWeight: lineWidth
		};
		guideLine = new google.maps.Polyline( lineOption );
	}
	
	/**
	 * ガイド表示２
	 * 電車でお越しの方(地下鉄中央線 谷町4丁目駅下車 8番出口 徒歩5分)
	 */
	function showGuide2() {
		
		// 画面
		var center = new google.maps.LatLng( 34.68109307802122 , 135.51575124263763 );
		map.setCenter( center );
		map.setZoom( 18 );
		
		// ガイド
		var title = '谷町4丁目駅 8番出口';
		var start = new google.maps.LatLng( 34.681170717451636 , 135.51676511764526 );
		var route = [
			start,
			new google.maps.LatLng( 34.68127438358711 , 135.51435381174088 ),
			new google.maps.LatLng( 34.68067885295496 , 135.51430016756058 ),
			iconLatLng
		];
		
		// マーカー生成
		var markerOption = {
			map: map,
			title: title,
			position: start,
			icon: gIcon,
			shadow: gShadow
		};
		guideMarker = new google.maps.Marker( markerOption );
		
		// ルート表示
		var lineOption = {
			map: map,
			path: route,
			strokeColor: lineColor,
			strokeOpacity: lineOpacity,
			strokeWeight: lineWidth
		};
		guideLine = new google.maps.Polyline( lineOption );
	}
	
	/**
	 * ガイド表示３
	 * 電車でお越しの方(地下鉄堺筋線 堺筋本町駅下車 3番出口 徒歩10分)
	 */
	function showGuide3() {
		
		// 画面
		var center = new google.maps.LatLng( 34.68115307213906 , 135.51083207130432 );
		map.setCenter( center );
		map.setZoom( 17 );
		
		// ガイド
		var title = '堺筋本町駅下車 3番出口';
		var start = new google.maps.LatLng( 34.68149715505616 , 135.50833493471146 );
		var route = [
			start,
			new google.maps.LatLng( 34.68147068719023 , 135.51038682460785 ),
			new google.maps.LatLng( 34.681389077883644 , 135.51211684942245 ),
			new google.maps.LatLng( 34.681389077883644 , 135.51211684942245 ),
			new google.maps.LatLng( 34.68127438358711 , 135.51435381174088 ),
			new google.maps.LatLng( 34.68067885295496 , 135.51430016756058 ),
			iconLatLng
		];
		
		// マーカー生成
		var markerOption = {
			map: map,
			title: title,
			position: start,
			icon: gIcon,
			shadow: gShadow
		};
		guideMarker = new google.maps.Marker( markerOption );
		
		// ルート表示
		var lineOption = {
			map: map,
			path: route,
			strokeColor: lineColor,
			strokeOpacity: lineOpacity,
			strokeWeight: lineWidth
		};
		guideLine = new google.maps.Polyline( lineOption );
	}
	
	/**
	 * ガイド表示４
	 * 自動車でお越しの方(谷町筋と中央大通りの交差点（谷町4）を西へ)
	 */
	function showGuide4() {
		
		// 画面
		var center = new google.maps.LatLng( 34.6809258884037 , 135.5150270462036 );
		map.setCenter( center );
		map.setZoom( 17 );
		
		// ガイド
		var title = '谷町4 交差点';
		var start = new google.maps.LatLng( 34.681214830716634 , 135.51717281341553 );
		var route = [
			start,
			new google.maps.LatLng( 34.681369226959085 , 135.5144101381302 ),
			new google.maps.LatLng( 34.680676647278 , 135.51436185836792 ),
			iconLatLng
		];
		
		// マーカー生成
		var markerOption = {
			map: map,
			title: title,
			position: start,
			icon: gIcon,
			shadow: gShadow
		};
		guideMarker = new google.maps.Marker( markerOption );
		
		// ルート表示
		var lineOption = {
			map: map,
			path: route,
			strokeColor: lineColor,
			strokeOpacity: lineOpacity,
			strokeWeight: lineWidth
		};
		guideLine = new google.maps.Polyline( lineOption );
		
		// パーキング表示
		showParking();
	}
	
	/**
	 * パーキング表示
	 */
	function showParking() {
		
		var title = '有料駐車場';
		var positions = [
			new google.maps.LatLng( 34.68041858266704 , 135.51694750785828 ),
			new google.maps.LatLng( 34.681115575837325 , 135.51629304885864 ),
			new google.maps.LatLng( 34.680756051611844 , 135.51425725221634 ),
			new google.maps.LatLng( 34.6806413564385 , 135.51269352436066 )
		];
		
		// マーカー生成
		var markerOption = {
			map: map,
			title: title,
			icon: pIcon
		};
		
		var len = positions.length;
		
		for ( var i = 0; i < len; i ++ ) {
			markerOption[ 'position' ] = positions[ i ];
			parkingMarkers.push( new google.maps.Marker( markerOption ) );
		}
	}
	
	function resetParking() {
		
		var len = parkingMarkers.length;
		
		for ( var i = 0; i < len; i ++ ) {
			parkingMarkers[ i ].setMap( null );
		}
		
		parkingMarkers = [];
	}
}

