/* addthis configuration is set in the ga-load.js file
  (because addthis_config.data_ga_tracker = pageTracker needs to be set there)
  also because it needs to be set before addthis <script> tag in footer.php

  relies on the following elements in the DOM:
    #add-this
    .content-wrapper
  relies on the following class names:
    hide-accessibly
    fixit
*/
// set up positioning of addthis widget

(function(win, doc, $) {

  var $win = $(win),
      $doc = $(doc);

  $doc.ready(function() {

    var $content = $('div.content-wrapper'),
        $addThis = $('#add-this');
    if ( !$addThis.length ) {
      if (win.console && win.console.log) { win.console.log('addthis not loaded');}
      return;
    }

    // test for position fixed.
    var test = $('<div class="hide-accessibly" style="top:0;height:100px;"><div style="position:fixed; top: 10px;" ></div></div>').appendTo('body');
    $.support.fixed = test.offset().top + 10 == test.find('div')[0].offsetTop;
    test.remove();

    // conditionally set addthis bar to fixed position, unless browser doesn't support it
    if ( $.support.fixed ) {

      $doc.bind('flipFixed', function(event) {
        var fixit = $win.scrollTop() > $content.offset().top - 21;
        $(doc.body).toggleClass('fixit', fixit);
      });

      $win
      .bind('scroll', function(event) {
        $doc.trigger('flipFixed');
      });

      $doc.trigger('flipFixed');

      // after all is set up, put it in the right place and show it
    	$addThis.animate({left: 0}, 600);

    }

    // hide addthis toolbar if window is too narrow
    $win
    .bind('resize', function() {

      win.setTimeout(function() {
        $('body').toggleClass('narrow', $win.width() < 1048);
      }, 100);

    })
    .trigger('resize');

  });

})(window, document, jQuery );

