/**
 * object for the social bar's js.
 * this script may be minified, use double ended comments only.
 * @author Josh Richet <jrichet@gowfb.com>
 */
function SocialBar(gaq, facebookAppId)
{
    var _gaq = gaq;
    var self = this;

    /**
     * main function,
     * handles all of the document related stuff,
     * and calls the analytics setup functions.
     */
    this.init = function(){
        /**
         * dynamically loaded buttons have inline styles from the social sites.
         * this is the only way to override what little we can.
         */
        $('#___plusone_0').css('margin-top','3px');
        $('#___plusone_0').css('width','65px');
        $('.fb_button_text').css('color','#3B5998');
        $('#shareThisAbsWrap').slideDown('fast');

        /** send the page body to the printer **/
         $('#printThisPage').bind('click',function(event){
           $('body #bd #cont #inner.wp').printElement({pageTitle:document.title});
            /* give the print button a google analytics event on click. */
            self.trackEvent('ShareClick','printButton',document.location);
        });

        /** bookmark **/
        $('#faveThisPage').jBrowserBookmark();
        /* give the bookmark button a google analytics event on click. */
        $('#faveThisPage').bind('click',function(){
            self.trackEvent('ShareClick','bookmarkButton',document.location);
        });

        /* dummy-friendly browser based renaming. */
        if($.browser.msie ){
            $('#faveText').html('Favorite');
        }

        this.loadFacebookAnalytics(facebookAppId);
        this.loadTwitterAnalytics();
    }
    /**
     * from the google analytics documentation. needed by twitter social event tracking.
     * @param uri
     * @param paramName
     */
    this.extractParamFromUri = function(uri, paramName)
    {
        if (!uri) {
            return;
        }
        var uri = uri.split('#')[0];
        var parts = uri.split('?');
        if (parts.length == 1) {
            return;
        }
        var query = decodeURI(parts[1]);
        paramName += '=';
        var params = query.split('&');
        for (var i = 0, param; param = params[i]; ++i) {
            if (param.indexOf(paramName) === 0) {
                return unescape(param.split('=')[1]);
            }
        }
    }

    this.trackEvent = function(eventType, eventName, eventDesc)
    {
        _gaq.push(['_trackEvent', eventType, eventName, eventDesc]);
    }

    this.loadFacebookAnalytics = function(appId){
        if (typeof(FB) != 'undefined' && FB != null ) {
            window.fbAsyncInit = function() {
                FB.init({
                    appId: appId,
                    cookie:true,
                    status:true,
                    xfbml:true,
                    channelUrl: window.location.protocol + '//' + window.location.hostname + '/fb-channel'
                });

                FB.Event.subscribe('edge.create', function(targetUrl) {
                  _gaq.push(['_trackSocial', 'facebook', 'like', targetUrl]);
                  self.trackEvent('ShareClick','facebookLikeButton',document.location);
                });
                FB.Event.subscribe('edge.remove', function(targetUrl) {
                  _gaq.push(['_trackSocial', 'facebook', 'unlike', targetUrl]);
                  self.trackEvent('ShareClick','facebookUnlikeButton',document.location);
                });
                FB.Event.subscribe('message.send', function(targetUrl) {
                  _gaq.push(['_trackSocial', 'facebook', 'send', targetUrl]);
                  self.trackEvent('ShareClick','facebookShareButton',document.location);
                });
            };
        }else{
            setTimeout('this.loadFacebookAnalytics()',1500);
        }
    }

    this.loadTwitterAnalytics = function(){
        if (typeof(twttr) != 'undefined' && twttr != null ) {
            twttr.events.bind('tweet', function(event) {
                if (event) {
                    var targetUrl;
                    if (event.target && event.target.nodeName == 'IFRAME') {
                        targetUrl = this.extractParamFromUri(event.target.src, 'url');
                    }

                    _gaq.push(['_trackSocial', 'twitter', 'tweet', targetUrl]);
                    self.trackEvent('ShareClick','TwitterTweetButton',document.location);
                }
            });
        }else{
            setTimeout('this.loadTwitterAnalytics()',1500);
        }
    }
}

