

/* ---  Copyright (c) 2006 Hugsmiðjan ehf. - www.eplica.is  --- */

// Stillingar.is : "Brute-force" style switcher.

if (!window.stillingarIs) { stillingarIs = { _:{} }; }
else if (!stillingarIs._) { stillingarIs._ = {}; }
stillingarIs._.setCookie = function(_name, _value, _path, _domain, _expires, _secure)
{
  if (!_expires || !_expires.getTime) // defaults to 1 year
  {
    _expires = new Date();
    _expires.setTime(_expires.getTime() + 365 * 24 * 60 * 60 * 1000);
  }
  var _curCookie = _name + "=" + escape(_value) + ((_expires) ? "; expires=" + _expires.toGMTString() : "") + ((_path) ? "; path=" + _path : "/") + ((_domain) ? "; domain=" + _domain.replace(/^www\d*\./, "") : "") + ((_secure) ? "; secure" : "");
  document.cookie = _curCookie;
};
stillingarIs._.getCookieValue = function(_theName)
{
  var c = document.cookie;
  if (c && _theName)
  {
    c = ";" + c;
    var _regex = new RegExp("(;|; )" + _theName + "=([^;]+)");
    if (c.match(_regex)) { return unescape( c.match(_regex)[2] ); }
  }
  return false;
};
stillingarIs._.getLang = function(_element)
{
  var e = _element || document.getElementsByTagName("html")[0];
  var _foundLang = null;
  var _extractLang = function(_element)
  {
    var _elmLang = _element.lang;  // use @lang by default
    if (!_elmLang) { try { _elmLang = _element.getAttribute("xml:lang"); } catch(err) {  } } // but fall back to @xml:lang using try{}catch{} because IE5&6 crash when searching for non-existing "xml:lang" attributes on <table> elements (go figure!).
    if (!_elmLang && _element.getAttributeNS) { _elmLang = _element.getAttributeNS("xml","lang"); }  // fall back to use .getAttributeNS() for Opera's sake
    // return _elmLang && (_elmLang.length >= 2) && _elmLang.substr(0,2);  //make sure the we return a language code that is at least two character long
    return _elmLang && _elmLang.toLowerCase();
  };
  while ( !(_foundLang = _extractLang(e)) )
  {
    if (e.tagName == "HTML") { break; }
    e = e.parentNode;
  }
  return _foundLang;
};

(function(){

  var t = stillingarIs._;
  if (t.userStyleElm) { return; }


  var texts = {
    "dk" : {
      onBtn       : "Benyt mine indstillinger",
      onBtnTitle  : "Skift til mine tekst- og farveindstillinger",
      menu2label  : "Sidens visning:",
      offBtn      : "Normal visning",
      offBtnTitle : "Skift til denne hjemmesides normale visning",
      cfgBtn      : "Redigér indstillinger",
      cfgBtnTitle : "Ændre mine tekst- og farveindstillinger",
    },
    "en" : {
      onBtn       : "Use my settings",
      onBtnTitle  : "Switch to my text and color settings",
      menu2label  : "Page style:",
      offBtn      : "Normal style",
      offBtnTitle : "Switch to the normal style of this website",
      cfgBtn      : "Edit settings",
      cfgBtnTitle : "Change my text and color settings",
    },
    "en-gb" : {
      onBtnTitle  : "Switch to my text and colour settings",
      cfgBtnTitle : "Change my text and colour settings",
    },
    "is" : {
      onBtn       : "Nota mitt útlit",
      onBtnTitle  : "Nota mínar lita- og leturstillingar",
      menu2label  : "Útlit síðu:",
      offBtn      : "Venjulegt útlit",
      offBtnTitle : "Skipta yfir í venjulegt útlit vefsins",
      cfgBtn      : "Breyta stillingum",
      cfgBtnTitle : "Breyta mínum lita- og leturstillingum",
    },
  }
  var l = t.getLang();
  if (!l || !texts[l]) {
    var baseLang = (l||"").substr(0,2);
    l = texts[baseLang] ? baseLang : "en";
  }
  stillingarIs._.lang = l;

  t.txt = texts[l];
  if (l.length > 2) {
    var textBase = texts[l.substr(0,2)];
    text[l] = Object.assign ? Object.assign({}, textBase, texts[l]) : textBase;
  }

  var baseUrl = "https://minar.stillingar.is";
  var h = document.getElementsByTagName("head")[0];
  var b = document.body;

  var findStylesheetElms = function()
  {
    t.normalStyleElms = [];
    var c = h.childNodes;
    for (var i = 0, e; (e = c[i]); i++)
    {
      if ( (e.tagName == "STYLE") || (e.tagName == "LINK" && e.rel && (/\bstylesheet\b/i).test(e.rel) ) )
      {
        t.normalStyleElms[t.normalStyleElms.length] = e;
      }
    }
  };

  t.disableStylesheets = function()
  {
    if (t.doWrapVowels)
    {
      var v = document.createElement("script");
      v.src = baseUrl+"/lesa/stilar/js/vowels/";
      h.appendChild(v);
    }
    var i, e;
    findStylesheetElms();
    h.appendChild(t.userStyleElm);
    h.appendChild(t.userStyleElm2);
    b.insertBefore(t.ctrlElm, b.childNodes[0]);
    if (t.onButtonCont) { t.onButtonCont.parentNode.removeChild(t.onButtonCont); }
    for (i = 0; (e = t.normalStyleElms[i]); i++)
    {
      h.removeChild(e);
    }
    t.setCookie("userstyles", "on", "/");
    return false;
  };

  t.redoStylesheets = function()
  {
    t.setCookie("userstyles", "off", "/");
    document.location = document.location.href.replace(/#.*$/,"").replace(/([&?])userstyles=on(\&)?/, "$1").replace(/[&?]$/, "");
    return false;
  };

  var s = document.createElement("link");
  s.rel = "stylesheet";
  s.href = baseUrl+"/lesa/stilar/css/";
  s.type = "text/css";
  s.media = "screen";
  t.userStyleElm = s;

  var s2 = s.cloneNode(-1);
  s2.href = baseUrl+"/files/toolbar.css";
  t.userStyleElm2 = s2;

  var ch = document.createElement("strong");
  ch.innerHTML = t.txt.menu2label;

  var a = document.createElement("a");
  a.className = "off";
  a.href = "#";
  a.innerHTML = t.txt.offBtn;
  a.title = t.txt.offBtnTitle;
  a.onclick = t.redoStylesheets;

  var a2 = document.createElement("a");
  a2.className = "settings";
  a2.href = baseUrl+"/lesa/form/?l="+l+"&redirect=yes";
  a2.innerHTML = t.txt.cfgBtn;
  a2.title = t.txt.cfgBtnTitle;

  var c = document.createElement("div");
  c.className = "stillingaris_bar";
  c.align = "right";
  c.appendChild( document.createTextNode(" ") );
  c.appendChild(a);
  c.appendChild( document.createTextNode(" ") );
  c.appendChild(a2);
  t.ctrlElm = c;

})();


(function(){
  var t = stillingarIs._;
  if (t.onButtonCont) { return; }

  var buttonScriptTag = (
      document.querySelector('script[data-stillingar-button]') ||
      document.querySelector('script[src*="//minar.stillingar.is/lesa/stilar/js/button"]')
  );
  if (!buttonScriptTag) { return }


  t.doWrapVowels = buttonScriptTag.dataset.stillingarVowels !== 'false';


  var _buttonFiles = {
    "1"  : "01-w-{lang}.png",
    "2"  : "01-g-{lang}.png",
    "3"  : "01-b-{lang}.png",
    "4"  : "02-w-{lang}.png",
    "5"  : "02-g-{lang}.png",
    "6"  : "02-b-{lang}.png",
    "7"  : "b-32.png",
    "8"  : "b-24.png",
    "9"  : "b-20.png",
    "10" : "b-16.png",
    "11" : "g-32.png",
    "12" : "g-24.png",
    "13" : "g-20.png",
    "14" : "g-16.png",
  };

  var buttonFile = _buttonFiles["8"] || _buttonFiles["1"];


  var s = document.createElement("span");
  s.id = "stillingarIs_turnUserstylesOnButton";

  var a = document.createElement("a");
  //  a.href = "https://minar.stillingar.is/lesa/virkja/" + ((t.lang != "is") ? "?l="+t.lang : "");
  a.href = "#";
  a.onclick = t.disableStylesheets;
  a.title = t.txt.onBtn;
  a.setAttribute("role", "button");
  a.setAttribute("aria-label", t.txt.onBtn)
  s.appendChild(a);

  var b = document.createElement("img");
  b.src = "https://minar.stillingar.is/files/btn/" + buttonFile.replace(/{lang}/, t.lang);
  b.border = "0";
  b.align  = "top";
  b.alt    = t.txt.onBtn;
  a.appendChild(b);

  buttonScriptTag.insertAdjacentElement("afterend", s);
  t.onButtonCont = s;

  if (/(\?|&)userstyles=on(&|$)/.test(document.location.search))
  {
    t.setCookie("userstyles", "on", "/");
  }
  if (t.getCookieValue("userstyles") == "on")
  {
    t.disableStylesheets();
  }

})();

