//if (GBrowserIsCompatible()) {
	

var ichigaya_latlng = null
var kudanshita_latlng = null;
var hanzoumon_latlmg = null;
var stitch_latlng = null;

var ichigayaicon;

var directions;
var map = null; 
var route = 0;

var manIcons = new Array;

var routeIchigaya = null;
var routeKudanshita = null;
var routeHanzomon = null;
var routeTimer = null;
var routing_idx = 0;
var man_layer = null;
var routeIchigayaCenter;
var routeKudanshitaCenter;
var routeHanzomonCenter;

function initMap() {
	anima();
	ichigaya_latlng = new GLatLng(35.691008, 139.735585);
	kudanshita_latlng = new GLatLng(35.695308, 139.7506320);
	hanzoumon_latlmg = new GLatLng(35.685703, 139.741644);
	stitch_latlng = new GLatLng(35.693386, 139.7441625);
	
	routeIchigaya = new Array( {latlng:new GLatLng(35.69122359834966, 139.7363841533661), icon:1},
								{latlng:new GLatLng(35.691297664509634, 139.7366765141487), icon:0},
								{latlng:new GLatLng(35.691386979493394, 139.73695278167725), icon:1},
								{latlng:new GLatLng(35.69146104550165, 139.73720490932465), icon:0},
								{latlng:new GLatLng(35.69153728985005, 139.7374677658081), icon:1},
								{latlng:new GLatLng(35.69161353412557, 139.7377386689186), icon:0},
								{latlng:new GLatLng(35.69168759992342, 139.7380417585373), icon:1},
								{latlng:new GLatLng(35.691766022457955, 139.73834484815598), icon:0},
								{latlng:new GLatLng(35.691833552912016, 139.73862379789352), icon:1},
								{latlng:new GLatLng(35.6919163321003, 139.7389242053032), icon:0},
								{latlng:new GLatLng(35.69199693280633, 139.73918974399567), icon:1},
								{latlng:new GLatLng(35.69207535503672, 139.73947942256927), icon:0},
								{latlng:new GLatLng(35.69216249075782, 139.7397556900978), icon:1},
								{latlng:new GLatLng(35.69223873443568, 139.7400051355362), icon:0},
								{latlng:new GLatLng(35.69233240514018, 139.7403109073639), icon:1},
								{latlng:new GLatLng(35.69241082704071, 139.74057108163834), icon:0},
								{latlng:new GLatLng(35.692500140777945, 139.74089831113815), icon:1},
								{latlng:new GLatLng(35.69260470307498, 139.74120408296585), icon:0},
								{latlng:new GLatLng(35.69270708685796, 139.74153131246567), icon:1},
								{latlng:new GLatLng(35.69280075701239, 139.74183708429337), icon:0},
								{latlng:new GLatLng(35.69289224868493, 139.74215894937515), icon:1},
								{latlng:new GLatLng(35.69298374025251, 139.74245935678482), icon:0},
								{latlng:new GLatLng(35.69308612354893, 139.74278390407562), icon:1},
								{latlng:new GLatLng(35.6931710798015, 139.74304139614105), icon:0},
								{latlng:new GLatLng(35.693258214325546, 139.74333107471466), icon:1},
								{latlng:new GLatLng(35.69336059726962, 139.74365830421448), icon:0}
	);
	routeIchigayaCenter = new GLatLng(35.69262866524874, 139.740000000000);
	
	routeKudanshita = new Array( 
	 							{latlng:new GLatLng(35.695375553996115, 139.75080907344818), icon:3},
	 							{latlng:new GLatLng(35.695331987902655, 139.750414788723), icon:2},
	 							{latlng:new GLatLng(35.695294956704494, 139.75004464387894), icon:3},
	 							{latlng:new GLatLng(35.695251390567016, 139.74970400333405), icon:2},
	 							{latlng:new GLatLng(35.69519257624365, 139.74937677383423), icon:3},
								{latlng:new GLatLng(35.69509455227498, 139.7490656375885), icon:2},
								{latlng:new GLatLng(35.69497910166848, 139.74873840808868), icon:3},
								{latlng:new GLatLng(35.69489414734187, 139.74842995405197), icon:2},
								{latlng:new GLatLng(35.69479394468646, 139.74810272455215), icon:3},
								{latlng:new GLatLng(35.694704633517915, 139.7478023171425), icon:2},
								{latlng:new GLatLng(35.69460443062439, 139.74744826555252), icon:3},
								{latlng:new GLatLng(35.69450640593282, 139.7471183538437), icon:2},
								{latlng:new GLatLng(35.694414916111974, 139.7467938065529), icon:3},
								{latlng:new GLatLng(35.69430599952177, 139.74646389484406), icon:2},
								{latlng:new GLatLng(35.69420579612737, 139.74610179662704), icon:3},
								{latlng:new GLatLng(35.694114305961676, 139.74578261375427), icon:2},
								{latlng:new GLatLng(35.69403152905466, 139.7454634308815), icon:3},
								{latlng:new GLatLng(35.69393132531544, 139.74512815475464), icon:2},
								{latlng:new GLatLng(35.693791911207946, 139.74480360746384), icon:3},
								{latlng:new GLatLng(35.693650318505576, 139.74443346261978), icon:2}
								
	);
	routeKudanshitaCenter = new GLatLng(35.694545615823905, 139.74785327911377);
	
	routeHanzomon = new Array( 	{latlng:new GLatLng(35.686613933766665, 139.7417539358139), icon:0},
								{latlng:new GLatLng(35.687064890969964, 139.7418263554573), icon:1},
								{latlng:new GLatLng(35.68756595153935, 139.7420033812523), icon:0},
								{latlng:new GLatLng(35.688021260370924, 139.7421830892563), icon:1},
								{latlng:new GLatLng(35.68844388920947, 139.7423467040062), icon:0},
								{latlng:new GLatLng(35.68892533479729, 139.74253177642822), icon:1},
								{latlng:new GLatLng(35.689450346809444, 139.74273562431335), icon:0},
								{latlng:new GLatLng(35.689853361984724, 139.74289655685425), icon:1},
								{latlng:new GLatLng(35.690358761921246, 139.74309235811234), icon:0},
								{latlng:new GLatLng(35.690875050791185, 139.74329084157944), icon:1},
								{latlng:new GLatLng(35.69130419975574, 139.7434625029564), icon:0},
								{latlng:new GLatLng(35.69176166565252, 139.74358588457108), icon:1},
								{latlng:new GLatLng(35.692321513203446, 139.74368780851364), icon:0}							
	);
	routeHanzomonCenter = new GLatLng(35.69058967634366, 139.74287509918213);

	map = new GMap2(document.getElementById("map"));

	//座標と地図倍率を設定
	map.setCenter(new GLatLng(35.693322, 139.744162), 15);

	//マップ切り替えボタンを設置
	map.addControl(new GMapTypeControl());
	//マップコントローラーを設置
	map.addControl(new GLargeMapControl());
	//マウスホイールで拡大・縮小
	map.enableScrollWheelZoom();
	
	//ルート
	directions = new GDirections(map);

	GEvent.addListener(directions, "addoverlay", onGDirectionsAddOverlay);

	//ステッチマーカー
	var myicon = new GIcon();
	myicon.image="http://www.stitch.co.jp/access/images/mapicon.png"; //マーカー画像
	myicon.shadow="http://www.stitch.co.jp/access/images/mapicon_shadow.png"; //マーカー影
	myicon.iconSize = new GSize(95,95); //マーカーサイズ
	myicon.shadowSize = new GSize(130,95); //影サイズ
	myicon.iconAnchor = new GLatLng(95,48); //マーカー位置
	//ステッチ位置
	var marker = new GMarker(stitch_latlng,myicon);
	map.addOverlay(marker);

	//市ヶ谷駅
	var ichigayaicon = new GIcon();
	ichigayaicon.image="http://www.stitch.co.jp/access/images/mapicon_ichigaya.png"; //マーカー画像
	ichigayaicon.shadow="http://www.stitch.co.jp/access/images/mapicon_shadow_mini.png"; //マーカー影
	ichigayaicon.iconSize = new GSize(66,66); //マーカーサイズ
	ichigayaicon.shadowSize = new GSize(91,66); //影サイズ
	ichigayaicon.iconAnchor = new GLatLng(66,33); //マーカー位置
	//市ヶ谷駅位置
	var ichigayamarker = new GMarker(ichigaya_latlng,ichigayaicon);
	map.addOverlay(ichigayamarker);

	//九段下駅
	var kudanicon = new GIcon();
	kudanicon.image="http://www.stitch.co.jp/access/images/mapicon_kudanshita.png"; //マーカー画像
	kudanicon.shadow="http://www.stitch.co.jp/access/images/mapicon_shadow_mini.png"; //マーカー影
	kudanicon.iconSize = new GSize(66,66); //マーカーサイズ
	kudanicon.shadowSize = new GSize(91,66); //影サイズ
	kudanicon.iconAnchor = new GLatLng(66,33); //マーカー位置
	//九段下駅位置
	var kudanmarker = new GMarker(kudanshita_latlng,kudanicon);
	map.addOverlay(kudanmarker);

	//半蔵門駅
	var kudanicon = new GIcon();
	kudanicon.image="http://www.stitch.co.jp/access/images/mapicon_hanzoumon.png"; //マーカー画像
	kudanicon.shadow="http://www.stitch.co.jp/access/images/mapicon_shadow_mini.png"; //マーカー影
	kudanicon.iconSize = new GSize(66,66); //マーカーサイズ
	kudanicon.shadowSize = new GSize(91,66); //影サイズ
	kudanicon.iconAnchor = new GLatLng(66,33); //マーカー位置

	//半蔵門駅位置
	var kudanmarker = new GMarker(hanzoumon_latlmg,kudanicon);
	map.addOverlay(kudanmarker);
	
	// 人アイコンづくり
	
	var manIcon1 = new GIcon();
	manIcon1.image="http://www.stitch.co.jp/access/images/b_r_1.png"; //マーカー画像
	manIcon1.iconSize = new GSize(42,42); //マーカーサイズ
	manIcon1.iconAnchor = new GLatLng(42,21); //マーカー位置
	var manIcon2 = new GIcon();
	manIcon2.image="http://www.stitch.co.jp/access/images/b_r_3.png"; //マーカー画像
	manIcon2.iconSize = new GSize(42,42); //マーカーサイズ
	manIcon2.iconAnchor = new GLatLng(42,21); //マーカー位置
	var manIcon3 = new GIcon();
	manIcon3.image="http://www.stitch.co.jp/access/images/f_l_1.png"; //マーカー画像
	manIcon3.iconSize = new GSize(42,42); //マーカーサイズ
	manIcon3.iconAnchor = new GLatLng(42,21); //マーカー位置
	var manIcon4 = new GIcon();
	manIcon4.image="http://www.stitch.co.jp/access/images/f_l_3.png"; //マーカー画像
	manIcon4.iconSize = new GSize(42,42); //マーカーサイズ
	manIcon4.iconAnchor = new GLatLng(42,21); //マーカー位置
	
	
	manIcons = new Array(manIcon1, manIcon2, manIcon3, manIcon4);

	map.setCenter(new GLatLng(35.6922452696047, 139.74320769309998));
	//showRoute(0);
}

function showRoute(num){

	var points = new Array();
	
	if(num == 0){ // 市ヶ谷
		points.push(ichigaya_latlng);//市ヶ谷の経度緯度
		points.push(stitch_latlng);//ステッチの経度緯度
	}else if(num == 1){// 九段下
		points.push(kudanshita_latlng);//市ヶ谷の経度緯度
		points.push(stitch_latlng);//ステッチの経度緯度
	}else if(num == 2){
		points.push(hanzoumon_latlmg);//市ヶ谷の経度緯度
		points.push(stitch_latlng);//ステッチの経度緯度
	}else{
		return;
	}
	
	route = num;
	directions.loadFromWaypoints(points, {preserveViewport:true});

	GEvent.addListener(directions, 'addoverlay', function (){ 
	    for( var i = 0, l = points.length; i < l; ++i ){ 
			var marker = directions.getMarker(i);
			map.removeOverlay(marker);
		} 
	});
	routing_idx = 0;
	startRouting(num);
}

function startRouting(num){
	if(man_layer) map.removeOverlay(man_layer);
	if(routeTimer) clearInterval(routeTimer);
	routeTimer = setInterval(routing,200);
}

function routing(){

	if(man_layer) map.removeOverlay(man_layer);
	
	var route_list;
	if(route == 0){
		route_list = routeIchigaya;
	}else if(route == 1){
		route_list = routeKudanshita;
	}else if(route == 2){
		route_list = routeHanzomon;
	}
	if(routing_idx == route_list.length) routing_idx = 0;
	
	man_layer = new GMarker(route_list[routing_idx].latlng,manIcons[route_list[routing_idx].icon]);
	map.addOverlay(man_layer);
	routing_idx++;
}

function onGDirectionsAddOverlay(){ 
	var polyline = directions.getPolyline();
	polyline.setStrokeStyle({color: "#ce5000", weight: 10 });
	if(route == 0){
		map.setZoom(16);
		map.panTo(routeIchigayaCenter);
	}else if(route == 1){
		map.setZoom(16);
		map.panTo(routeKudanshitaCenter);
	}else if(route == 2){
		map.setZoom(15);
		map.panTo(routeHanzomonCenter);
	}
}


