
(function(){
  function browserLang(){
    var lang = (navigator.language || navigator.userLanguage || 'en').toLowerCase();
    return lang.indexOf('ro') === 0 ? 'ro' : 'en';
  }
  function targetPage(current, lang){
    if(current.indexOf('-ro.html') !== -1) return current.replace('-ro.html', '-' + lang + '.html');
    if(current.indexOf('-en.html') !== -1) return current.replace('-en.html', '-' + lang + '.html');
    return current;
  }
  var links = document.querySelectorAll('.lang-link');
  for(var i=0;i<links.length;i++){
    links[i].addEventListener('click', function(e){
      e.preventDefault();
      localStorage.setItem('legalLang', this.getAttribute('data-lang'));
      window.location.href = targetPage(window.location.pathname, this.getAttribute('data-lang'));
    });
  }
  if(!localStorage.getItem('legalLang')){
    localStorage.setItem('legalLang', browserLang());
  }
})();
