/**
 * Created on : 2014.08.24., 5:26:26
 * Author     : Lajos Molnar <lajax.m@gmail.com>
 * since 1.0
 */

var translate = (function () {

    /**
     * @type string
     */
    var _originalMessage;

    /**
     * @param {object} $this
     */
    function _translateLanguage($this) {
        var data = {
            id: $this.data('id'),
            language_id: $('#language_id').val(),
            translation: $.trim($this.closest('tr').find('.translation').val())
        };

        helpers.post($('#language_id').data('url'), data);
    }

    /**
     * @param {object} $this
     */
    function _copySourceToTranslation($this) {

        if(typeof tm_googleApiKey == 'undefined') // default bahavior - copy original text to translation field
        {
            if ($.trim($this.closest('tr').find('.translation').val()).length === 0) {
                $this.closest('tr').find('.translation').val($.trim($this.val()));
            }

            _translateLanguage($this.closest('tr').find('button'));
        }
        else  // google translation is enabled - translate and copy translation ...
        {
            if ($.trim($this.closest('tr').find('.translation').val()).length === 0) {
                helpers.googleTranslate($.trim($this.val()), $('#language_id').val(), function(result) {
                    if (result.success) {
                        $this.closest('tr').find('.translation').val(result.text);
                    } else {
                        helpers.showMessages(lajax.t('Google translation failed, source text was used as translation!'));
                        $this.closest('tr').find('.translation').val($.trim($this.val()));
                    }

                    _translateLanguage($this.closest('tr').find('button'));
                });
            }
        }
    }

    return {
        init: function () {
            $('#translates').on('click', '.source', function () {
                _copySourceToTranslation($(this));
            });
            $('#translates').on('click', 'button', function () {
                _translateLanguage($(this));
            });
            $('#translates').on('focus', '.translation', function () {
                _originalMessage = $.trim($(this).val());
            });
            $('#translates').on('blur', '.translation', function () {
                if ($.trim($(this).val()) !== _originalMessage) {
                    _translateLanguage($(this).closest('tr').find('button'));
                }
            });
            $('#translates').on('change', "#search-form select", function(){
                $(this).parents("form").submit();
            });
        }
    };
})();

$(document).ready(function () {
    translate.init();
});

@@ -637,25 +637,24 @@
 this) %7B%0A
-%0A
         
 if(typeo
@@ -649,172 +649,110 @@
     
-if(typeof tm_googleApiKey == 'undefined') // default bahavior - copy original text to translation field%0A        %7B%0A            if ($.trim($this.closest('tr').find('.
+var $translation = $this.closest('tr').find('.translation'),%0A            isEmptyTranslation = $.trim($
 tran
@@ -750,34 +750,32 @@
 rim($translation
-')
 .val()).length =
@@ -774,32 +774,26 @@
 length === 0
-) %7B%0A    
+,%0A
             
@@ -796,53 +796,24 @@
     
-$this.closest('tr').find('.translation').val(
+sourceMessage = 
 $.tr
@@ -819,34 +819,34 @@
 rim($this.val())
-)
 ;
+%0A
 %0A            %7D%0A%0A
@@ -842,176 +842,277 @@
     
-    %7D%0A%0A            _translateLanguage($this.closest('tr').find('button'));%0A 
+if (!isEmptyTranslation) %7B%0A            return;%0A        %7D%0A%0A        if (typeof tm_googleApiKey === 'undefined') %7B%0A
        
-%7D%0A
      
-   else  // google translation is enabled - translate and copy translation ...
+// default bahavior - copy original text to translation field%0A            $translation.val(sourceMessage);%0A            _translateLanguage($this);
 %0A   
@@ -1116,16 +1116,23 @@
         
+%7D else 
 %7B%0A      
@@ -1141,87 +1141,74 @@
     
-if ($.trim($this.closest('tr').find('.translation').val()).length === 0) %7B%0A    
+// google translation is enabled - translate and copy translation%0A
     
@@ -1316,28 +1316,24 @@
             
-    
 if (result.s
@@ -1366,39 +1366,9 @@
     
-    $this.closest('tr').find('.
+$
 tran
@@ -1370,26 +1370,24 @@
 $translation
-')
 .val(result.
@@ -1405,28 +1405,24 @@
             
-    
 %7D else %7B%0A   
@@ -1414,32 +1414,81 @@
    %7D else %7B%0A    
+                $translation.val(sourceMessage);%0A
                 
@@ -1593,103 +1593,8 @@
 ));%0A
-                        $this.closest('tr').find('.translation').val($.trim($this.val()));%0A    
     
@@ -1616,36 +1616,32 @@
                 
- 
-   
 _translateLangua
@@ -1652,44 +1652,11 @@
 this
-.closest('tr').find('button'));%0A    
+);%0A
     
@@ -1667,30 +1667,16 @@
     %7D);%0A
-            %7D%0A
         

Refactor message copying to have less code duplication