function MachinaviMap(id, lng, lat, zoom){
	this.map = null;
	this.id = id;
	this.lng = lng; //経度
	this.lat = lat; //緯度
	this.zoom = zoom;
	this.mapCtlSize_w = 100;
	this.mapCtlSize_h = 100;
	this.chmarker = null;
	this.marker_backup = null;
	this.mn_spot_img = new Array();
	this.mn_spot_text = new Array();
	this.icon_size_x = 33;
	this.icon_size_y = 48;
	this.shadow = "http://r4hs.sakura.ne.jp/mnp0.5/imgs/icons/shadow.png";
	this.shadow_size_x = 58;
	this.shadow_size_y = 48;
	this.icon_anchor_x = 17;
	this.icon_anchor_y = 48;
	this.window_anchor_x = 16
	this.window_anchor_y = 0;
	this.window_size_w = 210;
	this.window_size_h = 150;
	//this.i=0;
}

//マップ作成
function MachinaviMap_loadMap(opt){
	var size = {size : new GSize(610, 500)};
	this.map = new GMap2(document.getElementById(this.id), size); //地図オブジェクト生成
	
	this.map.addControl(new GScaleControl()); //縮尺
	this.map.addControl(new GLargeMapControl); //拡大縮小
	//this.map.addControl(new GOverviewMapControl(new GSize(this.mapCtlSize_x, this.mapCtlSize_y)));
	
	//サテライト切り替え
	this.map.addControl(new GMapTypeControl());
	
	//小さい地図
	smap=new GOverviewMapControl(new GSize(this.mapCtlSize_w,this.mapCtlSize_h));
    this.map.addControl(smap);

	
	if(opt == 1){
		//航空写真にする場合
		this.map.setMapType(G_SATELLITE_MAP);
	}

	this.map.setCenter(new GLatLng(this.lat, this.lng), this.zoom); //Map初期位置決定
}
//マーカー作成
function  MachinaviMap_createMarker(x, y, msg, icon, id){
	var gpObj = new GLatLng(y, x)
	//this.map.setCenter(gpObj, this.zoom);
	var iconObj = new GIcon();
	iconObj.image = icon;
	iconObj.shadow = this.shadow;
	iconObj.iconSize = new GSize(this.icon_size_x, this.icon_size_y);
	iconObj.shadowSize = new GSize(this.shadow_size_x, this.shadow_size_y);
	iconObj.iconAnchor = new GPoint(this.icon_anchor_x, this.icon_anchor_y);
	iconObj.infoWindowAnchor = new GPoint(this.window_anchor_x, this.window_anchor_y);
	var marker = new GMarker(gpObj, iconObj);
	
	this.map.addOverlay(marker);
	
	//リスナ登録
	if(msg != null){
		var marker_backup = this.marker_backup;
		GEvent.addListener(marker, "click", function(){
			marker.openInfoWindowHtml(msg);
		});
		
		/*this.mn_spot_img[id] = document.getElementById("mn_spot_img"+ id);
		GEvent.addDomListener(this.mn_spot_img[id], "mouseover", function(){
			mnmap.chMarkerImg(x, y, icon, id);
		});*/
		
		this.mn_spot_text[id] = document.getElementById("mn_spot_text"+ id);
		GEvent.addDomListener(this.mn_spot_text[id], "mouseover", function(){
			//GEvent.clearListeners(mn_spot_text,  "mouseover");
			mnmap.chMarkerImg(x, y, icon, id);
		});
		
		/*this.mn_spot_img[id] = document.getElementById("mn_spot_img"+ id);
		GEvent.addDomListener(this.mn_spot_img[id], "mouseout", function(){
			//thismap.removeOverlay(mnmap.chmarker);
			//GEvent.clearListeners(mn_spot_img,  "mouseout");
			GEvent.clearInstanceListeners(mnmap.mn_spot_img[id]);
			mnmap.dispMarkerByJson(marker_backup);
		});*/
		
		this.mn_spot_text[id] = document.getElementById("mn_spot_text"+ id);
		GEvent.addDomListener(this.mn_spot_text[id], "mouseout", function(){
			//thismap.removeOverlay(mnmap.chmarker);
			//GEvent.clearListeners(mn_spot_text,  "mouseout");
			GEvent.clearInstanceListeners(mnmap.mn_spot_text[id]);
			mnmap.dispMarkerByJson(marker_backup);
		});
	}
}/*
//マーカー作成
function  MachinaviMap_createMarker(x, y, msg, icon, id){
	var point = new GLatLng(y, x);
	var marker = new GMarker(point, icon); //マーカー生成
	this.map.addOverlay(marker);
	//リスナ登録
	if(msg != null){
		thismap = this.map;
		GEvent.addListener(marker, "click", function(){
			marker.openInfoWindowHtml(msg);
		});
		
		mn_spot_img = document.getElementById("mn_spot_img"+ id);
		GEvent.addDomListener(mn_spot_img, "mouseover", function(){
			thismap.removeOverlay(marker);
			mnmap.chMarkerImg(x, y, msg, icon, id);
		});
		mn_spot_text = document.getElementById("mn_spot_text"+ id);
		GEvent.addDomListener(mn_spot_text, "mouseover", function(){
			thismap.removeOverlay(marker);
			mnmap.chMarkerImg(x, y, msg, icon, id);
		});
		
		mn_spot_img = document.getElementById("mn_spot_img"+ id);
		GEvent.addDomListener(mn_spot_img, "mouseout", function(){
			thismap.removeOverlay(mnmap.chmarker);
			mnmap.createMarker(x, y, msg, icon, id);
			
		});
		mn_spot_text = document.getElementById("mn_spot_text"+ id);
		GEvent.addDomListener(mn_spot_text, "mouseout", function(){
			thismap.removeOverlay(mnmap.chmarker);
			mnmap.createMarker(x, y, msg, icon, id);
		});
	}
}*/

//json_objの数だけマーカー表示
function MachinaviMap_dispMarkerByJson(obj){
	this.marker_backup = obj;
	this.map.clearOverlays();
	
	//var obj_num = 0;
	//for(key in obj){
	//	obj_num++;
	//}

	//var point = new Array(obj_num); //座標入れ
	var msg = new Array();//obj_num); //Windowメッセージ入れ

	var i=0;
	while(obj[i] != null){

		//イベントならマーカーは出さない
		//if (obj[i].event_flg == 1)
      	//	continue;

		//アイコン作成
		/*var icon = new GIcon();
		icon.image = "http://farm.infosys.soft.iwate-pu.ac.jp/~gourm/management/img/icons/IconGreen2.png";
		icon.shadow = "http://farm.infosys.soft.iwate-pu.ac.jp/~gourm/management/img/icons/Shadow.png"; //影の画像
		icon.iconSize = new GSize(32, 32);    //アイコンサイズ
		icon.shadowSize = new GSize(59, 32); //影のサイズ
		//アンカーポイントと画像の左上角からの相対オフセット値
		icon.iconAnchor = new GPoint(20,34); 
		icon.infoWindowAnchor = new GPoint(15,13);
		*/
		msg[i] = "<div style='width:"+ this.window_size_w +"px;height:"+ this.window_size_h +"px;text-align:center;'>"
				/*+ "<table style='width:300px;height:150px;'>"
				+ "<tr><td>名称</td><td>"+ obj[i].title +"</td></tr>"
				+ "<tr><td>住所</td><td>"+ obj[i].address +"</td></tr>"
				+ "<tr><td>URL</td><td>"+ obj[i].url +"</td></tr>"
				+ "<tr><td>画像</td><td>"+ obj[i].photo +"</td></tr>"
				+ "</table>";*/
				
		
		
				+ '<a href="'+ obj[i].detail_url +"/srch_str/"+ pm.getSrchStr()
				+"/ctg_id/"+ pm.getCtgId() +"/sctg_id/"+ pm.getSctgId()
				+"/area_id/"+ pm.getAreaId() +"/page_num/"+ pm.getPageNum() +'">'
				+ obj[i].title + "</a>" +"<br />"
				//+ "住所："+ obj[i].address +"<br />"
				+ '<a href="'+ obj[i].detail_url +"/srch_str/"+ pm.getSrchStr()
				+"/ctg_id/"+ pm.getCtgId() +"/sctg_id/"+ pm.getSctgId()
				+"/area_id/"+ pm.getAreaId() +"/page_num/"+ pm.getPageNum() +'">'
				//'URL：<a href="'+ obj[i].url +'">'+ obj[i].url +'</a><br />'+
				+ '<img src="'+ obj[i].photo +'" alt="画像なし"/></a></div>';
								
		lng = parseFloat(obj[i].lon_d) + (parseFloat(obj[i].lon_m) / 60) + (parseFloat(obj[i].lon_s) / 60 / 60);
		lat = parseFloat(obj[i].lat_d) + (parseFloat(obj[i].lat_m) / 60) + (parseFloat(obj[i].lat_s) / 60 / 60);
		this.createMarker(lng, lat, msg[i], obj[i].icon, obj[i].primary_key);
		i++;
	}
}

//マーカー表示(Detail用)
function MachinaviMap_dispMarker(obj){
	this.map.clearOverlays();
	var i;
	for(i=0; i<1; i++){							
		lng = parseFloat(obj[i].lon_d) + (parseFloat(obj[i].lon_m) / 60) + (parseFloat(obj[i].lon_s) / 60 / 60);
		lat = parseFloat(obj[i].lat_d) + (parseFloat(obj[i].lat_m) / 60) + (parseFloat(obj[i].lat_s) / 60 / 60);
		this.createMarker(lng, lat, null, obj[i].icon);//, icon);
	}
}

function MachinaviMap_dispNearSpotMarker(obj, num){
	this.marker_backup = obj;
	this.map.clearOverlays();

	var msg = new Array();//obj_num); //Windowメッセージ入れ

	var i=0;
	while(obj[i] != null){

		msg[i] = "<div style='width:"+ this.window_size_w +"px;height:"+ this.window_size_h +"px;text-align:center;'>"
				+ '<a href="'+ obj[i].detail_url +'/rcm_srch/1/">'
				+ obj[i].title + "</a>" +"<br />"
				+ '<a href="'+ obj[i].detail_url +'/rcm_srch/1/">'
				+ '<img src="'+ obj[i].photo +'" alt="noimage"/></a></div>';
								
		lng = parseFloat(obj[i].lon_d) + (parseFloat(obj[i].lon_m) / 60) + (parseFloat(obj[i].lon_s) / 60 / 60);
		lat = parseFloat(obj[i].lat_d) + (parseFloat(obj[i].lat_m) / 60) + (parseFloat(obj[i].lat_s) / 60 / 60);
		this.createMarker(lng, lat, msg[i], obj[i].icon, obj[i].primary_key);
		i++;
		if(i==num){
			break;
		}
	}
}

//マーカーの画像を変化させる
function MachinaviMap_chMarkerImg(x, y, icon, id){
	this.map.clearOverlays();
	var gpObj = new GLatLng(y, x)
	this.map.setCenter(gpObj);
	var iconObj = new GIcon();
	iconObj.image = icon;
	iconObj.shadow = this.shadow;
	iconObj.iconSize = new GSize(this.icon_size_x, this.icon_size_y);
	iconObj.shadowSize = new GSize(this.shadow_size_x, this.shadow_size_y);
	iconObj.iconAnchor = new GPoint(this.icon_anchor_x, this.icon_anchor_y);
	iconObj.infoWindowAnchor = new GPoint(this.window_anchor_x, this.window_anchor_y);
	var marker = new GMarker(gpObj, iconObj);
	this.map.addOverlay(marker);
	this.chmarker = marker;
}

/*
function MachinaviMap_getCategoryJson(str, season_id, category_id, producer_id){
	this.map.clearOverlays();
	var obj = new JSONscriptRequest("./index.php?Module=product&Controller=retJson&search_str="+str+"&search_season="+season_id+"&search_category="+ category_id+"&search_producer="+producer_id);
	obj.buildScriptTag();
	obj.addScriptTag();
}*/

function MachinaviMap_setCenter(point, zoom){
	this.map.setCenter(point, zoom);
}

new MachinaviMap(); //ダミー（Netscape Navigator Ver2, Ver3のバグへの対処）
//メソッドの設定
MachinaviMap.prototype.loadMap = MachinaviMap_loadMap;
MachinaviMap.prototype.createMarker = MachinaviMap_createMarker;
MachinaviMap.prototype.dispMarkerByJson = MachinaviMap_dispMarkerByJson;
MachinaviMap.prototype.dispMarker = MachinaviMap_dispMarker;
MachinaviMap.prototype.dispNearSpotMarker = MachinaviMap_dispNearSpotMarker;
MachinaviMap.prototype.chMarkerImg = MachinaviMap_chMarkerImg;
MachinaviMap.prototype.setCenter = MachinaviMap_setCenter;
//MachinaviMap.prototype.getCategoryJson = MachinaviMap_getCategoryJson;

