// Quick Chat 3.50 - translate
var quick_chat_languages = new Array();
if(jQuery.cookie('quick_chat_dest_lang'))
    var quick_chat_dest_lang = jQuery.cookie('quick_chat_dest_lang');
else
    quick_chat_dest_lang = quick_chat_js_vars.default_langugage_code;

function quick_chat_language_menu(){
    jQuery.ajax({
        url: 'http://api.microsofttranslator.com/V2/Ajax.svc/GetLanguagesForTranslate',
        data: {'appid': quick_chat_js_vars.bing_appid},
        dataType: 'jsonp',
        jsonp: 'oncomplete',
        success: function(language_codes, status) {
            var language_codes_string = jQuery.toJSON(language_codes);
            for (var i = 0; i < language_codes.length; i++){
                var single_language = new Array();
                single_language['language_code'] = language_codes[i];
                quick_chat_languages[i] = single_language;
            }

            jQuery.ajax({
                url: 'http://api.microsofttranslator.com/V2/Ajax.svc/GetLanguageNames',
                data: {'appid': quick_chat_js_vars.bing_appid, locale: 'en', languageCodes: language_codes_string},
                dataType: 'jsonp',
                jsonp: 'oncomplete',
                success: function(language_names, status) {
                    for (var j = 0; j < language_names.length; j++)
                        quick_chat_languages[j]['language_name'] = language_names[j];

                        var select_string = '<select class="quick-chat-lang-select">';
                        for(var i = 0; i < quick_chat_languages.length; i++)
                            select_string += '<option value="' + quick_chat_languages[i]['language_code'] + '">' + quick_chat_languages[i]['language_name'] + '</option>';
                        select_string += '</select>';

                        jQuery('span.quick-chat-language-container').append(select_string);

                        jQuery('select.quick-chat-lang-select').change(function(){
                            quick_chat_dest_lang = jQuery(this).val();
                            jQuery('select.quick-chat-lang-select').val(quick_chat_dest_lang);
                            jQuery.cookie('quick_chat_dest_lang', quick_chat_dest_lang, {path: quick_chat_js_vars.cookiepath, domain: quick_chat_js_vars.cookie_domain});
                        }).val(quick_chat_dest_lang);
                }
            });
        }
    });
};

jQuery.quick_chat_translate = function(BingAppId, textToTranslate, fromLanguage, toLanguage,
                                callbackFunctions)
{
    var data =
    {
        appid   : BingAppId,
        to      : toLanguage,
        from    : fromLanguage,
        text    : textToTranslate
    };

    var defaultCallbacks =
    {
        success         : function(data, status)
                            {
                            },
        complete        : function(request, status)
                            {
                            },
        error           : function(request, status, error)
                            {
                            },
        jsonpCallback   : 'ajaxTranslateCallback'
    }
    callbackFunctions = jQuery.extend({}, defaultCallbacks, callbackFunctions);

    jQuery.ajax({
            url             : 'http://api.microsofttranslator.com/V2/Ajax.svc/Translate',
            data            : data,
            dataType        : 'jsonp',
            jsonp           : 'oncomplete',
            jsonpCallback   : callbackFunctions.jsonpCallback,
            success         : callbackFunctions.success,
            complete        : callbackFunctions.complete,
            error           : callbackFunctions.error
    });
}

quick_chat_language_menu();

jQuery(".quick-chat-translate-link a").live('click', function(e) {
    e.preventDefault();
    var message_element = jQuery(this).parents('.quick-chat-history-message-alias-container').find('.quick-chat-history-message');
    var this_element = jQuery(this);

    jQuery(this).fadeTo(100, 0, function() {
        if(jQuery(this_element).css('text-decoration') == 'none'){
            var source_text = (jQuery(message_element).data('original_text', source_text) == undefined) ? jQuery(message_element).html() : jQuery(message_element).data('original_text', source_text);

            var translate_callback = {
                success: function(data, status){
                        jQuery(message_element).data('original_text', source_text);
                        jQuery(message_element).html(data);
                    }
            }

            jQuery.quick_chat_translate(    quick_chat_js_vars.bing_appid,
                                            source_text,
                                            '',
                                            quick_chat_dest_lang,
                                            translate_callback);

            jQuery(this_element).css('text-decoration','line-through');
        } else if(jQuery(this_element).css('text-decoration') == 'line-through'){
            jQuery(message_element).html(jQuery(message_element).data('original_text'));
            jQuery(this_element).css('text-decoration','none');
        }
        jQuery(this_element).fadeTo(100, 1);
    });
});