// Javascript code to manage social plugins for the page (currently just the
// Facebook "like" button). This script assumes MochiKit, and
// Google analytics have already been loaded.

// Ansynchronously load the AddtoAny script so it doesn't lock page loading.
// Note that it uses a2a_config to set a callback so no need to use the deferred
// object that loadScript returns.

var a2a_config = a2a_config || {};
a2a_config.linkname = document.title;
a2a_config.linkurl = document.location.href;

var addToAnyLoaded = function() {
  log("script loading done!");
  a2a_config.target = '.a2a_items';
  a2a.init('page');
  forEach(findDocElements('.social-media-share'), function(x) {showElement(x);});
}

var setupSocial = function() {
  // Before loading hide the social widgets. That way if it fails to load
  // (sometimes happens) we don't see a weird non-functional link. We'll make the
  // social stuff visible after the JS loads. It should be hidden from the
  // start, but just to make sure...
  forEach(findDocElements('.social-media-share'), function(x) {hideElement(x);});

  var loader = new ScriptLoader('http://static.addtoany.com/menu/page.js', addToAnyLoaded);
  log("Script was added");
}

addLoadEvent(setupSocial);

