// jQuery Plugin for easy Gigya sharing
$.fn.mmfShare = function(options) {

    var default_image = "http://www." + _MMF_GLOBAL.cookie_domain + "/public/images/core/icons/sites/" + _MMF_GLOBAL.site + "_logo.jpg";
    var gigya_key = _MMF_GLOBAL.api.gigya;
    var share_image;

    // Get og:image or use default site image
    var ogimagetag = $('meta[property="og:image"]').attr('content');

    if (ogimagetag !== '' | ogimagetag !== undefined) {
        share_image = $('meta[property="og:image"]').attr('content');
    } else {
        share_image = default_image;
    }

    var defaults = {
        mode: 'multiSelect',
        userMessage: '',
        title: document.title,
        description: escape($('meta[name="description"]').attr('content')),
        backlink: window.location.toString(),
        actionlink: window.location.toString(),
        actionTitle: '',
        imageSrc: share_image,
        imageHref: window.location.toString(),
        videoSrc: '',
        videoHref: ''
    }

    var options = $.extend({}, defaults, options);

    // Bind to the Share Element
    $(this).bind("click", function(e) {
        e.preventDefault();
        doShare();
    });


    function doShare() {

        // Gigya Code
        var act = new gigya.services.socialize.UserAction();

        // Shows in the Edit Box
        act.setUserMessage(options.userMessage);
        act.setTitle(options.title);
        act.setDescription(options.description);
        act.setLinkBack(options.backlink);
        act.addActionLink(options.actionTitle, options.actionlink);

        if (options.imageSrc !== '') {

            var imageHref = options.backlink;

            if (options.imageHref !== '') {
                imageHref = options.imageHref;
            }

            var image = {
                type: 'image',
                src: options.imageSrc,
                href: imageHref
            }
            act.addMediaItem(image);
        }

        if (options.videoSrc !== '') {

            var videoHref = options.backlink;

            if (options.videoHref !== '') {
                videoHref = options.videoHref;
            }

            var image = {
                type: 'image',
                src: options.videoSrc,
                href: videoHref
            }
            act.addMediaItem(video);
        }

        operationMode = options.mode;

        var cid = '';
        if (options.cid !== '') {
            cid = options.cid;
        }
        
        // Parameters for the showShareUI method, including the UserAction object
        var params =
        {
            userAction: act  // The UserAction object enfolding the newsfeed data.
            ,operationMode: operationMode// Opens the Share Plugin either in Simple or Multiselect mode according to the user connection status.
            ,snapToElementID: "btnShare" // Snaps the Simple Share Plugin to the share button
            ,onError: onError  // onError method will be summoned if an error occurs.
            ,onSendDone: onSendDone // onError method will be summoned after
            // Gigya finishes the publishing process.
            ,showMoreButton: true // Enable the "More" button and screen
            ,showEmailButton: true // Enable the "Email" button and screen
            ,useHTML: true  // Use the HTML implementation of the Plugin (rather then Flash implementation)
            ,cid: cid
        };

        // Show the "Share" dialog
        gigya.services.socialize.showShareUI(params);
    }


    // onError event handler
    function onError(event) {
        alert('An error has occured' + ': ' + event.errorCode + '; ' + event.errorMessage);
    }

    // onSendDone event handler.
    // Displays in the status field, the list of providers to which the newsfeed has been
    // successfully published.
    function onSendDone(event) {
        $('#mmfShareStatus').css("color","green");
        $('#mmfShareStatus').html('The newsfeed has been posted to:' + event.providers);
    }
}

