(function() {
try {
if(!ubk.exists) { throw "ogncObjUndefined"}

//Commonly used functions/helpers
ubk.facebook = 
{
	userObj:new Object,
	
	//Facebook Connect LogIn Method with link track
	fbLogInWt : function (){
		ognc.tracking.linkTrack('content','Facebook Login');
	},
	
	//Facebook Connect LogIn Method 
	fbLogIn : function (){
		//Render User Info thru FBML parser
		ubk.isLoggedIn = "FB";
		
		/*Get FB LoggedIn user Object*/
		var user = FB.Connect.get_loggedInUser();
		ognc.setCookie("FBUID",user);
			FB_RequireFeatures(["XFBML"], function () {
				FB.Facebook.apiClient.users_getInfo(user,['first_name','last_name','name'], function(result, ex){
					ubk.facebook.userObj = result[0];
					/*Check for forms and pre-fill with user data*/
					ubk.facebook.upDateFormFields();
					//Slow down render abit to give time fpage to render properly.
					ubk.facebook.drawUserBox();
					//Get Friends List
					if(document.location.href.indexOf("get_involved") != -1 && document.location.href.indexOf("sign") == -1) { 
						ubk.facebook.getAppFriends('friends');
					} 
				});
			});
	},
	
	//Draw FB connect user Info
	drawUserBox:function(){
			 $('#user').html('<span>'
	            + '<span id="userMessage">Welcome, <fb:name uid="loggedinuser" class="section_font_color" firstnameonly="true" useyou="false"></fb:name>.<br />'
	            + 'You are signed in.<br /><a id="fbLogOut" class="FB_Link" href="#" onclick="ubk.facebook.fbLogOut()">Logout</a></span>'
	            + '</span>'
				+ '<span id="userImage"><fb:profile-pic uid="loggedinuser" size="thumb" facebook-logo="true"></fb:profile-pic></span>');
			
			FB_RequireFeatures(["XFBML"], function () {
				FB.init(fb_api_key, "/content/xd_receiver.htm",null);
				
				FB.ensureInit (  function () {                                                                                                                                          
				       FB.XFBML.Host.refresh();                                                                                                                            
				 });
				
			});

			ubk.upDateLoginInfo();
	},
	
	//Update form fields for FB connect user
	upDateFormFields : function(){

		/*Comment Form Name Field*/
		if($('#comments_name')){
			$('#comments_name').attr('value', ubk.facebook.userObj['first_name']);
		}
		
		/*Ask Form Name Field*/
		if($('#ask_name')){
			$('#ask_name').attr('value', ubk.facebook.userObj['first_name']);
		}
		
		/*Spoof It Form Name Field*/
		if($('#spoof_it_name')){
			$('#spoof_it_name').attr('value', ubk.facebook.userObj['first_name']);
		}
		
		/*Create Ownit Forms*/
		if($('#name')){
			$('#name').attr('value', ubk.facebook.userObj['first_name']);
		}
			
	},
	
	//FB logout view
	fbLogOut : function(){
		FB.Connect.logout();
		$('#user').css('display','none');
		$('#kc_login').css('margin-top','0px');
		ognc.tracking.linkTrack('content','Facebook Logout');
		ognc.deleteCookie("FBUID");
	}, 
	
	//Change login view state
	showLogInNav : function(){
		$('#login').css('display','block');
		$('#kc_login').css('display','block');
		ognc.deleteCookie("FBUID");
	},
	
	//Show invite panal for FB user
	showInvite : function (argName,argDescription){
		var fbmlForm = '';
		
		fbmlForm += '<fb:fbml>';
			fbmlForm += '<fb:request-form action="'+document.location+'" method="POST" invite="true" type="Join The Cause." content="<b>'+argName+'</b> '+argDescription+'<fb:req-choice url=\''+document.domain+'\' label=\'I want to join the cause\' />">';
				fbmlForm += '<fb:multi-friend-selector showborder="false" actiontext="Invite your friends to join the cause." />';
			fbmlForm += '</fb:request-form>';
		fbmlForm += '</fb:fbml>';
		
		FB.ensureInit(function () {
			ubk.facebook.showFbmlDialog('Invite your friends to join the cause.', fbmlForm, '755','700');
		});
		
		ognc.tracking.linkTrack('content','Invite more friends');
		ubk.updateMissionFooter('invite','facebook_friends');
	},
	
	//FB connect wall publisher overlay 
	showFbmlDialog : function (title, fbml, width, height) {
	  var dlg = new FB.UI.FBMLPopupDialog(title, fbml, true, true);
	  if (width) {
	    width = parseInt(width);
	    dlg.setContentWidth(width);
	  }
	  if (height) {
	    height = parseInt(height);
	    dlg.setContentHeight(height);
	  }
	  dlg.set_placement(FB.UI.PopupPlacement.topCenter);
	  setTimeout(function () {
	    dlg.show();
	  }, 100);
	},
	
	//FB connect wall publisher
	callPublish : function(msg, attachment, action_link) {
		
	  FB.ensureInit(function () {
	    FB.Connect.streamPublish('', attachment, action_link);
	  });
	
	 ognc.tracking.linkTrack('content','Publish');
	 ubk.updateMissionFooter('publish','facebook');
	},
	
	//Friends list application
	getAppFriends : function (argTarget){
		var widget_div = document.getElementById(argTarget);
		if(widget_div){
			FB_RequireFeatures(["XFBML"], function () {
			  	FB.Facebook.apiClient.friends_getAppUsers(function(result) {
			   	 var markup = "";
				    var num_friends = result ? Math.min(12, result.length) : 0;
				    if (num_friends > 0) {
				      for (var i=0; i<num_friends; i++) {
						markup += '<fb:profile-pic size="square" uid="'+result[i]+'" facebook-logo="true"></fb:profile-pic>';
				      }
				 		
						FB.ensureInit (  function () {                                                                                                                                          
					       	  widget_div.innerHTML = markup;
							  FB.XFBML.Host.parseDomElement(widget_div);
							  $('#connect_friends').css('display','block');
							  $('#connect_share').css('display','none');                                                                                                                         
					 	});

				   }
			    });
			});
		}
		setTimeout('$("#slot_1").css("minHeight", $("#slot_3").height() - $("#connect_with_friends").height())',1000);
	}
	
}

} catch(e) { console.log("error: " + e);}
})();


//prevent jQuery from appending cache busting string to the end of the FeatureLoader URL 
var cache = jQuery.ajaxSettings.cache; 
jQuery.ajaxSettings.cache = false; 
 
// Load FeatureLoader asynchronously. Once loaded, execute Facebook init 
jQuery.getScript('http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US', function() {
	
	FB_RequireFeatures(["XFBML"], function () {
		FB.init(fb_api_key, "/content/xd_receiver.htm", {"ifUserConnected":ubk.facebook.fbLogIn,"ifUserNotConnected":ubk.facebook.showLogInNav});
	 });
}); 

// Restore jQuery caching setting 
jQuery.ajaxSettings.cache = cache;




