//
// hotelmap.js
// 施設情報画面で使用するJavaScript群
// GoogleMapを使用。
//
// Copyright
// 2008 Venture Republic Inc. All Rights Reserved.
//

var map;

function load(lat,lng) {
    
    if (GBrowserIsCompatible()) {
      //var st=document.getElementById("map").style;
      /*if(document.all){
          document.all("map").style.height = "300px";
      }else if(document.getElementById){
        document.getElementById("map").style.height = "300px";
      }*/
      map = new GMap2(document.getElementById("map"));
      map.setCenter(new GLatLng(lat,lng), 17);
      map.addControl(new GSmallMapControl());
      var icon= new GIcon();
      icon.image = '/images/common/map_icon01.png';
      icon.shadow = '/images/common/map_icon01_s.png';
      icon.iconSize = new GSize(25, 26);
      icon.iconAnchor = new GPoint(10, 10);
      icon.shadowSize = new GSize(25, 26);
      icon.infoWindowAnchor = new GPoint(13, 10);
      var opt = new Object();
      opt.icon=icon;
      marker=new GMarker(map.getCenter(),opt);
      map.addOverlay(marker);
    }
}


function ispoint(lat,lng){
    if (lat !='' && lng != '' ){
        if (!isNaN(lat) && !isNaN(lng)){
            if (lng <=180 && lng >=-180){
                if (lat <=90 && lat >=-90){
                    return true;
                }
            }
        }
    }
    return false;
}



function handleNoFlash(errorCode) {
    if (errorCode == 603) {
        alert("Error: Flashがストリーtビューに対応していません。インストールORアップデートしてください。");
        return;
    }
    else{
     
     myPano.hide();
     return;
    }
} 

function openDiag(url){
  window.open(url,"diagwindow","scrollbars=no,status=no,toolbar=no,height=300");
}


//登録画面用
function initGmap(){
    if (ispoint(document.forms[0].elements['tbl_hotel[y]'].value,document.forms[0].elements['tbl_hotel[x]'].value)){
        load(document.forms[0].elements['tbl_hotel[y]'].value,document.forms[0].elements['tbl_hotel[x]'].value);                
        GEvent.addListener(map, "click", function(overlay,latlng) {
            marker.setLatLng(latlng);
            document.forms[0].elements['tbl_hotel[y]'].value=latlng.lat();
            document.forms[0].elements['tbl_hotel[x]'].value=latlng.lng();
            myPano.setLocationAndPOV(latlng);
        });
        var defPoint = new GLatLng(document.forms[0].elements['tbl_hotel[y]'].value,document.forms[0].elements['tbl_hotel[x]'].value);
        panoramaOptions = { latlng:defPoint };
        myPano = new GStreetviewPanorama(document.getElementById("pano"), panoramaOptions);
        GEvent.addListener(myPano, "error", handleNoFlash);
        moveCenter(defPoint.lat(),defPoint.lng());


    }
}
      
function selectArea(){
    var url="/map?cmd=select&form=0&sform=tbl_hotel[mst_small_area_id]&mform=tbl_hotel[mst_mid_area_id]&pform=tbl_hotel[mst_pref_id]&rform=tbl_hotel[mst_region_id]&aform=area_name";
    if (ispoint(document.forms[0].elements['tbl_hotel[y]'].value,document.forms[0].elements['tbl_hotel[x]'].value)){
        url+= "&x=" + document.forms[0].elements['tbl_hotel[x]'].value + "&y=" + document.forms[0].elements['tbl_hotel[y]'].value
    }
    window.open(url,"area_select","scrollbars=no,status=no,toolbar=no,height=700");
}

var geocoder = new GClientGeocoder();
function formatAddress(){
    if (document.forms[0].elements['tbl_hotel[address]'].value == ''){
        return;
    }
    geocoder.getLocations(document.forms[0].elements['tbl_hotel[address]'].value, addAddressToMap);
    
}

function addAddressToMap(response) {
    
    if (!response || response.Status.code != 200) {
        alert("住所の場所を特定できませんでした。");
    } else {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1],
        place.Point.coordinates[0]);
        map.setCenter(point, 17);
        var strAddress= document.forms[0].elements['tbl_hotel[address]'].value
        try{
          strAddress=place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
          strAddress+=place.AddressDetails.Country.AdministrativeArea.Locality.LocalityName;
          strAddress+=place.AddressDetails.Country.AdministrativeArea.Locality.DependentLocality.DependentLocalityName;
          strAddress+=place.AddressDetails.Country.AdministrativeArea.Locality.DependentLocality.Thoroughfare.ThoroughfareName;
        }
        catch(e){
          document.forms[0].elements['selectarea'].focus();
          document.forms[0].elements['tbl_hotel[x]'].focus();
        }
    
        document.forms[0].elements['tbl_hotel[x]'].value=point.lng();
        document.forms[0].elements['tbl_hotel[y]'].value=point.lat();
        document.forms[0].elements['tbl_hotel[address]'].value=strAddress;


        myPano.hide();
        

        moveCenter(point.lat(),point.lng());



    }
}

function moveCenter(lat,lng){
    if (isNaN(lat) || isNaN(lng) || lat>90 || lat <-90 || lng>180 || lng < -180){
        return;
    }
    if (map==null){
      initGmap();
    }
    var flg=true;
    if (ispoint(lat,lng) && map != undefined){
        map.setCenter(new GLatLng(lat,lng), 17);
        flg=false;
    }
    
    if (flg && map != undefined){
        map.setCenter(new GLatLng(0,0), 17);
    }
    
    if (map != undefined && marker != undefined){
        marker.setLatLng(new GLatLng(lat,lng));
    }

    if (myPano != undefined){
      myPano.setLocationAndPOV(new GLatLng(lat,lng))
    }
    else{
        var defPoint = new GLatLng(document.forms[0].elements['tbl_hotel[y]'].value,document.forms[0].elements['tbl_hotel[x]'].value);
        panoramaOptions = { latlng:defPoint };
        myPano = new GStreetviewPanorama(document.getElementById("pano"), panoramaOptions);
        GEvent.addListener(myPano, "error", handleNoFlash);
    }
}

function selectOnsen(){
    if (document.forms[0].elements['tbl_hotel[mst_small_area_id]'].value=='' || isNaN(document.forms[0].elements['tbl_hotel[mst_small_area_id]'].value) || document.forms[0].elements['tbl_hotel[mst_small_area_id]'].value==0){
        alert('先にエリアを選択してください。');
        return;
    }
    var url = "/list/onsen/"
    url+=document.forms[0].elements['tbl_hotel[mst_small_area_id]'].value + "/"
    url+="?form=0&oform=tbl_hotel[mst_onsen_id]&onform=onsen_name"
    window.open(url,"area_select","scrollbars=no,status=no,toolbar=no,height=700");
}

