﻿var ShareArr = { "sharebuttons": [
{ "cssclass": "share-more", "tooltip": "More", "action": "#", "addWinOpen": "false", "link": "" },
{ "cssclass": "share-email", "tooltip": "Email this", "action": "mailto:?body=%PAGETITLE% - %ENCODEURL%&subject=Check out this link on SermonSearch.com", "addWinOpen": "false", "link": "" },
{ "cssclass": "share-facebook", "tooltip": "Share on Facebook", "action": "http://facebook.com/sharer.php?u=%ENCODEURL%", "addWinOpen": "true", "link": "" },
{ "cssclass": "share-twitter", "tooltip": "Share on Twitter", "action": "http://twitter.com/home?status=From%20@sermonsearch%20%PAGETITLE%%20%ENCODEURL%", "addWinOpen": "true", "link": "" },
{ "cssclass": "share-delicious", "tooltip": "Share on Delicious", "action": "http://del.icio.us/post?url=%ENCODEURL%", "addWinOpen": "true", "link": "" },
{ "cssclass": "share-digg", "tooltip": "Share on Digg", "action": "http://digg.com/submit?url=%ENCODEURL%&title=%PAGETITLE%", "addWinOpen": "true", "link": "" },
{ "cssclass": "share-stumbleupon", "tooltip": "Share on StumbleUpon", "action": "http://www.stumbleupon.com/submit?url=%ENCODEURL%&title=%PAGETITLE%", "addWinOpen": "true", "link": "" },
{ "cssclass": "share-reddit", "tooltip": "Share on Reddit", "action": "http://reddit.com/submit?url=%ENCODEURL%", "addWinOpen": "true", "link": "" },
{ "cssclass": "share-mixx", "tooltip": "Share on Mixx", "action": "http://www.mixx.com/submit?page_url=%ENCODEURL%", "addWinOpen": "true", "link": "" },
{ "cssclass": "share-technorati", "tooltip": "Share on Technorati", "action": "http://www.technorati.com/faves?add=%ENCODEURL%", "addWinOpen": "true", "link": "" },
{ "cssclass": "share-googlebuzz", "tooltip": "Share on Google Buzz", "action": "http://www.google.com/reader/link?url=%ENCODEURL%", "addWinOpen": "true", "link": "" },
{ "cssclass": "share-yahoobuzz", "tooltip": "Share on Yahoo! Buzz", "action": "http://buzz.yahoo.com/buzz?targetUrl=%ENCODEURL%", "addWinOpen": "true", "link": "" },
{ "cssclass": "share-typepad", "tooltip": "Share on Typepad", "action": "http://www.typepad.com/services/quickpost/post?v=2&qp_show=ac&qp_title=%PAGETITLE%&qp_href=%ENCODEURL%", "addWinOpen": "true", "link": "" },
{ "cssclass": "share-livejournal", "tooltip": "Share on Livejournal", "action": "http://www.livejournal.com/update.bml?event=%3Cbr%2F%3E%3Ca%20href%3D%22%ENCODEURL%%22%3E%PAGETITLE%%3C%2Fa%3E", "addWinOpen": "true", "link": "" },
{ "cssclass": "share-blogger", "tooltip": "Share on Blogger", "action": "http://www.blogger.com/blog_this.pyra?t=&u=%ENCODEURL%", "addWinOpen": "true", "link": ""}]
};

(function ($) {
    jQuery.fn.sharePanel = function (buttons, options) {
        var settings = jQuery.extend({
            buttonWidth: 32,
            moreWidth: 57,
			panelHeight: 32,
			moreDelay: 400
        }, options);

        var moreTimer = null;

        var curUrl = "" + window.location;
        if (curUrl.indexOf("#") != -1) {
            curUrl = curUrl.substring(0, curUrl.indexOf("#"));
        }
        var encodedCurUrl = encodeURIComponent(curUrl);
        var docTitle = encodeURIComponent(document.title);
		docTitle = docTitle.replace("'", "\\'");

        function buildLink(btnClass, tooltip, action, addOpen, link) {
            var linkHtml = "<a class=\"%BTNCLASS%\" title=\"%TITLE%\" href=\"%LINKPATH%\" onclick=\"%ACTION%\">%TITLE%</a>";

            if (action != "") {
                var actionLink = action.replace(/%URLURL%/g, curUrl);
                actionLink = actionLink.replace(/%ENCODEURL%/g, encodedCurUrl);
                actionLink = actionLink.replace(/%PAGETITLE%/g, docTitle);
                if (addOpen) {
                    actionLink = actionLink;
                    actionLink = "window.open('" + actionLink + "'); return false;";
                }
            }

            var pathLink = link.replace(/%URLURL%/g, curUrl);
            pathLink = pathLink.replace(/%ENCODEURL%/g, encodedCurUrl);
            pathLink = pathLink.replace(/%PAGETITLE%/g, docTitle);
            linkHtml = linkHtml.replace(/%LINKPATH%/g, pathLink);

            linkHtml = linkHtml.replace(/%BTNCLASS%/g, btnClass);
            linkHtml = linkHtml.replace(/%TITLE%/g, tooltip);
            linkHtml = linkHtml.replace(/%ACTION%/g, actionLink);
            return linkHtml;
        }

        var numShares = buttons.sharebuttons.length;
        var numBtns = Math.floor((this.width() - settings.moreWidth) / settings.buttonWidth) + 1;
        if (numShares < numBtns) {
            numBtns = numShares;
        }
        var moreShareButtonId = "moreShareButton" + Math.floor(Math.random() * 100000);
        var moreShareLinksId = "moreShareLinksContainer" + Math.floor(Math.random() * 100000);


        for (i = 1; i < numBtns; i++) {
            this.append(buildLink(buttons.sharebuttons[i].cssclass, buttons.sharebuttons[i].tooltip, 
                buttons.sharebuttons[i].action, buttons.sharebuttons[i].addWinOpen, buttons.sharebuttons[i].link));
        }
        //add the more button
        this.append("<a id=\"" + moreShareButtonId + "\" class=\"" + buttons.sharebuttons[0].cssclass + "\" title=\"" + buttons.sharebuttons[0].tooltip + "\" href=\"#\" >more</a>");
        //add the more links container
        $('body').append("<div id='" + moreShareLinksId + "' class='moreShareLinksContainer'></div>");
        //set the events for the more container
        $("#" + moreShareLinksId).hide();
        $("#" + moreShareButtonId).bind("click", function (event) { event.preventDefault(); });
        $("#" + moreShareButtonId).mouseenter(function() {
            var _pos = $("#" + moreShareButtonId).offset();
            $("#" + moreShareLinksId).css('left', _pos.left + "px");
            $("#" + moreShareLinksId).css('top', (_pos.top + settings.panelHeight + "px"));
            $("#" + moreShareLinksId).slideDown();
            if (moreTimer) { clearTimeout(moreTimer); moreTimer = null; }
        });
        $("#" + moreShareButtonId).mouseleave(function() {
            moreTimer = setTimeout(function() { $("#" + moreShareLinksId).slideUp(); }, settings.moreDelay);
        });
        $("#" + moreShareLinksId).mouseleave(function() {
            moreTimer = setTimeout(function() { $("#" + moreShareLinksId).slideUp(); }, settings.moreDelay);
        });
        $("#" + moreShareLinksId).mouseenter(function() {
            if (moreTimer) { clearTimeout(moreTimer); moreTimer = null; }
        });
        //add the rest of the buttons
        for (i = numBtns; i < numShares; i++) {
            $("#" + moreShareLinksId).append(buildLink(buttons.sharebuttons[i].cssclass, buttons.sharebuttons[i].tooltip,
                buttons.sharebuttons[i].action, buttons.sharebuttons[i].addWinOpen, buttons.sharebuttons[i].link));
        }
    };
})(jQuery);
