$(document).ready(function(){
	window.fbAsyncInit = function() {
		FB.init({
			appId      : '128980307204893', // App ID
			//channelURL : '//WWW.YOUR_DOMAIN.COM/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
		});

		// Additional initialization code here
		FB.getLoginStatus(function(response) {
			if (response.authResponse) {
				// Usuario conectado a Facebook
				FB.api('/me', function(user) {
					//console.log($.dump(user));
					$('.head .titulo-head').hide();
					$('.head .boton-facebook').hide();
					$('.head .avatar .foto img').attr('src','http://graph.facebook.com/'+user.id+'/picture');
					$('.head .avatar span').html('Hola <br />'+user.name.substring(0,17));
					$('.head').removeClass('connect');
		    		$('.head').addClass('connected');
		    		$('.head').css('background','url("/wp-content/themes/omo-new/img/derecha/bg-head-connected.png") no-repeat scroll 0% 0% transparent');
					$('.head .avatar').show();
					
				});
				//console.log($.dump(response));
				/*
				console.log("usuario logueado en facebook");
				console.log($.cookie('user_name'));
				$('.head').removeClass('connect');
		    	$('.head').addClass('connected');
		    	$('.head').load(urlGlobal+'/facebook-status.php');
		    	*/
		  	} else {
		    	// Usuario no conectado a Facebook
		    	//console.log("no hay usuario logueado");
		    	$('.head').addClass('connect');
		    }
		});
		//LOGIN FACEBOOK
		$('.boton-facebook a').click(function(event){
			event.preventDefault();
			//console.log("Facebook login");
			FB.login(function(response) {
				if (response.authResponse) {
					//console.log('Bienvenido!  Capturando su informacion.... ');
					FB.api('/me', function(user) {
					//console.log('Que bueno verte, ' + $.dump(response) + '.');
					$('.head .titulo-head').hide();
					$('.head .boton-facebook').hide();
					$('.head .avatar .foto img').attr('src','http://graph.facebook.com/'+user.id+'/picture');
					$('.head .avatar span').html('Hola <br />'+user.name.substring(0,17));
					$('.head').removeClass('connect');
					$('.head').addClass('connected');
					$('.head').css('background','url("/wp-content/themes/omo-new/img/derecha/bg-head-connected.png") no-repeat scroll 0% 0% transparent');
					$('.head .avatar').show();			       
							
					});
				}
				else {
					//console.log('El usuario cancelo el login o no autorizo a la aplicacion.');
				}
			}, {scope: 'email'});
		});
		//END LOGIN FACEBOOK
		
		//CIERRA LA SESION DE FACEBOOK
		$('.closeFB').click(function(event){
			event.preventDefault();
			FB.logout(function(response) {			
				//console.log('Logged out.');	         	
				$('.head .avatar .foto img').attr('src','');
				$('.head .avatar span').html('');
				$('.head').removeClass('connected');
	    		$('.head').addClass('connect');
				$('.head .avatar').hide();
				$('.head .titulo-head').show();
				$('.head .boton-facebook').show();                
	       	});
		});
		//END CERRAR SESSION
		
 	};
 	//END FACEBOOK INIT

	// 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));
});


/*
$(document).ready(function(){
	
	$('#boton-facebook').click(function(event){
		console.log("has hecho click en facebook");
	});
	
	window.fbAsyncInit = function() {
		FB.init({
			appId      : '128980307204893', // App ID      
			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
		});
		
		
		
		// Additional initialization code here
		//VERIFICA EL ESTADO DEL USUARIO EN FACEBOOK
		FB.getLoginStatus(function(response) {
		  if (response.authResponse) {
		    // Usuario conectado a Facebook
		    console.log("usuario logueado en facebook");
		    console.log($.cookie('user_name'));
		    $('.head').removeClass('connect');
		    $('.head').addClass('connected');
		    $('.head').load(urlGlobal+'/facebook-status.php');
		  } else {
		    // Usuario no conectado a Facebook
		    console.log("no hay usuario logueado");
		    $('.head').addClass('connect');
		  }
		});		
		
		
		$('#boton-facebook').click(function(event){
			event.preventDefault();
			console.log("Facebook login");
			FB.login(function(response) {
			   if (response.authResponse) {
			     console.log('Bienvenido!  Capturando su informacion.... ');
			     FB.api('/me', function(response) {
			       //console.log('Que bueno verte, ' + $.dump(response) + '.');
			       $.cookie('user_name', response.name);
			       $.cookie('user_id', response.id);
			       $('.head').removeClass('connect');
				    $('.head').addClass('connected');
				    $('.head').load(urlGlobal+'/facebook-status.php');				
			     });
			   } else {
			     console.log('El usuario cancelo el login o no autorizo a la aplicacion.');
			   }
			 }, {scope: 'email'});
		});
		
		//CIERRA LA SESION DE FACEBOOK
		$('.closeFB').live('click',function(event){
			event.preventDefault();
			FB.logout(function(response) {			
	         console.log('Logged out.');
	         $.cookie('user_name', null);
			 $.cookie('user_id', null);
			 $('.head').removeClass('connected');
			 $('.head').addClass('connect');
			 $('.head').load(urlGlobal+'/facebook-status.php');                
	       	});
		});
		
					
			
			//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));
			
			
		};
});

*/
