function ZFacebook () {
    this.service = '';
    this.host = '';
    this.time = '';
    this.signup = 0;
    this.signupURL = '/signup/free?fb_connect=1';
    this.init = function (service, host, time, signup, signupURL) {
        this.service = service;
        this.host = host;
        this.time = time;
        if (typeof signupURL != 'undefined') {
            this.signupURL = signupURL;
        }
        if (typeof signup != 'undefined') {
            this.signup = signup;
        }
        $('#fb_login_image').click(function(e) {
            zFacebook.login(true);
        });
        
        $('#fb_logout').click(function(e) {
            FB.Connect.logout(function() {
                zFacebook.login(false);
            });
            e.preventDefault();
        });
    };
    
    this.login = function (userInitiated) {
        FB_RequireFeatures(["Connect"],function() {
            FB.Connect.requireSession(function() {
                FB.Facebook.get_sessionState().waitUntilReady(function(session) {
                    $.get('/connect/getConnectStatus', {signup: zFacebook.signup}, function(response) {
                        if (response) {
                            response = JSON.parse(response);
                            if (response.connected === 1) {
                                if (zFacebook.signup) {
                                    $('#facebook_name').html(response.userInfo.name);
                                    $('#facebook_domain').html(response.domain);
                                    $('#facebook_login').click(function(e) {
                                     window.location = '/login/login?service=' + response.domain +
                                    '/zDashboard/home&host=' + response.domain + '&time=' + response.time + '&fb_connect=1';
                                    });
                                    $('#basicModalContent-UnlinkAccount').modal({closeClass: 'simplemodal-close-sm'});
                                } else {
                                    window.location = '/login/login?service=' + zFacebook.service +
                                    '&host=' + zFacebook.host + '&time=' + zFacebook.time + '&fb_connect=1';
                                }
                            } else if (response.connected === 0) {
                                FB.Connect.showPermissionDialog('read_stream, publish_stream, offline_access', function () {
                                    window.location = zFacebook.signupURL;
                                });
                            } else {
                                alert('An unknown error occured, please try again.');
                            }
                        } else {
                            alert('An unknown error occured, please try again.');
                        }
                    });
                });
            }, function() { if (!userInitiated) { window.location.reload() } });
        });
    };
}

zFacebook = new ZFacebook();
