/**
 * i18n GLOBALS
 */
var i18n_GET_TRANSLATION_TABLE_ARO;
var i18n_VOTE_UP_ARO;
var i18n_VOTE_DOWN_ARO;
var i18n_REPORT_ARO;
var i18n_DELETE_ARO;
var i18n_APPROVE_ARO;

/**
 * create_ajax_request_object - creates XMLHttpRequest object
 *
 * Returns XMLHttpRequest object or false on failure
 */
function create_ajax_request_object()
{
    try {
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e) {
        // Internet Explorer browsers
        try {
            ajaxRequest = new ActiveXObject('Msxml2.XMLHTTP');
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject('Microsoft.XMLHTTP');
            } catch (e) {
                // Something went wrong
                return false;
            }
        }
    }

    return ajaxRequest;
}

/**
 * ms_zeit - returns present time in ms, helpful with caching issues
 */
function ms_zeit()
{
    var zeit = new Date();
    
    return (zeit.getHours() * 24 * 60 * 1000) + (zeit.getMinutes() * 60 * 1000) + (zeit.getSeconds() * 1000) + zeit.getMilliseconds();
}

/**
 * show_hide_block - shows/hides various block elements
 * @id_part:    the unique part of the id element to show hide
 *
 * This version works only on elements where action button consists of the textual show/hide words.
 */
function show_hide_block(id_part) 
{
    var hide_id = 'trnsl_other_translations_container_' + id_part;
    var action_field = 'show_hide_action_' + id_part;

    var action_words = ['Show', 'Hide'];
    var action_state = document.getElementById(action_field).innerHTML;

    if (action_state == action_words[0]) {
        // Show element
        document.getElementById(hide_id).style.display = 'block';
        // Update wording
        document.getElementById(action_field).innerHTML = action_words[1];
    } else if (action_state == action_words[1]) {
        // Hide element
        document.getElementById(hide_id).style.display = 'none';
        // Update wording
        document.getElementById(action_field).innerHTML = action_words[0];
    }
}

/**
 * i18n_init - brings the translation screen up
 */
function i18n_init() 
{
    // Init
    var language = document.forms['i18n_form'].elements['language'].value;
    if (language == '') {
        document.getElementById('i18n_translation_area').style.display = 'none';
        return false;
    }

    document.getElementById('i18n_translation_table').innerHTML = 'Loading...';
    document.getElementById('i18n_translation_area').style.display = 'block';
    var language_pretty = language.replace('_', ' ');
    document.getElementById('i18n_header').innerHTML = 'Translate into ' + language_pretty + ':';

    // Fetch the translation table for the chosen language
    i18n_GET_TRANSLATION_TABLE_ARO = create_ajax_request_object();

    i18n_GET_TRANSLATION_TABLE_ARO.onreadystatechange = function()
    {
        if (i18n_GET_TRANSLATION_TABLE_ARO.readyState == 4) {
            document.getElementById('i18n_translation_table').innerHTML = i18n_GET_TRANSLATION_TABLE_ARO.responseText;
        }
    }

    i18n_GET_TRANSLATION_TABLE_ARO.open('GET', '/get_translation_table_submit.html?language=' + language + '&cache=' + ms_zeit(), true);
    i18n_GET_TRANSLATION_TABLE_ARO.send(null);
}

/**
 * i18n_translate - sends user translation to the processing script
 * @string_id:  the url encoded string_id
 * @language:   the url encoded language the string is being stranslated into
 * @original_text:  the url encoded text user translated from
 * @translation:    the url encoded translated string
 */
function i18n_translate(string_id, language, original_text, translation)
{
    // Silently ingnore empty translations
    if (translation == '') { 
        return false;
    }

    // Update UI
    document.getElementById('translate_action_container_' + string_id).innerHTML = '<span class="translated_tick">&#x2713;</span>';
    document.forms['i18n_form'].elements[decodeURIComponent(string_id)].disabled = true;
    
    // Pass data to the processing script
    i18n_PROCESS_TRANSLATION_ARO = create_ajax_request_object();
    i18n_PROCESS_TRANSLATION_ARO.open('GET', '/process_translation_submit.html?string_id=' + string_id + '&language=' + language + '&original_text=' + original_text + '&translation=' + translation + '&cache=' + ms_zeit(), true);
    i18n_PROCESS_TRANSLATION_ARO.send(null);
}

/**
 * i18n_vote_up - bumps vote score (+1)
 * @uuid:   the url encoded translation uuid
 */
function i18n_vote_up(uuid)
{
    i18n_VOTE_UP_ARO = create_ajax_request_object();
    i18n_VOTE_UP_ARO.open('GET', '/vote_up_submit.html?uuid=' + uuid + '&cache=' + ms_zeit(), true);
    i18n_VOTE_UP_ARO.send(null);
}

/**
 * i18n_vote_down - decreases vote score (-1)
 * @uuid:   the url encoded translation uuid
 */
function i18n_vote_down(uuid)
{
    i18n_VOTE_DOWN_ARO = create_ajax_request_object();
    i18n_VOTE_DOWN_ARO.open('GET', '/vote_down_submit.html?uuid=' + uuid + '&cache=' + ms_zeit(), true);
    i18n_VOTE_DOWN_ARO.send(null);
}

/**
 * i18n_report - bumps report score (+1)
 * @uuid:   the url encoded translation uuid
 */
function i18n_report(uuid)
{
    i18n_REPORT_ARO = create_ajax_request_object();
    i18n_REPORT_ARO.open('GET', '/report_submit.html?uuid=' + uuid + '&cache=' + ms_zeit(), true);
    i18n_REPORT_ARO.send(null);
}

/**
 * i18n_delete - sets status flag to 'd' for the selected entry
 * @uuid:   the url encoded translation uuid
 */
function i18n_delete(uuid)
{
    i18n_DELETE_ARO = create_ajax_request_object();
    i18n_DELETE_ARO.open('GET', '/admin/delete_submit.html?uuid=' + uuid + '&cache=' + ms_zeit(), true);
    i18n_DELETE_ARO.send(null);
}

/**
 * i18n_approve - sets approved flag on the selected entry
 * @uuid:   the url encoded translation uuid
 */
function i18n_approve(uuid)
{
    i18n_APPROVE_ARO = create_ajax_request_object();
    i18n_APPROVE_ARO.open('GET', '/admin/approve_submit.html?uuid=' + uuid + '&cache=' + ms_zeit(), true);
    i18n_APPROVE_ARO.send(null);
}

