(function ($) {

    $(document).ready(function () {

        Share = $.klass({

            map: {
                facebook: 'sharer.php?u="@href@"&t="@title@"',
                twitter: 'home?status=@title@ @href@',
                digg: 'submit?url="@href@"&title="@title@"',
                delicious: 'post?v=4&noui&jump=close&url=@href@&title=@title@',
                stumbledupon: 'submit?url=@href@&title=@title@',
                reddit: 'submit?url=@href@&title=@title@'
            },

            initialize: function() {
                var self = this;
                this.element.find('.figure a').click(function(){
                    self.element.find('.links').fadeIn(500);
                    return false;   
                });
                this.element.find('.links').hide()
                    .find('a').click(function(){
                        var id = this.parentNode.id;
                        if (id !== 'email') {
                            if (id === 'close') {
                                self.element.find('.links').hide(); 
                            } else {
                                self.handle.call(self, this.href, id);
                            }; 
                            return false;  
                        }
                    });
            },


            getPageTitle: function(id) {
                var title = $.trim(document.title);
                return id === 'twitter' ? title : encodeURIComponent(title);
            },


            handle: function(url, id) {
                var re = /@(href|title)@/g;
                var o = {
                    href: id === 'twitter' ? document.location.href : encodeURIComponent(document.location.href),
                    title: this.getPageTitle(id)
                };
                var query = this.map[id].replace(re, function(m, p) { 
                    return o[p]; 
                })
                window.open(url + query);
            }

        });

    });

})(jQuery);