//////////////////////////////////////////////////////////////////////////////
//
//  Copyright (c) 2010  Gunner Training, Inc.
//  All rights reserved.
//
//////////////////////////////////////////////////////////////////////////////


/**
 *  Utility class for working w/ the Facebook API.
 */
function FacebookApi(appId) {

    var _self = this;
    var _appId = appId;
    var _loginConnect = null;
    var _loggedInFn = null;
    var _loggedOutFn = null;
    var _hasAccessFn = null;
    var _noAccessFn = null;


    /**
     *  Initialises this instance.
     */
    this.init = function(options) {
            // Options.
            _loginConnect = (options['loginConnect'] || null);
            _loggedInFn   = (options['loggedInFn']   || null);
            _loggedOutFn  = (options['loggedOutFn']  || null);
            _hasAccessFn  = (options['hasAccessFn']  || null);
            _noAccessFn   = (options['noAccessFn']   || null);

            // Initialise the Facebook API.
            // window.fbAsyncInit = function() {
            //     FB.init(
            //         {
            //             appId: _appId,
            //             status: true,
            //             cookie: true,
            //             xfbml: true
            //         });
            // 
            //     FB.getLoginStatus(function(response) {
            //             if (response.session) {
            //                 if (_loggedInFn) {
            //                     _loggedInFn.call(_self, response)
            //                 }
            //             } else {
            //                 if (_loggedOutFn) {
            //                     _loggedOutFn.call(_self, response)
            //                 }
            //             }
            //         });
            // };
            window.fbAsyncInit = function() {
              FB.init({
                appId      : _appId, // App ID
                channelUrl : 'www.gunnertraining.com/channel.html', // Channel File
                status     : true, // check login status
                cookie     : true, // enable cookies to allow the server to access the session
                xfbml      : true  // parse XFBML
              });

              FB.getLoginStatus(function(response) {
                      if (response.session) {
                          if (_loggedInFn) {
                              _loggedInFn.call(_self, response)
                          }
                      } else {
                          if (_loggedOutFn) {
                              _loggedOutFn.call(_self, response)
                          }
                      }
                  });
              
            
            
          
            };
            

            // Load the FB library.
            // (function() {
            //     var e = document.createElement('script');
            //     e.type = 'text/javascript';
            //     e.src = document.location.protocol
            //                 + '//connect.facebook.net/en_US/all.js';
            //     e.async = true;
            //     document.getElementById('fb-root').appendChild(e);
            // }());
            // Load the SDK Asynchronously
            (function(d){
               var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
               js = d.createElement('script'); js.id = id; js.async = true;
               js.src = "//connect.facebook.net/en_US/all.js";
               d.getElementsByTagName('head')[0].appendChild(js);
             }(document));
            

            // Set up and activate an FB login link.
            // if (_loginConnect) {
            //      _loginConnect.click(function(e) {
            //              e.preventDefault();
            //              FB.login(function(response) {
            //                      var fn = null;
            //                      if (response.session && response.perms) {
            //                          if (_hasAccessFn) {
            //                              _hasAccessFn.call(_self, response, e);
            //                          }
            //                      } else {
            //                          if (_noAccessFn) {
            //                              _noAccessFn.call(_self, response, e);
            //                          }
            //                      }
            //                  },
            //                  { perms: 'email' });
            //          });
            // }





        };
}

