﻿var map;
var locs = new Array();
function LoadMap() {
    map = new VEMap('VEMap');
    map.LoadMap(null,12);
    map.Find(
        null,
        '10216 W. Chatfield Littleton, Co  80127',
        null,
        null,
        null,
        null,
        true,
        true,
        true,
        true,
        FindCallBack);
}

function FindCallBack(ShapeLayer, FindResult, Place, HasMore) {
    if (Place != null) {
        if (Place[0] != null) {
            PetrotekWeb.MapService.GetLocations(WebServiceCallBack);
        }
    }
}

function WebServiceCallBack(Response) {
    

    for (var i = 0; i < Response.length; i++) {
        AddPushPin(Response[i], i);
        AddRow(Response[i], i);
        var loc = new VELatLong(Response[i].Latitude, Response[i].Longitude);
        locs.push(loc);
    }
    map.SetMapView(locs);

}

function AddPushPin(point, index) {
    var pin = new VEPushpin(
        index,
        new VELatLong(point.Latitude, point.Longitude),
        null,   // default image
        point.Name,
        point.Description);
  //  map.AddPushpin(pin);

    var shape = new VEShape(VEShapeType.Pushpin, new VELatLong(point.Latitude, point.Longitude));
    var string = "<div class='pinStyle1'><div class='text'>" + index + "</div></div>";
    shape.SetCustomIcon(string);
    shape.SetTitle(point.Name);
    shape.SetDescription(point.Description);
    map.AddShape(shape);


}




function AddRow(point, index) {
    var table = $get("PointTable");

    var row = table.insertRow(table.rows.length);
    var idCell = row.insertCell(0);
    idCell.appendChild(document.createTextNode(index));

    var nameCell = row.insertCell(1);
    nameCell.appendChild(document.createTextNode(point.Name));

    var descriptionCell = row.insertCell(2);
    descriptionCell.appendChild(document.createTextNode(point.Description));

    var latCell = row.insertCell(3);
    latCell.appendChild(document.createTextNode(point.Latitude));

    var longCell = row.insertCell(4);
    longCell.appendChild(document.createTextNode(point.Longitude));

    $addHandler(row, "click", RowClick);

}

function RowClick(e) {
    //Center the map where the thing was just clicked   
    map.PanToLatLong(new VELatLong(e.target.parentElement.cells[3].innerText, e.target.parentElement.cells[4].innerText));
}





