﻿/*! Copyright © 2010 General Mills. All Rights Reserved. No permission is granted to use, copy or extend this code. */
;$(document).ready(function () {

    var host = QRV.PageManager.host;
    var pageTitle = QRV.PageManager.pageTitle;
    var pageUrl = QRV.PageManager.absoluteURI;

    var SHARE_DOMAINS =
    {
        facebook:
        {
            img: '/assets/images/icons/facebook_share_icon.gif',
            text: 'Facebook',
            url: 'http://www.facebook.com/share.php?u=' + encodeURIComponent(document.URL) + '&t=' + pageTitle + '&o=',
            dcs: true
        },
        twitter:
        {
            img: '/assets/images/icons/twitter_icon.png',
            text: 'Twitter',
            //url: 'http://twitter.com/home?status=' + encodeURIComponent('Found this on ' + host + ': '),
            url: 'http://twitter.com/intent/tweet?text=' + encodeURIComponent('Found this on ' + host + ': ' + document.URL) + '&o=',
            dcs: true
        },
        digg:
        {
            img: '/assets/images/icons/16x16-digg-guy.gif',
            text: 'Digg',
            url: 'http://digg.com/submit?phase=2&title=' + pageTitle + '&url=',
            dcs: false
        },
        delicious:
        {
            img: '/assets/images/icons/delicious_icon.png',
            text: 'Del.icio.us',
            url: 'http://del.icio.us/post?title=' + pageTitle + '&url=',
            dcs: false
        },
        stumbleupon:
        {
            img: '/assets/images/icons/icon_su.gif',
            text: 'StumbleUpon',
            url: 'http://www.stumbleupon.com/submit?title=' + pageTitle + '&url=',
            dcs: false
        },
        reddit:
        {
            img: '/assets/images/icons/reddit_icon.png',
            text: 'Reddit',
            url: 'http://reddit.com/submit?title=' + pageTitle + '&url=',
            dcs: false
        }
    };

    $.fn.shareThat = function (shareOptions) {
        var shareModal = null;                            //modal share pop-up box
        var target = $(this);                             //this = jQuery object of the selected HTML element
        shareOptions = $.extend(
        {
            facebook: true,
            twitter: true,
            digg: false,
            delicious: false,
            stumbleupon: false,
            reddit: false
        },
        shareOptions);

        function openShareLink(obj) {
            var share = obj.data;
            if (share.dcs) {
                dcsMultiTrack('DCS.dcsuri', '/sharethis/' + share.text.toLowerCase() + '/', 'WT.ti', 'Share This - ' + share.text);
            }
            shareModal.hide();

            var target = encodeURIComponent(pageUrl + shareOptions.path);
            window.open(share.url + target, share.text, 'status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=0'); //+ '?guid=' + shareOptions.GUID
        };

        function init() {
            if (shareModal == null) {
                //create modal popup containg list of share links
                shareModal = $(document.createElement('div'));
                shareModal.addClass('SHARETHAT_modal');
                shareModal.css(
                {
                    top: Math.round(target.offset().top + target.height() + 9) + 'px',
                    left: Math.round(target.offset().left - 6) + 'px',
                    position: 'absolute',
                    display: 'none'
                });
                shareModal.appendTo('body');

                //create popup close button
                var closeButton = $(document.createElement('a'));
                closeButton.attr(
                {
                    href: 'javascript:void(0);',
                    style: 'position: absolute; right: 4px;'

                });
                closeButton.appendTo(shareModal);
                closeButton.click(function () {
                    shareModal.hide();
                    return false;
                });

                var closeIcon = $(document.createElement('img'));
                closeIcon.attr(
                {
                    src: '/assets/images/icons/close_icon.gif'
                });
                closeIcon.appendTo(closeButton);

                var list = $(document.createElement('ul'));
                list.appendTo(shareModal);
                for (var x in shareOptions) {
                    if (shareOptions[x]) {
                        var share = SHARE_DOMAINS[x];
                        if (share != null) {
                            var li = $(document.createElement('li'));
                            li.addClass('SHARETHAT_li');
                            li.appendTo(list);

                            var a = $(document.createElement('a'));
                            a.attr('href', 'javascript:void(0);');
                            a.appendTo(li);
                            a.bind('click', share, openShareLink);

                            var img = $(document.createElement('img'));
                            img.attr('src', share.img);
                            img.appendTo(a);

                            var span = $(document.createElement('span'));
                            span.text(share.text);
                            span.appendTo(a);
                        }
                    }
                }
            }
            target.click(function () { shareModal.show(); });
        };
        init();
    };
});

