jQuery(document).ready(function() {
    /**
     * Initialize connect-in web services.
     *
     * development: 159384624080431
     * production:  151546328212900
     */
    FB.init({
        appId: '151546328212900',
        status: true,
        cookie: true,
        xfbml: true
    });
});

/**
 * User class (client-side).
 *
 * This class allows to manage a user via different web services.
 *
 * @author Maxime Lussier
 * @copyright PortailZelda (c) 2010
 */
// Constructor
function User() {
    // Properties
    /** @var string */
    this._type = null;
}

// Getters/Setters
/**
 * Get/Set an account type.
 *
 * @author Maxime Lussier
 *
 * @param mixed value
 * @return mixed
 */
User.prototype.type = function(value) {    
    if (value != undefined) {
        if (value == 'FB') {
            this._type = value;
        }
    }
    
    if (this._type == undefined) {
        this._type = 'FB'; // Temporary fix.
    }

    return this._type;
}

// Private methods
/**
 * Check if the user object has registered account type.
 *
 * @author Maxime Lussier
 *
 * @return bool
 */
User.prototype._hasType = function() {
    return this.type != null;
}

/**
 * Authenticate the user on the server-side (in the application).
 *
 * @author Maxime Lussier
 *
 * @param JSON object data
 */
User.prototype._authenticate = function(data, opts) {    
    if (opts.error == undefined || opts.success == undefined || opts.url == undefined) {
        console.error('Opts must be a JSON object that contains: error, success and url.');
        return;
    }
    
    opts.data = data;
    opts.dataType = 'json';
    opts.type = 'POST';
    
    jQuery.ajax(opts);
}

/**
 * Get Facebook basic information. The use of this function assumes that 
 * you are connected to the Graph API.
 *
 * @author Maxime Lussier
 *
 * @param mixed callback
 * @param JSON object opts
 * @param string permissions
 */
User.prototype._getFBInfos = function(callback, opts, permissions) {
    if (permissions != undefined) {
        if (permissions == 'all') {
            var fbPerms = 'email,user_birthday,user_website,user_location,user_about_me';
        }
        
        FB.getLoginStatus(function(loginStatusResponse) {
            if (loginStatusResponse.perms == undefined || loginStatusResponse.perms != fbPerms) {
                FB.login(function(loginResponse) {
                    if (loginResponse.perms) {
                        FB.api('/me', function(response) {
                            response.image = 'http://graph.facebook.com/' + response.id + '/picture';
                            
                            FB.api('/me/friends', function(response_friends) {
                                response.friends = new Array();
                            
                                for (var i = 0; i != response_friends.data.length; ++i) {
                                    response.friends[i] = 'FB-' + response_friends.data[i].id;
                                }
                            
                                callback(response, opts);
                            });     
                        });
                    }
                }, { perms: fbPerms });
            }
            else {
                FB.api('/me', function(response) {
                    response.image = 'http://graph.facebook.com/' + response.id + '/picture';
                    
                    FB.api('/me/friends', function(response_friends) {
                        response.friends = new Array();
                    
                        for (var i = 0; i != response_friends.data.length; ++i) {
                            response.friends[i] = 'FB-' + response_friends.data[i].id;
                        }
                        
                        callback(response, opts);
                    });     
                });
            }
        });
    }
    else {
        FB.api('/me', function(response) {
            response.image = 'http://graph.facebook.com/' + response.id + '/picture';
            
            FB.api('/me/friends', function(response_friends) {
                response.friends = new Array();
            
                for (var i = 0; i != response_friends.data.length; ++i) {
                    response.friends[i] = 'FB-' + response_friends.data[i].id;
                }
                
                callback(response, opts);
            });
        });
    }
}

// Public methods
/**
 * Connect a user with the account type web services.
 *
 * @author Maxime Lussier
 *
 * @param JSON object opts
 * @param string permissions
 */
User.prototype.connect = function(opts, permissions) {
    if (User.prototype._hasType()) {
        switch (User.prototype.type()) {
            case 'FB':
                FB.getLoginStatus(function(loginStatusResponse) {
                    if (!loginStatusResponse.session) {
                        var fbPermissions = '';
                    
                        if (permissions != undefined) {
                            if (permissions == 'all') {
                                var fbPermissions = 'email,user_birthday,user_website,user_location,user_about_me';
                            }
                        }
                    
                        FB.login(function(response) {
                            if (response.session) {
                                if (permissions == undefined) {
                                    User.prototype.getBasic(User.prototype._authenticate, opts);
                                }
                                else {
                                    User.prototype.getExtended(User.prototype._authenticate, permissions, opts);
                                }
                            }
                        }, {perms: fbPermissions});
                    }
                    else {
                        if (permissions == undefined) {
                            User.prototype.getBasic(User.prototype._authenticate, opts);
                        }
                        else {
                            User.prototype.getExtended(User.prototype._authenticate, permissions, opts);
                        }
                    }
                });
            break;
        }
    }
}

/**
 * Get basic user information via account type web services.
 *
 * @author Maxime Lussier
 *
 * @param mixed callback
 * @param JSON object opts
 */
User.prototype.getBasic = function(callback, opts) {
    if (User.prototype._hasType()) {    
        switch (User.prototype.type()) {
            case 'FB':
                FB.getLoginStatus(function(loginStatusResponse) {
                    if (!loginStatusResponse.session) {
                        FB.login(function(loginResponse) {
                            if (loginResponse.session) {
                                if (opts == undefined) {
                                    User.prototype._getFBInfos(callback);
                                }
                                else {
                                    User.prototype._getFBInfos(callback, opts);
                                }
                            }
                        });
                    }
                    else {                    
                        if (opts == undefined) {
                            User.prototype._getFBInfos(callback);
                        }
                        else {
                            User.prototype._getFBInfos(callback, opts);
                        }
                    }
                });
            break;
        }
    }
}

/**
 * Get extended user information via account type web services.
 *
 * @author Maxime Lussier
 *
 * @param mixed callback
 * @param string permissions
 * @param JSON object opts
 */
User.prototype.getExtended = function(callback, permissions, opts) {
    if (User.prototype._hasType()) {    
        switch (User.prototype.type()) {
            case 'FB':
                FB.getLoginStatus(function(loginStatusResponse) {
                    if (!loginStatusResponse.session) {
                        FB.login(function(loginResponse) {
                            if (loginResponse.session) {
                                if (opts == undefined) {
                                    User.prototype._getFBInfos(callback, {}, permissions);
                                }
                                else {
                                    User.prototype._getFBInfos(callback, opts, permissions);
                                }
                            }
                        });
                    }
                    else {                    
                        if (opts == undefined) {
                            User.prototype._getFBInfos(callback, {}, permissions);
                        }
                        else {
                            User.prototype._getFBInfos(callback, opts, permissions);
                        }
                    }
                });
            break;
        }
    }
}

