jQuery.fn.stripTags = function() {
     if (typeof(this.html) == 'function') {
        var html = this.html();
        if (html) {
            return html.replace(/<\/?[^>]+>/gi, '');
        }
    }
    return '';
};

function facebook_stream_publish(node) {
    var node = jQuery(node);
    var container = node.parentsUntil('.box').last();

    var content = jQuery('.message', container);
    var header  = jQuery('h3', container);
    if (header.size() == 0) {
        header = content;
        content = jQuery('');
    }
    var urlTag     = jQuery('.top .sourceText a', container);

    if (urlTag.size() == 0) {
         urlTag     = jQuery('.top a', container);
    }
    url = urlTag.attr('href');
	if(url.substring(0,4) != "http") {
		url = jQuery('base').attr('href') + url;
	}
    FB.ui(
      {
        method: 'stream.publish',
        attachment: {
          name: header.stripTags(),
          caption: content.stripTags(),
          href: url
        },
        action_links: [{
              text: header.stripTags(),
              href: url
          }]
        },
        function(response) {
          if (response && response.post_id) {
              alert('Post was published.');
          } else {
//              alert('Post was not published.');
          }
       }
    );
}
