1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//FACEBOOK

//Place your application id here
const APP_ID:String = "**********"; 
//Initilize the Facebook library
var FBshareScoreAfterLogin:Boolean=false;
var FBuser:Object=new Object();

function FBisLoading(bool:Boolean=true){
  
  var bt:MovieClip=screens.screen4.FBshare;
  var loader:MovieClip=screens.screen4.FBloader;
  var logo:MovieClip=screens.screen4.FBlogo;

  if (bool){
    logo.alpha=0.7;
    bt.mouseEnabled=false;
    loader.visible=true;
  }else{
    logo.alpha=1;
    bt.mouseEnabled=true;
    loader.visible=false;
  }
}


function FBShare(){
  FBisLoading();
  FBmessageBox("FBinit");
  FBshareScoreAfterLogin=true;
  Facebook.init(APP_ID, FBdidLogin);
  mytrace('FBShare()');
}
function FBmessageBox(msg:String){
  screens.screen4.FBdebug.text=msg;
}

//to check if user is logged
function FBisLogged(){
  if(FBuser.hasOwnProperty("id")) return true;
  return false;
}

function FBdoLoginAction(login:Boolean=true){

  if (login){
    FBmessageBox("Try Login");
    Facebook.login(FBdidLogin, {perms:"publish_stream"});
  }else{//logout
    FBmessageBox("Try Logout");
    Facebook.logout(FBdidLogout);
  }
}

function FBdidLogin(response:Object,fail:Object):void {
  FBmessageBox('FBdidLogin');
  mytrace(response,'FBdidLogin-response');
  FBisLoading(false);
  if (response!=null){
    FBmessageBox('Is Logged IN');
    FBisLoggedIn();
  }

    
}

function FBisLoggedIn():void {

  FBuserInfosLoad();
  
  if (FBshareScoreAfterLogin){
    FBmessageBox('Share score after login');
    //FBscoreShare(gameCroqueMC.game_points,gameCroqueMC.char_name);
    FBscoreShare(1250,'cheezy');
    FBshareScoreAfterLogin=false;
  }
  
}

function FBuserInfosLoad():void{
  FBmessageBox('FBuserInfosLoad');
  Facebook.api('/me', FBuserInfosLoaded);  
}
    
function FBdidLogout(response:Object):void {
  FBisLoading(false);
  FBmessageBox('FBdidLogout');
  if (response){
    FBmessageBox('Is Logged OUT');
    FBuser={};
  }
}

function mytrace(msg:*,label:String=null){
  
  if (typeof(msg)=='object'){
    msg=JSON.encode(msg);
  }
  
  if (label){
    msg = label+": "+msg;
  }
  
  trace(msg);
  
  //FOR FIREBUG
  if (debug){
    ExternalInterface.call( "console.log" , msg);
  }

}



function FBuserInfosLoaded(response:Object,fail:Object):void {
  mytrace(response,'FBuserInfosLoaded-response');
  if (!response.id)return;
  
  FBuser=response;
  FBmessageBox(JSON.encode(FBuser));
  mytrace(FBuser,'FBuserInfosLoaded');
  
}

function FBscoreShare(score:Number,charname:String):void{

  FBmessageBox('FBscoreShare');
  
  var charFullName:String;
  
  switch(charname){
    case ('cheezy') :
    charFullName="Cheezy'O";
    break;
    case ('explosif') :
    charFullName="l'Explosif";
    break;
    case ('original') :
    charFullName="l'Original";
    break;
    case ('salsa') :
    charFullName="l'Explosif";
    break;
  }

  var values:Object = { 
      name:"Jeu Concours Curly Dracula's Friends", 
      link:"http://www.curlydraculafriends.com/jeu", 
      picture:siteUrl+"/_inc/jpg/FB"+charname+".jpg", 
      //caption:"this is a caption", 
      description:FBuser.first_name+" vient de faire "+score+" points en jouant avec "+charFullName+" au Grand jeu Curly Dracula's friends ! Crois tu que tu peux battre son score ?",
      access_token:Facebook.getSession().accessToken
  
  };
  
  
  FBmessageBox(JSON.encode(values));

    Facebook.api("/" + FBuser.id + "/feed", FBscoreShared, values, "POST");

}

function FBscoreShared(response:Object,fail:Object):void {
  FBisLoading(false);
  if (response){
    FBmessageBox('FBscoreShared');
    messagebox_display('Votre score a été publié sur votre profil Facebook','msg');
  }
}