var shapeZoomId = "";
var shapeZoomLvl = "";
function updateMapAdverts(data) { 
    // Clear current advert layer
    advertsShapeLayer.DeleteAllShapes();

    if (data.adverts.length == 0 && inSearch) {
        if (firstSearch) {
            var latlong = map.GetCenter();
            expandMapView(latlong.Longitude, latlong.Latitude);
            firstSearch = false;
        }
        else {
            alert('Unfortunately your search has not returned any results, please try one of the following:\n\n1. Widen your search area by zooming the map out\n2. Check your selected categories on the left-hand side of the map');
            inSearch = false;
        }
    }
    else {
        // bulk add shapes - far higher performance
        var shapes = new Array();
        
        // Cycle through results.
        jQuery.each(data.adverts, function(i, advert) {
            // Create new advert pushpin.
            var pushpin = createAdvertPushpin(advert);
            shapes.push(pushpin);
        });
        // add shapes to map
        advertsShapeLayer.AddShape(shapes);
    }
    advertsShapeLayer.Show();
}
function expandMapView(longitude, latitude) {
    gotoLocation(longitude, latitude, (map.GetZoomLevel() - 1));
}

function createAdvertPushpin(advert) {
    // Create VELatLong instance for this advert location.
    var location = new VELatLong(advert.Latitude, advert.Longitude);

    // Create VEShape instance for the advert.
    var pin = new VEShape(VEShapeType.Pushpin, location);

    // Set advert title.
    pin.SetTitle(advert.Title);
    pin.SetDescription(advert.Description);
    if (advert.Url) {
        pin.SetMoreInfoURL(advert.Url);
    }
    pin.X_Id = advert.Id;

    // Set the icon to use for this advert.
    if (advert.Logo != "") {
        pin.SetCustomIcon(advert.Logo);
    }
    return pin;
}

function advertClusteringCallback(clusters) {
    // This function is called when a group of icons are clustered.
    for (var i = 0; i < clusters.length; ++i) {
        // Customise the icon and mouse-over infobox for each cluster.
        var cluster = clusters[i];

        // Loop through each shape in the cluster.
        var list = "<ul>";
        for (var n = 0; n < cluster.Shapes.length; n++) {
            var shape = cluster.Shapes[n];
            var id = shape.GetID();

            if (shape.X_Id == shapeZoomId && shapeZoomLvl != 20) {
                zoomAdvert(id, shapeZoomLvl+1);
            }
            
            var item;
            if (shape.Url) {
                item = "<a href='"+shape.Url+"'>"+shape.Title+"</a>";
            } else {
            item = "<a href='#' onclick='zoomAdvert(\"" + id +"\");return false;'>" + shape.Title + "</a>";
            }
            list += "<li>"+item+"</li>";
        }
        list += "</ul>";

        var clusterShape = cluster.GetClusterShape();
        clusterShape.SetTitle("There are "+cluster.Shapes.length+" locations of interest here...");
        clusterShape.SetDescription(list);
        clusterShape.SetCustomIcon('<div class=\"pin\"><img src=\"/images/emagin/pin_multi_green.gif\" /><div class=\"pinText\">&nbsp;</div></div>');
    }
}

function zoomAdvert(ID, zoom) {
    var shape = map.GetShapeByID(ID);
    var latlong = shape.GetIconAnchor();
    
    shapeZoomId = shape.X_Id;
    shapeZoomLvl = zoom != null ? zoom : 12;
   
    if (zoom == null)
        gotoLocation(latlong.Longitude, latlong.Latitude);
    else
        gotoLocation(latlong.Longitude, latlong.Latitude, zoom);
}
