
//Initialize Facebook
function fbInit(pApi_key){
	var absoluteUrlToChannelFile  = "http://www.condomcondom.org/xd_receiver.htm";
	FB.init({appId: pApi_key, status: true, cookie: true, xfbml: true, channelUrl: absoluteUrlToChannelFile});
	FB.Event.subscribe('auth.sessionChange', function(response) {
        window.location.reload();
     });
}

function login(){
	FB.login(function(response) {
	if (response.session) {
		if (response.perms) {
		  // user is logged in and granted some permissions.
		  // perms is a comma separated list of granted permissions
		} else {
		  // user is logged in, but did not grant any permissions
		}
	  } else {
		// user is not logged in
	  }
	}, {perms:'read_stream,publish_stream,offline_access'});

}

function logout(){
	FB.logout(function(response) {
 	 	window.location.reload();
	});

}

function shareInfo() {
	
	var attachment = new Object();
	attachment.media = new Array();
	
	attachment.name = 'Get Your CondomCondom Badge';
	attachment.href = 'http://www.condomcondom.org/';
	attachment.description = 'condomcondom.org is part of a mass media initiative to prevent transmission of HIV and other sexually transmitted infections in India.';
	
	var temp = new Object();
	temp.type = 'image';
	temp.src = 'http://www.condomcondom.org/images/img-condoms-are-cool.jpg';
	temp.href = 'http://www.condomcondom.org/';

	attachment.media.push(temp);
	
	
	var body = 'just visited www.condomcondom.org and got this cool badge.!!';
	
	FB.ui(
	   {
		 method: 'stream.publish',
		 message: 'just visited www.condomcondom.org and got this cool badge.!!',
		 attachment: attachment,
		 action_links: [
		   { text: 'Get Your Badge!', href: 'http://www.condomcondom.org/' }
		 ],
		 user_message_prompt: 'Get your CondomCondom Badge'
	   },
	   function(response) {
		 if (response && response.post_id) {
		   //
		 } else {
		   //
		 }
	   }
	 );


}

//Method to dispatch an Event to Flash
function inviteFriends() {
	var fbml = getInviteFBML() ;                
	var dialog = new FB.UI.FBMLPopupDialog("CondomCondom.org Invite", fbml);                
	dialog.setFBMLContent(fbml);                
	dialog.setContentWidth(650);                
	dialog.setContentHeight(565);
	dialog.show();
	
} 

function getInviteFBML() {
	var fbml = '';    
	fbml =  '<fb:fbml>\n' +        
				'<fb:request-form\n'+                            
				//Redirect back to this page                            
				' action="'+ document.location +'"\n'+ 
				' method="POST"\n'+                            
				' invite="true"\n'+                            
				' type="CondomCondom"\n' +                            
				' content="Tell your friends about CondomCondom.org \n'+   
					//Callback the server with the appropriate                           
					' <fb:req-choice url=\'' + document.location + '\' label=\'CondomCondom.org\' />" >\n'+                
					' <fb:multi-friend-selector\n'+                            
					' rows="2"\n'+                            
					' cols="4"\n'+                            
					' bypass="Cancel"\n'+                            
					' showborder="false"\n'+                            
					' actiontext="Tell your friends about CondomCondom.org" />\n'+                
				' </fb:request-form>'+        
			' </fb:fbml>';    
	return fbml;
}
