// spanishorse.js
//
// This file is used for setting and getting the language

// expire = seconds from now, it can be omitted (in that case the cookie will expire at the end of the session)
function setCookie(name, value, expire)
{
    var expire_date, text;
    if (expire != null) {
        expire_date = new Date();
        expire_date.setTime(expire_date.getTime() + expire * 1000); // The * 1000 is to get the milliseconds
    }
    text = name + "=" + escape(value) + ((expire == null) ? "" : ("; expires=" + expire_date.toGMTString()));
    document.cookie = text;
}
// Reads the cookie named 'name' and returns its value or null
function getCookie (name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
        return getCookieFromOffset(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return null;
}

// This is used internally by getCookie
function getCookieFromOffset (offset) {
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1) {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
}

// Sets a variable inside the flash movie
function setFlashVar(variable, value) {
   window.document.movie.SetVariable(variable, value);
}

// Sets a cookie and redirects the browser to the html file in the chosen language
function setLanguage(language) {
    if (language == 'en') language = 'es';
    setCookie('idioma_elegido', language, 86400 * 365); // Expires in 1 year from now
    window.location = 'index_' + language + '.html';
}

// Here we do all the initialization stuff, at the moment we just
// we just check if a cookie named saltar_introduccion is set, and
// we pass it to flash. If it's not set, then we set it.
function sendVariablesToFlash() {
    var value = getCookie('SaltarIntro');
    if (value) {
        setFlashVar('SaltarIntro', '1');
    } else {
        setCookie('SaltarIntro', '1');
        setFlashVar('SaltarIntro', '0');
    }
}
