/**
 * facebookApiKey: KEY de facebook
 * facebookUrlLogin: URL para luego de hacer login
 * facebookUrlLogout: URL para luego de hacer logout
 */

var facebookApiOk = false;
var fbShareInterval = 0;

window.fbAsyncInit = function() {
    FB.init({
      appId      : facebookApiKey, // App ID
      //channelUrl : '//bimbo-corporativo.local/channel.html', // Channel File
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      oauth      : true, // enable OAuth 2.0
      xfbml      : true  // parse XFBML
    });

  if(typeof(facebookLoad) != "undefined")
  {
      facebookLoad();
  }

  fbShareInterval = setInterval("fbShareHidden()", 500);
  facebookApiOk = true;
};

  (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/es_ES/all.js";
     d.getElementsByTagName('head')[0].appendChild(js);
   }(document));


function fbShareHidden()
{
  if($('.fb_share_count_hidden').length > 0)
  {
    $('.fb_share_count_hidden').removeClass('fb_share_count_hidden');
    clearInterval(fbShareInterval);
  }
}

function facebookLogin(successFunction) {
  if(!facebookApiOk) {
    return;
  }

  var cb = function(response) {
    console.log('FB.login callback', response);
    if (response.status === 'connected') {
        successFunction();
      console.log('User logged in');
    } else {
      console.log('User is logged out');
    }
  };
  FB.login(cb, { scope: 'email,create_event,publish_stream,user_about_me,user_birthday' });
}


function shareWall(params)
{
   FB.ui(
   {
     method: 'stream.publish',
     message: params.message,
     attachment: {
       name: params.aname,
       caption: params.asubtitle,
       description: (params.adescription),
       href: params.ahref,
       media: [
       {
          type: 'image',
          href: params.ahref,
          src: params.aimage
       }
       ]
     }
   },
   function(response) {
   }
 );
}
