
/**
 * Google map
 */
var map;

var areaNamePos = [];
areaNamePos[1] = [new google.maps.LatLng(35.70290764976886, 139.560670), new google.maps.LatLng(35.702509601306474, 139.560670)];
areaNamePos[2] = [new google.maps.LatLng(35.70328119275636, 139.560670), new google.maps.LatLng(35.70213496564417, 139.560670)];
areaNamePos[3] = [new google.maps.LatLng(35.70359701490806, 139.560670), new google.maps.LatLng(35.701818048636795, 139.560670)];
areaNamePos[4] = [new google.maps.LatLng(35.704010848937266, 139.560670), new google.maps.LatLng(35.70140529229514, 139.560670)];
areaNamePos[5] = [new google.maps.LatLng(35.70492454330489, 139.560670), new google.maps.LatLng(35.70049155759502, 139.560670)];
areaNamePos[6] = [new google.maps.LatLng(35.70682595236217, 139.560670), new google.maps.LatLng(35.69858890835393, 139.560670)];
areaNamePos[7] = [new google.maps.LatLng(35.71033461908178, 139.560670), new google.maps.LatLng(35.695080813833314, 139.560670)];
areaNamePos[8] = [new google.maps.LatLng(35.717579058881746, 139.560670), new google.maps.LatLng(35.687836507860396, 139.560670)];
areaNamePos[9] = [new google.maps.LatLng(35.72820333628705, 139.560670), new google.maps.LatLng(35.67721229656134, 139.560670)];
areaNamePos[10] = [new google.maps.LatLng(35.738707543845095, 139.560670), new google.maps.LatLng(35.666707065298695, 139.560670)];
areaNamePos[11] = [new google.maps.LatLng(35.747622894768476, 139.560670), new google.maps.LatLng(35.65779301286692, 139.560670)];

// cookieでmapTypes、zoomを各ページで維持
var mapMode;
var mapZoom;
var c = document.cookie;
c = c.split(";");
for(var i = 0; i < c.length; i++) {
	cookie = c[i].split("=");
	cookie[0] = cookie[0].replace(/ /g, "");
	if(cookie[0] == "mapMode") mapMode = cookie[1];
	if(cookie[0] == "mapZoom") mapZoom = cookie[1];
}
mapMode = mapMode || google.maps.MapTypeId.ROADMAP;
mapZoom = mapZoom || 15;

var stylez = [
  {
    featureType: "poi",
    elementType: "all",
    stylers: [
	  { lightness: -20 },
      { saturation: -40 }
    ]
  },{
    featureType: "landscape",
    elementType: "all",
    stylers: [
	  { lightness: -10 },
      { saturation: -60 },
      { gamma: 0.4 }
    ]
  },{
    featureType: "road",
    elementType: "all",
    stylers: [
      { lightness: -30 },
      { saturation: -70 }
    ]
  },{
    featureType: "transit",
    elementType: "all",
    stylers: [
      { lightness: -20 },
      { saturation: -40 }
    ]
  }
]
var styledMapOptions = {name:"Taiyokai-Walk"};
var taiyokaiMapType = new google.maps.StyledMapType(stylez, styledMapOptions);

function setMap(lat, lng) {
	// lat, lng. 初期値 = 三鷹駅
	lat = lat || 35.702708; 
	lng = lng || 139.560670;
	
	var latlng = new google.maps.LatLng(lat, lng);
   	var myOptions = {
		zoom:Number(mapZoom),
		center:latlng,
		//mapTypeId:google.maps.MapTypeId.ROADMAP,
		mapTypeControl:false,
		navigationControl:false,
		scaleControl:false,
		streetViewControl:false
    };
	
    map = new google.maps.Map(document.getElementById("map"), myOptions);
	
	/*var overlayBounds = new google.maps.LatLngBounds(
		new google.maps.LatLng(35.657793, 139.505387),
		new google.maps.LatLng(35.747618, 139.616189)
	);
	var areaOverlay = new google.maps.GroundOverlay("/images/area.png", overlayBounds);
	areaOverlay.setMap(map);
	*/
	
	for(var i = 1; i <= 11; i++) {
		new google.maps.Marker({
			position:areaNamePos[i][0],
			map:map,
			icon:new google.maps.MarkerImage('/images/markers/area_' + i + '_t.png', new google.maps.Size(80, 15), new google.maps.Point(0,0), new google.maps.Point(40, 8)),
			zIndex:i
		});
		new google.maps.Marker({
			position:areaNamePos[i][1],
			map:map,
			icon:new google.maps.MarkerImage('/images/markers/area_' + i + '_b.png', new google.maps.Size(80, 15), new google.maps.Point(0,0), new google.maps.Point(40, 7)),
			zIndex:i + 50
		});
	}


	if(mapMode == google.maps.MapTypeId.HYBRID) {
		$("#map_btn").attr("src", $("#map_btn").attr("src").replace("_act.", "_off."));
		$("#satellite_btn").attr("src", $("#satellite_btn").attr("src").replace("_off.", "_act."));
		map.setMapTypeId(google.maps.MapTypeId.HYBRID);
	} else {
		map.mapTypes.set('taiyokai', taiyokaiMapType);
		map.setMapTypeId('taiyokai');
	}
	//google.maps.event.addListener(map, 'dblclick', function(e) { alert(e.latLng.toString()); });
	google.maps.event.addListener(map, 'zoom_changed', function() { document.cookie = "mapZoom=" + map.getZoom() + "; path=/"; });
	
	drawCircle();
}

function drawCircle() {
	var center = new google.maps.LatLng(35.702708, 139.560670);
	//冥王星エリア
	new google.maps.Circle({
		center:center,
		fillOpacity:0,
		map:map,
		radius:5000,
		strokeColor:'#404243',
		strokeWeight:1
	});
	
	//海王星エリア
	new google.maps.Circle({
		center:center,
		fillOpacity:0,
		map:map,
		radius:4007.6,
		strokeColor:'#3b0387',
		strokeWeight:1
	});
	
	//天王星エリア
	new google.maps.Circle({
		center:center,
		fillOpacity:0,
		map:map,
		radius:2838.3,
		strokeColor:'#00619d',
		strokeWeight:1
	});
	
	//土星エリア
	new google.maps.Circle({
		center:center,
		fillOpacity:0,
		map:map,
		radius:1655.55,
		strokeColor:'#e83817',
		strokeWeight:1
	});
	
	//木星エリア
	new google.maps.Circle({
		center:center,
		fillOpacity:0,
		map:map,
		radius:849.1, 
		strokeColor:'#781a32',
		strokeWeight:1
	});
	
	//ケレスエリア
	new google.maps.Circle({
		center:center,
		fillOpacity:0,
		map:map,
		radius:458.55,
		strokeColor:'#c1ff12',
		strokeWeight:1
	});
	
	//火星エリア
	new google.maps.Circle({
		center:center,
		fillOpacity:0, 
		map:map,
		radius:246.85,
		strokeColor:'#ff008f',
		strokeWeight:1
	});
	
	//地球エリア
	new google.maps.Circle({
		center:center,
		fillOpacity:0,
		map:map,
		radius:145.2,
		strokeColor:'#0074f5',
		strokeWeight:1
	});
	
	//金星エリア
	new google.maps.Circle({
		center:center,
		fillOpacity:0,
		map:map,
		radius:99.15,
		strokeColor:'#995a31',
		strokeWeight:1
	});
	
	//水星エリア
	new google.maps.Circle({
		center:center,
		fillOpacity:0,
		map:map,
		radius:63.85,
		strokeColor:'#00deef',
		strokeWeight:1
	});
	
	//太陽エリア
	new google.maps.Circle({
		center:center,
		fillOpacity:0,
		map:map,
		radius:22.25,
		strokeColor:'#fb4e00',
		strokeWeight:1
	});
}




/**
 * メインコンテンツの開閉時の MAP アップデート
 * updata map when main contents open or close.
 */
function updataMapBounds() {
	var center = map.getCenter();
	google.maps.event.trigger(map, 'resize');
	map.panTo(center);
}

/**
 * MAPタイプの切り替え
 * change map type.
 * MapTypeId.ROADMAP は通常のマップビューを表示します。
 * MapTypeId.SATELLITE はGoogle Earth の衛星画像を表示します。
 * MapTypeId.HYBRID は通常と衛星画像の統合された画像を表示します。
 * MapTypeId.TERRAIN は、地図的な情報に基づいて、物理的な地図を表示します。
 */
function changeMapMode(mode) {
	if(mode == 'roadmap') {
		map.mapTypes.set('taiyokai', taiyokaiMapType);
		map.setMapTypeId('taiyokai');
		$("#map_btn").attr("src", $("#map_btn").attr("src").replace("_on.", "_act."));
		$("#map_btn").attr("src", $("#map_btn").attr("src").replace("_off.", "_act."));
		$("#satellite_btn").attr("src", $("#satellite_btn").attr("src").replace("_on.", "_off."));
		$("#satellite_btn").attr("src", $("#satellite_btn").attr("src").replace("_act.", "_off."));
	} else {
		map.setMapTypeId(google.maps.MapTypeId.HYBRID);
		$("#map_btn").attr("src", $("#map_btn").attr("src").replace("_on.", "_off."));
		$("#map_btn").attr("src", $("#map_btn").attr("src").replace("_act.", "_off."));
		$("#satellite_btn").attr("src", $("#satellite_btn").attr("src").replace("_on.", "_act."));
		$("#satellite_btn").attr("src", $("#satellite_btn").attr("src").replace("_off.", "_act."));
	}
	document.cookie = "mapMode=" + map.getMapTypeId() + "; path=/";
}

/**
 * Markerをセット
 * set marker.
 */
var zIndex = 0;
var markerArray = [];
function setMarker(markers) {
	markerArray = [];
	var leng = markers.length;
	for(var i = 0; i < leng; i++) {
		var iconImg = '/images/markers/' + markers[i]['icon'] + '.png';
		var latLng = new google.maps.LatLng(markers[i]['lat'], markers[i]['lng']);
		var beachMarker = new google.maps.Marker({
			position:latLng,
			map:map,
			icon:iconImg,
			zIndex:i + 100
		});
		beachMarker.id = i;
		beachMarker.img = markers[i]['icon'];
		
		if(markers[i]['link']) {
			google.maps.event.addListener(beachMarker, 'mouseover', markerOver(i));
			google.maps.event.addListener(beachMarker, 'mouseout', markerOut(i));
			google.maps.event.addListener(beachMarker, 'click', markerClick(markers[i]['link']));
		} else {
			beachMarker.setClickable(false);
		}
		markerArray.push(beachMarker);
		
		if(!i) map.setCenter(latLng);
	}
	zIndex = i + 100;
}

function removeMarkers() {
	for(var i = 0; i < markerArray.length; i++) {
		markerArray[i].setMap();
		delete markerArray[i];
	}
	markerArray = [];
}

function markerOver(id) {
	return function(event) {
		var area = this.img.split('_')[0];
		this.setIcon('/images/markers/' + area + '_on.png');
		this.setZIndex(++zIndex);
		focusListItem($('#scroll_inner').children().children(), id);
		
		if(event && resetInterval) resetInterval(id);
	}
}
function markerOut(id) {
	return function(event) {
		this.setIcon('/images/markers/' + this.img + '.png');
		blurListItem($('#scroll_inner').children().children(), id);
	}
}
function markerClick(url) {
	return function(event) { window.location.href = url; }
}
//
function markerFocus(id) {
	for(var i = 0; i < markerArray.length; i++) markerBlur(i);
	
	var marker = markerArray[id];
	var area = marker.img.split('_')[0];
	marker.setIcon('/images/markers/' + area + '_on.png');
	marker.setZIndex(++zIndex);
	map.panTo(marker.getPosition());
	
	if(resetInterval) resetInterval(id);
}
function markerBlur(id) {
	var marker = markerArray[id];
	marker.setIcon('/images/markers/' + marker.img + '.png');
}

function initPanTo(id) {
	var marker = markerArray[id];
	marker.setZIndex(++zIndex);
	map.panTo(marker.getPosition());
}


/**
 * 
*/
var positionMarker;
var posIntervalIDID;
$(document).ready(function() {
	if(navigator.userAgent.match(/iPad/i)) {
		positionMarker = new google.maps.Marker({
			icon:'/images/markers/postion.png',
			zIndex:5000
		});
		
		setTimeout(function() { navigator.geolocation.getCurrentPosition(setMarkerPosition, handleError); }, 3000);
		posIntervalID = setInterval(function() { navigator.geolocation.getCurrentPosition(setMarkerPosition, handleError); }, 2000);
		//navigator.geolocation.watchPosition(setMarkerPosition, handleError);
	}
});


function setMarkerPosition(pos) {
	//alert(pos.coords.latitude +","+ pos.coords.longitude);
	if(map && !positionMarker.getMap()) {
		positionMarker.setMap(map);
	}
	positionMarker.setPosition(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));
}
function handleError() {
	//alert("GPS情報が取得出来ませんでした。\nもう一度おためしください。");
} 
