/*
    $Rev: 83559 $
    XXX: REQUIREMENTS
        -- OOP.js
*/


var BaseFriendRenderer = {

    init: function () { 
        this.rowID = 'agi-fb-thumbrow';
        this.gridRow = '<li class="'+ this.rowID +'"><ul>FRIENDS_ROW</ul></li>';
        this.extraAttrs = {};
    },

    makeMarkup: function (users, friendContainer, numShow, numCols) {
        /* Given a friend's users (as returned by Facebook), build up
           the necessary HTML to display them.

           Display the friends as either a list or a grid

                -- numShow  # of friends to display
                -- numCols  # of columns to display
                                - if empty, assume a single list
        */
        if (!numShow) {
            numShow = users.length;
        }

        // Grid or List ?
        var makeGrid = numCols ? true : false;

        var friends = [];
        var friendRows = [];

        // loop through and build up HTML for each friend (as a list)
        for(i=0; i<numShow; i++) {
            if (i >= users.length) {
                break;
            }

            // add a friend to the list
            friends.push(this._extractFriendInfo(users[i], friendContainer))

            // start new row ?
            if (makeGrid && ((i+1) % numCols == 0 || i+1 == numShow)) {
                friendRows.push(this.gridRow.replace(/FRIENDS_ROW/g, 
                            friends.join('\n')));
                friends = [];
            }
        }

        return (makeGrid ? friendRows.join('\n') : friends.join('\n'));
    },

    _extractFriendInfo: function(friend, friendContainer) {
        /* Given a Friend object, inject friend attributes into the HTML
           that will represent a friend on the page.
        */
        var birthday = friend.birthday || '';
        var birthday_date = friend.birthday_date || '';

        var picSquare = friend.pic_square_with_logo ||
            imghost + DEFAULTPIC;

        // replace quotes in name (in a safeguarded fashion)
        // if 'null' encountered, provide a default
        var name = friend.name;
        if (!name) {
            name = "name not found";
        } else {
            name = name.replace(/'/g,"").replace(/"/g, "");
        }

        // swap in the friend data; return markup
        friendContainer = this._swapExtraAttrs(friendContainer);

        return friendContainer.
            replace(/NAME/g, name).
            replace(/UID/g, friend.uid).
            replace('PICTURE', picSquare).
            replace('BIRTHDAY_FRIENDLY_NO_YEAR', this._removeYear(birthday)).
            replace('BIRTHDAY_SLASHES_NO_YEAR', this._removeYear(birthday_date));
    },

    _swapExtraAttrs: function(friendContainer) {
        /* Loop through the dictionary of extra string replacements 
           passed in. These would be values not part of the USER object.

           A good example would be that of the access_token.
                
                e.g. { 'TOKEN': 'adstlya84h4hl;54th.as5;55s.58ah.' }
        */
        var attrs = this.extraAttrs;

        for (attr in attrs) {
            friendContainer = friendContainer.replace(attr, attrs[attr]);
        }

        return friendContainer;
    },

    _removeYear: function(dateString) {
        /* It is oft desired to display a user's birthday, but without the year.
           This method handles dates like '09/13/1975' and 'September 13, 1975'.
        */
        if (!dateString) {
            return '';
        }

        if (dateString.indexOf('/') > -1) {
            var dateParts = dateString.split('/');
            return dateParts[0] + '/' + dateParts[1];
        }
        else {
            return dateString.split(',')[0];
        }

        return '';
    }
};
