var map,mgr, geocoder, marker;
var myPano;
function GLoadMapsScript(){
    document.write("<script src='http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAi7BYpTnw_WbMesSXi_0dNRRjk5Zh6SX_hyyIn7BZmjxh_3AxNxR8U97-Bly6masy6R9CTHew1Pg6QA&sensor=true' type='text/javascript'></script>");
}
GLoadMapsScript();

function singleAddress() { //取得店家地圖(單點)
    //取得經緯度與店家地址
    var gPoint1 = document.getElementById('gPoint_1').innerHTML;//緯度
    var gPoint2 = document.getElementById('gPoint_2').innerHTML;//經度
    var address = document.getElementById('address').innerHTML;
    if (GBrowserIsCompatible()) {
        //取得是否顯示街景：0為不顯示,1為顯示
        var streetview = document.getElementById('streetview').innerHTML
        if (streetview == "0"){ 
            document.getElementById("GMap_1").style.width="625px";
            document.getElementById("GMap_2").style.display="none";
        }
        //判斷有無經緯度內容，沒有經緯度則由地址轉經緯度
        if ( gPoint1!="" && gPoint2!="" ){
            var point = new GLatLng(gPoint1,gPoint2);
            //GMap_1內容放置地圖，GMap_2內容放置街景
            map = new GMap2(document.getElementById("GMap_1"));
            map.enableRotation();
            map.setUIToDefault();
            map.setCenter(point, 16);
            marker = new GMarker(point);
            map.disableScrollWheelZoom();
            map.addOverlay(marker);
            var povopts = { yaw:180, pitch:0, zoom:1 };
            var panoramaOptions = { latlng:point, pov:povopts };
            myPano = new GStreetviewPanorama(document.getElementById("GMap_2"), panoramaOptions);
            //若街景顯示錯誤(此處若無街景資料)，則呼叫handleNoFlash
            GEvent.addListener(myPano, "error", handleNoFlash);    
        } else{
            //宣告轉譯編碼
            geocoder = new GClientGeocoder();
            geocoder.getLatLng(address, function(point) {
                if (!point) {
                    map.setCenter(new GLatLng(25.048167,121.517102), 16);
                    marker = new GMarker(new GLatLng(25.048167,121.517102));
                    map.addOverlay(marker);
                    marker.openInfoWindowHtml("找不到該地址，無法顯示相關地圖");
                } else {
                    map = new GMap2(document.getElementById("GMap_1"));
                    map.enableRotation();
                    map.setUIToDefault();
                    map.setCenter(point, 16);
                    marker = new GMarker(point);
                    map.disableScrollWheelZoom();
                    map.addOverlay(marker);
                    //把轉譯後的經緯度顯示出來
                    document.getElementById("gPoint_1").innerHTML = point.lat();
                    document.getElementById("gPoint_2").innerHTML = point.lng();
                    var povopts = { yaw:180, pitch:0, zoom:1 };
                    var panoramaOptions = { latlng:point, pov:povopts };
                    myPano = new GStreetviewPanorama(document.getElementById("GMap_2"), panoramaOptions);
                    GEvent.addListener(myPano, "error", handleNoFlash);    
                }
              }
            );
        }
    }
}

function handleNoFlash(errorCode) {
  //alert(errorCode);
  if (errorCode == 600){//NO_NEARBY_PANO
    //alert("Error: No panorama data was found.");
    //return;
   document.getElementById("GMap_2").innerHTML = "本地址目前尚無街景檢視資訊。";
   
  }
  if (errorCode == 601){//NO_PHOTO
    alert("Error: No user photo was found.");
    return;
  }
  if (errorCode == 603) {//FLASH_UNAVAILABLE
    alert("Error: Flash doesn't appear to be supported by your browser.");
    return;
  }
}
//start

function multipleAddress(address, shopno) { //取得店家地圖(多點)
     //alert(address);
     if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("GMap"));
        map.setCenter(new GLatLng(25.032971, 121.543679), 12);
        map.setUIToDefault();
        setupMarkers(address, shopno);
     }
}
function setupMarkers(address, shopno) {
    var address2 = address.split("|");
    var shopno2 = shopno.split("|");
    var leng=address2.length;   
    var i=0;   
    var gmarker = [];
    mgr = new GMarkerManager(map);
    while(i<leng-1){
        //alert(address2[i]);
        var marker2 = createMarker(address2[i], shopno2[i]); 
        gmarker.push(marker2);  
        mgr.refresh();
        i++;
    }
    mgr.addMarkers(gmarker, 4);
    mgr.refresh();
}

function createMarker(address, shopno) {          
    var address2 = address.split(",");//此處的address不是地址而是經緯度
    var point = new GLatLng(address2[0],address2[1]);
    map.setCenter(point, 12);
    marker = new GMarker(point);
    
    GEvent.addListener(marker,"click", function() {
        var iframeDiv = document.createElement("iframe");
        iframeDiv.setAttribute('id','iframeDiv');
        iframeDiv.setAttribute('name','iframeDiv');
        iframeDiv.setAttribute('frameborder','0',0);
        iframeDiv.setAttribute('allowtransparency','true',0);
        iframeDiv.src = "shopinfo2.aspx?shopno=" + shopno;
        map.openInfoWindowHtml(point,iframeDiv);
    });
    return marker;
}

function showAddress3(address) { //取得店家地圖(單點)
    if (GBrowserIsCompatible()) {
      //alert(address);
      map = new GMap2(document.getElementById("GMapinfo2"));
      map.setUIToDefault();
      //map.addControl(new GSmallMapControl());
      geocoder = new GClientGeocoder();
      geocoder.getLatLng(address, function(point) {
            if (!point) {
              map.setCenter(new GLatLng(25.048167,121.517102), 16);
              marker = new GMarker(new GLatLng(25.048167,121.517102));
              map.addOverlay(marker);
              marker.openInfoWindowHtml("找不到該地址，無法顯示相關地圖");
            } else {
              map.setCenter(point, 16);
              marker = new GMarker(point);
              map.disableScrollWheelZoom();
              map.addOverlay(marker);
            }
          }
       );
    }
}