﻿var map = null;
var links = new Array();
var completedcount = 0;
var totallayers;

function GetMap() {
    map = new VEMap('myMap');
    map.SetCredentials("AgHkWEmeS4tbn6XmmN5_VtSWp5QII0u7JLYXS6_no-kb95h1TmhcDl3UcNBG7O6_");
    map.LoadMap(new VELatLong(37.579412, -96.987304), 4);
    EktronPushPin.GetFundIds(DisplayAllFunds, DisplayError);
}

function popUpWindow(URLStr, left, top, width, height) {
    popUpWin = open('/UserForms/PropertyDetails.aspx?ID=' + URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=yes,width=' + width + ',height=' + height + ',left=' + left + ', top=' + top + ',screenX=' + left + ',screenY=' + top + '');
}

// Replaces newline with <br />
function nl2br(text) {
    return text = text.replace(/\n/g, "<br />");
}


// Hide all layers.
function HideAllLayers() {
    map.HideAllShapeLayers();
}

// Show all layers.
function ShowAllLayers() {
    map.ShowAllShapeLayers();
}


//
function DisplayAllFunds(FundIds) {

    totallayers = FundIds.length;

    var RsltElem = document.getElementById("Links");

    RsltElem.innerHTML = "<p><em>Please wait while pins load...</em></p>";

    for (var count = 0; count < FundIds.length; count++) {
        EktronPushPin.GetPinsByFund(FundIds[count], AddPushpinLayer);
    }
    //var t = setTimeout(GetLayerTitles, 3000);
}

function GetLayerTitles() {

    var total = map.GetShapeLayerCount()
    for (var x = 1; x < total; x++) {
        var layer = map.GetShapeLayerByIndex(x);
        var FundId = layer.GetDescription();
        var FundName = layer.GetTitle();
        FundName = FundName.replace("Behringer Harvard", "");


        links[x - 1] = '<input id="' + FundName + '" name="' + FundName + '" type="checkbox" checked="checked" value="' + FundId + '" onclick="ToggleLayer(\'' + x + '\');" /><label for="' + FundName + '"><img width="12" height="17" src="/images/pushpins/3/' + FundId + '.png" />' + FundName + '</label>';
    }
    links.sort();

    var sortedlinks = "";
    var link;
    for (link in links) {
        sortedlinks += links[link] + '<br />';
    }

    var RsltElem = document.getElementById("Links");

    RsltElem.innerHTML = sortedlinks;
}

function HideLayer(selIndex) {
    layer = map.GetShapeLayerByIndex(selIndex);
    if (layer.IsVisible()) {
        layer.Hide();
    }
}

function ToggleLayer(layerIndex) {
    var layer = map.GetShapeLayerByIndex(layerIndex);
    if (layer.IsVisible()) {
        layer.Hide();
    }
    else
        layer.Show();
}

function AddPushpinLayer(pins) {

    var l = new VEShapeLayer();
    map.AddShapeLayer(l);

    if (pins.length > 0) {
        l.SetTitle(pins[0].FundName);
        l.SetDescription(pins[0].FundId.toString());
    }
    else {
        l.SetTitle("Empty Fund");
        l.SetDescription("Empty Fund");
    }


    for (i = 0; i < pins.length; i++) {

        var newpin = new VEShape(VEShapeType.Pushpin, new VELatLong(pins[i].Latitude, pins[i].Longitude));
        newpin.SetTitle(pins[i].PropertyName);

        var desc = "<div><strong>" + 
        "<br />" + pins[i].City + "</strong><br /><br /><div style='clear:left;padding:3px;height:250px;overflow:auto;'>" + pins[i].Description +
        "</div><br /><br /><strong>" + pins[i].FundName + "</strong><br /><br /><a href=\"javascript: void(0)\" onclick=\"popUpWindow(" +
        pins[i].PropertyId + ", 240, 70, 748, 500);  return false;\">View property details and images.</a>";


        desc = nl2br(desc);

        newpin.SetDescription(desc);
        //newpin.MoreInfoURL = getPopupLink(callBackData[i].PropertyId"].ToString());

        if (pins[i].propertyPhotoPath == null) {
            newpin.SetPhotoURL(null);
        }
        else
            newpin.SetPhotoURL(pins[i].propertyPhotoPath);


        if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
            var ieversion = new Number(RegExp.$1) // capture x.x portion and store as a number
            if (ieversion <= 7)
                newpin.SetCustomIcon("<img src='" + "/images/pushpins/3/" + pins[i].FundId + ".gif'" + "style='margin: -5px 0px 0px 7px'/>"); //without bug
            else
                newpin.SetCustomIcon("<img src='" + "/images/pushpins/3/" + pins[i].FundId + ".png'" + "style='margin: -5px 0px 0px 7px'/>"); //without bug
        }
        else
            newpin.SetCustomIcon("<img src='" + "/images/pushpins/3/" + pins[i].FundId + ".png'" + "style='margin: -5px 0px 0px 7px'/>"); //without bug


        //                map.AddShape(newpin);

        l.AddShape(newpin);

    }
    completedcount++;


    if (completedcount == totallayers)
        GetLayerTitles();
    //l.SetClusteringConfiguration(VEClusteringType.Grid);
}

function GetInfo() {
    alert('The latitude,longitude at the center of the map is: ' + map.GetCenter());
}
function ClearMap() {
    map.DeleteAllShapes();
}
function GetCount() {
    alert("Layers:" + map.GetShapeLayerCount());
}
function DisplayError() {
    alert("unable to access server - pins not loaded");
}
function CenterMap(target) {
    switch (target) {
        case 'DFW':
            map.SetCenterAndZoom(new VELatLong(32.88052, -96.974721), 10);
            break;
        case 'Houston':
            map.SetCenterAndZoom(new VELatLong(29.817412, -95.506896), 11);
            break;
        case 'DC':
            map.SetCenterAndZoom(new VELatLong(38.933775, -77.104797), 11);
            break;
        case 'LA':
            map.SetCenterAndZoom(new VELatLong(33.894357, -118.070068), 10);
            break;
        case 'Philly':
            map.SetCenterAndZoom(new VELatLong(39.953503, -75.165238), 16);
            break;
        case 'Chicago':
            map.SetCenterAndZoom(new VELatLong(41.879275, -87.636222), 16);
            break;
        case 'Atlanta':
            map.SetCenterAndZoom(new VELatLong(33.902336, -84.317321), 11);
            break;
        case 'Europe':
            map.SetCenterAndZoom(new VELatLong(50.861444, 11.074218), 5);
            break;
        case 'US':
            map.SetCenterAndZoom(new VELatLong(37.579412, -96.987304), 4);
        default:
            map.SetCenterAndZoom(new VELatLong(37.579412, -96.987304), 4);
    }
}     

