﻿function isDuplicateCoordinate(shapeArray, shape) {
    var retVal = false;
    if ((shapeArray !== undefined) && (shapeArray !== null) && (shapeArray.length > 0)) {
        if ((shape !== undefined) && (shape !== null)) {
            var index = 0, checkShape = null;
            for (index in shapeArray) {
                checkShape = shapeArray[index];
                if (checkShape.Latitude === shape.Latitude) {
                    if (checkShape.Longitude === shape.Longitude) {
                        retVal = true;
                        break;
                    }
                }
            }
        }
    }
    return retVal;
}

function DisplaySolds(jsonData)
{
    var maxElementDisplayCount = 100, displayMessage = '';
    // Populate listing object from text object results
	var soldCount = jsonData.totalRecords

	if (soldCount > maxElementDisplayCount) {
        ShowSoldSearchWarning("<span style=\"font-size:1.2em;font-weight:bold;color:red;margin-left:3px;\">"+parseFloat(soldCount)+"</span><span style=\"color:red;\"> sold properties.<br />&nbsp;Please <a href=\"javascript:zoomIn(\'GetSolds()\');\">zoom in</a></span>&nbsp;<a href=\"javascript:zoomIn(\'GetSolds()\');\"><img src=\"../App_Themes/Default/Images/Map/circle_plus.png\" border=\"0\"></a>");
        return;
    }

    if (soldCount == 0)
    {
        ShowSoldSearchWarning("<span style=\"color:red;margin-left:3px;\">No sold properties.<br />&nbsp;Please expand your search.</span>");
        return;
    }
    	
     // Check to see if listing layer exists
    if (typeof(_soldLayer)!="undefined")
    {
        _map.DeleteShapeLayer(_soldLayer);
        // Delete all existing listing icons
        _soldLayer = new VEShapeLayer();
        _map.AddShapeLayer(_soldLayer);        
    }
    else
    {
        // Create listing layer
        _soldLayer = new VEShapeLayer();
        _map.AddShapeLayer(_soldLayer);
    }

    var solds = jsonData.results, shapeArray = [], coordArray = [], displayCounter = 0, n;
    
	for (n = 0; n < solds.length; n++) {
	    if (solds[n]) {
	        //Populate a ve_listing_object
	        var soldListing = new SoldListing();
	        soldListing.Populate(solds[n]);
	        if (isCoordInDisplayedMap(_map, soldListing)) {
	            var shape = soldListing.GetShape();
	            if (!isDuplicateCoordinate(coordArray, soldListing)) {
	                coordArray.push({ Latitude: soldListing.Latitude, Longitude: soldListing.Longitude });
	                shapeArray.push(shape);
	                displayCounter++;
	            }
	        }
	    }
	}

	// Display number of found listings
	displayMessage = '';
	if (displayCounter != soldCount) {
	    displayMessage += 'Displaying ';
	}
	displayMessage += '<span style="font-size:1.0em;font-weight:bold;color:green;margin-left:3px;">' + displayCounter + '</span> sold';
	if (displayCounter > 1) {
	    displayMessage += ' properties<br />';
	} else {
	    displayMessage += ' property<br />';
	}
	displayMatchingSolds(displayMessage);
    //Display listing on map
	soldListing.DisplayShapeArray(shapeArray);
}
