

/* ---  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;
};

/* MSIE sniffing */
stillingarIs._.MSIE/*@cc_on ={ v : parseFloat(navigator.appVersion.match(/MSIE ([^;]+)/)[1]) }@*/;

/* X(HT)ML-friendly document-write clone */
stillingarIs._.injectElm = function(e)
{
  var t = document.all || document.getElementsByTagName('*');
  var p = t[t.length-1].parentNode;
  if (p.tagName == "SCRIPT") { p = p.parentNode; }  // added because Opera was acting up for some reason, claiming t[t.length-1].nodeType == 3
  p.appendChild(e);
};


(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 _elms = document.all || document.getElementsByTagName('*');
  var l = t.getLang(_elms[_elms.length-1].parentNode);
  if (!l || !texts[l])
  {  
    l = (l && texts[l.substr(0,2)]) ? l.substr(0,2) : "en";
  }
  stillingarIs._.lang = l;

  t.txt = texts[l];
  if (l == "en-gb") Object.merge(texts["en-gb"], texts.en, true)

  var u = "http"+((document.location.href.toLowerCase().indexOf("https://")===0)?'s':'')+"://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 = u+"/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 = u+"/lesa/stilar/css/";
  s.type = "text/css";
  s.media = "screen";
  t.userStyleElm = s;

  var s2 = s.cloneNode(-1);
  s2.href = u+"/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 = u+"/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; }

  t.doWrapVowels = true

  var buttonContId = "stillingarIs_turnUserstylesOnButton"; // + parseInt(Math.random()*100000, 10);
  var protocol = "http" + ((document.location.href.toLowerCase().indexOf("https")===0)?'s':'');

  var _buttonFiles = {
    "1"  : "01-w-is.png",
    "2"  : "01-g-is.png",
    "3"  : "01-b-is.png",
    "4"  : "02-w-is.png",
    "5"  : "02-g-is.png",
    "6"  : "02-b-is.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 bid = "9";
  if (!_buttonFiles[bid]) { bid = "1"; }

  var s = document.createElement("span");
  s.id = buttonContId;
  var a = document.createElement("a");
//  a.href = protocol+"://minar.stillingar.is/lesa/virkja/" + ((t.lang != "is") ? "?l="+t.lang : "");
  a.href = "#";
  a.onclick = t.disableStylesheets;
  a.title = t.txt.onBtn;
  s.appendChild(a);

  var b = document.createElement("img");
  b.src = protocol+"://minar.stillingar.is/files/btn/" + _buttonFiles[bid].replace(/(-[a-z]{2}){1,2}(\.[^.]+)$/, "-"+t.lang+"$2");

  if (t.MSIE && t.MSIE.v < 7 && t.MSIE.v >= 5.5) // MSIE 5.5-6.0 needs massaging to make transparent png work
  {
    var as = a.style;
    as.width    = b.width+"px";
    as.height   = b.height+"px";
    as.overflow = "hidden";
    as.display  = "inline-block";
    as.cursor   = "hand";
    as.filter   = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+b.src+"',sizingMethod='scale')";
    a.innerHTML = '<span style="_padding:0;_display:block;_overflow:hidden;_border:none;_line-height:0;_text-indent:-9999em;_font-size:1px;_letter-spacing:-1em;">'+t.txt.onBtn+'<'+'/span>';
  }
  else // all other browsers
  {
    b.border = "0";
    b.align  = "top";
    b.alt    = t.txt.onBtn;
    a.appendChild(b);
  }
  t.injectElm(s);
  t.onButtonCont = s;

  if (/userstyles=on(\&|$)/.test(document.location.href))
  {
    t.setCookie("userstyles", "on", "/");
  }
  if (t.getCookieValue("userstyles") == "on")
  {
    t.disableStylesheets();
  }

})();


