$(function(){

var page = '';

if($('#formDealPurchase').attr('id') =='formDealPurchase') {

    page = '/cart/';

}else {

    page = '/user/account/';

}

if(typeof(historyURL) != 'undefined') {

    if(historyURL!='') {

        page = '/' + historyURL + '/';

    }

}

window.fbAsyncInit = function() {

    FB.init({

      appId  : fbApplicationID,

      status : true, // check login status

      cookie : true, // enable cookies to allow the server to access the session

      xfbml  : true,  // parse XFBML
	  
	  oauth      : true,

      channelUrl : siteUrl + '/public/api/facebook/xd_receiver.htm'

    });

    FB.Event.subscribe('auth.login', function(response) {

       //alert(response.status);

        //window.location.href = siteUrl + page;

    });

    FB.Event.subscribe('auth.logout', function(response) {
   

       // window.location.href = siteUrl + '/user/logout/';

    });

    };

    (function() {

    var e = document.createElement('script');

    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';

    e.async = true;

    document.getElementById('fb-root').appendChild(e);

    }());

    $('#logout-link').click(function(){

        var loc = $(this).attr('href')

        var status = false;

        FB.getLoginStatus(function(response) {

          if (response.authResponse) {

             status = true

          } else {

             status = false;

          }

        });

        if(typeof FB!='undefined' && status == true){

            FB.logout(function(response){

                location.href = loc;

            })

            return false;

         }   else {

            return true;

        }

    })    

})
