﻿(function ($) {
    $.facebook = {
        chooseUsernameHttps: function (callback) {
            $.facebook.getUser(function (user) {
                if (user != null) {
                    $.facebook.doesUserExistHttps(user.email, function (response) {
                        if (response.length == 0) {
                            var html = "<div style='margin: 10px;'><b>Please choose a publicly displayed Username for inthepearl.com.</b><br /><br />";
                            html += "<input type='text' id='txtUsername' /><br /><br /><div style='font-size: 7pt;'>*No spaces or special characters. Minimum three characers.</div>";
                            html += "<a href='#' class='ui-button-text-only ui-state-default ui-corner-all'>";
                            html += "<span class='ui-button-text' style='color: #700;' id='btnSubmit'>Submit</span></a></div>";

                            $.fancybox({
                                "Modal": true,
                                "autoDimensions": true,
                                "autoScale": true,
                                "scrolling": "no",
                                "padding": 10,
                                "content": html,
                                "showCloseButton": false,
                                "onComplete": function () {
                                    $("#btnSubmit").unbind().click(function () {
                                        var username = $("#txtUsername").val();

                                        if (username.length < 3 || username.indexOf(' ') > -1) {
                                            $("#txtUsername").css("background-color", "red");
                                            return;
                                        }

                                        callback(username);
                                    });
                                }
                            });
                        }
                        else {
                            callback(response);
                        }
                    });
                }
                else {
                    callback("Error connecting to Facebook");
                }
            });
        },
        chooseUsername: function (callback) {
            $.facebook.getUser(function (user) {
                if (user != null) {
                    $.facebook.doesUserExist(user.email, function (response) {
                        if (response.length == 0) {
                            var html = "<div style='margin: 10px;'><b>Please choose a publicly displayed Username for inthepearl.com.</b><br /><br />";
                            html += "<input type='text' id='txtUsername' /><br /><br /><div style='font-size: 7pt;'>*No spaces or special characters. Minimum three characers.</div>";
                            html += "<a href='#' class='ui-button-text-only ui-state-default ui-corner-all'>";
                            html += "<span class='ui-button-text' style='color: #700;' id='btnSubmit'>Submit</span></a></div>";

                            $.fancybox({
                                "Modal": true,
                                "autoDimensions": true,
                                "autoScale": true,
                                "scrolling": "no",
                                "padding": 10,
                                "content": html,
                                "showCloseButton": false,
                                "onComplete": function () {
                                    $("#btnSubmit").unbind().click(function () {
                                        var username = $("#txtUsername").val();

                                        if (username.length < 3 || username.indexOf(' ') > -1) {
                                            $("#txtUsername").css("background-color", "red");
                                            return;
                                        }

                                        callback(username);
                                    });
                                }
                            });
                        }
                        else {
                            callback(response);
                        }
                    });
                }
                else {
                    callback("Error connecting to Facebook");
                }
            });
        },
        postToItpWall: function (name, body, link, caption, description, source, picture, callback) {
            FB.api('/91330122884/feed', 'post', { access_token: "207998765894942|e091baec5ff9982608799ea4.1-100002365705099|91330122884|2Kax_t1sJktyLp4T6YC1vKGZ3Vc", name: name, body: body, link: link, caption: caption, description: description, source: source }, function (response) {
                callback(response);
            });
        },
        postToWall: function (name, body, link, caption, description, source, picture, callback) {
            $.facebook.getUser(function (user) {
                $.facebook.getAccessToken(function (token) {
                    if (token) {
                        FB.ui({ method: 'stream.publish', display: "iframe", access_token: token, name: name, body: body, link: link, caption: caption, description: description, source: source }, function (response) {
                            callback(response);
                        });
                    }
                    //                    if (token) {
                    //                        FB.api('/me/feed', 'post', { access_token: token, name: name, body: body, link: link, caption: caption, description: description, source: source, picture: picture }, function (response) {
                    //                            callback(response);
                    //                        });
                    //                    }
                });
            });
        },
        loginOrCreateUserHttps: function (username, callback) {
            $.facebook.getUser(function (user) {
                if (user != null) {
                    $.ajax({
                        type: 'POST',
                        url: "/Account/FacebookCreateOrLoginHttps",
                        data: { username: username, email: user.email, firstName: user.first_name, lastName: user.last_name, gender: user.gender },
                        success: function (response) {
                            callback(response);
                        },
                        error: function (response) {
                            callback(response);
                        }
                    });
                }
                else {
                    callback("Error connecting to Facebook");
                }
            });
        },
        loginOrCreateUser: function (username, callback) {
            $.facebook.getUser(function (user) {
                if (user != null) {
                    $.ajax({
                        type: 'POST',
                        url: "/Account/FacebookCreateOrLogin",
                        data: { username: username, email: user.email, firstName: user.first_name, lastName: user.last_name, gender: user.gender },
                        success: function (response) {
                            callback(response);
                        },
                        error: function (response) {
                            callback(response);
                        }
                    });
                }
                else {
                    callback("Error connecting to Facebook");
                }
            });
        },
        getAccessToken: function (callback) {
            FB.getLoginStatus(function (response) {
                if (response.session) {
                    callback(response.session.access_token);
                }
                callback(null);
            });
        },
        doesUserExistHttps: function (email, callback) {
            $.ajax({
                type: 'POST',
                url: "/Account/GetUsernameFromEmailHttps",
                data: { email: email },
                success: function (response) {
                    callback(response);
                },
                error: function (response) {
                    $.common.modalDialog("Error logging on through Facebook.", true);
                }
            });
        },
        doesUserExist: function (email, callback) {
            $.ajax({
                type: 'POST',
                url: "/Account/GetUsernameFromEmail",
                data: { email: email },
                success: function (response) {
                    callback(response);
                },
                error: function (response) {
                    $.common.modalDialog("Error logging on through Facebook.", true);
                }
            });
        },
        getLoginStatus: function (callback) {
            FB.getLoginStatus(function (response) {
                if (response.session) {
                    callback(true);
                }
                callback(null);
            });
        },
        getUser: function (callback) {
            FB.api('/me', function (response) {
                if ($.isFunction(callback)) {
                    callback(response);
                }
            });
        }
    }
})(jQuery);
