// <![CDATA[

/*
 * RR.Sociable.js
 * Version 0.2
 * Romain Ruetschi
 * http://romac.github.com
 * Copyright (c) 2009 Romain Ruetschi
 * License: BSD
 * 
 * 
 * Copyright (c) 2009, Romain Ruetschi <romain.ruetschi@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * The images used by this script come from WP-Sociable wordpress plugin
 * by BlogPlay.com — http://wordpress.org/extend/plugins/sociable
 */
 
( function()
{
    
    if( !window.RR ) {
        
        window.RR = {};
    }
    
    var _settings        = {
        cssURL    : 'http://www.markmuller.ch/js/RRSociable/RRSociable.css',
        imagesURL : 'http://www.markmuller.ch/js/RRSociable/images/'
    };
    
    var _title          = _encodeURIComponent( document.title );
    var _URL            = _encodeURIComponent( document.URL );
    var _socialServices = {
        delicious : {
            image : 'delicious',
            name  : 'Partager sur Delicious',
            url   : 'http://del.icio.us/post?url={url}&amp;title={title}'
        },
        twitter : {
            image : 'twitter',
            name  : 'Partager sur Twitter',
            url   : 'http://twitter.com/home?status={title}%20:%20{url}'
        },
        facebook : {
            image : 'facebook',
            name  : 'Partager sur Facebook',
            url   : 'http://www.facebook.com/share.php?u={url}&amp;t={title}'
        },
        email : {
            image : 'email',
            name  : 'Envoyer par e-mail',
            url   : 'mailto:?subject={title}&amp;body={url}'
        }
    };
    
    function _encodeURIComponent( string )
    {
        return encodeURIComponent( string );
    }
    
    function _write( socialService )
    {
        if( !_socialServices[ socialService ] ) {
            
            return;
        }
        
        socialService = _socialServices[ socialService ];
        
        var image = _settings.imagesURL + socialService.image + '.png';
        var url   = socialService.url.replace( '\{url\}', _URL );
            url   = url.replace( '\{title\}', _title );
        
        var html = '<span class="RRSociable-Button" id="RRSociable-Button-' + image + '">'
                 + '<a href="' + url + '" title="' + socialService.name +'">'
                 + '<img src="' + image + '" alt="' + socialService.name + '" />'
                 + '</a>'
                 + '</span>';
        
        document.write( html );
    }
    
    function _writeAll()
    {
        document.write( '<div class="RRSociable-Bookmarks">' );
        _write( 'facebook' );
        _write( 'twitter' );
        _write( 'delicious' );
        _write( 'email' );
        document.write( '</div>' );
    }
    
    RR.Sociable = {
        writeAll : _writeAll,
        write    : _write
    };
    
} )();

// ]]>