function PersonAPI()
{
	this.viewer        = null;
	this.viewerFriends = null;
	this.owner         = null;
	this.ownerFriends  = null;
	this.callbackViewerFunc = null;
	this.callbackOwnerFunc  = null;


    this.loadViewerFriends = function(callback_viewer)
	{
		this.callbackViewerFunc = callback_viewer;

    	var req = opensocial.newDataRequest();
	    req.add(req.newFetchPersonRequest(opensocial.DataRequest.Group.VIEWER), 'viewer');
	    req.add(req.newFetchPeopleRequest(opensocial.DataRequest.Group.VIEWER_FRIENDS), 'viewerFriends');
	    req.send(this.onLoadFriends.bind(this));
	}

	this.onLoadFriends = function(data)
	{
 		this.viewer = data.get('viewer').getData();
	    this.viewerFriends = data.get('viewerFriends').getData();

	    if(this.callbackViewerFunc != undefined && this.callbackViewerFunc != null)
	    {
		    this.callbackViewerFunc();
		}
	}

	this.loadOwnerFriends = function(callback_owner)
	{
		this.callbackOwnerFunc = callback_owner;

        var req = opensocial.newDataRequest();
	    req.add(req.newFetchPersonRequest(opensocial.DataRequest.Group.OWNER), 'owner');
	    req.add(req.newFetchPeopleRequest(opensocial.DataRequest.Group.OWNER_FRIENDS), 'ownerFriends');
	    req.send(this.onLoadOwnerFriends.bind(this));
	}

	this.onLoadOwnerFriends = function(data)
	{
        this.owner = data.get('owner').getData();
	    this.ownerFriends = data.get('ownerFriends').getData();

	    if(this.callbackOwnerFunc != undefined && this.callbackOwnerFunc != null)
	    {
		    this.callbackOwnerFunc();
		}
	}


	this.getViewerData = function()
	{
		return this.viewer;
	}

	this.getViewerFriendsData = function()
	{
		return this.viewerFriends;
	}

	this.getOwnerData = function()
	{
		return this.owner;
	}

	this.getOwnerFriendsData = function()
	{
		return this.ownerFriends;
	}

	this.getThumbForOwnerFriendByUID = function(uid)
	{
		if(this.ownerFriends != null)
		{
			for(var i = 0; i < this.ownerFriends.length; i++)
			{
				if(this.ownerFriends[i].getField(opensocial.Person.Field.ID) == uid)
				{
					return this.ownerFriends[i].getField(opensocial.Person.Field.THUMBNAIL_URL);
				}
			}
		}

		return null;
	}

	this.getThumbForViewerFriendByUID = function(uid)
	{
        if(this.viewerFriends != null)
		{
			for(var i = 0; i < this.viewerFriends.length; i++)
			{
				if(this.viewerFriends[i].getField(opensocial.Person.Field.ID) == uid)
				{
					return this.viewerFriends[i].getField(opensocial.Person.Field.THUMBNAIL_URL);
				}
			}
		}

		return null;
	}

	this.getThumbForViewer = function()
	{
		if(this.viewer != null)
		{
			return this.viewer.getField(opensocial.Person.Field.THUMBNAIL_URL);
		}

		return null;
	}

	this.getThumbForOwner = function()
	{
        if(this.owner != null)
		{
			return this.owner.getField(opensocial.Person.Field.THUMBNAIL_URL);
		}

		return null;
	}

    this.getNameForViewer = function()
	{
		if(this.viewer != null)
		{
			return this.viewer.getDisplayName();
		}

		return null;
	}

	this.getNameForOwner = function()
	{
        if(this.owner != null)
		{
			return this.owner.getDisplayName();
		}

		return null;
	}

	this.getFieldForOwner = function(os_field)
	{
    	if(this.owner != null)
		{
			return this.owner.getField(os_field);
		}

		return null;
	}

    this.getFieldForViewer = function(os_field)
	{
    	if(this.owner != null)
		{
			return this.viewer.getField(os_field);
		}

		return null;
	}


	this.getFieldForViewerFriendByUID = function(uid, os_field)
	{
        if(this.viewerFriends != null)
		{
			for(var i = 0; i < this.viewerFriends.length; i++)
			{
				if(this.viewerFriends[i].getField(opensocial.Person.Field.ID) == uid)
				{
					return this.viewerFriends[i].getField(os_field);
				}
			}
		}

		return null;
	}

    this.getFieldForOwnerFriendByUID = function(uid, os_field)
	{
        if(this.ownerFriends != null)
		{
			for(var i = 0; i < this.ownerFriends.length; i++)
			{
				if(this.ownerFriends[i].getField(opensocial.Person.Field.ID) == uid)
				{
					return this.ownerFriends[i].getField(os_field);
				}
			}
		}

		return null;
	}
}

