//verify to see if we have defined the global namespace.
if (typeof Haaretz === 'undefined') {
	Haaretz = {};
}
var FACEBOOK_LOGGED_IN = 1;
var FACEBOOK_LOGGED_0UT = 2;
var ANONYMOUS = 3;

Haaretz.Facebook = function(){
	
	//var HaaretzApplicationKey    = "3367610576860a72dc2dc1284de3f075";//http://wiki.developers.facebook.com/index.php/Connect/Setting_Up_Your_Site
	//var HaaretzCallBackURL       = "/static/xd_receiver.htm";//http://wiki.developers.facebook.com/index.php/Cross_Domain_Communication_Channel
	
	/** 
	 * Usage example for this param:
	 * 
	 * comment author name can be found in one of three input fields:
	 * - comment_author_notLoggedIn - if the user is logged out from facebook and never tried to login (userStatus - FACEBOOK_LOGGED_0UT) 
	 * - comment_author_anonymous - if user is logged in to facebook but choose to submit the comment with different name. (userStatus - ANONYMOUS)
	 * - html value of facebook tag fb:name (userStatus - FACEBOOK_LOGGED_IN)
	 * Which input field value will be sent to the server as author name,
	 * will be defined according to the value of this parameter.  
	 */
	var userStatus = ANONYMOUS;
	var facebookid = "";
	
	return {
		
		initFaceBook : function(){
		    /*  FB.init( HaaretzApplicationKey,HaaretzCallBackURL, {
		    	  			"ifUserConnected":Haaretz.Facebook.onFacebookLogedInCallback, 
		    	  			"ifUserNotConnected":Haaretz.Facebook.onFacebookLogedOutCallback
		    	  			} 
		      		 );*/	
		FB.init({appId: '381931042052', status: true, cookie: true, xfbml: true});
		var fbMeta = jQuery('<meta property="og:url"></meta>');
		fbMeta.attr( 'content', document.location.href)
		jQuery( 'head' ).append( fbMeta); 
		
	     /* All the events registered */
	     FB.Event.subscribe('auth.login', function(response) {
	         // do something with response
	    	 Haaretz.Facebook.onFacebookLogedInCallback();
	     });
	     FB.Event.subscribe('auth.logout', function(response) {
	         // do something with response
	    	 Haaretz.Facebook.onFacebookLogedOutCallback();
	     });

	     FB.getLoginStatus(function(response) {
	         if (response.session) {
	             // logged in and connected user, someone you know
	        	 Haaretz.Facebook.onFacebookLogedInCallback();
	         } else {
	        	 Haaretz.Facebook.onFacebookLogedOutCallback();
	         }
	     });

	    },
	    
	    /**  The client was found logged in facebook. Display users picture and name, hide facebook connect button. */
		onFacebookLogedInCallback : function(){
	    	jQuery(".facebookUserLoggedOut").each(function(){
        		jQuery(this).hide();
        	});
	    	jQuery(".facebookAnonymousUser").each(function(){
        		jQuery(this).removeClass("fbError");
        	});	 	    	
	    	jQuery(".facebookUserLoggedIn").each(function(){
        		jQuery(this).show();
        	});	    
			FB.api('/me', function(response) {
				facebookid=response.id;
		    	jQuery('#fbusername').html('');
		    	jQuery('#fbusername').append('<fb:name uid="'+ response.id+'" useyou="false" linked="true"></fb:name>');
		    	FB.XFBML.parse(document.getElementById('fbusername'));
				});
	    	userStatus = FACEBOOK_LOGGED_IN;

			//FB.XFBML.Host.parseDomTree(); 
	    	//FB.XFBML.parse(document.getElementById('fbusername'));
		},
		
		/** The client is not logged in to his/her facebook account, display the facebook connect button*/
	    onFacebookLogedOutCallback : function(){
			jQuery(".facebookUserLoggedIn").each(function(){
        		jQuery(this).hide();
        	});
			jQuery(".facebookAnonymousUser").each(function(){
        		jQuery(this).removeClass("fbError");
        	});	 			
			jQuery(".facebookUserLoggedOut").each(function(){
        		jQuery(this).show();
        	});
			userStatus = FACEBOOK_LOGGED_0UT;
		},
		
		/** 
		 * Post the comment on facebook profile.
		 * @param form containing comment data to be posted on the wall.
		 * */
		submitComment : function(formObj) {
			var commentTitle = jQuery( "[name=comment_title]", formObj).val();
			var commentText = jQuery( "[name=comment_text]", formObj).val();
			
			var attachment = {
					'name' : commentTitle,
					'href' : location.href,
					'description' : commentText
			};
			//alert(attachment);
			var body = commentTitle + "  " + location.href + "   " + commentText;
			//alert(body);
		/*    FB.api('/me/feed', 'post', { message: body}, function(response) {
		            if (!response || response.error) {
		                 alert('Error occured');
		            } else {
		                 alert('Post ID: ' + response.id);
		            }
		       });*/
            FB.ui(
                    {
                        method: 'stream.publish',
                        message: '',
                        attachment: {
                            name: commentTitle,
                            caption: '',
                            description: (commentText),
                            href: location.href
                        },
                        action_links: [
                            { text: "www.haaretz.com", href: location.href }
                        ],
                        user_prompt_message: "Share haaretz.com"
                    },
                    function(response) {

            });


		},
		
		
		/**
		 * returns current user facebook connection status:
		 * loggedIn/loggedOut/anonymous
		 */
		getUserStatus : function(){
			return userStatus;
		},
		setUserStatus : function(status){
			userStatus = status;
		},
		getFacebookId:function(){
			return facebookid;
		}
	};//return
}();
