// Javascript originally by Paul Snowden and Peter-Paul Koch.
// See http://alistapart.com/articles/alternate/.
// Modifications by Galdur to support two sets of alternate stylesheets

function setActiveStyleSheet(title,type) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && isStyleSheetType(a.getAttribute("title"),type)) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
  setStyleSheetImages(title,type);
}

function setStyleSheetImages (title,type) {
  if (type == "width") { setStyleSheetWidthImages(title) }
  else                 { setStyleSheetFontImages(title) }
}

function setStyleSheetFontImages (title) {
  if (!(document.createElement && document.getElementsByTagName)) {
	return;
  }
  var elem = document.getElementById("leturblokk");
  if (!elem) { return; }
  var imgs = elem.getElementsByTagName('img');
  if (!imgs[0]) { return; }
  var names = new Array('Lítið', '', 'Stórt');
  var i, set;
  set = 0;
  for (i=0; i<3 ;i++) {
    var x = imgs[i];
    if (x) {
       if (names[i] == title) {
         x.src = '/letur/letur_'+(i+1)+'b.gif';
         set = 1;
       } else {
         x.src = '/letur/letur_'+(i+1)+'.gif'; 
       }
    }
  }
  if (set == 0) {
    imgs[0].src = '/letur/letur_2b.gif';
  }
}

function setStyleSheetWidthImages (title) {
  if (!(document.createElement && document.getElementsByTagName)) {
	return;
  }
  var elem = document.getElementById("breiddarblokk");
  if (!elem) { return; }
  var imgs = elem.getElementsByTagName('img');
  if (!imgs[0]) { return; }
  var names = new Array('Minni skjábreidd (800 dílar)', '', 'Meiri skjábreidd (1280 dílar)');
  var i, set;
  set = 0;
  for (i=0; i<3 ;i++) {
    var x = imgs[i];
    if (x) {
       if (names[i] == title) {
         x.src = '/img/utlit/breidd/breidd_'+(i+1)+'b.gif';
         set = 1;
       } else {
         x.src = '/img/utlit/breidd/breidd_'+(i+1)+'.gif'; 
       }
    }
  }
  if (set == 0) {
    imgs[1].src = '/img/utlit/breidd/breidd_2b.gif';
  }
}

function mozRepaintFix () { // Fixes Gecko problem with position of top ad
  if (navigator.userAgent.search(/Gecko/) != -1) { window.resizeBy(-1,-1) && window.resizeBy(1,1); }
}

function isStyleSheetType(str,type) {
  if      (str == "")                             { return true; }
  else if (type == "width" && str.match(/\d/))    { return true; }
  else if (type != "width" && str.match(/^\D*$/)) { return true; }
  return false;
}

function getActiveStyleSheet(type) {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && isStyleSheetType(a.getAttribute("title"),type) && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

// This function is redefined in ie5win-fix.js for IE 5.x on Windows.
// As defined below it has no effect except in Mac IE 5.x.
function ie5widthfix (title) {
  if (navigator.appVersion.indexOf('Mac') != -1 && document.all) {
    var ih = new getObj("innihald");
	if (ih && ih.style) {
      if (title.match(/800/)) {
        ih.style.width = "501px";
      } else if (title.match(/1280/)) {
        ih.style.width = "801px";
      } else {
        ih.style.width = "551px";
      }
    }
  }
  return true; 
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title,"fontsize");
  cookie = readCookie("wstyle");
  title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title,"width");
  ie5widthfix(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet("fontsize");
  createCookie("style", title, 365);
  title = getActiveStyleSheet("width");
  createCookie("wstyle", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title,"fontsize");
cookie = readCookie("wstyle");
title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title,"width");
ie5widthfix(title);