var apollo = apollo || {};

for(var key in opensocial) {
    apollo[key] = apollo[key] || opensocial[key];
}
apollo.utils = apollo.utils || {};


apollo.newDataRequest = function () {
    var arr = (new RegExp("^/gadget/([0-9]+)\.do$", "i")).exec(window.location.pathname);
    var appId = (arr == null? "": arr[1]);
    var req = opensocial.newDataRequest();
    
    var DataRequestHelper = function (path, data) {
        this.getPath = function () {
            return "apollo/data/" + path;
        };

        this.getData = function () {
            data = data || {};
            data.appId = appId;
            return data;
        };
    };

    req.newFetchItemsRequest = function (data) {
        var groupId = "@all";
        if (typeof data == 'object' && typeof data.groupId == 'string') {
            if (data.groupId == "@all" || data.groupId == "@self") {
                groupId = data.groupId; 
            }
            delete data.groupId;
        }
        return new DataRequestHelper(groupId, data);
    };

    req.newFetchItemRequest = function (id, data) {
        return new DataRequestHelper("" + id, data);
    };

    req.newRateItemRequest = function (id, sc) {
        return new DataRequestHelper("" + id + "/rate", {score: sc});
    };

    return req;
};

apollo.utils.getImageUrl = function (file, format) {
    return file.imageUrl + "_" + format;
};
