﻿// ===================================================================
// GoogleTranslation2.js - Google AJAX Translation functions
// ===================================================================
var itemId = document.getElementById("ItemIdLabel").innerHTML;

// Translates item title and description, and inserts Google brand
function translate() {

    document.getElementById("googleBrand").innerHTML = '';
    google.language.getBranding("googleBrand");
    var translated = document.getElementById("googleTranslationResultLabel");
    google.language.translate(GetTextToTranslate(), translateFromLanguage, translateToLanguage, function(result) {
        if (!result.error) {
            if (result.translation) {
                translated.innerHTML = result.translation;
            }
        }
        else {
            translated.innerHTML = "An error in the Google Translation has occured. Please try again.";
        }
    });    
}

// Determine the start up view of specific item based on the users previous visit (cookies)
function startTranslate() {
    if (HasCookie() == "true") {    
        enableTranslation();
    }
    else {
        disableTranslation();
    }
}
        
// Enables display of Google translation.
function enableTranslation() {
    document.getElementById("GoogleTransDiv").style.display = "block";
    document.getElementById("showTranslationDiv").style.display = "none";
    document.getElementById("hideTranslationDiv").style.display = "block";
    SetCookie("true");

    // Call google translation API v2
    var newScript = document.createElement('script');
    newScript.type = 'text/javascript'; 
    var source = 'https://www.googleapis.com/language/translate/v2?key=AIzaSyCf2PqFPi9Fsxkfq_EU2nUgcUn-ccC-J34&source=' + translateFromLanguage + '&target=' + translateToLanguage + '&callback=translateText&q=' + GetTextToTranslate();
    newScript.src = source;
    document.getElementsByTagName('head')[0].appendChild(newScript);
}

// Disable display of Google translation.
function disableTranslation() {
    document.getElementById("GoogleTransDiv").style.display = "none";
    document.getElementById("showTranslationDiv").style.display = "block";
    document.getElementById("hideTranslationDiv").style.display = "none";
    SetCookie("false");
}

// Writes a cookie to the users browser with Google translation choices.
function SetCookie(valueBoolean) {
    var expiredate = new Date();
    expiredate.setDate(expiredate.getDate() + 1);
    itemId = itemId.replace(/<\/?[^>]+(>|$)/g, ""); //Removes any html tags injected by iPhone browsers
    document.cookie = "GoogleTrans" + itemId + "=" + valueBoolean + ";expires=" + expiredate.toGMTString();
}

// Checks if a specific item has a Google translation cookie.
function HasCookie() {
    if (document.cookie.indexOf("GoogleTrans" + itemId) != -1) {
        cookiestart = document.cookie.indexOf("GoogleTrans" + itemId) + ("GoogleTrans" + itemId).length + 1;
        cookieend = document.cookie.indexOf(';', cookiestart - 1);
        return (document.cookie.substring(cookiestart, cookieend));
    } else { return "false"; }
}

// Retrieve title and description of item in original language for translation.
function GetTextToTranslate(){
    var text
    text = "<b>"
    text = text + document.getElementById('ItemTitleLabel').innerHTML;
    text = text + "</b><br/>";
    text = text + document.getElementById('ItemDescription').innerHTML;
return text;
}

// Callback to handle the response from google API v2
function translateText(response) {
    var translated = document.getElementById("googleTranslationResultLabel");

    if (!response.error) {
        if (response.data.translations) {
            translated.innerHTML += response.data.translations[0].translatedText; ;
        }
    }
    else {
        translated.innerHTML = "An error in the Google Translation has occured. Please try again.";
    }
}
