/**
 * Created by IntelliJ IDEA.
 * User: igor.zinken
 * Date: 17-10-11
 * Time: 10:06
 */
var SocialSharer =
{
    /**
     * creates a deeplink to a given page for Facebook using the new sharer
     * allowing us to format the meta data directly
     *
     * @param aURL         {String} URL to the page
     * @param aTitle       {String} title of the page
     * @param aDescription {String} description of the page, max. 400 chars
     * @param aImages      {String/Array} optional: either a single String containing an image path
     *                     or an Array containing image path Strings.
     */
    facebook: function( aURL, aTitle, aDescription, aImages )
    {
        var theURL = "https://www.facebook.com/sharer/sharer.php?s=100";
        aURL       = SocialSharer.sanitizeDeeplink( aURL );

        theURL += "&p[url]="     + aURL;
        theURL += "&p[title]="   + aTitle;
        theURL += "&p[summary]=" + aDescription.split( " " ).join( "+" );

        if ( aImages !== null && aImages !== undefined )
        {
            var theImages;

            if ( aImages instanceof Array )
            {
                theImages = aImages;
            }
            else
            {
                theImages = [ aImages ];
            }

            for ( var i = 0; i < theImages.length; ++i )
            {
                theURL += "&p[images][" + i + "]=" + theImages[ i ];
            }
        }

        window.open( encodeURI( theURL ) );
    },

    twitter: function( aURL, aText, aVia )
    {
        var theURL = "https://twitter.com/share";
        aURL       = SocialSharer.sanitizeDeeplink( aURL );

        theURL += "?url="   + aURL;
        theURL += "&via="   + aVia;
        theURL += "&text="  + aText;

        window.open ( encodeURI ( theURL ) );
    },

    /**
     * we cannot use hashes in deeplinks as it might upset the social
     * networks sharing page! we translate hashes to ?deeplink=
     * parameters which are picked up by .htaccess
     */
    sanitizeDeeplink: function( aDeeplink )
    {
        if ( aDeeplink.indexOf( "#" ) > -1 )
        {
            aDeeplink = aDeeplink.replace( "#", "?deeplink=" );
        }
        return aDeeplink;
    },

    // has a dependency on retweet.js

    retweet: function( aTweetId )
    {
        var theURL = "http://api.twitter.com/1/statuses/retweet/" + aTweetId + ".json";
    },

    /**
     * adds the Facebook like button
     * @param aURL       {String} URL of the page to like
     * @param aContainer {HTML element} the container where the button should be appended to
     */
    addFBlike: function( aURL, aContainer )
    {
        var theButton;

        // IE 7 requires IFRAME as the custom attributes upset the browser
        if ( window.navigator.userAgent.indexOf( "MSIE 7" ) > -1 )
        {
            aContainer.innerHTML = '<iframe src="//www.facebook.com/plugins/like.php?href=' + aURL + '&amp;send=false' +
                    '&amp;layout=button_count&amp;width=75&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;' +
                    'height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:110px; height:21px;"' +
                    ' allowTransparency="true"></iframe>';

        }
        else {
            // all other browsers can use the FB SDK
            theButton = document.createElement( "div" );

            theButton.className = "fb-like";
            theButton.setAttribute( "data-href", aURL );
            theButton.setAttribute( "data-send", false );
            theButton.setAttribute( "data-layout", "button_count" );
            theButton.setAttribute( "data-width", 75 );
            theButton.setAttribute( "data-show-faces", false );

            aContainer.appendChild( theButton );

            var id = "facebook-jssdk";
            var js, fjs = document.getElementsByTagName( "script")[0];
            if ( document.getElementById( id )) return;
            js = document.createElement( "script" ); js.id = id;
            js.src = "//connect.facebook.net/nl_NL/all.js#xfbml=1";
            fjs.parentNode.insertBefore( js, fjs );
        }
    },

    /**
     * adds the Google Plus button
     * @param aURL       {String} URL of the page to plus
     * @param aContainer {HTML element} the container where the button should be appended to
     */
    addPlus: function( aURL, aContainer )
    {
        var theButton = document.createElement( "g:plusone" );
        theButton.setAttribute( "size", "medium" );
        theButton.setAttribute( "href", "http://www.usmedia.nl" );

        aContainer.appendChild( theButton );

        var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
        po.src = 'https://apis.google.com/js/plusone.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore( po, s );
    }
};

