/**
 * Inserts translation widgets.
 * 
 * Inserts a Google-translation-Widget or a Microsoft-translation-Widget into the target element on which this widget is executed on.
 * 
 * ### Examples
 * The Widget is executed on a page element and is called with passing optional parameters. With passing 'microsoft' as service parameter the translator Widget of Microsoft is appendet to the target page element.
 * 
 * JavaScript:
 * 
 *     
 *     
 *     <script>
 *     		jQuery.ready({
 *     				plugin: [ 'e_epages.externalcontent.ui.translate' ],
 *     				DOM:    true
 *     			}, function($){
 *     				var widget = de_epages('<div>').externalcontentUiTranslate({
 *     					service : 'microsoft'
 *     				});
 *     		});
 *     </script>
 * 
 * The Widget is executed on a page element and is called with passing optional parameters. With passing 'google' as service parameter the translator Widget of Google is appendet to the target page element.
 * 
 * JavaScript:
 * 
 *     
 *     
 *     <script>
 *     		jQuery.ready({
 *     				plugin: [ 'e_epages.externalcontent.gadget.translate' ],
 *     				DOM:    true
 *     			}, function($){
 *     				var widget = de_epages('<div>').externalcontentUiTranslate({
 *     					service : 'google'
 *     				});
 *     		});
 *     </script>
 * 
 * 
 * @class jQuery.ui.externalcontentUiTranslation
 * @extends jQuery.widget
 * 
 * @uses de_epages
 * @uses jQuery.ui.widget
 * @uses jQuery.uid
 * @since 6.15.0
 */

/**
 * @cfg {String} [services] defines which of the available translator Widgets gets appended
 */

/**
 * See `jQuery.ui.externalcontentUiTranslation` for details.
 * 
 * @param {Object} [options] A map of additional options to pass to the method.
 * @param {String} [services] defines which of the available translator Widgets gets appended
 * 
 * @method externalcontentUiTranslation
 * @member jQuery
 * 
 * @since 6.15.0
 */

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

    // The actual widget.
    $.widget('ui.externalcontentUiTranslation', {

        options: {
            service: 'google' // Or 'microsoft'. (You be the judge!)
        },

        _create: function () {
            var self = this,
                o = self.options,
                googleTranslateUrl = '//translate.google.com/translate_a/element.js',
                googleTranslateScriptSrc = googleTranslateUrl + '?cb=' + 'googleTranslationCallback',
				microsoftTranslateScriptSrc = ((window.location && window.location.href && window.location.href.indexOf('https') === 0) ? "https://ssl.microsofttranslator.com" : "http://www.microsofttranslator.com") + "/ajax/v2/widget.aspx?mode=manual&from=en&layout=ts";


            if (o.service === 'google') {
                // Extend the widget prototype class.
                window.googleTranslationCallback = function () {
                    var translateElement = new window.google.translate.TranslateElement({
                        pageLanguage: ep.config.language,
                        layout: window.google.translate.TranslateElement.InlineLayout.SIMPLE
                    }, 'google_translate_element');
                };

                self.container = $('<div id="google_translate_element">').appendTo(self.element.addClass(gClassNames));

                if (window.google && window.google.translate) {
                    window.googleTranslationCallback();
                } else {
                    $('<script type="text/javascript" src="' + googleTranslateScriptSrc + '">').appendTo('body');
                }
            }
            if (o.service === 'microsoft') {
                self.container = $('<div id="MicrosoftTranslatorWidget">').appendTo(self.element.addClass(gClassNames));
                $('<script type="text/javascript" src="' + microsoftTranslateScriptSrc + '">').appendTo('body');
            }
        },

        /**
         * This method removes the container on which the Google-Plus button has been appended to.
         * 
         * @method destroy
         * @member jQuery.ui.externalcontentUiTranslation
         * 
         * @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;


});