// DHTML dropbox menu script  ** version 1.1 **

function dropMenu()
{
  if (!document.getElementById) return true;
  var theMenu = this.parentNode.parentNode;

  for (var i = 0; i < theMenu.childNodes.length; i++) 
  {
    var elm = theMenu.childNodes[i];
    if ((elm.className != null) && 
        (elm.className.indexOf("dmblock") > -1)
       )
    {
      elm.style.visibility = (elm.style.visibility == "hidden") ? "visible" : "hidden";
    }
  }
  return false;
}

function closeDropMenus()
{
  if (!document.getElementsByTagName) return true;

  var dmHeads1 = document.getElementsBySelector("div.dropmenu h2.boxhd");
  var dmBlocks1 = document.getElementsBySelector("div.dropmenu div.content");
  var dmHeads2 = document.getElementsBySelector("div.dropmenu h3.label");
  var dmBlocks2 = document.getElementsBySelector("div.dropmenu ul.block");
  var dropMenuHeads = mergeArrays(dmHeads1, dmHeads2);
  var dropMenuBlocks = mergeArrays(dmBlocks1, dmBlocks2);

  for (var i = 0; i < dropMenuHeads.length; i++)
  {
    var theLink = document.createElement("A");
    var theText = document.createTextNode( dropMenuHeads[i].childNodes[0].nodeValue );
    theLink.appendChild( theText );
    theLink.setAttribute( "href", "")
    theLink.onclick = dropMenu;
    dropMenuHeads[i].replaceChild( theLink, dropMenuHeads[i].childNodes[0] );
  }
  for (var i = 0; i < dropMenuBlocks.length; i++) 
  { 
    dropMenuBlocks[i].parentNode.className = dropMenuBlocks[i].parentNode.className + " dm-active";
    dropMenuBlocks[i].className = dropMenuBlocks[i].className + " dmblock";
    dropMenuBlocks[i].style.visibility = "hidden";
  }
  return true;
}



// DHTML nested menu script  ** version 1.0 **
function  openDhtmlSubMenu() { return toggleDhtmlSubMenu(this, 1); }
function closeDhtmlSubMenu() { return toggleDhtmlSubMenu(this, 0); }

function toggleDhtmlSubMenu(lItem, open)
{
  var nodes = lItem.childNodes;
  var myLevel = "." + lItem.parentNode.className.match(/level\d/);
  var siblings = document.getElementsBySelector("div.dhtmlmenu " + myLevel + " ul");
  for (var i = 0; i < siblings.length; i++) { siblings[i].className = siblings[i].className.replace(/ open/, " closed"); }
  for (var i = 1; i < nodes.length; i++)
  {
    if (nodes[i].tagName == "UL")
    {
      nodes[i].className = (open == 1) ? nodes[i].className.replace(/ closed/, " open") :  nodes[i].className.replace(/ open/, " closed");
      return true;
    }
  }
  return true;
}

function closeDhtmlMenus()
{
  if (!document.getElementsByTagName) return true;
  var subMenus = document.getElementsBySelector("div.dhtmlmenu .level1 ul");
  for (var i = 0; i < subMenus.length; i++)
  {
    subMenus[i].className += " closed";
    subMenus[i].parentNode.onmouseover = openDhtmlSubMenu;
    subMenus[i].parentNode.onmouseout = closeDhtmlSubMenu;

  }
  return true;
}




// Gereric Popup window script ** version 1.2 **

var popupLinkConfig = new Array;
popupLinkConfig["popup"] = new Array ( "", "width=350,height=450");
popupLinkConfig["glossary"] = new Array ( "", "width=550,height=350");

function initPopupLinks()
{
  if (!document.getElementsByTagName) return true;
  for (var theKey in popupLinkConfig) 
  {
    var theLinks = document.getElementsBySelector("a."+theKey);
    for (var i = 0; i < theLinks.length; i++) 
    {
      theLinks[i].onclick = popUp;
    }
  }
  return true;
}

function popUp()
{
  for (var theKey in popupLinkConfig) 
  {
    if (this.className.indexOf(theKey) > -1)
    {
      var target = ((this.target != "") && (this.target != null)) ? this.target : (popupLinkConfig[theKey][0] != "") ? popupLinkConfig[theKey][0] : theKey;
      var settings = popupLinkConfig[theKey][1];
      newWin = window.open(this.href, target, settings);
      newWin.focus();
      return false;
    }
  }
  return false;
}



// Enable form reset buttons with confirmation step  ** version 1.1 **
// Markup for buttons: <input type="button" class="reset" value="Clear All">
function initResetButtons()
{
  if (!document.getElementsByTagName) return true;
  var confirmText = "Þú ert í þann mund að hreinsa öll innslegin gildi úr leitarreitunum...";
  var theForms = document.getElementsByTagName("form");
  for (var g = 0; g < theForms.length; g++) 
  {
    var theFields = theForms[g].getElementsByTagName("input");
    for (var i = 0; i < theFields.length; i++) 
    {
      if (((theFields[i].className != null) && (theFields[i].className.indexOf("reset") > -1)) ||
          ((theFields[i].type != null) && (theFields[i].type == "reset"))
         )
      {
        theFields[i].outerForm = theForms[g];
        theFields[i].onclick = function() { if (confirm(confirmText)) { this.outerForm.reset(); } else { return false; } }
      }
    }
  }
  return true;
}



// Enforce pagebody-is-taller-than-side-columns requirement  ** version 1.3 **
var isChanged = false;
function fixPagebodyHeight()
{
  if (document.layers == null)
  {
    var bd = document.getElementById('pagebody');
    var e1 = document.getElementById('pageextra1');
    var e2 = document.getElementById('pageextra2');
    var e3 = document.getElementById('pageextra3');
    var e4 = document.getElementById('pageextra4');
    var hBody = bd.offsetHeight;
    var hExtra1 = (e1 != null) ? e1.offsetHeight : 0;
    var hExtra2 = (e2 != null) ? e2.offsetHeight : 0;
    var hExtra3 = (e3 != null) ? e3.offsetHeight : 0;
    var hExtra4 = (e4 != null) ? e4.offsetHeight : 0;
    if (hExtra1 > hBody) { hBody = hExtra1; isChanged = true; }
    if (hExtra2 > hBody) { hBody = hExtra2; isChanged = true; }
    if (hExtra3 > hBody) { hBody = hExtra3; isChanged = true; }
    if (hExtra4 > hBody) { hBody = hExtra4; isChanged = true; }
    if (isChanged == true) { bd.style.height = hBody + "px"; }
    if ((document.body.className == null) || (document.body.className == ""))
      { document.body.className = "heightfixed" }
    else
      { document.body.className = document.body.className + " heightfixed" }
  }
}


/*  ** version 1.1 **
 Browser sniffing code borrowed from: http://www.webreference.com/tools/browser/javascript.html
 The following bit of javascript code is Free Software[tm]. Licence: GNU/GPL v2.0 or later.
*/
var agt=navigator.userAgent.toLowerCase();
var appVer = navigator.appVersion.toLowerCase();

// *** BROWSER VERSION ***
var is_minor = parseFloat(appVer);
var is_major = parseInt(is_minor);

var iePos  = appVer.indexOf('msie');
if (iePos !=-1) {
  is_minor = parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos)))
  is_major = parseInt(is_minor);
}

var is_opera = agt.indexOf('opera') != -1;

var is_safari = ((agt.indexOf('safari')!=-1)&&(agt.indexOf('mac')!=-1))?true:false;
var is_konq = agt.indexOf('konqueror') != -1;
var is_khtml  = (is_safari || is_konq);

var is_ie   = ((iePos!=-1) && (!is_opera) && (!is_khtml));
var is_ie5dn = (is_ie && is_minor < 5.0);

// *** PLATFORM ***
var is_mac = (agt.indexOf("mac")!=-1);




// Disable all CSS in stupid browsers  ** version 1.01 **
function disableCSSinStupidBrowsers()
{
  if (is_ie5dn) 
  {
    var x = (document.getElementsByTagName) ? document.getElementsByTagName('link') : (document.all) ? document.all.tags('link') : null;
    if (x != null)
    {
      x[0].disabled = true;
      x[1].disabled = true;
    }
  }
}



// Enlarge/Shrink font-sizes script  ** version 1.1 **
var fontSizes = new Array ( 68.75, 75, 87.5, 100, 118.75, 143.75, 168.75 );
var defaultFontSize = 0;
var currentFontSize = getFontCookie();

function resizeFont(delta)
{
  var newSize = currentFontSize + delta;
  if ((newSize < 0) || (newSize >= fontSizes.length)) { newSize = currentFontSize; }
  setFontCookie(newSize);
  document.location.href = document.location.href;
  return;
}
/* Legacy compatibility functions */
function enlargeFont() { resizeFont(1); return; }
function shrinkFont() { resizeFont(-1); return; }


// Copyright (c) 1996-1997 Athenia Associates. http://www.webreference.com/js/
// License is granted if and only if this entire copyright notice is included. By Tomer Shiran.
// Severly modified by Már Örlygsson (mar@anomy.net)  ** version 1.0 **
function setCookie (name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}
function setFontCookie(theSize)
{
  var now = new Date();
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0) { now.setTime(now.getTime() - skew); }
  now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
  setCookie('fontSize', theSize, now, '/', document.location.host, '');
}
function getFontCookie (fontSize) {
  var prefix = 'fontSize=';
  var c = document.cookie;
  var cookieStartIndex = c.indexOf(prefix);
  if (cookieStartIndex == -1) { return defaultFontSize; }
  var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
  if (cookieEndIndex == -1) { cookieEndIndex = c.length; }
  var cookieFontSize = parseInt( unescape( c.substring(cookieStartIndex + prefix.length, cookieEndIndex) ) );
  cookieFontSize = (cookieFontSize > 10) ? defaultFontSize : (cookieFontSize > fontSizes.length) ? fontSizes.length : cookieFontSize;
  return cookieFontSize;
}
// end copyrighted part





//  getElementsBySelector and getAllChildren is written by Simon Willison.
//  For original source see: http://simon.incutio.com/archive/2003/03/25/getElementsBySelector

function getAllChildren(e) {
  return e.all ? e.all : e.getElementsByTagName('*');
}

document.getElementsBySelector = function(selector) {
  if (!document.getElementsByTagName) {
    return new Array();
  }
  var tokens = selector.split(' ');
  var currentContext = new Array(document);
  for (var i = 0; i < tokens.length; i++) {
    token = tokens[i].replace(/^\s+/,'').replace(/\s+$/,'');;
    if (token.indexOf('#') > -1) {
      var bits = token.split('#');
      var tagName = bits[0];
      var id = bits[1];
      var element = document.getElementById(id);
      if (tagName && element.nodeName.toLowerCase() != tagName) {
        return new Array();
      }
      currentContext = new Array(element);
      continue;
    }
    if (token.indexOf('.') > -1) {
      var bits = token.split('.');
      var tagName = bits[0];
      var className = bits[1];
      if (!tagName) {
        tagName = '*';
      }
      var found = new Array;
      var foundCount = 0;
      for (var h = 0; h < currentContext.length; h++) {
        var elements;
        if (tagName == '*') {
            elements = getAllChildren(currentContext[h]);
        } else {
            elements = currentContext[h].getElementsByTagName(tagName);
        }
        for (var j = 0; j < elements.length; j++) {
          found[foundCount++] = elements[j];
        }
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      for (var k = 0; k < found.length; k++) {
        if (found[k].className && found[k].className.match(new RegExp('\\b'+className+'\\b'))) {
          currentContext[currentContextIndex++] = found[k];
        }
      }
      continue;
    }
    if (token.match(/^(\w*)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/)) {
      var tagName = RegExp.$1;
      var attrName = RegExp.$2;
      var attrOperator = RegExp.$3;
      var attrValue = RegExp.$4;
      if (!tagName) {
        tagName = '*';
      }
      var found = new Array;
      var foundCount = 0;
      for (var h = 0; h < currentContext.length; h++) {
        var elements;
        if (tagName == '*') {
            elements = getAllChildren(currentContext[h]);
        } else {
            elements = currentContext[h].getElementsByTagName(tagName);
        }
        for (var j = 0; j < elements.length; j++) {
          found[foundCount++] = elements[j];
        }
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      var checkFunction;
      switch (attrOperator) {
        case '=': checkFunction = function(e) { return (e.getAttribute(attrName) == attrValue); }; break;
        case '~': checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('\\b'+attrValue+'\\b'))); }; break;
        case '|': checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('^'+attrValue+'-?'))); }; break;
        case '^': checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) == 0); }; break;
        case '$': checkFunction = function(e) { return (e.getAttribute(attrName).lastIndexOf(attrValue) == e.getAttribute(attrName).length - attrValue.length); }; break;
        case '*': checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) > -1); }; break;
        default : checkFunction = function(e) { return e.getAttribute(attrName); };
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      for (var k = 0; k < found.length; k++) {
        if (checkFunction(found[k])) {
          currentContext[currentContextIndex++] = found[k];
        }
      }
      continue;
    }
    tagName = token;
    var found = new Array;
    var foundCount = 0;
    for (var h = 0; h < currentContext.length; h++) {
      var elements = currentContext[h].getElementsByTagName(tagName);
      for (var j = 0; j < elements.length; j++) {
        found[foundCount++] = elements[j];
      }
    }
    currentContext = found;
  }
  return currentContext;
}

// End of code by Simon Willison


function mergeArrays(array1, array2)
{
  var nA = new Array;
  for (var i = 0; i < array1.length; i++) { nA[nA.length] = array1[i]; }
  for (var i = 0; i < array2.length; i++) { nA[nA.length] = array2[i]; }
  return nA;
}






// = = = = = = = = = = = = = = = = = = = = =
// = = = = = = = = = = = = = = = = = = = = =

function initPage() 
{
  if (is_ie && !is_mac) {
    document.body.style.fontSize = fontSizes[currentFontSize] + "%";
  }
  disableCSSinStupidBrowsers();
  initPopupLinks();
  fixPagebodyHeight();
  closeDropMenus();
  initResetButtons();
  closeDhtmlMenus();
}

window.onload = initPage;

