
(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}}/gi, options['url']).replace(/{{title}}/gi, 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 = {
      reddit: "http://reddit.com/submit?url={{url}}&title={{title}}",
      digg: "http://digg.com/submit?url={{url}}&title={{title}}",
      facebook: "http://www.facebook.com/sharer.php?u={{url}}&title={{title}}",
      delicious: "http://del.icio.us/post?v=2&url={{url}}&title={{title}}",
      myspace: "http://www.myspace.com/Modules/PostTo/Pages/?l=3&u={{url}}&t={{title}}&c=",
      twitter: "http://twitthis.com/twit?url={{url}}",
      livejournal: "http://www.livejournal.com/update.bml?subject={{title}}&event=<a href={{url}}>{{title}}</a>",
      google: "http://www.google.com/bookmarks/mark?op=edit&bkmk={{url}}&title={{title}}",
      tumblr: "http://tumblr.com/share?v=2&u={{url}}&t={{title}}&s={{title}}%0Avia%20{{url}}"
    };
 
    //default settings
    $.fn.sharehelper.defaults = {
        new_window: true,
        title: encodeURIComponent(document.title),
        url: encodeURIComponent(window.location.href),
        callback: null
    };
})(jQuery);