﻿var map = null;
var marketName = "";
var marketCoordinates = null;
var subdivisionObjects = new Array();
var startupFunctions = new Array();
var mapObjectIDs = new Array();
var ovalDimensions = new Object();
ovalDimensions.center = new Object();
var polygonLongitudes = "";
var polygonLatitudes = "";
var fieldSheetKeys = new Array();
var sessionPolygonLongitudes = "";
var sessionPolygonLatitudes = "";
var sessionFieldSheetKeys = "";
var sessionOvalDimensions = "";
var sessionMapView = "";
var mgAreaObjects = new Array();
var areaObjectIDs = new Array();

var subdivisionShapeLayer = null;
var areasShapeLayer = null;

function addStartupFunction(func) {

    startupFunctions.push(func);
}
         
 function CreateBingMap() {
   map = new VEMap('maparea');

   map.AttachEvent("oncredentialserror", MyHandleCredentialsError);
   map.AttachEvent("oncredentialsvalid", MyHandleCredentialsValid);
   map.onLoadMap = PostLoadBingMap;
}

function LoadBingMap()
{
    map.LoadMap();
}

function PostLoadBingMap() {
    for (var i = 0; i < startupFunctions.length; i++) {
        eval(startupFunctions[i]);
    }
}

function MyHandleError(e) {
    alert("The map had an error: " + e.error);
}

function MyHandleOnMouseOver(e) {
    if (e.elementID) {
        // Disable Nothing
        return false;
    }
}


function MyHandleCredentialsError() {
    alert("The credentials are invalid.");
}
function MyHandleCredentialsValid()
 {
    //alert("The credentials are valid.");
}
function MyHandleOnClick(e) {
    var i; // An index
    // Check if a subdivision was clicked on
    //alert("Handling click on " + e.elementID + "!");
    if (e.elementID != null && !circleToolReady && !polygonToolReady) {
        var shape = map.GetShapeByID(e.elementID);
        //alert("Shape ID: " + shape.GetID() + "!");
        for (i = 0; i < subdivisionObjects.length; i++) {
            if (subdivisionObjects[i].mapId == shape.GetID()) {
                //alert("Matched Shape IDs: " + subdivisionObjects[i].mapId + " to " +shape.GetID() + "!");
                //centerOnSubdivision(subdivisionObjects[i].id);
                // Make sure that no more processing is done
                //return true;
            }
        }
    }
    //alert("Missed it!");
    return false;
}

function addSubdivision(id, name, longitude, latitude, description) {
    var obj = new Object();

    obj.id = id;
    obj.mapId = null;
    obj.name = name;
    obj.description = description;
    obj.latlng = new VELatLong(latitude, longitude);

    // Push the object on to the array of objects
    subdivisionObjects.push(obj);
}

//Add locations using the core classes. The number of locations is
//specified in the count parameter.
function drawSubdivisionsFast() {

    // Create and add the subdivisions layer to the map
    subdivisionShapeLayer = new VEShapeLayer();
    subdivisionShapeLayer.SetTitle("Subdivisions Layer");
    subdivisionShapeLayer.Show();
    map.AddShapeLayer(subdivisionShapeLayer);

    var shapes = new Array();
    var latlongs = new Array();
    var i = 0;
    for (i = 0; i < subdivisionObjects.length; i++) 
    {
        var subdivision = subdivisionObjects[i]; // The current subdivision
        var shape = new VEShape(VEShapeType.Pushpin, subdivision.latlng);
        
        shape.SetTitle(name);
        shape.SetDescription(subdivision.description);
        shape.SetMinZoomLevel(3);
        
        //subdivisionShapeLayer.AddShape(shape);
        shapes.push(shape);
        latlongs.push(subdivision.latlng);
    }
    subdivisionShapeLayer.AddShape(shapes);
    // Get the map ids back
    for (i = 0; i < subdivisionObjects.length; i++) {
        subdivisionObjects[i].mapId = shapes[i].GetID();
    }
    map.SetMapView(latlongs);
}

/*
* Remove all locations using the core API (object ID array mapObjectIDs).
*/
function removeSubdivisionsFast() {
    if (subdivisionShapeLayer) {
        subdivisionShapeLayer.DeleteAllShapes();
    }
    for (i = 0; i < subdivisionObjects.length; i++) {
        subdivisionObjects[i].mapId = null;
    }
}

