
!(function(global, rankings){
    var getRankingIndex = function(unitId){ return rankings.indexOf(unitId); };

    var getJSONLD = function(title, description, url, pageNumber, itemsPerPage, whenDone) {
        var pageIndex = pageNumber -1;
        var unitIds = rankings.slice( itemsPerPage * pageIndex, itemsPerPage * (pageIndex + 1) );
        var ajaxRequests = [];
        var ajaxResponses = [];

        $.each(unitIds, function(index, unitId){
            ajaxRequests.push( $.getJSON('/tools/_data/college-json-ld/' + unitId + '.json', function(collegeJson) { ajaxResponses[index] = collegeJson; }) );
        });

        $.when.apply($, ajaxRequests).done(function(){
            whenDone({
                "@context": "http://schema.org",
                "@type": "ItemList",
                "name": title,
                "description": description,
                "itemListOrder": "http://schema.org/ItemListOrderDescending",
                "numberOfItems": rankings.length,
                "url" : url,
                "itemListElement": ajaxResponses.map(function(collegeJson, index){ return { "@type": "ListItem", "position": pageIndex * itemsPerPage + index + 1, "url" : collegeJson.mainEntityOfPage["@id"] }; })
            });
        });
    };

    global.getRankingIndex = getRankingIndex;
    global.getRankingJSONLD = getJSONLD;
})(
    window,
    []
);
    