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

function createCookie(name,value,days,path) {
  if (!path) { path = "/"; }
  var expires = "";
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    expires = "; expires="+date.toGMTString();
  }
  document.cookie = name+"="+value+expires+"; domain=sja.is; path="+path;
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}


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('', 'Stærra letur', 'Breyta litum á bakgrunn og texta', 'Larger font', 'Change background and text colours');
  var i, set;
  set = 0;
}

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)', 'Small screen (800 pixels)', 'Larger screen (1280 pixels)');
}

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("sja_style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title,"fontsize");
  cookie = readCookie("sja_wstyle");
  title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title,"width");
  ie5widthfix(title);
}

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

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




