/**
 * Inserts Google-Plus button.
 * 
 * The Widget inserts a Google-Plus button into the target element on which the widget is executed on.
 * 
 * ### Examples
 * The Widget is executed on a pageelement and is called with passing optional parameters. In this example the Google-Plus button references to the web page of ePages, has the biggest available size and shows the amount of already given +1 for that page in a bubble next to the button.
 * 
 * JavaScript:
 * 
 *     
 *     <script>
 *     		jQuery.ready({
 *     				plugin: [ 'e_epages.externalcontent.ui.googleplus' ],
 *     				DOM:    true
 *     			}, function($){
 *     				var widget = de_epages('<div>').externalcontentUiGoogleplus({
 *     					href :	"http://www.epages.com/de/",
 *     					size :	"tall",
 *     					annotation : "bubble"
 *     				});
 *     
 *     			});
 *     </script>
 * 
 * 
 * @class jQuery.ui.externalcontentUiGoogleplus
 * @extends jQuery.widget
 * 
 * @uses de_epages, jQuery.ui.widget
 * @uses de_epages.externalcontent.ui.googleplus
 * @since 6.15.0
 */

/**
 * @cfg {String} [href] A reference to the target page to receive the +1
 */

/**
 * @cfg {String} [size] Defining the size of the Google button. Possible values are: 'small', 'medium', 'standard', 'tall'. The default value is 'standard'
 */

/**
 * @cfg {String} [annotation] The parameter defining the display of the amount of +1 the button target already has. The possible values are: 'inline', 'bubble', 'none'. Default value is 'bubble'
 */

/**
 * See `jQuery.ui.externalcontentUiGoogleplus` for details.
 * 
 * @param {Object} [options] A map of additional options to pass to the method.
 * @param {String} [href] A reference to the target page to receive the +1
 * @param {String} [size] Defining the size of the Google button. Possible values are: 'small', 'medium', 'standard', 'tall'. The default value is 'standard'
 * @param {String} [annotation] The parameter defining the display of the amount of +1 the button target already has. The possible values are: 'inline', 'bubble', 'none'. Default value is 'bubble'
 * 
 * @method externalcontentUiGoogleplus
 * @member jQuery
 * 
 * @since 6.15.0
 */

/*
 * @copyright		© Copyright 2006-2012, epages GmbH, All Rights Reserved.
 *
 * @module			de_epages.externalcontent.ui.googleplus
 */
/*jslint nomen: true*/
/*global define, window*/
define('de_epages/externalcontent/ui/googleplus', [
	'jquery/ui/widget',
	'ep',
	'de_epages'
], function ($, ep, de_epages) {
    'use strict';
    // Constants.
    var gClassNames = 'SocialWeb',
        ajaxCache;

    // The actual widget.
    $.widget('ui.externalcontentUiGoogleplus', {
        options: {
            href: ep.config.canonicalUrl || ep.config.webUrl,
            // URL to which +1 should reference.
            layout: 'standard',
            // String specifying the size of the button.
            // Possible values: 'small', 'medium', 'standard', 'tall'.
            type: 'bubble'
            // String specifying the annotation (counter) shown.
            // Possible values: 'none', 'bubble'.
        },

        _create: function () {
            var self = this,
                o = self.options;
            // Tell google to wait for us to tell *gapi.plusone* to render the button. (See below.)
            window.___gcfg = {
                parsetags: 'explicit',
                lang: ep.config.language
            };
            // Append *container* for gadget to *self.element*.
            self.container = $('<div>').appendTo(self.element.addClass(gClassNames));

            // Load script from Google.
            if (!ajaxCache) {
                if (window.gapi && window.gapi.plusone) {
                    ajaxCache = $.Deferred().resolve();
                } else {
                    ajaxCache = $.ajax({
                        dataType: 'script',
                        url: 'https://apis.google.com/js/plusone.js',
                        cache: true
                    });
                }
            }

            ajaxCache.done(function () {
                // Render button.
                window.gapi.plusone.render(self.container[0], {
                    href: o.href,
                    size: o.layout,
                    annotation: o.type
                });
            });
        },

        /**
         * This method removes the container on which the Google-Plus button has been appended to.
         * 
         * @method destroy
         * @member jQuery.ui.externalcontentUiGoogleplus
         * 
         * @since 6.15.0
         */
        destroy: function () {
            // Remove all traces of the widget.
            this.element.removeClass(gClassNames);
            this.container.remove();
            this._superApply(arguments);
        }

    });

    return de_epages;

});