var geocoder;
var marker;
var map;
var client;
var initialLocation; //存放偵測使用者的位置座標
var shopdata;
//Google Map http://code.google.com/intl/zh-TW/apis/maps/documentation/javascript/

//詢價館index首頁，以IP判斷位置，顯示多個店家標記
function InquirySearch() {
    //預設起始的座標位置
    var latLng = new google.maps.LatLng(25.032971, 121.543679);
    //宣告街景服務
    client = new google.maps.StreetViewService();
    //設定地圖顯示的div
    map = new google.maps.Map(document.getElementById("GMapinfo1"), {
        zoom: 14,
        center: latLng,
        streetViewControl: true, 
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    //宣告資訊視窗
    var infowindow = new google.maps.InfoWindow();
    //取得店家的shopid和經緯度
    shopdata = document.getElementById("litShop").value;
    //設置各點標記
    setupMarkers(shopdata);

    //http://code.google.com/intl/zh-TW/apis/maps/documentation/javascript/basics.html#Geolocation

    //偵測使用者的位置
    //使用感應器 (例如 GPS 定位器) 來判斷使用者的位置。 令sensor=true
    //所以aspx必須先載入 <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true">  
    //支援 W3C 地理定位標準 Try W3C Geolocation (Preferred)
    //透過 W3C 取得位置 或是從 Google Gears 取得位置
    if (navigator.geolocation) {
        browserSupportFlag = true;
        navigator.geolocation.getCurrentPosition(
            function(position) {
                initialLocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                map.setCenter(initialLocation);
            }, function() {
                //發生錯誤時處理
                handleNoGeolocation(browserSupportFlag);
            } 
        );
    }
    //或是從 Google Gears 取得位置 Try Google Gears Geolocation
    else if (google.gears) {
        browserSupportFlag = true;
        var geo = google.gears.factory.create('beta.geolocation');
        geo.getCurrentPosition(function(position) {
            initialLocation = new google.maps.LatLng(position.latitude, position.longitude);
            map.setCenter(initialLocation);
        }, function() {
            //發生錯誤時處理
            handleNoGeoLocation(browserSupportFlag);
        });
    }
    //瀏覽器不支援 Browser doesn't support Geolocation
    else {
        browserSupportFlag = false;
        handleNoGeolocation(browserSupportFlag);
    }
    //當發生錯誤時處理 開始
    function handleNoGeolocation(errorFlag) {
        if (errorFlag == true) {
            //alert("Geolocation service failed.");
            //編碼錯誤(ex:地址轉經緯度錯誤) 設置初始地圖顯示位置
            initialLocation = latLng;
        } else {
        //alert("Your browser doesn't support geolocation. We've placed you in Siberia.");
        //編碼錯誤(ex:瀏覽器不支援編碼) 設置初始地圖顯示位置
            initialLocation = latLng;
        }
        //地圖中心座標位置
        map.setCenter(initialLocation);
    }
}
//詢價館productprice.aspx價格資訊頁
function InquirySearch2(shopdata) {
    //alert(shopdata);
    //預設起始的座標位置
    var latLng = new google.maps.LatLng(25.032971, 121.543679);
    //設定地圖顯示的div
    map = new google.maps.Map(document.getElementById("GMap"), {
        zoom: 12,
        center: latLng,
        streetViewControl: true,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    
    //取得店家的shopid和經緯度
    //shopdata = document.getElementById("gShopData").value;
    //設置各點標記
    setupMarkers(shopdata);
}

//設置各點標記 開始
function setupMarkers(shopdata) {
    //宣告資訊視窗
    var infowindow = new google.maps.InfoWindow();
    //切割各店家資訊
    var address2 = shopdata.split("|");
    var leng = address2.length;
    var i = 0;
    var gmarker = [];
    while (i < leng - 1) {
        //建立各點標記
        createMarker(address2[i]);
        i++;
    }
    //建立各點標記 開始
    function createMarker(address) {
        //切割店家資訊 [0]:Shopid [1]:緯度 [2]:經度
        var address2 = address.split(","); //此處的address不是地址而是經緯度
        //宣告店家的點座標
        var point = new google.maps.LatLng(address2[1], address2[2]);
        var hostname = location.pathname;
        if (hostname == "/price/productprice.aspx") { map.setCenter(point); }
        //設置標記資訊
        var marker = new google.maps.Marker({
            map: map, //將點放置在之前宣告的GMapinfo1中
            position: point //標記的位置是之前取得店家的點座標位置
        });
        //宣告一個 iframe 放置資訊視窗的內容
        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=" + address2[0]; //取得某店家的店家資訊
        //宣告偵聽事件
        google.maps.event.addListener(marker, 'click', function() {
            //當點擊地圖上某標記時 將內容iframeDiv設置在資訊視窗內
            infowindow.setContent(iframeDiv)
            infowindow.open(map, marker);
        });

        google.maps.event.addListener(map, 'click', function() {
            //當點擊其他點或是地圖上任一處 觸發資訊視窗關閉事件
            infowindow.close();
        });
    }
}



//詢價館首頁 地圖查詢按鈕觸發事件
function mapSearchtest() {
    //宣告編碼器
    geocoder = new google.maps.Geocoder();
    //取得輸入值 ex:要查詢的地址
    var address = document.getElementById("tbAddress").value;
    if (geocoder) {
        //編碼成功 由地址轉經緯度
        geocoder.geocode({ "address": address }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                //取出經緯度 將地圖中心設置此經緯度
                latLng = results[0].geometry.location;
                map.setCenter(latLng);
            }
            else {
                alert("請輸入正確的地址，謝謝。");
            }
        });
    }
}