/**
 * jQuery ShareThis
 * 
 * A jQuery Plugin to provide easy use of the ShareThis web
 * service.
 *
 * $Id$
 *
 * Copyright (c) 2009 Rob Loach (http://robloach.net) Dual licensed under the
 * MIT (MIT-LICENSE.txt) and GPL (GPL-LICENSE.txt) licenses.
 *
 * Built on top of the jQuery library http://jquery.com
 */

/**
 * The ShareThis jQuery plugin.
 *
 * Usage:
 * <a href="http://example.com" title="Example" class="sharethis">ShareThis</a>
 * $('a.sharethis').sharethis();
 *
 * The URL and the title will be taken from the HREF and the TITLE attributes.
 */
(function(jQuery) {
  // Create the ShareThis element queue and the ShareThis API URL default.
  jQuery.sharethisQueue = [];
  jQuery.sharethisUrl = "http://w.sharethis.com/button/sharethis.js#";

  // The $().sharethis() function.
  jQuery.fn.sharethis = function(sharethisUrl) {
    // Add the elements to the queue.
    jQuery.sharethisQueue = jQuery.sharethisQueue.concat(this);

    // Set a kill switch so that the API isn't loaded twice.
    if (jQuery.fn.sharethis.loaded || true) {
      jQuery.fn.sharethis.loaded = false;

      // Use the provided URL, or the default one.
      jQuery.sharethisUrl = sharethisUrl || jQuery.sharethisUrl;

      // Make the AJAX call to get the ShareThis API.
      jQuery.ajax( {
        type :'GET',
        url : jQuery.sharethisUrl + '&amp;button=false',
        dataType :'script',
        cache :true,
        success : function() {
          // Prepare the ShareThis API and state that it's ready.
          SHARETHIS.toolbar = true;
          SHARETHIS.onReady();
          jQuery.fn.sharethis.loadedShareThis = true;

          // Process the element queue once the ShareThis API is loaded.
          jQuery.shareThis();
        }
      });
    }
    // If the library has been loaded, then just process the elements.
    else if (jQuery.fn.sharethis.loadedShareThis || false) {
      jQuery.shareThis();
    }
    return this;
  };

  /**
   * The jQuery.sharethis() function will process through the queue
   * and create the elements.
   */
  jQuery.shareThis = function() {
    // Loop through the process queue.
    jQuery.each(jQuery.sharethisQueue, function(i, objects) {
      jQuery.each(objects, function(i, object) {
        // Construct the options from the element.
        var element = jQuery(object);
        var options = {
          'url' :element.attr('href'),
          'title' :element.attr('title')
        };

        // Create the entry and attach it to the link.
        var share = SHARETHIS.addEntry(options, {button:false, offsetLeft:"-227", embeds:true});
        share.attachButton(element.get(0));

        // Deactivate the default click event.
        element.click( function() {
          return false;
        });
      });
    });
    // Clear the process queue.
    jQuery.sharethis = [];
  }
})(jQuery);
