var TOP_ASIA_X = 36.033333; // トップのアジア全体の緯度
var TOP_ASIA_Y = 120.15;    // トップのアジア全体の経度
var TOP_ASIA_ZOOM = 4;      // トップのアジア全体のズーム

var map; // GoogleMapオブジェクト

// 巌のマーカーを作成する
function createIwaoMaker() {
	createTokyoMarker();
	createTaipeiMarker();
	createSoulMarker();
	createShanghaiMarker();
	createHongkongMarker();
	createMacauMarker();
}

// 初期化
function load() {
	map = new GMap2(document.getElementById("map"));
	
	if (GBrowserIsCompatible()) {
		// map.disableDragging(); // ドラッグできないようにする
		map.addControl(new GLargeMapControl()); // 操作パネル表示
		map.addControl(new GMapTypeControl()); // 種別切り換え表示
		map.setCenter(new GLatLng(36.033333, 120.15), 4); // 初期表示位置
		map.setMapType(G_HYBRID_MAP); // 地図＋写真
		map.enableContinuousZoom(); // 左クリックズームイン
		map.enableDoubleClickZoom(); // 右クリックズームアウト
		map.addControl(new GOverviewMapControl()); // オーバビュー表示
      
		// マーカー表示
		createTopMarker();
	}
	
	//Adsense広告が表示されるように設定する
	var adsManager = new GAdsManager(map, "ca-pub-9003538929990257 ");
	adsManager.enable();
	
	if(document.movie) {
		var movie_place = document.movie.movie_place.value;
		var movie_index = document.movie.movie_index.value;
		showMovie(movie_place, movie_index);
		showLargeMovie(movie_place, movie_index);
	}
}

function showMovie(place, index) {
	var iwaoMarker = null;
	
	if (place == "tokyo") {
		setPlace(35.691099, 139.737084, 13);
		iwaoMarker = vTokyo.getElementAt(index);
	} else if(place == "taipei") {
		setPlace(25.07532, 121.531498, 13);
		iwaoMarker = vTaipei.getElementAt(index);
	} else if(place == "seoul" || place == "soul") {
		setPlace(37.5639, 126.9828, 13);
		iwaoMarker = vSoul.getElementAt(index);
	} else if(place == "shanghai") {
		setPlace(31.230222, 121.475, 13);
		iwaoMarker = vShanghai.getElementAt(index);
	} else if(place == "hongkong") {
		setPlace(22.296873, 114.171346, 13);
		iwaoMarker = vHongkong.getElementAt(index);
	} else if(place == "macau") {
		setPlace(22.196745, 113.540873, 15);
		iwaoMarker = vMacau.getElementAt(index);
	}
	
	iwaoMarker.showInfoWindow(place, index);
}

function showLargeMovie(place, index) {
	var iwaoMarker = null;
	
	if (place == "tokyo") {
		iwaoMarker = vTokyo.getElementAt(index);
	} else if(place == "taipei") {
		iwaoMarker = vTaipei.getElementAt(index);
	} else if(place == "soul") {
		iwaoMarker = vSoul.getElementAt(index);
	} else if(place == "shanghai") {
		iwaoMarker = vShanghai.getElementAt(index);
	} else if(place == "hongkong") {
		iwaoMarker = vHongkong.getElementAt(index);
	} else if(place == "macau") {
		iwaoMarker = vMacau.getElementAt(index);
	}
	
	iwaoMarker.showLMovie();
}

// トップのマーカーをすべて削除する
function clearTopMarker() {
	for(var i=0; i<vTop.size; i++) {
		var topMarkerObj = vTop.getElementAt(i);
		map.removeOverlay(topMarkerObj.marker);
	}
}

// 巌のマーカーをすべて削除する
function clearIwaoMarker() {
	for(var i=0; i<vTokyo.size; i++) {
		var iwaoMarkerObj = vTokyo.getElementAt(i);
		map.removeOverlay(iwaoMarkerObj.marker);
	}
	
	for(var i=0; i<vTaipei.size; i++) {
		var iwaoMarkerObj = vTaipei.getElementAt(i);
		map.removeOverlay(iwaoMarkerObj.marker);
	}
	
	for(var i=0; i<vSoul.size; i++) {
		var iwaoMarkerObj = vSoul.getElementAt(i);
		map.removeOverlay(iwaoMarkerObj.marker);
	}
	
	for(var i=0; i<vShanghai.size; i++) {
		var iwaoMarkerObj = vShanghai.getElementAt(i);
		map.removeOverlay(iwaoMarkerObj.marker);
	}
	
	for(var i=0; i<vHongkong.size; i++) {
		var iwaoMarkerObj = vHongkong.getElementAt(i);
		map.removeOverlay(iwaoMarkerObj.marker);
	}
	
	for(var i=0; i<vMacau.size; i++) {
		var iwaoMarkerObj = vMacau.getElementAt(i);
		map.removeOverlay(iwaoMarkerObj.marker);
	}
}

// 地図の場所を設定する
function setPlace(x, y, zoom) {
	map.setCenter(new GLatLng(x, y), zoom);
	
	if(x == TOP_ASIA_X && y == TOP_ASIA_Y && zoom == TOP_ASIA_ZOOM) {
		clearIwaoMarker();
		createTopMarker();
		map.setMapType(G_HYBRID_MAP); // 地図＋写真
	} else {
		clearTopMarker();
		createIwaoMaker();
		map.setMapType(G_SATELLITE_MAP); // 写真
	}
}

// トップのマーカーを保持するクラス
function TopMarker(x, y, place, desc) {
	this.x = x;
	this.y = y;
	this.place = place;
	
	var iconTop = new GIcon();
	iconTop.iconSize = new GSize(30, 84) ;
	iconTop.iconAnchor = new GPoint(30, 84) ;
	iconTop.image = "img/icon_top.gif";
	iconTop.infoWindowAnchor = new GPoint(11, 1);
	
	var point = new GLatLng(x, y);
	var objMarker = new GMarker(point, iconTop);
	this.marker = objMarker;
	
	GEvent.addListener(objMarker, "click", 
		function() { 
			if (place == "tokyo") {
				setPlace(35.691099, 139.737084, 13);
			} else if(place == "taipei") {
				setPlace(25.07532, 121.531498, 13);
			} else if(place == "soul") {
				setPlace(37.5639, 126.9828, 13);
			} else if(place == "shanghai") {
				setPlace(31.230222, 121.475, 13);
			} else if(place == "hongkong") {
				setPlace(22.296873, 114.171346, 13);
			}else if(place == "macau") {
				setPlace(22.196745, 113.540873, 15);
			}
			createIwaoMaker();
	});
	
	//マーカーにマウスオーバーイベント追加
    GEvent.addListener(objMarker, "mouseover", function() {
        //吹き出し表示
        objMarker.openInfoWindowHtml("<div>" + desc + "</div>");
    });
    
    //マーカーにマウスアウトイベント追加
    GEvent.addListener(objMarker, "mouseout", function() {
        //吹き出し非表示
        objMarker.closeInfoWindow();
    }); 
		
	map.addOverlay(this.marker);
	
	return this;
}

// 巌のマーカーを保持するクラス
var gIndex;
var gPlace;
function IwaoMarker(place, index, x, y, youtube, film_at, title, comment, tag) {
	this.place = place;
	this.index = index;
	this.x = x;
	this.y = y;
	this.youtube = youtube;
	this.film_at = film_at;
	this.title = title;
	this.comment = comment;
	this.tag = tag;
	
	var iconIwao = new GIcon();
	iconIwao.iconSize = new GSize(30, 84) ;
	iconIwao.iconAnchor = new GPoint(30, 84) ;
	iconIwao.image = "img/icon_glasses.gif";
	iconIwao.infoWindowAnchor = new GPoint(11, 1);
	
	var strHtml = "<div class='movie'><div class='movie_youtube'>" +
	"<object width=\"240\" height=\"200\">" +
	"<param name=\"movie\" " +
	"value=\"" + youtube + "\">" +
	"</param><param name=\"wmode\" value=\"transparent\">" +
	"</param>" +
	"<embed src=\"" + youtube + "\" " +
	"type=\"application/x-shockwave-flash\" wmode=\"transparent\" " +
	"width=\"240\" height=\"200\"></embed></object><br><br><a href='javascript:showLargeMovie(\"" + place + "\", " + index + ");'>" + G_LARGE_MOVIE + "</a></div>";
	
	strHtml += "<div class='movie_detail'><table class='matrix'>";
	strHtml += "<tr><th>" + G_TITLE + "</th><td>" + title + "</td></tr>";
	strHtml += "<tr><th>" + G_DESC + "</th><td>" + comment + "</td></tr>";
	strHtml += "<tr><th>" + G_KEYWORD + "</th><td>" + tag + "</td></tr>";
	strHtml += "</table></div></div>";
	
	//strHtml += "<script type=\"text/javascript\"><!--\n";
	//strHtml += "google_ad_client = \"pub-9003538929990257\";\n";
	//strHtml += "/* 234x60, 作成済み 08/07/30 */\n";
	//strHtml += "google_ad_slot = \"0967328098\";\n";
	//strHtml += "google_ad_width = 234;\n";
	//strHtml += "google_ad_height = 60;\n";
	//strHtml += "//-->\n";
	//strHtml += "</script>\n";
	//strHtml += "<script type=\"text/javascript\"\n";
	//strHtml += "src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\";>\n";
	//strHtml += "</script>\n";
	
	var objPoint = new GLatLng(x, y);
	var objMarker = new GMarker(objPoint, iconIwao);
	this.marker = objMarker;
	
	GEvent.addListener(objMarker, "click", function(){ objMarker.openInfoWindowHtml(strHtml); });
	map.addOverlay(objMarker);
	
	this.showInfoWindow = function(place, index) {
		objMarker.openInfoWindowHtml(strHtml);
	}
	
	this.showLMovie = function() {
		var strHtml = "<div class='lmovie'><div class='movie_youtube'>" +
		"<object width=\"425\" height=\"355\">" +
		"<param name=\"movie\" " +
		"value=\"" + this.youtube + "\">" +
		"</param><param name=\"wmode\" value=\"transparent\">" +
		"</param>" +
		"<embed src=\"" + this.youtube + "\" " +
		"type=\"application/x-shockwave-flash\" wmode=\"transparent\" " +
		"width=\"425\" height=\"355\"></embed></object><br><br><a href='javascript:showMovie(\"" + place + "\", " + index + ");'>" + G_SMALL_MOVIE + "</a></div>";
		objMarker.openInfoWindowHtml(strHtml);
	}
	
	return this;
}

