/**
 * jQuery (a)Social plugin
 *
 * Copyright (c) 2010 Anton Shevchuk
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * @author 	Anton Shevchuk AntonShevchuk@gmail.com
 * @version 0.0.1
 */
;(function($) {
    /**
     * Create a new instance of plugin
     *
     * @return {Object}	Returns a new plugin object.
     * @constructor	
     */
    $.fn.asocial = function(settings) {
        var defaults  = {
            social:[
                'favorites',
                'twitter',
                'bitly',
                'facebook',
                'digg',
                'reddit',
                'stumbleupon',
                'delicious'
                ]
        };
        var plugin = this;
        
        /**
         * Construct
         */
        plugin.each(function(){
            var $this = $(this);
                $this.append('<ul class="asocial"></ul>');
            
            var $list = $this.find('ul');
            var url   = encodeURIComponent(document.location.toString());
            var title = encodeURIComponent(document.title);
            var description = encodeURIComponent($('meta[name=description]').text());
            
            // initialize
            this.init = function() {
                for (var i = 0; i<this.options.social.length; i++) {
                    if (this[this.options.social[i]]) {
                        this[this.options.social[i]]();
                    }
                }
            }
            
            this.link = function(caption, href) {
                var link = $('<a href="'+href+'" title="'+caption+'" class="'+caption+'">'+caption+'</a>');
                var item = $('<li></li>');
                    item.append(link);
                $list.append(item);
            }
            
            // to browser favorities
            this.favorites = function() {
                var link = $('<a href="#" title="add to bookmarks" class="favorites">Add to bookmarks</a>');
                    link.click(function(){
                        if (document.all) {
                            window.external.AddFavorite(document.location.toString(), document.title); 
                        } else {
                            window.sidebar.addPanel(document.title, document.location.toString(),'');
                        }
                    });
                var item = $('<li></li>');
                    item.append(link);
                $list.append(item);
            }
            // @see http://www.addthis.com/bookmark.php
            // social networks
            // @see http://about.digg.com/button
            this.digg = function() {
                this.link('digg', 'http://digg.com/submit?url='+url+'&title='+title+'&bodytext='+description);
            }
            // @see http://www.reddit.com/buttons/
            this.reddit = function() {
                this.link('reddit', 'http://www.reddit.com/submit?url='+url+'&title='+title);
            }
            // @see ???
            this.stumbleupon = function() {
                this.link('stumbleupon', 'http://www.stumbleupon.com/submit?url='+url+'&title='+title);
            }
            this.twitter = function() {
                this.link('twitter', 'http://twitter.com/home?status='+title+'%20'+url);
            }
            this.bitly = function() {
                this.link('bitly', 'http://bit.ly/?url='+url);
            }
            // @see http://developerwiki.myspace.com/index.php?title=How_to_Add_Post_To_MySpace_to_Your_Site
            this.myspace = function() {
                this.link('myspace', 'http://www.myspace.com/Modules/PostTo/Pages/?u='+url);
                // this.link('myspace', 'http://www.myspace.com/index.cfm?fuseaction=postto&t='+title+'&c='+description+'&u='+url);
            }
            // @see http://wiki.developers.facebook.com/index.php/Facebook_Share
            this.facebook = function() {
                this.link('facebook', 'http://facebook.com/sharer.php?u='+url+'&t='+title);
            }
            // @see http://delicious.com/help/savebuttons
            this.delicious = function() {
                this.link('delicious', 'http://delicious.com/save?v=5&noui&jump=close&url='+url+'&title='+title);
            }
            // @see ???
            this.technorati = function() {
                this.link('technorati', 'http://technorati.com/faves/?add='+url);
            }
            // @see http://www.mister-wong.com/stuff/#button
            this.misterwong = function() {
                this.link('misterwong', 'http://www.mister-wong.com/index.php?action=addurl&bm_url='+url+'&bm_description='+title);
            }
            // ???
            this.google = function() {
                this.link('google', 'http://www.google.com/bookmarks/mark?op=add&bkmk='+url+'&title='+title+'&annotation'+description);
            }
            // @see http://bookmarks.yahoo.com/tools/showsave
            this.yahoo = function() {
                this.link('yahoo', 'http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&u='+url+'&t='+title);
            }
            // russian social bookmarks
            // @see http://www.mister-wong.ru/stuff/#button
            this.misterwongru = function() {
                this.link('misterwong', 'http://www.mister-wong.ru/index.php?action=addurl&bm_url='+url+'&bm_description='+title);
            }
            this.moemesto = function() {
                this.link('moemesto', 'http://moemesto.ru/post.php?url='+url+'&title='+title);
            }
            this.memori = function() {
                this.link('memori', 'http://memori.ru/link/?sm=1&u_data[url]='+url+'&u_data[name]='+title);
            }
            this.bobrdobr = function() {
                this.link('bobrdobr', 'http://bobrdobr.ru/addext.html?url='+url+'&title='+title);
            }
                        
            // now initialize options
            this.options = $.extend({}, defaults, settings);
            this.init();
            
            return $this;
        });

    }
})(jQuery);

// Ugly patch for IE
[].indexOf || (Array.prototype.indexOf = function(v){
    for(var i = this.length; i-- && this[i] != v;);
    return i;
});
