var OnlyGreenFB = function() {
	var receiver = '/static/facebook/xd_receiver.htm';
	
	var api;
	
	var connect_string = '';
		
	function init() {
		FB.init(FACEBOOK_API_KEY, OnlyGreenFB.receiver);
		FB_RequireFeatures(["Api"], function() { 
			OnlyGreenFB.api = FB.Facebook.apiClient;
		});
		
		$('#facebook-friend-list-modal').jqm();
	}
	
	function showFriendsListWithPost() {	
		var containerElement = document.getElementById('facebook-friend-list-modal');
		var containerElementInner = document.getElementById('facebook-friend-list');
		if (!containerElement || !containerElementInner) {
			return false;
		}
		
		$('#facebook-friend-list-modal').jqmShow(); 
		
		OnlyGreenFB.getFriendList(function(friendList) {			
			var markup = '<ul>';
			if (friendList.length > 0) {
				for (var i=0; i < friendList.length; i++) {
					var className = 'odd';
					if (i%2 == 0) {
						className = 'even';
					}
					
					markup += '<li class="' + className + '">' +
					'<fb:profile-pic size="square" ' +
					'uid="'+friendList[i]+'" facebook-logo="true">' +
					'</fb:profile-pic> <span class="name"><fb:name ' +
					'linked="false" uid="'+friendList[i]+'"> </fb:name>' +
					'</span>' +
					'<button class="button post-to-facebook-wall"' +
					' id="fb_post_'+ friendList[i]+'"><span>Post to Wall' +
					'</span></button></li>';
				}
			}
			markup += '</ul>';
						
			document.getElementById('facebook-friend-list').innerHTML = markup;
			FB.XFBML.Host.parseDomElement(containerElement);
			
			OnlyGreenFB.postToFriendsWall();
		});
	}
	
	function getFriendList(callback) {		
		OnlyGreenFB.api.requireLogin(function(callback) {			
			OnlyGreenFB.api.friends_get(null, callback); 	
		}(callback));	
	}
	
	function postToFriendsWall() {
		$('.post-to-facebook-wall').click(function() {
			var facebookUid = this.id.replace('fb_post_', '');
			var attachment = {
				'media': [{
						'type': 'image',
						'src': FACEBOOK_FRIENDS_WALL_IMAGE,
						'href': 'http://www.onlygreen.org.uk/'
				}]
			};
			var actionLinks = [{ 
					'text': 'Visit onlygreen.org.uk', 
					'href': 'http://www.onlygreen.org.uk/'
			}];
			
			FB.Connect.streamPublish(FACEBOOK_FRIENDS_WALL_MESSAGE, 
						attachment, actionLinks, facebookUid);
			
			OnlyGreen.track('event', {
				url: 'post-to-friends-wall',
				action: 'facebook',
				opt_label: FACEBOOK_ACTION
			});
		});
	}
	
	function importFacebookConnect() {		
		if ($('#facebook-share').length == 0) {
			return false;
		}
		
		if (OnlyGreenFB.connect_string.toString() == '') {
			OnlyGreenFB.connect_string = $('#facebook-share').html();
		}
		$('#modal-facebook-share-content').html(
				OnlyGreenFB.connect_string
		);
						
		OnlyGreenFB.init();
	}	
	
	function postToMyWall() {				
		var attachment = {
				'media': [{
						'type': 'image',
						'src': FACEBOOK_OWN_WALL_IMAGE,
						'href': 'http://www.onlygreen.org.uk/'
				}]
			};
			var actionLinks = [{ 
					'text': 'Visit onlygreen.org.uk', 
					'href': 'http://www.onlygreen.org.uk/'
			}];
			
			FB.Connect.streamPublish(FACEBOOK_OWN_WALL_MESSAGE, 
						attachment, actionLinks);	
			
			OnlyGreen.track('event', {
				url: 'post-to-own-wall',
				action: 'facebook',
				opt_label: FACEBOOK_ACTION
			});			
	}

	return {
		init: init,
		getFriendList: getFriendList,
		showFriendsListWithPost: showFriendsListWithPost,
		postToFriendsWall: postToFriendsWall,
		postToMyWall: postToMyWall,
		importFacebookConnect: importFacebookConnect,
		api: api,
		receiver: receiver,
		connect_string: connect_string
	};
}();
