﻿var COLORS = [["red", "#ff0000"], ["orange", "#ff8800"], ["green","#008000"], ["blue", "#000080"], ["purple", "#800080"]];
var defaultCenter = new GLatLng(23.6, 121);
var options = {};
var colorIndex_ = 0;
var map;
var bounds;
var polygons = new Array();

function mapLoad(containerid)
{
    if (containerid == null) containerid = "my_map";
    if ( GBrowserIsCompatible() )
    {
        map = new GMap2($get(containerid), {draggableCursor:'auto', draggingCursor:'move'});
        
        map.setCenter(defaultCenter, 7);            // 初始化地圖
        map.setUIToDefault();
        
        bounds = new GLatLngBounds();
    }
}

function getColor(named)
{
    return COLORS[(colorIndex_++) % COLORS.length][named ? 0 : 1];
}

function getIcon(color)
{
    var icon = new GIcon();
    icon.image = "http://google.com/mapfiles/ms/micons/" + color + ".png";
    icon.iconSize = new GSize(32, 32);
    icon.iconAnchor = new GPoint(15, 32);
    return icon;
}

function startShape()
{
    var color = getColor(false);
    var polygon = new GPolygon([], color, 1, 0.7, color, 0.2);
    startDrawing(polygon, color);
}

function startLine() {
    var color = getColor(false);
    var line = new GPolyline([], color);
    startDrawing(line, color);
}

function startDrawing(poly, color)
{
    map.addOverlay(poly);
    poly.enableDrawing(options);
    poly.enableEditing({onEvent: "mouseover"});
    poly.disableEditing({onEvent: "mouseout"});
    
    GEvent.addListener(poly, "endline", function() {
        GEvent.addListener(poly, "click", function(latlng, index) {
          if (typeof index == "number")
          {
            poly.deleteVertex(index);
          }
          else
          {
            var newColor = getColor(false);
            poly.setStrokeStyle({color: newColor, weight: 4});
          }
        });
    });
}

function positionLatLng(lng, lat)
{
    try
    {
        if ( isNaN(lng) || isNaN(lat) )
        {
            alert("經緯度格式錯誤");
            return;
        }
        
        var point = new GLatLng(lng, lat);
        map.setCenter(point);
    }
    catch (e)
    {
        // 不處理
    }
}

function displayPolygon(id)
{
    var oCheck = $get("chkPolygon" + id);
    var oPolygon = polygons[id - 1];
    
    if (oCheck.checked == true)
    {
        //map.addOverlay(oPolygon);
        oPolygon.show();
    }
    else
    {
        //map.removeOverlay(oPolygon);
        oPolygon.hide();
    }
}

function addGeoXML(_Site, _Village)
{
    //var geoXml = new GGeoXml(_Site + "kmldata/CityKML.ashx?c_id=" + _Village + "&ls=80,330000,3&ps=25,ffffcc");
    var geoXml = new GGeoXml("http://ecolife.epa.gov.tw/kmldata/CityKML.ashx?c_id=" + _Village + "&ls=80,330000,3&ps=25,ffffcc");
    map.addOverlay(geoXml);
}

//function generateArea()
//{
//    var polyPoints = new Array();
//    
//    polyPoints.push(new GLatLng(25.03683007216890000, 121.56207919120800000));
//    polyPoints.push(new GLatLng(25.03602325049870000, 121.56206846237200000));
//    polyPoints.push(new GLatLng(25.03600380894720000, 121.56333446502700000));
//    polyPoints.push(new GLatLng(25.03735498944220000, 121.56335592269900000));
//    polyPoints.push(new GLatLng(25.03736471011120000, 121.56241178512600000));
//    polyPoints.push(new GLatLng(25.03683007216890000, 121.56207919120800000));
//    
//    var color = getColor(false);
//    var polygon1 = new GPolygon(polyPoints, color, 2, 0.7, color, 0.2);
//    
//    map.addOverlay(polygon1);
//    
//    color = getColor(false);
//    var polygon2 = new GPolygon(
//        [
//            new GLatLng(25.03570246450490000, 121.56384944915800000),
//            new GLatLng(25.03500256487930000, 121.56382799148600000),
//            new GLatLng(25.03496368144970000, 121.56517982482900000),
//            new GLatLng(25.03568302290250000, 121.56513690948500000),
//            new GLatLng(25.03570246450490000, 121.56384944915800000)
//        ], color, 2, 0.7, color, 0.2);
//    
//    map.addOverlay(polygon2);
//    
//    var point = new GLatLng(25.03683007216890000, 121.56207919120800000);
//    map.setCenter(point, 16);
//}