
var wodFB=function(){

    var permits = {
	scope: 'email, user_birthday, offline_access, publish_stream'
    };

    var AbsoluteUrlToChannelFile  = "http://www.worldofdragons.com/facebook/ChannelFile.htm";
    
    //Init facebook
    var fb_init= function(){
	if(typeof(FB)!="undefined"){
	    FB.init({
		appId: '163743123641823',
		cookie: true,
		xfbml: true,
		oauth: true
	    //channelUrl: AbsoluteUrlToChannelFile
	    });
	    fb_loaded=true;
	    return true;
	}
	else{
	    return false;
	}

	(function() {
	    var e = document.createElement('script');
	    e.async = true;
	    e.src = document.location.protocol +
	    '//connect.facebook.net/en_US/all.js';
	    document.getElementById('fb-root').appendChild(e);
	}());

    };
    
    //Get token from facebook
    var fb_accessWOD= function(){
	FB.login(function(response){
	    if (response.authResponse) {
		var fb_token  = response.authResponse.accessToken;
		if(fb_token == undefined){
		    alert('There was an error while connecting to Facebook');
		}
		else{

		    FB.api('/me', function(response) {

			if(response == undefined){
			    alert('There was an error while connecting to Facebook');
			}
			else{

			    //send email and facebookId
			    $.ajax ({
				url: "/inc/ajax.php",

				data: ({
				    op: 'syncFacebook',
				    fb_id: response.id,
				    fb_email: response.email
				}),
				
				cache: false,
				type: "post",
				datatype: "json",
				success: function(data) {
				    var json_data = jQuery.parseJSON(data);

				    //Login
				    if(json_data.option_facebook == 'login'){
					window.location.href = '/mmorpg/user_profile.php';
				    }
				    //Register
				    else{
					window.location.href = '/index.php?facebook_register=ok';
				    }
				}
			    });
			}
			
		    });
		}
	    }
	    else return false;
	}, permits);
    };

    return {
	fb_init:fb_init,
	fb_accessWOD: fb_accessWOD
    };
}();

/**
*
/*Alternative method to send private messages
*
*function fbshare(){
    FB.login(function(response) {
      if (response.authResponse) {
	    console.log("User is connected to the application.");
	    token  = response.authResponse.accessToken;
	    function sortByName(a, b) {
		var x = a.name.toLowerCase();
		var y = b.name.toLowerCase();
		return ((x < y) ? -1 : ((x > y) ? 1 : 0));
	    }
	    FB.api('/me/friends', function(response) {        
	       $("#friends_selector").html("<div id='diagfb_header'><div id='diagfb_headertxt'>Seleccione Amigos a los que invitar a jugar a MaxDriving</div></div><div class='confirmation_stripes'></div>");
	       _friend_data = response.data.sort(sortByName);
	        $.each(response.data, function(i, friend) {
		    $("#friends_selector").append("<div onclick='addFriend(this);' class='jfmfs-friend fbhov' id='" + friend.id  +"'><img class='friend-picture' src='https://graph.facebook.com/" + friend.id + "/picture' /><div class='friend-name'>" + friend.name + "</div></div>");
		});
		$("#dialog_friendsselector").fadeTo("slow",1.0);
	    });
      } else {
	//console.log('User cancelled login or did not fully authorize.');
      }
    }, {scope: 'email, user_birthday,user_about_me,offline_access,publish_stream'});	
    
}*/

