/**
* A very simple self made localization function.
* uses the imported i18n.js file and displays the lang strings.
*
*/
var addI18n = function(lang,data){
    $('*[data-i18n]').each(function(){
        var key = $(this).data('i18n').split('.');

        // stupid but works
        if(key.length === 2)
            $(this).append(i18n[lang][key[0]][key[1]]);
        if(key.length === 3)
            $(this).append(i18n[lang][key[0]][key[1]][key[2]]);
        if(key.length === 4)
            $(this).append(i18n[lang][key[0]][key[1]][key[2]][key[3]]);
    });
    $('*[data-i18n_title]').each(function(){
        var key = $(this).data('i18n_title').split('.');

        // stupid but works
        if(key.length === 2)
            $(this).prop ( 'title' , i18n[lang][key[0]][key[1]]);
        if(key.length === 3)
            $(this).prop ( 'title' , i18n[lang][key[0]][key[1]][key[2]]);
        if(key.length === 4)
            $(this).prop ( 'title' , i18n[lang][key[0]][key[1]][key[2]][key[3]]);
    });
};

var getI18n = function ( literal , data , lang )
{
    if ( typeof data == 'undefined' || data == null )
    {
        data = i18n;
    }
    if ( typeof lang == 'undefined' || lang == null )
    {
        lang =  ( navigator.language.substr ( 0 , 2 ) === 'es' ) ? 'es' : 'en';
    }
    var key = literal.split ( '.' );
    data = data[lang];
    if ( typeof data == 'undefined' )
    {
        return literal;
    }
    for ( var i = 0 ; i < key.length ; i++ )
    {
        if ( /^\d+$/.test ( key[i] ) )
        {
            continue;
        }
        data = data[key[i]];
        if ( typeof data == 'undefined' )
        {
            return literal;
        }
    }
    return typeof data == 'string' ? data : literal;
}