﻿(function($) {
    $.fn.extend({
        trans: function(options) {
            $.translate(function() {  //when the Google Language API is loaded
                function translateTo(destLang) { //this can be declared in the global scope too if you need it somewhere else
                    $('body').translate('english', destLang, {   //translate from english to the selected language
                        not: '#divLang, .jq-translate-ui',  //by default the generated element has this className
                        fromOriginal: true,   //always translate from english (even after the page has been translated)
                        start: function() { $('#throbber').show() },
                        complete: function() { $('#throbber').hide() },
                        error: function() { $('#throbber').hide() }
                    });
                }
                $.translate().ui('select', 'option')
                     .appendTo('#divLang')    //insert the element to the page
                     .change(function() {   //when selecting another language
                         translateTo($(this).val());
                         $.cookie('destLang', $(this).val());
                         // set a cookie to remember the selected language
                         return false; //prevent default browser action
                     })
                var destLang = $.cookie('destLang'); //get previously translated language
                if (destLang) {  //if it was set then
                    translateTo(destLang);
                    $('.jq-translate-ui').val(destLang);
                }
                else {
                    $('.jq-translate-ui').val("English");
                }
            }); //end of Google Language API loaded
        }
    });
})(jQuery)

