var map;
var geocoder = new GClientGeocoder();


/* 地図読み込み */
function mapLoad(defaultSite) {
  /* 地図生成 */
  if(GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    /* map.addControl(new GMapTypeControl());  */
    map.addControl(new GLargeMapControl());

  }
	
	
  /* 表示位置設定 */
  showAddress(defaultSite);
  
}
function my_mapLoad(defaultSite) {
  /* 地図生成 */
  if(GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    /* map.addControl(new GMapTypeControl());  */
    map.addControl(new GLargeMapControl());

  	var point = new GLatLng("35.69002328128623" , "139.69061493873596");
	var icon = new GIcon();
    icon.iconSize = new GSize(30, 30);
    icon.iconAnchor = new GPoint(6, 20);
    icon.infoWindowAnchor = new GPoint(5, 1);
	//icon.infoWindowAnchor = new GPoint(5, 1);
	// マーク
    var marker = new GMarker(point);
	map.addOverlay(marker);

  }

	
	
  /* 表示位置設定 */
  showAddress(defaultSite);
  
}
/* クリック地点の緯度経度を吹き出し表示(編集時) */
function mapLoad_Edit(Latitude,Longitude) {
	if (GBrowserIsCompatible()) {
        //地図を作成
        map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl());
        map.setCenter(new GLatLng(Latitude,Longitude), 16);
        //マーカーを追加
		var marker = new GMarker(new GLatLng(Latitude,Longitude));
		map.addOverlay(marker);
  	
        //地図をクリックしたら、マーカーを追加
        GEvent.addListener(map, "click", function(overlay, point) {
        	if (point) {
        		/* input に緯度経度を入力 */                           
                document.all['Latitude'].value = point.y;
                document.all['Longitude'].value = point.x;                  	               
                /* マーカ表示 */
                var marker = new GMarker(point);
                map.clearOverlays();
                map.addOverlay(marker);
            }
        });
    }
}

/* クリック地点の緯度経度を吹き出し表示 */
function addClickEvent() {
  GEvent.addListener(map, 'click',
                     function(overlay, point) {
                       if (point) {
                         /* input に緯度経度を入力 */                           
                         document.all['Latitude'].value = point.y;
                         document.all['Longitude'].value = point.x;

                         /* 吹き出し表示 */
                         //map.openInfoWindowHtml(point, "<br/>" + "緯度：" + point.y + "<br/>" + "経度：" + point.x)
                         
                         /* マーカ表示 */
                         var marker = new GMarker(point);
                         map.clearOverlays();
                         map.addOverlay(marker);
                       }
                     }
                    );
}

/* ジオコーディング */
function showAddress(address) {
  geocoder.getLatLng(address, 
                      function(latlng) {
                        if(latlng) {                   
                          map.setCenter(latlng, 16);
                        }
                      }
                    );
                    
}

/* 吹き出し表示処理 */
function showMapMessage(id, latitude, longitude) {
  if(latitude != "" && longitude != "") {
    map.setCenter(new GLatLng(latitude, longitude), 13);
    //map.openInfoWindowHtml(map.getCenter(), document.createTextNode("緯度：" + latitude + " " + "経度：" + longitude));
    
    /* マーカ表示 */
    var point = new GLatLng(latitude, longitude);
    marker = new GMarker(point);
    map.addOverlay(marker);    
  }
}


/* 入力チェック */
function entry_chk(obj) {

  // 入力値から空白削除
  var Title = obj.Title.value.replace(/ /g,"");
  var Link = obj.Link.value.replace(/ /g,"");
  var Description = obj.Description.value.replace(/ /g,"");
  var Latitude = obj.Latitude.value.replace(/ /g,"");
  var Longitude = obj.Longitude.value.replace(/ /g,"");  
  var Attached = obj.Attached.value.replace(/ /g,"");
  var Pref = obj.Pref.value.replace(/ /g,"");
  var PublicStatus = obj.PublicStatus.value.replace(/ /g,"");  

  // 必須入力チェック
  if (Title=="" || Description=="" || Latitude=="" || Longitude=="" || Pref=="" || PublicStatus=="") {
    alert("入力の無い項目があります。");
    return false;
  }
  
  // 画像ファイルの拡張子チェック
  if (Attached!="") {
 
    if(Attached.toLowerCase().match(/.gif/)) {
      obj.AttachedExt.value = "gif";
    } 
    else if(Attached.toLowerCase().match(/.bmp/)) {
      obj.AttachedExt.value = "bmp";
    }
    else if(Attached.toLowerCase().match(/.png/)) {
      obj.AttachedExt.value = "png";
    }
    else if(Attached.toLowerCase().match(/.jpeg/)) {
      obj.AttachedExt.value = "jpeg";
    }  
    else if(Attached.toLowerCase().match(/.jpg/)) {
      obj.AttachedExt.value = "jpg";
    }
    else {
      alert("不明な画像ファイルが指定されています。");
      obj.AttachedExt.value = "";
      return false;
    }
  }
  else {
    obj.AttachedExt.value = "";
  }


  // 入力の最大長チェック
  if(100 < Title.length) {
    alert("タイトルは最大100文字までです。");
    return false;
  }
  else if(400 < Description.length) {
    alert("説明は最大400文字までです。");
    return false;
  }
  else if(200 < Link.length) {
    alert("リンクURLは最大200文字までです。");
    return false;
  }    
  else if(200 < Attached.length) {
    alert("画像ファイルのアドレスは最大200文字までです。");
    return false;
  }  
 
  // 都道府県の名称を抜き出す
  obj.PrefName.value = obj.Pref.options[obj.Pref.value].text;
  
  return true;
}



