// ---------------------------------------------------------- recordFacebookLike
var recordFacebookLike = function()
{
  var urlTag = $$( 'meta[property=og:url]' ).first();
  new Ajax.Request( '/likes', {'method': 'post', 'parameters': { 'url': urlTag.content } } );
}
// -------------------------------------------------------------- getProfileHash
function getProfileHash()
{
  var img = $$("meta[property=og:image]").first().content;
  var url = $$("meta[property=og:url]").first().content;
  var pageTitle = $$("meta[property=og:title]").first().content;
  var title = pageTitle.replace( /on TeamPages\.com/, '' );
  var desc = $$("meta[property=og:description]").first();
  desc = desc ? desc.content : '';
  return {
      'message': 'Check out ' + title + ' ' + CustomLike.pageType + ' on TeamPages.com!',
      'action_links': [{"text": "View the " + CustomLike.pageType, "href": url }],
      'method': 'stream.publish',
      "attachment": 
        { "name": pageTitle,
          "href": url,
          "description": desc,
          "media": [{"type": "image", "src": img, "href": url }]
        },
      "user_message_prompt": "Share your " + CustomLike.pageType  + " with your friends:"
      };
}
// -------------------------------------------------------------- getLoginStatus
customLike = function()
{
  var session = FB.getSession( getLoginStatusCallback( getProfileHash ) );
  FB.getLoginStatus( getLoginStatusCallback( getProfileHash ) );
}
// ------------------------------------------------------ getLoginStatusCallback
function getLoginStatusCallback( f )
{
  try {
  return function(response )
      {
        if( response.status == 'unknown' )
        {
          FB.login( function( r )
              {
                if( response.status != 'unknown' )
                {
                  FB.ui( f() );
                }
              } );
        }
        else
        {
          FB.ui( f() );
        }
      };
  } catch(e){ alert(e); }
}
// ------------------------------------------------------------------- likeEvent
likeEvent = function( title, eventType )
{
  var session = FB.getSession( getLoginStatusCallback( getEventHash.curry( title, eventType ) ) );
  FB.getLoginStatus( getLoginStatusCallback( getEventHash.curry( title, eventType ) ) );
}
// -------------------------------------------------------------- getEventHash
function getEventHash( title, eventType )
{
  var img = $$("meta[property=og:image]").first().content;
  var url = $$("meta[property=og:url]").first().content;
  var pageTitle = $$("meta[property=og:title]").first().content;
  var desc = $$("meta[property=og:description]").first();
  desc = desc ? desc.content : '';
  return {
      'message': title,
      'action_links': [{"text": "View the " + eventType, "href": url }],
      'method': 'stream.publish',
      "attachment": 
        { "name": pageTitle,
          "href": url,
          "description": desc,
          "media": [{"type": "image", "src": img, "href": url }]
        },
      "user_message_prompt": "Share your event with your friends:"
      };
}
