String.prototype.replaceAll = function (stringToFind, stringToReplace) {
    var temp = this;
    var index = temp.indexOf(stringToFind);
    while (index != -1) {
        temp = temp.replace(stringToFind, stringToReplace);
        index = temp.indexOf(stringToFind);
    }
    return temp;
}

function doBookmarks(containerID) {
    jQuery.noConflict();
    jQuery(document).ready(function () {
        jQuery("#" + containerID + " .bookmarks a.external").attr("target", "_blank");

        var docTitle = document.title;
        var docURL = document.location.href;
        var href = "";

        jQuery("#" + containerID + " .bookmarks a.external").each(function (index) {
            href = jQuery(this).attr("href");
            href = href.replaceAll("[TITLE]", docTitle);
            
            //href = href.replace("[URL]", "<a href='" + docURL + "'>" + docURL + "</a>");
            href = href.replaceAll("[URL]", docURL);
            jQuery(this).attr("href", href);
        });

        var c = jQuery("#" + containerID + " .bookmarks").height(),
        d = jQuery("#" + containerID + " .bookmarks ul.socials").height();
        d > c && jQuery("#" + containerID + " .bookmarks-expand").hover(function () {
            jQuery(this).animate({
                height: d + "px"
            }, {
                duration: 400,
                queue: false
            })
        }, function () {
            jQuery(this).animate({
                height: c + "px"
            }, {
                duration: 400,
                queue: false
            })
        });
        if (jQuery("#" + containerID + " .bookmarks-center") || jQuery("#" + containerID + " .bookmarks-spaced")) {
            var a = jQuery("#" + containerID + " .bookmarks").width(),
            b = jQuery("#" + containerID + " .bookmarks:first ul.socials li").width(),
            e = jQuery("#" + containerID + " .bookmarks:first ul.socials li").length,
            f = Math.floor(a / b);
            b = Math.min(f, e) * b;
            if (jQuery("#" + containerID + " .bookmarks-spaced").length > 0) {
                a = Math.floor((a - b) / (Math.min(f, e) + 1));
                jQuery("#" + containerID + " .bookmarks ul.socials li").attr("style", 'margin-left:' + a + 'px !important')
            } else if (jQuery(true)) {
                a = (a - b) / 2;
                jQuery("#" + containerID + " .bookmarks-center").attr("style", 'margin-left:' + a + 'px !important')
            }
        }
    });
}

