﻿var returnPoint = null;             // 回傳用->點位坐標
var returnLat = null;               // 回傳用->點位坐標 X
var returnLng = null;               // 回傳用->點位坐標 y

//================================== 新增點位 ==================================
function AddPointClick(overlay, point)
{	
    if (point)
    {	
    	if (markers.length > 0)
    	{
    		map.removeOverlay(markers[0]);
    		markers.splice(0, 1);
    	}	
        
        var marker = new GMarker(point, {draggable:true, bouncy:true, dragCrossMove:false});
        markers.push(marker);
        map.addOverlay(marker);

        GEvent.addListener(marker, "drag", function() {
            drawPoint();
        });

        GEvent.addListener(marker, "click", function() {
            map.removeOverlay(markers[0]);
            markers.splice(0, 1);
            drawPoint();
        });

        drawPoint();
    }
}

function drawPoint()
{
    if (markers == null || markers.length == 0) return;
    if (returnPoint != null) returnPoint.value = markers[0].getLatLng();
    if (returnLat != null) returnLat.value = markers[0].getLatLng().lat();
    if (returnLng != null) returnLng.value = markers[0].getLatLng().lng();
}
//==============================================================================


//================================== 檢視點位 ==================================
function ShowPointList(_Points, pointLevel)
{
    var arrayPoint = (_Points) ? _Points.substring(1, _Points.length - 1).split("),(") : null;
    
    if (arrayPoint)
    {
        for (var i = 0; i < arrayPoint.length; i++)
        {
            ShowPoint(arrayPoint[i].split(", ")[0], arrayPoint[i].split(", ")[1], arrayPoint[i].split(", ")[2], arrayPoint[i].split(", ")[3], arrayPoint[i].split(", ")[4], pointLevel, false);
        }
    }
}

function ShowPoint(pointX, pointY, iconUrl, pointTitle, pointInfo, pointLevel, isLanLat)
{
	if (pointX == "" || pointY == "") return;
	
    var marker = null;
    var point = new GLatLng(pointX, pointY);
    
    if (pointTitle == null) pointTitle = "";
    
    if (isLanLat) pointTitle += "(" + point.lng() + ", " + point.lat() + ")";
    
    if (iconUrl)
    {
        var square = new GIcon();
        
        square.maxHeight = 1;
        addIcon(square, 43, 43, iconUrl);
        
        marker = new GMarker(point, {icon:square, draggable:false, bouncy:false, dragCrossMove:true, title:pointTitle} );
    }
    else
    {
        marker = new GMarker(point, {draggable:false, bouncy:false, dragCrossMove:true, title:pointTitle} );
    }
    
    if (pointInfo != null && pointInfo != "null")
    {
        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml("<div class='infowindow'><iframe src='" + pointInfo + "' frameborder='0' scrolling='no' width='280' height='80'><\/iframe><\/div>");
        });
    }
    
    map.addOverlay(marker);
    map.setCenter(point, (pointLevel) ? pointLevel : 17);
}
//==============================================================================


//================================== 編輯點位 ==================================
function EditPoint(pointX, pointY)
{
	if (pointX == null || pointX.value == "") return;
    
    var point = new GLatLng(pointX.value, pointY.value);
    
    AddPointClick(map, point);
    map.setCenter(point, 17);
}
//==============================================================================

//================================== 跟隨點位 ==================================
var specialMarker;

function follow(icon, sizeX, sizeY)
{
    var dog = true;
    var noMore = false;

    //map.clearOverlays();
    if (specialMarker != null)
    {
        map.removeOverlay(specialMarker);
    }

    var mouseMove = GEvent.addListener(map, 'mousemove', function(cursorPoint)
    {
        if (!noMore)
        {
            if (icon != null)
            {
                var square = new GIcon();
                addIcon(square, sizeX, sizeY, icon);
                square.maxHeight = 1;   //拖拽標記時視覺上垂直「上升」的距離
                
                specialMarker = new GMarker(cursorPoint, {icon:square, draggable:true, autoPan:false} );
            }
            else
            {
                specialMarker = new GMarker(cursorPoint, {draggable:true, autoPan:false} );
            }
            
            //markers.push(marker);
            map.addOverlay(specialMarker);
            specialMarker.setImage(icon);
            noMore = true;
            
            // This function deletes the marker when dragged outside map
            GEvent.addListener(specialMarker, 'drag', function(markerPoint)
            {
                if (!map.getBounds().containsLatLng(markerPoint))
                {
                    map.removeOverlay(specialMarker);
                }

                drawSpecial();
            });
        }
        if (dog)
        {
            specialMarker.setLatLng(cursorPoint);
        }
        
        drawSpecial();
    });
    var mapClick = GEvent.addListener(map, 'click', function()
    {
        dog = false;
        // 'mousemove' event listener is deleted to save resources
        GEvent.removeListener(mouseMove);
        GEvent.removeListener(mapClick);
    });
}

function drawSpecial()
{       
    // 寫入經緯度
    if (specialMarker != null)
    {
        if (returnPoint != null) returnPoint.value = specialMarker.getLatLng();
        if (returnLat != null) returnLat.value = specialMarker.getLatLng().lat();
        if (returnLng != null) returnLng.value = specialMarker.getLatLng().lng();
    }
}
//==============================================================================