window.onload = initPage;

function initPage() 
{
  // If the page is inside a frameset, then escape 
  if (parent.frames.length) { top.location.replace(document.location); }

  if (isIE && !isMac) {
    var fontSize = readFontSizeCookie();
    if (fontSize != "normal") {
      document.getElementById('bodywrap').className = document.getElementById('bodywrap').className + " " + fontSize + "-font";
    }
  }
  disableCSSinStupidBrowsers();

  initPopupLinks();
  fixPagebodyHeight();
  closeDropMenus();
  initResetButtons();
}






// Accessible DHTML dropmenu script

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 dropMenuMouseOut() { this.style.visibility = "hidden"; }

function closeDropMenus()
{
  if (!document.getElementsByTagName) return true;

  var dmHeads1 = document.getElementsBySelector("div.dropmenu h2.boxhd a");
  var dmBlocks1 = document.getElementsBySelector("div.dropmenu div.content");
  var dmHeads2 = document.getElementsBySelector("div.dropmenu h3.label a");
  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++) { dropMenuHeads[i].onclick = dropMenu; }
  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].onmouseout = dropMenuMouseOut;
    dropMenuBlocks[i].style.visibility = "hidden";
  }
  return true;
}



// Gereric Popup window script

var popupConfig = new Array;
popupConfig["popup"] = new Array ( "", "width=350,height=450");
popupConfig["glossary"] = new Array ( "stuff", "width=550,height=350");

function popup()
{
  if (this.target == "") { this.target = (popupConfig[this.keyName][0] != "") ? popupConfig[this.keyName][0] : this.keyName; }
  newWin = window.open(this.href, this.target, popupConfig[this.keyName][1]);
  newWin.focus();
  return false;
}

function initPopupLinks()
{
  if (!document.getElementsByTagName) return true;
  for (var theKey in popupConfig) 
  {
    var theLinks = document.getElementsBySelector("a."+theKey);
    for (var i = 0; i < theLinks.length; i++) 
    {
      theLinks[i].keyName = theKey;
      theLinks[i].onclick = popup;
    }
  }
  return true;
}


// Cookie-based font size selection for Internet Explorer
function changeFontSize(size) {
 createCookie(size);
 document.location = document.location;
}

function createCookie(value) {
 var cookieExpireDate = new Date();
 cookieExpireDate.setTime(cookieExpireDate.getTime()+(365*24*60*60*1000)); //One Year
 var expires = "; expires="+cookieExpireDate.toGMTString();
 document.cookie = "font_size="+value+expires+"; path=/";
}

function readFontSizeCookie() {
 var cookieName = "font_size=";
 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(cookieName) == 0) return c.substring(cookieName.length,c.length);
 }
 return "normal";
}


// Enable form reset buttons with confirmation step
// 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;
}



// Make sure that the pagebody block is at least equal height to 
// each of the side columns
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" }
  }
}



// Very simple browser-check  |  Inspired by this: http://www.webreference.com/tools/browser/javascript.html
var agent    = navigator.userAgent.toLowerCase();
var appVer   = navigator.appVersion.toLowerCase();

var maybeIE  = (appVer.indexOf('msie') != -1);
var isMac    = (agent.indexOf('mac') != -1);
var isWin    = (!isMac);
var isOpera  = (agent.indexOf('opera') != -1);
var isSafari = (agent.indexOf('safari') != -1) && (isMac);
var isKonq   = (agent.indexOf('konqueror') != -1);
var isIE     = ((maybeIE) && (!isOpera) && (!isKonq) && (!isSafari));

var versNum  = (!maybeIE) ? parseFloat( navigator.appVersion.toLowerCase() ) : parseFloat( appVer.substring(maybeIE+5,appVer.indexOf(';',maybeIE)) );


/* Disable all CSS in stupid browsers */
function disableCSSinStupidBrowsers()
{
  if (isIE && (versNum < 5.0)) { 
    var x = document.all.tags('link'); 
    if (x != null) { x[0].disabled = true; x[1].disabled = true; }
  }
}





//  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)
{
  for (var i = 0; i < array2.length; i++) { array1[array1.length] = array2[i]; }
  return array1;
}




function searchValid(myForm) 
{
  return true;
}
