function unload(){
	GUnload();
}

var Global = this;

var icon_ = new GIcon();
icon_.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
icon_.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
icon_.iconSize = new GSize(12, 20);
icon_.shadowSize = new GSize(22, 20);
icon_.iconAnchor = new GPoint(6, 20);
icon_.infoWindowAnchor = new GPoint(5, 1);

var icon_fix_image = new Image();
icon_fix_image.src = "/contents/images/default/space/map_marker_fix.png";
var icon_fix = new GIcon();
icon_fix.image = icon_fix_image.src;
icon_fix.shadow = "/contents/images/default/space/map_marker_shadow.png";
icon_fix.iconSize = new GSize(30, 51);
icon_fix.shadowSize = new GSize(56, 51);
icon_fix.iconAnchor = new GPoint(13, 51);
icon_fix.infoWindowAnchor = new GPoint(13, 3);
icon_fix.infoShadowAnchor = new GPoint(27, 38);

var icon_unfix_image = new Image();
icon_unfix_image.src = "/contents/images/default/space/map_marker_unfix.png";
var icon_unfix = new GIcon();
icon_unfix.image = icon_unfix_image.src;
icon_unfix.shadow = "/contents/images/default/space/map_marker_shadow2.png";
icon_unfix.iconSize = new GSize(24, 36);
icon_unfix.shadowSize = new GSize(50, 36);
icon_unfix.iconAnchor = new GPoint(9, 34);
icon_unfix.infoWindowAnchor = new GPoint(9, 2);
icon_unfix.infoShadowAnchor = new GPoint(18, 19);

var icon_thb1 = new GIcon();
icon_thb1.image = "http://maps.google.com/mapfiles/ms/icons/yellow.png";
icon_thb1.iconAnchor = new GPoint(20,30);

var icon_thb2 = new GIcon();
icon_thb2.image = "http://maps.google.com/mapfiles/ms/icons/green.png";
icon_thb2.iconAnchor = new GPoint(20,30);

var icon_thb3 = new GIcon();
icon_thb3.image = "http://maps.google.com/mapfiles/ms/icons/lightblue.png";
icon_thb3.iconAnchor = new GPoint(20,30);

var icon_thb4 = new GIcon();
icon_thb4.image = "http://maps.google.com/mapfiles/ms/icons/pink.png";
icon_thb4.iconAnchor = new GPoint(20,30);


//マーカー固定ボタン
function MyFixControl() {}
MyFixControl.prototype = new GControl();
MyFixControl.prototype.initialize = function(map){
			var btnFix = $("btnFix");
			GEvent.addDomListener(btnFix, "click", function() {
				if(Global.map().markerCacheGT && Global.map().markerDraggable){
					Global.map().showMarker(Global.map().markerCacheGT.getPoint().lat(), Global.map().markerCacheGT.getPoint().lng(), 'fix');
				}
			});
			map.getContainer().appendChild(btnFix);
			return btnFix;
}
MyFixControl.prototype.getDefaultPosition = function(){
	return new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(5, 20));
}

//マーカー固定解除ボタン
function MyUnfixControl() {}
MyUnfixControl.prototype = new GControl();
MyUnfixControl.prototype.initialize = function(map){
			var btnUnFix = $("btnUnFix");
			GEvent.addDomListener(btnUnFix, "click", function() {
				if(Global.map().markerCacheGT && !Global.map().markerDraggable){
					Global.map().showMarker(Global.map().markerCacheGT.getPoint().lat(), Global.map().markerCacheGT.getPoint().lng(), 'unfix');
					GEvent.trigger(Global.map().map,"moveend");
				}
			});
			map.getContainer().appendChild(btnUnFix);
			return btnUnFix;
}
MyUnfixControl.prototype.getDefaultPosition = function(){
	return new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(110, 20));
}

//spaceEntryMap(作成・編集)クラス
var spaceEntryMap = Class.create();
spaceEntryMap.prototype = {
	initialize: function(lat, lng, zoom, map_type, show_flg){
		if (GBrowserIsCompatible()) {
			this.mapElement = $("gmap");
			this.markerDraggable = false;
			this.threadShow = false;
			this.markerCacheGT = false;
			this.thread_list = new Array();
			this.markerCache = {};
			this.markerHtmlCache = {};
			this.geocoder = new GClientGeocoder();
			this.map = new GMap2(this.mapElement);
			this.map.addControl(new GLargeMapControl());
			this.map.addControl(new GMapTypeControl());
			this.map.addControl(new MyFixControl());
			this.map.addControl(new MyUnfixControl());
			this.map.setCenter(new GLatLng(lat, lng), zoom);
			if(0 <= map_type && map_type < 3){
				this.map.setMapType(G_DEFAULT_MAP_TYPES[map_type]);
			}
			GEvent.addListener(this.map, "dblclick", this.onDoubleClick.bind(this));
			GEvent.addListener(this.map, "moveend", this.onMove.bind(this));
			GEvent.addListener(this.map, "maptypechanged", this.onMove.bind(this));
			if(show_flg){
				this.showMarker(lat, lng, 'unfix');
			}else{
				this.setLatLng(0, 0, 12);
			}
		}else{
		    $('notice').innerHTML += 'ご利用の環境では、Google Mapsが利用できません。';
		}
    },
	addMarker: function(lat, lng, id, icon, tip){
		if(!this.markerCache[id]){
			var marker = new GMarker(new GLatLng(lat, lng), {icon:icon,title:tip});
			GEvent.addListener(marker, "click", this.setMarkerHtml.bind(this, id));
			GEvent.addListener(marker, "dragend", this.setMarkerPos.bind(this));
			this.map.addOverlay(marker);
		    this.markerCache[id] = marker;
		}
	},
    setMarkerHtml: function(id){
    	if(this.markerHtmlCache[id]){
		    this.markerCache[id].openInfoWindowHtml(this.markerHtmlCache[id]);
    	}
    },
	initMap: function(){
		this.markerCache = {};
		this.markerHtmlCache = {};
		this.markerCacheGT = false;
		this.map.clearOverlays();
	},
    onDoubleClick: function(marker, point){
		if(!this.markerCacheGT){
			this.showMarker(point.y, point.x, 'unfix');
		}
    },
    onMove: function(){
		if(this.markerCacheGT && !this.map.getBounds().contains(this.markerCacheGT.getPoint()) && this.markerDraggable ){
			var c = this.map.getCenter();
			this.showMarker(c.lat(), c.lng(), 'unfix');
		}
		if(this.threadShow) this.getThreadOnMapPoint();
    },
    showMarker: function(lat, lng, fix_sw){
		//$('notice').innerHTML = lat + "," + lng;
		this.map.removeOverlay(this.markerCacheGT);
		var marker = null;
    	if(fix_sw=='fix'){
			this.markerDraggable = false;
			marker = new GMarker(new GLatLng(lat, lng), {icon:icon_fix});
		}else{
			this.markerDraggable = true;
			marker = new GMarker(new GLatLng(lat, lng), {icon:icon_unfix, draggable:true});
		}
	    this.map.addOverlay(marker);
	    this.markerCacheGT = marker;
		this.setLatLng(lat, lng, this.map.getZoom());
    },
	moveGeocode: function(address) {
		if (this.geocoder) {
			this.geocoder.getLatLng(address,function(point) {
				if (!point) {
					$("notice").innerHTML += address + " に一致する場所が見つかりません。";
				} else {
					if(!this.map().markerCacheGT){
						this.map().showMarker(point.y, point.x, 'unfix');
					}
					this.map().map.setCenter(point);
				}
			});
		}
	},
    setMarkerPos: function(){
		if(this.markerCacheGT){
			var point = this.markerCacheGT.getPoint();
			this.setLatLng(point.y, point.x, this.map.getZoom());
		}
    },
    setLatLng: function(lat, lng, zoom){
		//$('notice').innerHTML = lat + "," + lng + "," + zoom;
		if( $("com_lat") && $("com_lng") && $("com_zoom")){
		    $("com_lat").value = lat;
		    $("com_lng").value = lng;
		    $("com_zoom").value = zoom;
		}
    },
    removeThredPoint: function(){
		this.threadShow = false;
		for(i=0;i<this.thread_list.length;i++){
			var tid = this.thread_list[i];
			var marker = this.markerCache[tid];
			if(marker) marker.remove();
			this.markerCache[tid] = null;
			this.markerHtmlCache[tid] = null;
		}
		this.markerCache = {};
		this.markerHtmlCache = {};
		this.thread_list = new Array();
    },
	getThreadOnMapPoint: function(){
		this.threadShow = true;
		var options = {
			method : 'get',
			onFailure: function() { alert("通信エラー"); },
			onComplete: function(res) {
				var obj = eval('(' + res.responseText + ')');
				var icon = new GIcon();
				icon.shadow = "/contents/images/default/map/mm_sh_shadow.png";
				icon.iconSize = new GSize(16, 24);
				icon.shadowSize = new GSize(30, 24);
				icon.iconAnchor = new GPoint(6, 20);
				icon.infoWindowAnchor = new GPoint(5, 1);
				for(i=0;i<obj.result.length;i++){
					lat = obj.result[i].lat;
					lng = obj.result[i].lng;
					id = 'th' + obj.result[i].id;
					id4url = my_uZeroPadding(obj.result[i].id,12);
					ttype = obj.result[i].type;
					title = obj.result[i].title;
					text = obj.result[i].text;
					timg = obj.result[i].timg;
					uid = obj.result[i].uid;
					uname = obj.result[i].uname;
					ccnt = obj.result[i].ccnt;
					icon.image = "/contents/images/default/map/mm_th" + ttype + ".png";
					this.addMarker(lat, lng, id, icon, title);
					if(!this.markerHtmlCache[id]){
						this.thread_list.push(id);
						var tempHtml = '';
						if( timg!= '' ){
							tempHtml = '<div style="width:400px"><p>投稿者:' + uname + 'さん</p><p>コメント:' + ccnt + '件</p><p><strong>' + title + '</strong></p><table><tr><td><img src=\'' + $('thread_image_url_base').value + 'bti/' + timg + '.ss?i=' + $('thread_image_pc').value + '\' alt=\'' + title + '\' /></td><td style="padding:5px"><p>' + text + '</p><p><a href="/space/' + id4url + '/">このスレッドを見る</a></p></td></tr></table></div>';
						}else{
							tempHtml = '<div style="width:400px"><p>投稿者:' + uname + 'さん</p><p>コメント:' + ccnt + '件</p><p><strong>' + title + '</strong></p><p>' + text + '</p><p><a href="/space/' + id4url + '/">このスレッドを見る</a></p></div>';
						}
					    this.markerHtmlCache[id] = tempHtml;
					}
				}
			}.bind(this)
		};
		var lat = this.map.getCenter().y;
		var lng = this.map.getCenter().x;
		var bounds = this.map.getBounds();
		var nelat = bounds.getNorthEast().lat();
		var nelng = bounds.getNorthEast().lng();
		var swlat = bounds.getSouthWest().lat();
		var swlng = bounds.getSouthWest().lng();
		new Ajax.Request('/space/get_thread.js?cmd=lp&cnt=200&lat=' + lat + '&lng=' + lng + '&nelat=' + nelat + '&nelng=' + nelng + '&swlat=' + swlat + '&swlng=' + swlng  + '&rd=' + (new Date).getTime(), options);
	}
}

//広場新規作成ボタン
function MyThreadEntryControl() {}
MyThreadEntryControl.prototype = new GControl();
MyThreadEntryControl.prototype.initialize = function(map){
	var btnThEntry = $("thread_entry_on_map");
	map.getContainer().appendChild(btnThEntry);
	return btnThEntry;
}
MyThreadEntryControl.prototype.getDefaultPosition = function(){
	return new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(80, 5));
}

//myplMapList(リスト)クラス
var myplMapList = Class.create();
myplMapList.prototype = {
	initialize: function(lat, lng, zoom, map_type, map_case, img_path){
		if(img_path == undefined)img_path = "default";
		if (GBrowserIsCompatible()) {
			this.mapElement = $("gmap");
			this.markerDraggable = false;
			this.shopOnThreadMap = false;
			this.fleamarketOnThreadMap = false;
			this.fleamarket_list = new Array();
			this.shop_list = new Array();
			this.thread_list = new Array();
			this.map_case = map_case;
			this.map_type = map_type;
			this.img_path = img_path;
			this.markerCache = {};
			this.markerHtmlCache = {};
			this.map = new GMap2(this.mapElement);
			this.map.addControl(new GLargeMapControl());
			this.map.addControl(new GMapTypeControl());
			var overviewControl = new GOverviewMapControl(new GSize(150, 150));
			this.map.addControl(overviewControl);
			overviewControl.hide();
			this.map.setCenter(new GLatLng(lat, lng), zoom);
			if(0 <= map_type && map_type < 3){
				this.map.setMapType(G_DEFAULT_MAP_TYPES[map_type]);
			}
			this.map.enableDoubleClickZoom();
			this.map.enableContinuousZoom();
			if( map_case=='shop'){
				GEvent.addListener(this.map, "moveend", this.onMoveShop.bind(this));
			}else if( map_case=='thread'){
				GEvent.addListener(this.map, "moveend", this.onMoveThread.bind(this));
				this.map.addControl(new MyThreadEntryControl());
			}else if( map_case=='fleamarket'){
				GEvent.addListener(this.map, "moveend", this.onMoveFleamarket.bind(this));
			}
		}else{
		    $('notice').innerHTML += 'ご利用の環境では、Google Mapsが利用できません。';
		}
    },
	addMarker: function(lat, lng, id, icon, tip){
		if(!this.markerCache[id]){
			var marker = new GMarker(new GLatLng(lat, lng), {icon:icon,title:tip});
			GEvent.addListener(marker, "click", this.setMarkerHtml.bind(this, id));
			this.map.addOverlay(marker);
		    this.markerCache[id] = marker;
		}
	},
    setMarkerHtml: function(id){
    	if(this.markerHtmlCache[id]){
		    this.markerCache[id].openInfoWindowHtml(this.markerHtmlCache[id]);
    	}
    },
    showMapInfo: function(){
		//$('this_url').innerHTML = $("site_url").value + "space/maps?lat=" + this.map.getCenter().y + '&lng=' + this.map.getCenter().x + '&zoom=' + this.map.getZoom();
		//var bounds = this.map.getBounds();
	    //$('notice').innerHTML = '[Center]    lat:' + this.map.getCenter().y + '  lng:' + this.map.getCenter().x + '<br />';
	    //$('notice').innerHTML += '[NorthEast] lat:' + bounds.getNorthEast().lat() + '  lng:' + bounds.getNorthEast().lng() + '<br />';
	    //$('notice').innerHTML += '[SouthWest] lat:' + bounds.getSouthWest().lat() + '  lng:' + bounds.getSouthWest().lng() + '<br />';
    },
    onMoveThread: function(){
		$('th_entry_form').lat.value = this.map.getCenter().y;
		$('th_entry_form').lng.value = this.map.getCenter().x;
		$('th_entry_form').z.value = this.map.getZoom();
	    this.getThreadOnMapPoint();
	    if(this.shopOnThreadMap) this.getShopOnMapPoint(1);
	    if(this.fleamarketOnThreadMap) this.getFleamarketOnMapPoint(1);
	    this.getThreadOnMapLatest();
    },
    onMoveShop: function(){
	    this.getShopOnMapPoint(this.map_type);
    },
    onMoveFleamarket: function(){
	    this.getFleamarketOnMapPoint(this.map_case);
    },
    removeShopPoint: function(){
    	this.initShopPoint();
		this.shopOnThreadMap = false;
    },
    removeFleamarketPoint: function(){
    	this.initFleamarketPoint();
		this.fleamarketOnThreadMap = false;
    },
    initShopPoint: function(){
		for(i=0;i<this.shop_list.length;i++){
			var sid = this.shop_list[i];
			var marker = this.markerCache[sid];
			if(marker) marker.remove();
			this.markerCache[sid] = null;
			this.markerHtmlCache[sid] = null;
		}
		this.shop_list = new Array();
    },
    initFleamarketPoint: function(){
		for(i=0;i<this.fleamarket_list.length;i++){
			var sid = this.fleamarket_list[i];
			var marker = this.markerCache[sid];
			if(marker) marker.remove();
			this.markerCache[sid] = null;
			this.markerHtmlCache[sid] = null;
		}
		this.fleamarket_list = new Array();
    },
	getFleamarketOnMapPoint: function(map_type){
		var options = {
			method : 'get',
			onFailure: function() { alert("通信エラー"); },
			onComplete: function(res) {
				if(this.fleamarket_list.length > 600){
					this.initFleamarketPoint();
				}
				var obj = eval('(' + res.responseText + ')');
				var icon = new GIcon();
				icon.image = "/contents/images/default/map/mm_fleam1.png";
				icon.shadow = "/contents/images/default/map/mm_sh_shadow.png";
				icon.iconSize = new GSize(16, 24);
				icon.shadowSize = new GSize(30, 24);
				icon.iconAnchor = new GPoint(6, 20);
				icon.infoWindowAnchor = new GPoint(5, 1);

				for(i=0;i<obj.result.length;i++){
					id = 'fleam' + obj.result[i].id;
					id4url = my_uZeroPadding(obj.result[i].id,11);
					name = obj.result[i].name;
					img = obj.result[i].img;
					address = obj.result[i].address;
					odate = obj.result[i].odate;
					otime = obj.result[i].otime;
					discript = odate + ' ( ' + otime + ' )';
					lat = obj.result[i].lat;
					lng = obj.result[i].lng;
					this.addMarker(lat, lng, id, icon, name);
					if(!this.markerHtmlCache[id]){
						this.fleamarket_list.push(id);
						var tempHtml = '';
						if( img!= '' ){
							if(map_type==0){
								tempHtml = '<div style="width:400px"><p><strong>' + name + '</strong></p><table><tr><td><img src=\'' + $('fleamarket_image_url_base').value + img + '\' alt=\'' + name + '\' style="height:60px" /></td><td style="padding:5px"><p>' + discript + '</p><p>' + address + '</p><p><a href="/fleamarket/' + id4url + '/">詳細を見る</a></p></td></tr></table></div>';
							}else{
								tempHtml = '<div style="width:400px"><p><strong>フリマ情報　' + name + '</strong></p><table><tr><td><img src=\'' + $('fleamarket_image_url_base').value + img + '\' alt=\'' + name + '\' style="height:60px" /></td><td style="padding:5px"><p>' + discript + '</p><p>' + address + '</p><p><a href="/fleamarket/' + id4url + '/">詳細を見る</a></p></td></tr></table></div>';
							}
						}else{
							if(map_type==0){
								tempHtml = '<div style="width:400px"><p><strong>' + name + '</strong></p><p>' + discript + '</p><p>' + address + '</p><p><a href="/fleamarket/' + id4url + '/">詳細を見る</a></p></div>';
							}else{
								tempHtml = '<div style="width:400px"><p><strong>フリマ情報　' + name + '</strong></p><p>' + discript + '</p><p>' + address + '</p><p><a href="/fleamarket/' + id4url + '/">詳細を見る</a></p></div>';
							}
						}
					    this.markerHtmlCache[id] = tempHtml;
					}
				}
			}.bind(this)
		};
		var lat = this.map.getCenter().y;
		var lng = this.map.getCenter().x;
		var bounds = this.map.getBounds();
		var nelat = bounds.getNorthEast().lat();
		var nelng = bounds.getNorthEast().lng();
		var swlat = bounds.getSouthWest().lat();
		var swlng = bounds.getSouthWest().lng();
		if(map_type==0){
			var cnt = $('map_cnt').value;
			var kw = $('map_kw').value;
			var m = $('map_m').value;
			new Ajax.Request('/fleamarket/get_fleamarket.js?cnt=200&kw=' + kw + '&lat=' + lat + '&lng=' + lng + '&nelat=' + nelat + '&nelng=' + nelng + '&swlat=' + swlat + '&swlng=' + swlng  + '&m=' + m  + '&rd=' + (new Date).getTime(), options);
		}else{
			this.fleamarketOnThreadMap = true;
			new Ajax.Request('/fleamarket/get_fleamarket.js?cnt=100&lat=' + lat + '&lng=' + lng + '&nelat=' + nelat + '&nelng=' + nelng + '&swlat=' + swlat + '&swlng=' + swlng  + '&rd=' + (new Date).getTime(), options);
		}
	},
	getShopOnMapPoint: function(map_type){
		var options = {
			method : 'get',
			onFailure: function() { alert("通信エラー"); },
			onComplete: function(res) {
				if(this.shop_list.length > 600){
					this.initShopPoint();
				}
				var obj = eval('(' + res.responseText + ')');
				var icon = new GIcon();
				icon.shadow = "/contents/images/default/map/mm_sh_shadow.png";
				icon.iconSize = new GSize(16, 24);
				icon.shadowSize = new GSize(30, 24);
				icon.iconAnchor = new GPoint(6, 20);
				icon.infoWindowAnchor = new GPoint(5, 1);

				for(i=0;i<obj.result.length;i++){
					id = 'sh' + obj.result[i].id;
					id4url = my_uZeroPadding(obj.result[i].id,11);
					title = '[' + obj.result[i].sub_str + '] ' + obj.result[i].name;
					ttype = obj.result[i].cat_id;
					cat_str = obj.result[i].cat_str;
					cat_id = obj.result[i].cat_id;
					sub_str = obj.result[i].cat_str;
					b_flg = obj.result[i].b_flg;
					dpr = obj.result[i].dpr;
					timg = obj.result[i].timg;
					lat = obj.result[i].lat;
					lng = obj.result[i].lng;
					icon.image = "/contents/images/default/map/mm_sh" + cat_id + ".png";
					this.addMarker(lat, lng, id, icon, title);
					if(!this.markerHtmlCache[id]){
						this.shop_list.push(id);
						var tempHtml = '';
						if( timg!= '' ){
							if(map_type==0){
								tempHtml = '<div style="width:400px"><p><strong>' + title + '</strong></p><table><tr><td><img src=\'' + $('thread_image_url_base').value + timg + '\' alt=\'' + title + '\' /></td><td style="padding:5px"><p>' + dpr + '&nbsp;&nbsp;&nbsp;<a href="/shop/' + id4url + '/">詳細を見る</a></p></td></tr></table></div>';
							}else{
								tempHtml = '<div style="width:400px"><p><strong>ショップ情報　' + title + '</strong></p><table><tr><td><img src=\'' + $('shop_image_url_base').value + timg + '\' alt=\'' + title + '\' /></td><td style="padding:5px"><p>' + dpr + '&nbsp;&nbsp;&nbsp;<a href="/shop/' + id4url + '/">詳細を見る</a></p></td></tr></table></div>';
							}
						}else{
							if(map_type==0){
								tempHtml = '<div style="width:400px"><p><strong>' + title + '</strong></p><p>' + dpr + '</p><p><a href="/shop/' + id4url + '/">詳細を見る</a></p></div>';
							}else{
								tempHtml = '<div style="width:400px"><p><strong>ショップ情報　' + title + '</strong></p><p>' + dpr + '</p><p><a href="/shop/' + id4url + '/">詳細を見る</a></p></div>';
							}
						}
					    this.markerHtmlCache[id] = tempHtml;
					}
				}
			}.bind(this)
		};
		var lat = this.map.getCenter().y;
		var lng = this.map.getCenter().x;
		var bounds = this.map.getBounds();
		var nelat = bounds.getNorthEast().lat();
		var nelng = bounds.getNorthEast().lng();
		var swlat = bounds.getSouthWest().lat();
		var swlng = bounds.getSouthWest().lng();
		if(map_type==0){
			var cid = $('map_cid').value;
			var sid = $('map_sid').value;
			var cnt = $('map_cnt').value;
			var kw = $('map_kw').value;
			new Ajax.Request('/shop/get_shop.js?cid=' + cid  + '&sid=' + sid + '&cnt=200&kw=' + kw + '&lat=' + lat + '&lng=' + lng + '&nelat=' + nelat + '&nelng=' + nelng + '&swlat=' + swlat + '&swlng=' + swlng  + '&rd=' + (new Date).getTime(), options);
		}else{
			this.shopOnThreadMap = true;
			new Ajax.Request('/shop/get_shop.js?&cnt=100&lat=' + lat + '&lng=' + lng + '&nelat=' + nelat + '&nelng=' + nelng + '&swlat=' + swlat + '&swlng=' + swlng  + '&rd=' + (new Date).getTime(), options);
		}
	},
	getThreadOnMapPoint: function(){
		var options = {
			method : 'get',
			onFailure: function() { alert("通信エラー"); },
			onComplete: function(res) {
				var obj = eval('(' + res.responseText + ')');
				var icon = new GIcon();
				icon.shadow = "/contents/images/default/map/mm_sh_shadow.png";
				icon.iconSize = new GSize(16, 24);
				icon.shadowSize = new GSize(30, 24);
				icon.iconAnchor = new GPoint(6, 20);
				icon.infoWindowAnchor = new GPoint(5, 1);
				for(i=0;i<obj.result.length;i++){
					lat = obj.result[i].lat;
					lng = obj.result[i].lng;
					id = 'th' + obj.result[i].id;
					id4url = my_uZeroPadding(obj.result[i].id,12);
					ttype = obj.result[i].type;
					title = obj.result[i].title;
					text = obj.result[i].text;
					timg = obj.result[i].timg;
					uid = obj.result[i].uid;
					uname = obj.result[i].uname;
					ccnt = obj.result[i].ccnt;
					icon.image = "/contents/images/default/map/mm_th" + ttype + ".png";
					this.addMarker(lat, lng, id, icon, title);
					if(!this.markerHtmlCache[id]){
						this.thread_list.push(id);
						var tempHtml = '';
						if( timg!= '' ){
							tempHtml = '<div style="width:400px"><p>投稿者:' + uname + 'さん</p><p>コメント:' + ccnt + '件</p><p><strong>' + title + '</strong></p><table><tr><td><img src=\'' + $('thread_image_url_base').value + 'bti/' + timg + '.ss?i=' + $('thread_image_pc').value + '\' alt=\'' + title + '\' /></td><td style="padding:5px"><p>' + text + '</p><p><a href="/space/' + id4url + '/">このスレッドを見る</a></p></td></tr></table></div>';
						}else{
							tempHtml = '<div style="width:400px"><p>投稿者:' + uname + 'さん</p><p>コメント:' + ccnt + '件</p><p><strong>' + title + '</strong></p><p>' + text + '</p><p><a href="/space/' + id4url + '/">このスレッドを見る</a></p></div>';
						}
					    this.markerHtmlCache[id] = tempHtml;
					}
				}
			}.bind(this)
		};
		var lat = this.map.getCenter().y;
		var lng = this.map.getCenter().x;
		var bounds = this.map.getBounds();
		var nelat = bounds.getNorthEast().lat();
		var nelng = bounds.getNorthEast().lng();
		var swlat = bounds.getSouthWest().lat();
		var swlng = bounds.getSouthWest().lng();
		new Ajax.Request('/space/get_thread.js?cmd=lp&cnt=200&lat=' + lat + '&lng=' + lng + '&nelat=' + nelat + '&nelng=' + nelng + '&swlat=' + swlat + '&swlng=' + swlng  + '&rd=' + (new Date).getTime(), options);
	},
	getThreadOnMapLatest: function(){
		img_path = this.img_path;
		var win_name = img_path;
		if(img_path == "default")win_name = "mypl";		
		$("map_latest_thread").innerHTML = '<p class="alnC pt10"><img src=\'/contents/images/'+ img_path +'/common/ajax-loader.gif\' alt=\'処理中です。しばらくお待ちください。\' width=\'32\' height=\'32\' /></p>';
		var options = {
			method : 'get',
			onFailure: function() { alert("通信エラー"); },
			onComplete: function(res) {
				var obj = eval('(' + res.responseText + ')');
				//地図上の最新スレッドテーブル
				var mtable = $("map_latest_thread");
				mtable.innerHTML = '';
				var thread_list_html = '<table class="stripedtable" >';
//				thread_list_html += '<tr><th><p>タイトル（コメント数）</p></th>';
//				thread_list_html += '<th><p>スレッド投稿者</p></th>';
//				thread_list_html += '<th><p>スレッド投稿日</p></th></tr>';
				if(obj.status<0){
					thread_list_html += '<tr class="oddline"><td colspan="4"><p class="alnL">この地域にスレッドはありません。</p></td></tr>';
				}else{
					for(i=0;i<obj.result.length;i++){
						ttype = obj.result[i].type;
						type_str = obj.result[i].type_str;
						title = obj.result[i].title;
						uid = obj.result[i].uid;
						uname = obj.result[i].uname;
						upr = obj.result[i].upr;
						uimg = obj.result[i].uimg;
						id = obj.result[i].id;
						//update_date = obj.result[i].update_date.replace('-','/').replace('-','/');
						update_date = obj.result[i].update_date;
						community_id = obj.result[i].community_id;
						mbase_id = obj.result[i].mbase_id;
						ccnt = obj.result[i].ccnt;
						if (i % 2 == 0) thread_list_html += '<tr class="oddline">';
						else thread_list_html += '<tr class="evenline">';
						thread_list_html += '<td><p class="alnL">';
						if(community_id>0){
							thread_list_html += '<img src="/contents/images/'+ img_path +'/space/icon/icn_commu_thread_' + ttype + '.gif" title="' + type_str + '" alt="' + type_str + '"> ';
							thread_list_html += '<a href="/community/' + my_uZeroPadding(community_id,12) + '/space?tid=' + id + '&cmt=' + ccnt + '"';
						}else if(mbase_id>0){
							thread_list_html += '<img src="/contents/images/'+ img_path +'/space/icon/icn_machi_thread_' + ttype + '.gif" title="' + type_str + '" alt="' + type_str + '"> ';
							thread_list_html += '<a href="/space/' + my_uZeroPadding(id,12) + '/?cmt=' + ccnt + '"';
						}else{
							thread_list_html += '<img src="/contents/images/'+ img_path +'/space/icon/icn_thread_' + ttype + '.gif" title="' + type_str + '" alt="' + type_str + '"> ';
							thread_list_html += '<a href="/space/' + my_uZeroPadding(id,12) + '/?cmt=' + ccnt + '"';
						}
						thread_list_html += '>'+ title + '</a>（' + ccnt + '）</p></td>';
						thread_list_html += '<td><p id="p1_' + id + '"><a href="/user/' + my_uZeroPadding(uid,8) + '/"';
						thread_list_html += ' onmouseout="Windows.close(\'w1_' + id + '\', event);"';
						thread_list_html += ' onmouseover="openWindowElement(\'tipmap11_' + id + '\',\'p1_' + id + '\',\'' + win_name + '03\',\'\',\'250px\',\'138px\',false,14,0,true);">' + uname + '</a> ';
						thread_list_html += '</p><div id="tipmap11_' + id + '" style="display:none;"><div class="textbox_03 clearfix mt10 mr10">';
						thread_list_html += '<img class="fltL mr10" src="' + $('thread_image_url_base').value + 'pui/' + uimg + '?i=' + $('thread_image_pc').value + '" alt="' + uname + 'さん" />';
						thread_list_html += '<p><strong>' + uname + '</strong>さん<br />' + upr + '</p></div></div></td>';
						thread_list_html += '<td><p>' + update_date + '</p></td></tr>';
					}
				}
				thread_list_html += '</table>';
				mtable.innerHTML = thread_list_html;
			}.bind(this)
		};
		var lat = this.map.getCenter().y;
		var lng = this.map.getCenter().x;
		var bounds = this.map.getBounds();
		var nelat = bounds.getNorthEast().lat();
		var nelng = bounds.getNorthEast().lng();
		var swlat = bounds.getSouthWest().lat();
		var swlng = bounds.getSouthWest().lng();
		new Ajax.Request('/space/get_thread.js?cmd=lt&cnt=10&lat=' + lat + '&lng=' + lng + '&nelat=' + nelat + '&nelng=' + nelng + '&swlat=' + swlat + '&swlng=' + swlng  + '&rd=' + (new Date).getTime(), options);
	}
}

//myplMapクラス
var myplMap = Class.create();
myplMap.prototype = {
	initialize: function(lat, lng, zoom, map_type){
		if (GBrowserIsCompatible()) {
			this.mapElement = $("gmap");
			this.markerDraggable = false;
			this.map = new GMap2(this.mapElement);
			this.map.setCenter(new GLatLng(lat, lng), zoom);
			if(0 <= map_type && map_type < 3){
				this.map.setMapType(G_DEFAULT_MAP_TYPES[map_type]);
			}
		}else{
		    $('notice').innerHTML += 'ご利用の環境では、Google Mapsが利用できません。';
		}
    },
	addMarker: function(lat, lng, id, ttype, ic, tip){
		var icon = new GIcon();
		icon.image = "/contents/images/default/map/mm_" + ic + ttype + ".png";
		icon.shadow = "/contents/images/default/map/mm_shl_shadow.png";
		icon.iconSize = new GSize(30, 45);
		icon.shadowSize = new GSize(60, 48);
		icon.iconAnchor = new GPoint(15, 43);
		icon.infoWindowAnchor = new GPoint(5, 1);
		var marker = new GMarker(new GLatLng(lat, lng), {icon:icon,title:tip});
		this.map.addOverlay(marker);
	},
	addMarkerFix: function(lat, lng, id, tip){
		var marker = new GMarker(new GLatLng(lat, lng), {icon:icon_fix,title:tip});
		this.map.addOverlay(marker);
	},
	addStandardControl: function(){
		this.map.addControl(new GLargeMapControl());
		this.map.addControl(new GMapTypeControl());
	},
	addSmallControl: function(){
		this.map.addControl(new GSmallMapControl());
		this.map.addControl(new GMapTypeControl());
	},
	disableDrag: function(){
		this.map.disableDragging();
	}	
}

//汎用Mapクラス
var commonMap = Class.create();
commonMap.prototype = {
	initialize: function(lat, lng, zoom, map_type){
		if (GBrowserIsCompatible()) {
			this.mapElement = $("gmap");
			this.map = new GMap2(this.mapElement);
			this.markerCache = {};
			this.markerHtmlCache = {};
			this.map.setCenter(new GLatLng(lat, lng), zoom);
			if(0 <= map_type && map_type < 3){
				this.map.setMapType(G_DEFAULT_MAP_TYPES[map_type]);
			}
		}else{
		    $('notice').innerHTML += 'ご利用の環境では、Google Mapsが利用できません。';
		}
    },
	addMarker: function(lat, lng, id, html, icon, tip){
		if(tip == undefined)tip = '';
		var marker = new GMarker(new GLatLng(lat, lng), {icon:icon,title:tip});
		GEvent.addListener(marker, "click", this.setMarkerHtml.bind(this, id));
		this.markerCache[id] = marker;
		this.markerHtmlCache[id] = html;
		this.map.addOverlay(marker);
	},
    setMarkerHtml: function(id){
    	if( this.markerHtmlCache[id] && this.markerHtmlCache[id]!='' ){
		    this.markerCache[id].openInfoWindowHtml(this.markerHtmlCache[id]);
    	}
    },
	addStandardControl: function(){
		this.map.addControl(new GLargeMapControl());
		this.map.addControl(new GMapTypeControl());
	},
	disableDrag: function(){
		this.map.disableDragging();
	},
	addTopEntry: function(){
		this.map.addControl(new TopEntryControl());
	}
}

//telShopMapクラス
var telShopMap = Class.create();
telShopMap.prototype = {
	initialize: function(lat, lng, zoom, map_type){
		if (GBrowserIsCompatible()) {
			this.mapElement = $("gmap");
			this.markerDraggable = false;
			this.map = new GMap2(this.mapElement);
			this.map.setCenter(new GLatLng(lat, lng), zoom);
			this.markerCache = {};
			this.markerHtmlCache = {};
			if(0 <= map_type && map_type < 3){
				this.map.setMapType(G_DEFAULT_MAP_TYPES[map_type]);
			}
		}else{
		    $('notice').innerHTML += 'ご利用の環境では、Google Mapsが利用できません。';
		}
    },
	addMarker: function(lat, lng, id, ttype, tip, add, tel){
		var icon = new GIcon();
		icon.image = "/contents/images/default/map/mm_" + "sh" + ttype + ".png";
		icon.shadow = "/contents/images/default/map/mm_sh_shadow.png";
		icon.iconSize = new GSize(16, 24);
		icon.shadowSize = new GSize(30, 24);
		icon.iconAnchor = new GPoint(6, 20);
		icon.infoWindowAnchor = new GPoint(5, 1);
		var marker = new GMarker(new GLatLng(lat, lng), {icon:icon,title:tip});
		this.map.addOverlay(marker);
		var tempHtml = '<div><p><strong>' + tip + '</strong></p><p>' + add + '</p><p>' + tel + '</p></div>';
		GEvent.addListener(marker, "click", function(){ this.openInfoWindowHtml(tempHtml); });
		this.markerCache[id] = marker;
		this.markerHtmlCache[id] = tempHtml;
	},
	addMarkerFix: function(lat, lng, id, tip){
		var marker = new GMarker(new GLatLng(lat, lng), {icon:icon_fix,title:tip});
		this.map.addOverlay(marker);
	},
	addStandardControl: function(){
		this.map.addControl(new GLargeMapControl());
		this.map.addControl(new GMapTypeControl());
	},
	showTooltip: function(id){
		if(this.markerCache[id]){
			var marker = this.markerCache[id];
			marker.openInfoWindowHtml(this.markerHtmlCache[id]);
		}
	},
	disableDrag: function(){
		this.map.disableDragging();
	}
}

//eventMapクラス
var eventMap = Class.create();
eventMap.prototype = {
	initialize: function(lat, lng, zoom, map_type){
		if (GBrowserIsCompatible()) {
			this.mapElement = $("gmap");
			this.markerDraggable = false;
			this.map = new GMap2(this.mapElement);
			this.map.setCenter(new GLatLng(lat, lng), zoom);
			if(0 <= map_type && map_type < 3){
				this.map.setMapType(G_DEFAULT_MAP_TYPES[map_type]);
			}
		}else{
		    $('notice').innerHTML += 'ご利用の環境では、Google Mapsが利用できません。';
		}
    },    
	addMarker: function(lat, lng, id, title, open_date, img_id, event_category){
		if(lat<1) return;
		if(event_category == undefined)event_category = 'event';
		var icon = new GIcon();
		icon.image = "/contents/images/default/map/mm_ev1.png";
		icon.shadow = "/contents/images/default/map/mm_sh_shadow.png";
		icon.iconSize = new GSize(16, 24);
		icon.shadowSize = new GSize(30, 24);
		icon.iconAnchor = new GPoint(6, 20);
		icon.infoWindowAnchor = new GPoint(5, 1);
		var marker = new GMarker(new GLatLng(lat, lng), {icon:icon,title:title});
		this.map.addOverlay(marker);
		var tempHtml = '';
		if(this.img_id){
			tempHtml = '<div><table><tr><td><img src="' + img_id + '" class="pr10" /></td><td><p><strong>' + title + '</strong></p><p>' + open_date + '</p><p><a href="/event/' + my_uZeroPadding(id,11) + '/">詳細を見る</a></p></td></tr></table></div>';
		}else{
			tempHtml = '<div><p><strong>' + title + '</strong></p><p>' + open_date + '</p><p><a href="/' + event_category + '/' + my_uZeroPadding(id,11) + '/">詳細を見る</a></p></div>';
		}
		GEvent.addListener(marker, "click", function(){ this.openInfoWindowHtml(tempHtml); });
	},
	addMarkerFix: function(lat, lng, id, title){
		var marker = new GMarker(new GLatLng(lat, lng), {icon:icon_fix,title:tip});
		this.map.addOverlay(marker);
	},
	addStandardControl: function(){
		this.map.addControl(new GLargeMapControl());
		this.map.addControl(new GMapTypeControl());
	},
	disableDrag: function(){
		this.map.disableDragging();
	}
}

//トップログイン表示
function TopEntryControl() {}
TopEntryControl.prototype = new GControl();
TopEntryControl.prototype.initialize = function(map){
	var btnThEntry = $("top_entry_on_map");
	map.getContainer().appendChild(btnThEntry);
	return btnThEntry;
}
TopEntryControl.prototype.getDefaultPosition = function(){
	return new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(5, 18));
}
