(function(){
    
    /*
        USAGE:
            
            NB: access_mode — "{owner-rights},{friends-rights},{others-rights}", например “RW,RW,RW”, “RW,R,R”, “RW,W,-"
                        
            var req = new Social.DataRequest( widget.getAuth() );
            var p1 = Social.AppData.readOnly("name");

            req.add( p1.Get() );
            req.add( p2.Set("new value"));
            req.add( Social.AppData.readOnly("name2").Get());
            req.add( Social.AppData.readOnly("name2").Set("new value") );
            req.send( function(results, status){
                if (results["name1"].prototype != Social.DataRequest.Exception)
                    alert(results["name1"].content);
                if (results["name2"].prototype != Social.DataRequest.Exception)
                {
                    alert(results["name2"].updated);
                    alert(results["name2"].access_mode);
                }
            });
    */
    Social.AppData = function(data)
    {
        if (!data.accessMode)
        {
            var pieces = data.title.split("+");
            data.accessMode = pieces[pieces.length-1];
            delete pieces[pieces.length-1];
            data.title = pieces.join("+");
        }
        var fields = ["title", "accessMode", "content", "updated"];
        for (var i=0; i<fields.length; i++)
            this[fields[i]] = data[fields[i]];
    }
    Social.AppData.prototype.Get = function()
    {
        return { "request"    : "social.GetAppData", 
                 "key"        : this.title,
                 "params"     : { "title":this.title+"+"+this.accessMode } 
            };
    }
    Social.AppData.prototype.Set = function(newValue)
    {
        return { "request"    : "social.SetAppData", 
                 "key" : this.title, 
                 "params" : {
                        "title":this.title+"+"+this.accessMode,
                        "content":newValue
            }};
    }
    Social.AppData.custom = function( title, accessMode )
    {   
        return new Social.AppData({ title:title, accessMode:accessMode });
    }
    var presets = [
        [ "readOnly",   "RW,R,R" ],
        [ "writeOnly",  "RW,W,W" ],
        [ "freeForAll", "RW,RW,RW" ],
        [ "friendOnly", "RW,RW,-"]
        ];
    for(var i=0; i<presets.length;i++)
        Social.AppData[ presets[i][0] ]= eval("t=function(title){ return Social.AppData.custom(title, '"+presets[i][1]+"') }");
          
})();
