var map;
var LocationSearchHandler = "/_layouts/VirtualEarth/LocationSearch.ashx";
var _listid;
var _webid;
var _titlefield;
var _descfield;
var tmpPushpinTitle;
var findIcon;

function VEWebPart_LoadMap(divid, latitude, longitude, zoom, type, listid, webid, titlefield, descfield, showsearchbox) {
    // Show the map and hook up events
    map = new VEMap(divid);
    map.SetDashboardSize(VEDashboardSize.Normal);

    map.LoadMap(new VELatLong(latitude, longitude), zoom, type, false);
    map.AttachEvent("onchangeview", view_Changed);

    _listid = listid;
    _webid = webid;
    _titlefield = titlefield;
    _descfield = descfield;

    AddPushPin(latitude, longitude);

    if (showsearchbox) {
        ShowSearchBox();
    }

    view_Changed();
}

function view_Changed() {
    var view = map.GetMapView();
    var geoRSSUrl = LocationSearchHandler + "?hlat=" + view.TopLeftLatLong.Latitude + "&llng=" + view.TopLeftLatLong.Longitude + "&llat=" + view.BottomRightLatLong.Latitude + "&hlng=" + view.BottomRightLatLong.Longitude + "&titlefld=" + _titlefield + "&listid=" + _listid + "&webid=" + _webid;

    geoRSSUrl = geoRSSUrl + "&" + (new Date()).getTime();
    if (_descfield != "") {
        geoRSSUrl = geoRSSUrl + "&descfld=" + _descfield;
    }

    LoadGeoRSS(geoRSSUrl);
}

function LoadGeoRSS(url) {
    var l = new VEShapeLayer();
    map.AddShapeLayer(l);
    var veLayerSpec = new VEShapeSourceSpecification(VEDataType.GeoRSS, url, l);
    map.ImportShapeLayerData(veLayerSpec, onFeedLoad, 0);
}

function AddPushPin(latitude, longitude) {
    findIcon = new VEShape(VEShapeType.Pushpin, new VELatLong(latitude, longitude));
    findIcon.SetTitle(tmpPushpinTitle);
    findIcon.SetCustomIcon("/_layouts/1033/images/grontmij/pin.gif");
    map.AddShape(findIcon);
    map.SetZoomLevel(12);
}


function onFeedLoad(shapeLayer) {
    for (i = 0; i < shapeLayer.GetShapeCount(); i++) {
        var s = shapeLayer.GetShapeByIndex(i);
        s.SetCustomIcon("<img src=\"/_layouts/1033/images/grontmij/pin.gif\" />");
    }

    var resultsdiv = document.getElementById('VEWebPart_SearchResults');
    if (resultsdiv) {
        if (resultsdiv.hasChildNodes()) {
            while (resultsdiv.childNodes.length >= 1) {
                resultsdiv.removeChild(resultsdiv.firstChild);
            }
        }

        for (i = 0; i < shapeLayer.GetShapeCount(); i++) {
            var shape = shapeLayer.GetShapeByIndex(i);
            if (shape.GetTitle() != "dummy") {
                var el = document.createElement("div");
                el.id = "velocation_" + i;
                var strHtml = "<span class=\"title\">" + shape.GetTitle() + "</span>";
                strHtml += "<div>" + shape.GetDescription() + "</div>";
                strHtml += "<hr />";

                el.innerHTML = strHtml;
                resultsdiv.appendChild(el);
            }
        }
    }
}

function ShowButtons() {

    var el = document.createElement("div");
    el.style.left = 40;


    var strHtml = "<input type=\"button\" value=\"Road\"  id=\"VWebPart_RoadSubmit\" onclick=\"VEWebPart_ShowRoadView(); return false;\">"
    strHtml = strHtml + "<input type=\"button\" value=\"Aerial\"  id=\"VWebPart_AerialSubmit\" onclick=\"VEWebPart_ShowAerialView(); return false;\">"
    strHtml = strHtml + "<input type=\"button\" value=\"Birds Eye\"  id=\"VWebPart_BirdsEyeSubmit\" onclick=\"VEWebPart_ShowBirdsEyeView(); return false;\">"

    el.innerHTML = strHtml;
    map.AddControl(el);
}

function VEWebPart_ShowRoadView() {
    map.SetMapStyle(VEMapStyle.Road);
}

function VEWebPart_ShowAerialView() {
    map.SetMapStyle(VEMapStyle.Aerial);
}

function VEWebPart_ShowBirdsEyeView() {
    map.SetMapStyle(VEMapStyle.Birdseye);
    map.HideInfoBox();
}




function ShowSearchBox() {
    var el = document.createElement("div");
    el.id = "VEWebPart_SearchForm";
    el.style.top = 14;
    //el.style.left = GetMapWidth() - 168; // = Width of the map - width of the div - margin right
    el.style.left = 40;
    el.style.border = "1px solid gray";
    el.style.background = "#f1f7f7";
    el.style.padding = 3;
    el.style.width = "163px";
    el.style.fontSize = "10px";

    var strHtml = "Address:<br/><input type=\"text\" id=\"VEWebPart_findAddress\"><br />";
    strHtml = strHtml + "<input type=\"submit\" value=\"Search\"  id=\"VWebPart_AddressSearchSubmit\" onclick=\"VEWebPart_FindAddress(); return false;\">";

    el.innerHTML = strHtml;
    map.AddControl(el);
}

function VEWebPart_FindAddress() {
    var address = document.getElementById('VEWebPart_findAddress').value;
    tmpPushpinTitle = address;

    try {
        map.Find(null, address, null, null, null, null, true, true, true, true, onLocationFound);
    }
    catch (e) {
        alert(e.message);
    }
}

function onLocationFound(layer, resultsArray, places, hasMore, veErrorMessage) {
    var latlon;

    latlon = places[0].LatLong;
    if (findIcon)
        map.DeleteShape(findIcon);

    findIcon = new VEShape(VEShapeType.Pushpin, latlon);
    findIcon.SetTitle(tmpPushpinTitle);
    findIcon.SetCustomIcon("/_layouts/images/ewr206l.gif");
    map.AddShape(findIcon);
    map.SetZoomLevel(12);
}

function GetMapWidth() {
    var view = map.GetMapView();
    var bottomright = view.BottomRightLatLong;

    return map.LatLongToPixel(bottomright).x;
}

function GetMapHeight() {
    var view = map.GetMapView();
    var bottomright = view.BottomRightLatLong;

    return map.LatLongToPixel(bottomright).y;
}