﻿/*
* social.js v1
* http://www.magiclick.com
* Copyright: MagiClick Digital Solutions
* License: Commercial
*/
function SocialShare() { }

SocialShare.FACEBOOK = 1;
SocialShare.TWITTER = 2;
SocialShare.FRIENDFEED = 3;
SocialShare.YAHOO = 4;
SocialShare.GOOGLE = 5;
SocialShare.DELICIOUS = 6;
SocialShare.LIVE = 7;
SocialShare.MYSPACE = 8;
SocialShare.BLOGGER = 9;

SocialShare.prototype = {
    setURL: function (url) {
        this._url = url;
    },
    getURL: function () {
        return escape(this._url);
    },
    setTitle: function (title) {
        this._title = title;
    },
    getTitle: function () {
        return encodeURI(this._title);
    },
    share: function (o) {
        var socialURL;
        var asPopup = false;
        switch (o) {
            case SocialShare.FACEBOOK:
                socialURL = "http://www.facebook.com/sharer.php?u=" + this.getURL() + "&title=" + this.getTitle();
                break;
            case SocialShare.TWITTER:
                socialURL = "http://twitter.com/home?status=" + this.getTitle() + "+" + this.getURL();
                break;
            case SocialShare.FRIENDFEED:
                socialURL = "http://friendfeed.com/?url=" + this.getTitle() + " - " + this.getURL();
                break;
            case SocialShare.YAHOO:
                socialURL = "http://myweb2.search.yahoo.com/myresults/bookmarklet?u=" + this.getURL() + "&t=" + this.getTitle();
                break;
            case SocialShare.GOOGLE:
                socialURL = "http://www.google.com/bookmarks/mark?op=edit&bkmk=" + this.getURL() + "&title=" + this.getTitle();
                break;
            case SocialShare.DELICIOUS:
                socialURL = "http://del.icio.us/post?url=" + this.getURL() + "&title=" + this.getTitle();
                break;
            case SocialShare.LIVE:
                socialURL = "https://favorites.live.com/quickadd.aspx?marklet=1&mkt=tr-tr&top=1&url=" + this.getURL() + "&title=" + this.getTitle();
                break;
            case SocialShare.MYSPACE:
                socialURL = "http://www.myspace.com/Modules/PostTo/Pages/?u=" + this.getURL() + "&title=" + this.getTitle();
                break;
            case SocialShare.BLOGGER:
                socialURL = "http://www.blogger.com/blog_this.pyra?t&u=" + this.getURL() + "&title=" + this.getTitle();
                break;
            case SocialShare.WORDPRESS:
                socialURL = "http://www.addtoany.com/ext/wordpress/press_this?linkurl=" + this.getURL() + "&linkname=Worldcard&linknote=" + this.getTitle();
        }

        if (asPopup) {
            window.open(socialURL, "_blank", "width=500,height=500");
        } else {
            window.open(socialURL, "_blank");
        }
    }
};
if ("MagiClick" in window) {
    MagiClick.trackSocial = {
        // Requires async. google analytics code
        like: function (targetUrl) {
            if ('_gaq' in window) {
                _gaq.push(['_trackSocial', 'facebook', 'like', targetUrl]);
            }
        },
        unlike: function (targetUrl) {
            if ('_gaq' in window) {
                _gaq.push(['_trackSocial', 'facebook', 'unlike', targetUrl]);
            }
        },
        send: function (targetUrl) {
            if ('_gaq' in window) {
                _gaq.push(['_trackSocial', 'facebook', 'send', targetUrl]);
            }
        },
        tweet: function (targetUrl) {
            if ('_gaq' in window) {
                _gaq.push(['_trackSocial', 'twitter', 'tweet', targetUrl]);
            }
        },
        follow: function (targetUrl) {
            if ('_gaq' in window) {
                _gaq.push(['_trackSocial', 'twitter', 'follow', targetUrl]);
            }
        }
    }

    MagiClick.buttonSocial = {
        likeButton: function (options) {
            var defaults = {
                send: false,
                layout: 'standard',
                width: 450,
                show_faces: false,
                tracking: true
            };
            var opts = $.extend(defaults, options);
            this.each(function () {
                var likeBtnTemplate = '<fb:like ' + 'href="' + $(this).attr('title') +
'" send="' + opts.send +
'" layout="' + opts.layout +
'" width="' + opts.width +
'" show_faces="' + opts.show_faces +
'"></fb:like>';
                $(this).attr('title', '');
                $(this).html(likeBtnTemplate);
                FB.XFBML.parse(this);
            });
            if (opts.tracking) {
                FB.Event.unsubscribe('edge.create', MagiClick.trackSocial.like);
                FB.Event.subscribe('edge.create', MagiClick.trackSocial.like);
                FB.Event.unsubscribe('edge.remove', MagiClick.trackSocial.unlike);
                FB.Event.subscribe('edge.remove', MagiClick.trackSocial.unlike);
                if (options.send) {
                    FB.Event.unsubscribe('message.send', MagiClick.trackSocial.send);
                    FB.Event.subscribe('message.send', MagiClick.trackSocial.send);
                }
            }
        },
        tweetButton: function (options) {
            var defaults = {
                via: 'magiclick',
                count: 'horizontal',
                // none, horizontal, vertical
                lang: 'en',
                width: 130,
                height: 20
            };
            var opts = $.extend(defaults, options);
            this.each(function () {
                var url, countUrl;
                if ($(this).attr('title').indexOf('|') >= 0) {
                    url = $(this).attr('title').split('|')[0];
                    countUrl = $(this).attr('title').split('|')[1];
                } else {
                    url = $(this).attr('title');
                    countUrl = url;
                }

                $(this).attr('title', '');
                var tweetBtnTemplate = '<iframe allowtransparency="true" frameborder="0" scrolling="no" src="http://platform.twitter.com/widgets/tweet_button.html?' +
'url=' + encodeURI(url) +
'&via=' + opts.via +
'&count=' + opts.count +
'&lang=' + opts.lang +
'&counturl=' + encodeURI(countUrl) +
'" style="width:' + opts.width + 'px; height:' + opts.height + 'px;"></iframe>';
                $(this).html(tweetBtnTemplate);
            });
        },
        followButton: function (options) {
            var defaults = {
                show_count: false,
                button: 'blue ',
                // blue,grey
                text_color: '444444',
                link_color: '0474A6',
                lang: 'en',
                width: 240,
                height: 20
            };
            var opts = $.extend(defaults, options);
            this.each(function () {
                var screenName = $(this).attr('title');
                $(this).attr('title', '');
                var followBtnTemplate = '<iframe allowtransparency="true" frameborder="0" scrolling="no" src="http://platform.twitter.com/widgets/follow_button.html?' +
'screen_name=' + screenName +
'&show_count=' + opts.show_count +
'&button=' + opts.button +
'&text_color=' + opts.text_color +
'&link_color=' + opts.link_color +
'&lang=' + opts.lang +
'" style="width:' + opts.width + 'px; height:' + opts.height + 'px;"></iframe>';
                $(this).html(followBtnTemplate);
            });
        },
        plusOneButton: function (options) {
            var defaults = {
                size: 'medium',
                // small, medium, standard, tall
                count: true,
                lang: 'en-US'
            };
            var opts = $.extend(defaults, options);
            this.each(function () {
                $(this).html('<div></div>');
                gapi.plusone.render($(this).children('div')[0], {
                    size: opts.size,
                    count: opts.count,
                    href: $(this).attr('title')
                });
            });
        }
    }
}
(function ($) {
    $.loadFBConnect = function (initOptns, lang, cb) {
        if ('FB' in window) {
            cb();
        } else {
            var initOptions = initOptns;
            var language = lang || 'en_US';
            var fbRoot = document.createElement('div');
            fbRoot.id = 'fb-root';
            document.body.appendChild(fbRoot);
            window.fbAsyncInit = function () {
                FB.init(initOptions);
                cb();
            };
            (function () {
                var e = document.createElement('script');
                e.async = true;
                e.src = document.location.protocol + '//connect.facebook.net/' + language + '/all.js';
                document.getElementById('fb-root').appendChild(e);
            } ());
        }
    };

    $.loadTwitterWidgets = function (cb) {
        if ('twttr' in window) {
            cb();
        } else {
            $.getScript('http://platform.twitter.com/widgets.js', function (e) {
                twttr.events.bind('tweet', function (event) {
                    if (event.target && event.target.nodeName === 'IFRAME') {
                        var targetUrl = MagiClick.getParamFromUri(event.target.src, 'url');
                    }
                    MagiClick.trackSocial.tweet();
                });
                twttr.events.bind('follow', function (event) {
                    MagiClick.trackSocial.follow();
                });
                cb();
            });
        }
    };

    $.loadGooglePlusOne = function (cb) {
        if ('gapi' in window) {
            cb();
        } else {
            $.getScript('https://apis.google.com/js/plusone.js', function (e) {
                cb();
            });
        }
    };
})(jQuery);

