//Local Variable with Flash Content id. default = flashcontent
var as_swf_name = "flashContent";
var loggedIn=false;
var loginAttempts=0;
//Initialize Facebook
function fbInit(pAsSwfName,pApi_key,pReceiver){
	as_swf_name = pAsSwfName;
	FB.init(pApi_key,pReceiver);
}

//JavaScript Connect methods
function getstatus(){
	log("getstatus")
	FB.ensureInit(function(){
		FB.Connect.get_status().waitUntilReady( function( status ) {
      		switch ( status ) {	
      			case FB.ConnectState.connected:
      				//log("connected")
        			onConnectHandler(true);
        			loggedIn=true;
         			break;
      			case FB.ConnectState.appNotAuthorized:
      			case FB.ConnectState.userNotLoggedIn:
      				//log("not connected");
            		onConnectHandler(false);
      		}
   		});
	});
}
function log(s){
	//document.forms[0].elements[0].value=document.forms[0].elements[0].value+'\n'+s;
}
function login(){
	log("logging in");
	FB.ensureInit(function() {
		log("inited");
   		
    	FB.Connect.get_status().waitUntilReady( function( status ) {
	     	switch ( status ) {
	      		case FB.ConnectState.connected:
	      			//log("connected")
	        		onLoginHandler();
	        		break;
	      		case FB.ConnectState.appNotAuthorized:
	      			//log("notauthorized");
	      			FB.Connect.requireSession(onLoginHandler,onLoginFailHandler,true);
	      			break;
	      		case FB.ConnectState.userNotLoggedIn:
	      			//log('notloggedin');
	            	FB.Connect.requireSession(onLoginHandler,onLoginFailHandler,true);
	      	}
   		});

	});
}
function logoutAndReconnect(){
	log("logout and reconnect");
	FB.Connect.logout(login);
}
function reset(){
	log("reset acct");
	FB.Connect.forceSessionRefresh(function(){})
	setTimeout(function(){
   		FB.Connect.requireSession(onLoginHandler,onLoginFailHandler,true);
	},1000);
}
//JavaScript Share methods
function post(){
	FB.ensureInit(function() {
		FB.Connect.requireSession(function(){
			//FB.Connect.showShareDialog( "http://www.nationalguard.com/momentsofpride", onShareHandler );
			var fbml = fb_getInviteFBML() ;
            var dialog = new FB.UI.FBMLPopupDialog("Share With Friends", fbml) ;
            //dialog.setFBMLContent(fbml);
            dialog.setContentWidth(650);
            dialog.setContentHeight(620);
            dialog.set_placement(FB.UI.PopupPlacement.topCenter);
            dialog.show();
		});
	});
}
function share(){
	FB.ensureInit(function(){
		FB.Connect.requireSession(function(){
			var actionlinks=[{"text":"Share","href":"http://www.facebook.com/sharer.php?u="+encodeURIComponent('http://www.nationalguard.com/momentsofpride')+'&t='+encodeURIComponent('Moments of Pride')}];
			var attachment={'name':'Moments of Pride','description':'Moments of Pride, brought to you by the National Guard, is a personalized video experience that puts YOU in the action by connecting with your Facebook&reg; account. You\'re getting dropped right in the middle of it! How will you respond?','media':[{'type':'image','src':'http://moments-of-pride.com/img/moments_of_pride_share.jpg','href':'http://www.nationalguard.com/momentsofpride'}],'href':'http://www.nationalguard.com/momentsofpride'};
			FB.Connect.streamPublish('',attachment,actionlinks,null,"What did you think of the experience?",onPostHandler);
		});
	});	
}

function fb_getInviteFBML() {
    var uid = FB.Facebook.apiClient.get_session().uid;
    var fbml = "";
    fbml = 
    '<fb:fbml>\n' +
        '<fb:request-form\n'+
                            //Redirect back to this page
                            ' action="http://www.moments-of-pride.com/"\n'+
                            ' method="GET"\n'+
                            ' target="_top"\n'+
                            ' invite="true"\n'+
                            ' type="Moments of Pride"\n' +
                            ' content="Moments of Pride, brought to you by the National Guard, is a personalized video experience that puts YOU in the action by connecting with your Facebook&reg; account. You\'re getting dropped right in the middle of it! How will you respond?'+ 
                            ' <fb:req-choice url=\'http://www.nationalguard.com/momentsofpride\' label=\'Check out Moments of Pride\' />"\n'+
                      '>\n'+
                      ' <style type="text/css">.inputaux, .skip_top, #skip_top{visibility: hidden;}</style>\n'+
                       ' <fb:multi-friend-selector\n'+
                            ' rows="2"\n'+
                            ' cols="4"\n'+
                            ' bypass="Cancel"\n'+
                            ' showborder="false"\n'+
                            ' actiontext="Use this form to invite your friends to connect with Moments of Pride." email_invite="false"/>\n'+
                ' </fb:request-form>'+
        ' </fb:fbml>';
    return fbml;
}

//Event Handlers
function onLoginFailHandler(){
	log('login fail')
	loggedIn=false;
	flashCallBack("onLogInFail");
}

function onLoginHandler(){
	log('login handler');
	loggedIn=true;
	flashCallBack( "onLogIn" );
}
function onConnectHandler(passfail){
	flashCallBack("onConnect",passfail);
}
function onShareHandler(){

	flashCallBack( "onShare" );
}
function onPostHandler(){
	flashCallBack("onPost");
}
function onFanHandler(){
	flashCallBack("onFan");
}

//Method to dispatch an Event to Flash
function flashCallBack ( func ) {
	if( arguments.length > 1 ){
		document[as_swf_name][func]( Array.prototype.slice.call(arguments).slice(1)[0]);
	}else{
		document[as_swf_name][func]();
	}
}
