// David Zhou
// jQuery Sharehelper
//
// usage:
// $('#reddit').sharehelper('reddit');
//
// options:
// $('#reddit').sharehelper('reddit', {title: "custom title", url: "custom url", callback: callback_function});
//
// title (string): custom title
// url (string): custom url
// callback (function): custom callback function
// new_window (boolean): if true, opens share link in new window
//
// You can also set global options:
// $.fn.sharehelper.defaults.title = "Custom Title";
// $('#reddit').sharehelper('reddit') //uses "Custom Title" for title
//
// It's also possible to override defaults:
// $('#reddit').sharehelper('reddit', {title: "New Custom Title"});

(function($) {
    $.fn.sharehelper = function(service, options) {
        var options = $.extend({}, $.fn.sharehelper.defaults, options);
        return this.each(function() {
            if (SERVICES[service]===undefined)
                throw "Unrecognized service: " + service;
            $(this).attr('href', SERVICES[service].replace('{{url}}', options['url']).replace('{{title}}', options['title']));
            if (!!options['callback'])
                $(this).bind('click', function(){ options['callback'].call(this);});
            if (options['new_window'])
                $(this).bind('click', function(e){ e.preventDefault(); window.open(this.href);});
        });
    };
    
    var SERVICES = {
      facebook:   "http://www.facebook.com/sharer.php?u={{url}}&title={{title}}",
      delicious:  "http://del.icio.us/post?v=2&url={{url}}&title={{title}}",
      linkedin: "http://www.linkedin.com/shareArticle?mini=true&url={{url}}&title={{title}}&summary=An%20Article%20from%20TEI%20EJ&source=tei%2Dej.org}",
      googlebuzz: "http://www.google.com/buzz/post?message={{title}}&url={{url}}",
      twitter: "http://twitter.com/home?status=Just%20Read%20{{title}}%20at%20{{url}}",
      emailit: "http://iitweb.bloomu.edu/drupal/forward?path={{url}}"
    };
    

    //default settings
    $.fn.sharehelper.defaults = {
        new_window: true,
        title: encodeURIComponent(document.title),
        url: encodeURIComponent(window.location.href),
        callback: null
    };
})(jQuery);