﻿var bookmarks;
var bookmarkTemplate = '<li class="{0}"><a title=\'{1}\' href="{2}" rel="nofollow" class="external" target="_blank">{3}</a></li>';
function initializeBookmarks() {
    if (bookmarks != null) {
        return;
    } else {
        bookmarks = new Array(10);
        for (i = 0; i < bookmarks.length; ++i)
            bookmarks[i] = new Array(4);

        //digg
        bookmarks[0][0] = "digg";
        bookmarks[0][1] = "Digg";
        bookmarks[0][2] = "http://digg.com/submit?phase=2&amp;url={url}&amp;title={title}";
        bookmarks[0][3] = "Digg this!";
        //twitter
        bookmarks[1][0] = "twitter";
        bookmarks[1][1] = "Twitter";
        bookmarks[1][2] = "http://twitter.com/home?status={url}";
        bookmarks[1][3] = "Tweet This!";
        //stumbleupon
        bookmarks[2][0] = "stumbleupon";
        bookmarks[2][1] = "Stumbleupon";
        bookmarks[2][2] = "http://www.stumbleupon.com/submit?url={url}";
        bookmarks[2][3] = "Stumble upon something good? Share it on StumbleUpon";
        //delicious
        bookmarks[3][0] = "delicious";
        bookmarks[3][1] = "Delicious";
        bookmarks[3][2] = "http://del.icio.us/post?url={url}&amp;title={title}";
        bookmarks[3][3] = "Share this on del.icio.us";
        //facebook
        bookmarks[4][0] = "facebook";
        bookmarks[4][1] = "Facebook";
        bookmarks[4][2] = "http://www.facebook.com/share.php?u={url}";
        bookmarks[4][3] = "Share this on Facebook";
        //myspace
        bookmarks[5][0] = "myspace";
        bookmarks[5][1] = "MySpace";
        bookmarks[5][2] = "http://www.myspace.com/Modules/PostTo/Pages/?l=3&amp;u={url}";
        bookmarks[5][3] = "Post this to MySpace";
        //google
        bookmarks[6][0] = "google";
        bookmarks[6][1] = "Google";
        bookmarks[6][2] = "http://www.google.com/bookmarks/mark?op=edit&amp;bkmk={url}";
        bookmarks[6][3] = "Add this to Google Bookmarks";
        //technorati
        bookmarks[7][0] = "technorati";
        bookmarks[7][1] = "Technorati";
        bookmarks[7][2] = "http://www.technorati.com/faves?add={url}";
        bookmarks[7][3] = "Share this on Technorati";
    }
}

function initBookmarkItem(index) {
    if (bookmarks[index][2] != undefined) {
        var url = bookmarks[index][2];
        url = url.replace('{url}', entryPermaLink);
        url = url.replace('{title}', entryEncodeTitle);

        var item = bookmarkTemplate;
        item = item.replace('{0}', bookmarks[index][0]);
        item = item.replace('{1}', bookmarks[index][3]); //'Submit ' + entryTitle);
        item = item.replace('{2}', url);
        item = item.replace('{3}', bookmarks[index][1]);

        return item;
    }
}

jQuery(document).ready(function() {
    if (jQuery('.share-block').length) {
        initializeBookmarks();
        var template = "";
        for (i = 0; i < bookmarks.length; ++i) {
            template = template + initBookmarkItem(i);
        }

        jQuery(template).appendTo('.share-buttons');
        
        function appendShareItItem() {
            // check out whether object 'SHARETHIS' exists
            if (typeof(SHARETHIS) != 'undefined') {
                var shareIt = '<li class="sharethis"><a title=\'{0}\' href="javascript:void(0);" rel="nofollow" id="sharethis">ShareThis</a>';
                shareIt = shareIt.replace('{0}', 'Submit ' + entryTitle);
                shareIt = shareIt.replace('{1}', entryTitle);
                shareIt = shareIt.replace('{2}', entryPermaLink);

                jQuery('.share-buttons li:last').before(shareIt);
                var object = SHARETHIS.addEntry({ title: "{1}", url: "{2}", summary: "" }, { button: false });
                var element = document.getElementById("sharethis");
                object.attachButton(element);
            }
        }

        appendShareItItem();
    }
});
