﻿// Global Variables
var toolMode = 0;
var currentPoly = null;
var polyCount = 0;


// Vertex class
function Vertex( x, y )
{
    // Member Variables  
    this.x = x;
    this.y = y;
}


// Polygon class
function Polygon( )
{
    // Define Functions
    function addVertex( x, y )
    {
        // Create a new vertex and add it to the collection
        var vtx = new Vertex( x, y );
        this.m_verticies[ this.m_vertexCount ] = vtx;
        
        // Increment the vertex counter
        this.m_vertexCount++;
    }


    function display( )
    {
        if ( this.m_vertexCount > 2 )
        {
            var points = new Array( );
        
            for ( var i = 0; i < this.m_vertexCount; i++ )
            {
                points[i] = new VELatLong( this.m_verticies[i].x ,  
                                           this.m_verticies[i].y );
            }
            
            // Add the first point again to close the polygon
            points[ points.length ] = new VELatLong( this.m_verticies[0].x ,  
                                                     this.m_verticies[0].y );

            // Create the polygon
            var name = "region_" + polyCount;
            var poly = new VEPolygon( name, points );
            poly.SetOutlineWidth(3);
            poly.SetOutlineColor( new VEColor( 0, 150, 100, 1.0 ) );
            poly.SetFillColor( new VEColor( 0, 150, 100, 0.5 ) );
            map.AddPolygon( poly );
            polyCount++;
        }
    }

    // Member Variables
    this.m_verticies = new Array( );
    this.m_vertexCount = 0;

    
    // Member Functions
    this.addVertex = addVertex;
    this.display = display;
}


function regionHandler(e)
{
    // Local Variables
    var latLong = e.view.LatLong;
    var button = null;

    if ( toolMode == 1 )
    {
        // Identify the button that was pressed
        button = window.event.button;

        // Process the click   
        if ( latLong != null )
        {
            if ( button != 2 )
            {
                // Get a reference to the current polygon
                if ( currentPoly == null ) 
                {
                    currentPoly = new Polygon( );
                }
        
                var lat = latLong.Latitude;
                var lng = latLong.Longitude;

                currentPoly.addVertex( lat, lng );
            }
            else
            {
                if ( currentPoly != null )
                {
                    // Instruct the virtual earth api to create the new polygon
                    currentPoly.display( );
        
                    // Dereference the current polygon
                    currentPoly = null;
                }

                // Clear the current tool
                setScrollTool( );
            }
        } 
    }
}


function regionCloseHandler(e)
{
    // Disable the context menu
}


function setScrollTool( )
{
    document.getElementById("myMap").childNodes[0].style.cursor = "default";

    toolMode = 0;
}


function setRegionTool( )
{
    document.getElementById("myMap").childNodes[0].style.cursor = "crosshair";

    toolMode = 1;
}
