define([
    'text!translate.html!strip'
  , 'css!translate'
  ],
  function (source) {
    var response = {};

    response.data = source;
    response.callback = function (self) {

      var that = {};
      var priv = {};

      // ====================================================================
      //                            ATTRIBUTES
      // ====================================================================

      // this gadget
      priv.gadget = self;

      // document
      priv.doc = $.mobile.document;

      // ====================================================================
      //                            INTERNAL METHODS
      // ====================================================================

      //
      // prefix element id with the parent id to allow instantiation
      // @method prefixId
      // @param  {string} id > of current gadget/element
      // @param  {object} parent gadget
      // @return {string} new id for gadget/element
      //
      priv.prefixId = function (id, gadget) {
        return gadget.dom.attr('id') + "__" + id;
      }

      //
      // modify the icon of the popup button
      // @method setIcon
      // @param {object} gadget > gadget object
      // @param {string} icon > class name for icon
      //
      priv.setIcon = function (gadget, language) {
        // TODO: remove setTimeout!
        window.setTimeout(function(){
          $(".pick_language")
            .removeClass("[class|='ui-icon']")
            .addClass("ui-icon-" + language);
        },50);
      };

      //
      // translate all page elements labelled with class "translate"
      // @method translate
      // @param {string} language > language to set
      //
      priv.translate = function (language, target) {
        var collection = target || $('body');
        collection.find("." + window.App.settings.language_selector).i18n();
      };

      //
      // setup i18n plugin
      // @method setupTranslations
      // @param {string} language > default language to set
      //
      priv.setTranslations = function (language) {
        if (window.App.settings.language_set === false) {
          $.i18n.init({
            "lng": language,
            "load": 'current',
            "detectLngQS": 'lang',
            "fallbackLng": false,
            "resGetPath": 'lang/__lng__/__ns__.json',
            "ns": 'app'
          }, function () {
            priv.translate(language);
          });
          window.App.settings.language_set = true;
        } else {
          priv.translate(language);
        }
      };

      //
      // initialize i18n translation plugin
      // @method initialize
      // @param {string} language > language to set
      //
      priv.initialize = function (language) {
        // setup i18n
        priv.setTranslations(language);

        // setup icon
        priv.setIcon(language);
      };

      // ========================================================================
      //                            BINDINGS
      // ========================================================================
      // TODO: remove once callable per link!
      // TODO: why do I need to call $.i18n.setLng again?
      priv.doc.on("ready", function () {
        var that = $(this);

        that
          .filter(function() {
            return that.attr("i18n") !== "set";
          })
          .attr("i18n", "set")
          .on("click", "a.change_language", function (e) {
            var language = this.getAttribute("href").replace('?lang=','');
            e.preventDefault();
            e.stopPropagation();

            $(".popup_all").popup( 'close' );

            if (window.App.settings.language_current !== language) {
              // update language
              window.App.settings.language_current = language;

              // update icon
              priv.setIcon(language);

              // translate
              $.i18n.setLng(language, function() {
                priv.setTranslations(language);
              });
            }
          });
      });

      // ====================================================================
      //                            PUBLISH METHODS
      // ====================================================================
      // TODO: remove once callable via link
      // methods should either be available INTERNALLY
      // > gadget.translate("en-EN")
      // > same for iFrame/Sandbox = public methods must be "exported"
      // or methods are available EXTERNALLY
      // > get a credit-score ?param=username
      window.App.translate = priv.translate;

      // prefix instance-ids, pass JSON, trigger("create")
      window.App.renderGadgets(priv.gadget);

      // ====================================================================
      //                            INITIALIZE
      // ====================================================================
      priv.initialize(window.App.settings.language_current);
    }

  // return response object
  return response;
  }
);
