if (typeof TM === 'undefined') {
  TM = {};
}

TM.FaceBook = function() {

  var TMTemplateBundleId  = "55643540921";
  var TMApplicationKey    = "193a5fbc3d1a295ebd6322a3eb4620f1";
  var TMCallBackURL       = "http://www.themarker.com/tmc/news/fbRedirect.html";
  var FBDialogId          = "#RES_ID_fb_pop_dialog_table";
  var FBDialogTimer;

  return {

    // This prefix is used to identify the user in the DB.
    // The FaceBook UID is stored in the database in the username field.
    prefix : "_FB_",

    // Init the themarker application
    initFaceBook : function(){
      FB.init(TMApplicationKey,TMCallBackURL, {"ifUserConnected" : function(){ TM.FaceBook.updateUserProfile('userNotLoggedIn', 'userLoggedIn') } } );
      },

    // Connect to facebook
    connect: function(){
      FB.Connect.requireSession(
        function(){
          TM.FaceBook.updateUserProfile('userNotLoggedIn', 'userLoggedIn')
          }
        );
      },

    // Dissconnect form facebook
    disconnect: function(){
      FB.Connect.logout(
        function(){
          TM.FaceBook.displayFaceBookLogin('userNotLoggedIn', 'userLoggedIn'); }
          );
      },

    // Update the userProfile section.
    updateUserProfile : function(){
      jQuery( '#' + arguments[0] ).hide();
      jQuery( '#' + arguments[1] ).show();

      // Get the userid from the link
      jQuery( '#TMTalkbackEditorName').hide();
      jQuery( '#teditor_name' ).val( TM.FaceBook.prefix + FB.Connect.get_loggedInUser() );

      // Tell Facebook processor to parse the dom after we have updated it.
      FB.XFBML.Host.parseDomTree();

    },

    // Display the facebook login box
    displayFaceBookLogin : function(){
      jQuery( '#' + arguments[0] ).show();
      jQuery( '#' + arguments[1] ).hide();
      // Show the name field again
      jQuery( '#teditor_name' ).val( '' );
      jQuery( '#TMTalkbackEditorName').show();

    },

    /**
    * Post the comment on facebook profile.
    */
    submitComment : function(){
      var commentBox = jQuery( '[name=tmsg_content]');

      var templateData = {
        "images"      : [{ "src" : "http://www.themarker.com/ibo-bac/images/logos/themarkersmall.jpg",
                           "href": "http://www.themarker.com"
                        }],
        "articleTitle": jQuery( '#headlineTitle strong' ).attr('innerHTML'),
        "comment"     : commentBox.val(),
        "articleURL"  : window.location.href
      };

      // Display facebook comment type dialog
      FB_RequireFeatures(["XFBML"], function(){ FB.Facebook.init(TMApplicationKey, TMCallBackURL, null);});
      FB.Connect.showFeedDialog(  TMTemplateBundleId, templateData );

    // Set timeout to catch the facebook dialg and fix its position
    TM.FaceBook.checkForFacebookDialog();
    },

  // This function is a patch to fix the facebook hebrew dialog positioning.
  checkForFacebookDialog : function(){

    var dialog = jQuery( FBDialogId );
    var setTimer = true;
    // Check to see if the dialog is displayed or not.
    if( dialog.size() > 0 ){
      var top = dialog.get(0).style.top;
      // remove the px from the value
      top = top.substring(0, top.length-2);
      if( top > 0 ){
        dialog.css({'right': '0'});
        dialog.css({'left': '-1000'});
        // Cancel the timer since we fixed the popup position
        clearTimeout( FBDialogTimer );
        setTimer =false;
      }
    }

    if( setTimer ){
      FBDialogTimer = setTimeout( TM.FaceBook.checkForFacebookDialog , 250);
    }
  }
  }; // return
}();

