
/**
 * Asynchronously load the Facebook Javascript library.
 * This code must be accompanied by an fb-root div directly after the <body>
 * tag. This code has been modified slightly compared to the online
 * example code from Facebook.com -- this one actually puts the
 * script data nicely in the <head> area.
 */
$(function() {	
	var e = document.createElement('script'); e.async = true;
	e.src = document.location.protocol +
	'//connect.facebook.net/en_US/all.js';    
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(e, s);
});

/**
 * Callback for the getLoginStatus() call and the
 * auth.login event. It updates the fb_logged_in div
 * with the user's profile picture and name.
 * 
 * @param session
 * @return
 */
function updateFacebookDiv(session) {
	
	FB.api({
		method: 'fql.query',
		query: 'SELECT name FROM user WHERE uid=' + session.uid
	},
	function(response) {
		$('#fb_login').css('display', 'block');
		$('#fb_picture').html('<img src="https://graph.facebook.com/' + session.uid + '/picture" width="40" height"40" style="border: 1px solid #dddddd;" />');
		$('#fb_picture').css('display', 'block');
		$('#fb_message').html("Welcome, " + response[0].name);
		$('#facebook_table').fadeIn(3000);
	});
	
}

/**
 * The callback for when the Facebook Javascript code has been downloaded
 * and initialised. Here is where we set up the event listeners and check
 * the current login status for the user.
 */
window.fbAsyncInit = function() {
	FB.init({appId: '185286061490783', status: true, cookie: true, xfbml: true});
	FB.getLoginStatus(function(response) {
		if (response.session) {
			updateFacebookDiv(response.session);
			// Check to see if the transactionSuccess function is defined (only defined on cart_complete_results.fb.tpl.html
			if (typeof transactionSuccess == 'function') {
				transactionSuccess();
			}
		} else {
			// no user session available, someone you dont know
			$('#fb_message').html('Share the experience!');
			$('#fb_login').css('display', 'block');	
			$('#facebook_table').fadeIn(3000);
		}
		
	});
	FB.Event.subscribe('auth.login', function(response) {
		// Logged in, so update the facebook div to show name, pic, etc.
		updateFacebookDiv(response.session);
	});     
	FB.Event.subscribe('auth.logout', function(response) {
		// Logged out, so hide the extra information like profile pic, etc.
		$('#fb_picture').css('display', 'none');
		$('#fb_message').html('Share the experience!');
	}); 
};

/**
 * Post a post-sale status update to the user's wall.
 * 
 * Despite what the code and Facebook documentation may say,
 * 'actions' are limited to ONE (1). Adding more than one
 * action will not work.
 *  
 * @param textToPost (String) the text to post to the user's wall
 * @param feedURL (String) the URL of the feed to direct people to
 */
function postToWall(textToPost, feedURL) {

	var actionlinks = [{'name':"Buy Now!", 'link':feedURL}];

	FB.api('/me/feed', 'post', 
			{ message: textToPost, 
		actions: actionlinks ,
		user_message_prompt: 'Let your friends know!',
		auto_publish: true
			}, function(response) {
				if (!response || response.error) {
					// An error occurred trying to update the user's wall.
					// Don't bother worrying them with it, but output any errors for debug
					if (response.error) { console.log(response.error); }
				} else {
					// Success. 
				}
			});        
}

$(function(){
	positionFooter(); 
	function positionFooter(){
		var padding_top = 0; //$(".footer").css("padding-top").replace("px", "");
		var page_height = $(document.body).height() - padding_top;
		var window_height = $(window).height();
		var difference = window_height - page_height;
		if (difference < 0) 
			difference = 0;
 
		$(".footer").css({
			padding: difference + "px 0 0 0"
		});
	}
 
	$(window)
		.resize(positionFooter);
});
