var ninemsn = window.ninemsn || {};
ninemsn.social = ninemsn.social || {};
ninemsn.social.gigya = ninemsn.social.gigya || {};

ninemsn.social.gigya.plugins = function () {
    function constructor() {

        //private attributes        
        var _conf = {};

        // private methods
        function TrackUserInteraction(eventObj) {

            //calling functions from omntr_s_code.js
            var category = fixVar(getCategory());
            var site = fixVar(getSite());
            var section = fixVar(getSection());
            var subsection = fixVar(getSubSection());
            var pagename = fixVar(getPageName());

            //page fix for slideshows.
            if (pagename == "assetslideshow") {
                pagename += "-" + fixVar(document.title);
            } else if (pagename == "video") {
                var videoTitle = $(".playlist_pane li.selected .itemRight a").attr("title").replace(/([^A-Za-z0-9'&.\s]*)/g, "");
                pagename += "-" + fixVar(videoTitle);
            }

            var omniSite = category + ":" + site;
            var omniSection = !isNullOrEmpty(section) ? omniSite + ":" + section : "";
            var omniSubsection = !isNullOrEmpty(subsection) ? omniSection + ":" + subsection : "";

            var omniPageName = category + ":" + site + ":";
            omniPageName += !isNullOrEmpty(section) ? section + ":" : "";
            omniPageName += !isNullOrEmpty(subsection) ? subsection + ":" : "";
            omniPageName += pagename;

            var clinfo = 'unknown';
            var arrEvar = new Array(51);
            var arrProp = new Array(51);
            var arrEvt = new Array(58);
            var linkName = '';

            arrEvar[2] = omniSite;
            arrEvar[3] = omniSection;
            arrEvar[4] = omniSubsection;
            arrEvar[5] = omniPageName;

            if (eventObj.eventName.toLowerCase() == 'senddone') {
                var socialProvider = fixVar(getBaseProvider(eventObj.providers));
                var socialProviderFunction = fixVar(eventObj.providers);
                clinfo = eventObj.context + ":" + socialProviderFunction;
                linkName = 'GigyaShareToolBar';
                arrEvar[10] = socialProviderFunction;
                arrEvar[49] = socialProvider;
                arrEvt[57] = true;
            } else if (eventObj.eventName.toLowerCase() == 'commentsubmitted') {				
                clinfo = eventObj.context + ":comment";
                linkName = 'GigyaComment';
				arrEvar[49] = eventObj.user.loginProvider;
                arrEvt[58] = true;
            }

            OMNTRLinkTracking(true, linkName, clinfo, arrEvar, arrEvt);
        }

        function getBaseProvider(provider) {
            var baseProvider = provider;

            if (!isNullOrEmpty(baseProvider)) {
                var indexOfDash = baseProvider.indexOf("-");
                baseProvider = (indexOfDash == -1) ? baseProvider : baseProvider.substr(0, indexOfDash);
            }

            return baseProvider;
        }

        function isNullOrEmpty(str) {
            if ((typeof str != 'string') || (str == '')) {
                return true;
            }
            return false;
        }



        //public methods
		this.renderShareToolBar = function (containerID, cid, context, url, title, description, shareButton) {

            if (isNullOrEmpty(shareButton)) {
                shareButton = 'Facebook-Like,Facebook-Send,Twitter-Tweet,Messenger,Email,Share,Google-plusone';
            }
            if (isNullOrEmpty(cid)) {
                cid = "undefined";
            }

            var act = new gigya.services.socialize.UserAction();
            if (!isNullOrEmpty(url)) {
                act.setLinkBack(url);
            }

            if (!isNullOrEmpty(title)) {
                act.setTitle(title);
            }

            if (!isNullOrEmpty(description)) {
                act.setDescription(description);
            }

            var showShareBarUI_params =
            {
                containerID: containerID,
                shareButtons: shareButton,
                emailProviders: [],
                cid: cid,
                context: context,
                onSendDone: TrackUserInteraction,
                userAction: act
            }
			gigya.services.socialize.showShareBarUI(_conf, showShareBarUI_params);

        };

        this.renderComments = function(containerID, cid, context, categoryID, streamID, feedID, loginUIConfig) {

            var showCommentsUI_params = {
                containerID: containerID,
                context: context,
                categoryID: categoryID,
                streamID: streamID,
                width: 600,
                //login config
                enabledProviders: "facebook,twitter,linkedin",
                loginUIConfig: loginUIConfig,
                sessionExpiration: 0,
                //context id contains site.
                cid: cid,
                // Activity config
                privacy: "friends",
                scope: 'both',
                feedID: feedID,
                onCommentSubmitted: TrackUserInteraction
            }

            gigya.services.socialize.showCommentsUI(showCommentsUI_params);
        };

    };

    return new constructor();
} ();

if ( (JS_SITEID == "4387" || JS_SITEID == "2339" || JS_SITEID == "4371") && Ninemsn.Index && Ninemsn.Index.Video && Ninemsn.Index.Video.RenderShareToolBar) {
    Ninemsn.Index.Video.RenderShareToolBar = function (uuid) {

        var videoUrl = replaceQueryString(window.location.href, "uuid", uuid);
        var videoTitle = $(".playlist_pane li.selected .itemRight a").attr("title").replace(/([^A-Za-z0-9'&.\s]*)/g, "");
        var videoDescription = $(".playlist_pane li.selected .itemRight").attr("title").replace(/([^A-Za-z0-9'&.\s]*)/g, "");
		var sButton = '';
		if (JS_SITEID == "2339" || JS_SITEID == "4371")
		{
			sButton = "Facebook-Like,Facebook-Send,Twitter-Tweet,Email,Share";
		}
		ninemsn.social.gigya.plugins.renderShareToolBar("ShareToolBar", JS_SITE, JS_SITE, videoUrl, videoTitle, videoDescription, sButton);
    }
}
