//Local Variable with Flash Content id. default = goodMoodMissionFlash
var as_swf_name = "goodMoodMissionFlashSWF";

//Method to dispatch an Event to Flash
function flashCallBack ( func ){
	//alert('document[as_swf_name] ' + func);
	if (document[as_swf_name] != null) {
	    if( arguments.length > 1 ){
		    document[as_swf_name][func]( Array.prototype.slice.call(arguments).slice(1)[0]);
	    }else{
		    document[as_swf_name][func]();
	    }
    }
}

// init facebook
function fbinit(apiKey, receiver) {
    FB_RequireFeatures(["Api","Connect","XFBML"], function(){ 
        FB.init(apiKey, receiver, {"ifUserConnected":onLoggedIn});  
    });
}

function fblogout() {
    FB_RequireFeatures(["Api","Connect", "XFBML"], function(){ 
        FB.Connect.logout(onLoggedOut);
    });
}

function fblogin() {
    //alert ("logging in");
    FB_RequireFeatures(["Api","Connect", "XFBML"], function(){ 
        FB.Connect.requireSession(onLoggedIn);
    });
}

function onLoggedIn(user_id) {
    //alert ("Connected");
    var status;
    if (user_id != null) {
      //is user a Fan?
      FB.Facebook.apiClient.pages_isFan (197522929219, user_id, function(result, ex) {
          var user = {};
          user.isUserFan = result;
          //user is logged in, get user
          var users = new Array();
          users[0] = user_id;
          var fields = new Array();
          fields[0] = "first_name";
          fields[1] = "pic_square";
          fields[2] = "profile_url";
          fields[3] = "current_location";
          FB.Facebook.apiClient.users_getInfo(users, fields, function (result,ex) {
            if (result != null) {
                user.userName = result[0].first_name;
                user.userId = user_id;
                user.userImagePath = result[0].pic_square;
                if (result[0].current_location != null) {
                    user.city = result[0].current_location.city; 
                    user.state = result[0].current_location.state;
                    user.country = result[0].current_location.country;
             }
             
             //Invoke Flash Callback function
             flashCallBack( "onLoginHandler", user );      
             
             FB.Facebook.apiClient.fql_query('SELECT src_big, src_small, src FROM photo WHERE aid IN (SELECT aid FROM album WHERE owner="'+user_id+'")', 
                function (result, ex) {
                    if (result != null && result.length > 0) {
                        //alert (result[0].src_big);
                        flashCallBack( "onGetPhotosHandler", result );  
                    }    
             });
             
            }
          });
      });    
    }
} 


function onLoggedOut() {
    //alert ("Not Connected");    
    //Invoke Flash Callback function
	flashCallBack( "onLogoutHandler" );

}

function showPermissions() {
    FB_RequireFeatures(["Api","Connect"], function(){ 
        FB.Connect.showPermissionDialog("publish_stream", function(perms) {
           if (!perms) {
             flashCallBack( "onNoPublishPermission" );
           } else {
             flashCallBack( "onAllowPublish" );
           }
         });
    });
}

function fbpublish_stream(message, baseURL, verb) {
    //alert ("publishing");
    var attachment = {'media': [{'type':'image',
                                 'src': baseURL + 'images/fb/good-mood-mission-post.gif',
                                 'href': baseURL }],
                      'caption' : '{*actor*} ' + verb + ' ' + message + '.',
                      'description' : 'Tell us what puts you in a good mood and help fight hunger with Feeding America.', 
                      'properties' : {'Go to Oscar Mayer ': {'text': 'goodmoodmission.com', 'href' : baseURL}} 
                      };
                                 
   var actionLinks = [{ "text": "Visit goodmoodmission.com", "href": baseURL }];

    FB_RequireFeatures(["Api","Connect"], function(){ 
		FB.Connect.streamPublish(
			"",
			attachment,
			actionLinks,
			null,
			"Share your good mood",
			function (post_id,ex) {
			    //alert(post_id);
                if (post_id != null) {
                    //User clicked Publish successfully
                    flashCallBack("onPublishHandler");
                }
			}
		);
	});
}


function promptBecomeFan(){
	window.open('fb-fan.html','','location=no,resizeable=no,toolbar=no,width=440,height=242');
	return false;
}

