// Preload images
if (document.images) {
  '/images/menu/startsida_h.jpg'
  $A(['startsida', 'bohososs', 'konferens', 'retreat', 'konfirmation',
      'galleri', 'kontaktaoss', 'lankar']).collect(function(x) {
        return '/images/menu/'+x+'_h.jpg'; }).each(function(n) {
        var i = new Image(0, 0);
        i.src = n;
      });
}

// Mail obfuscation
function protect_mail(a, b, c) {
  if (a == false) {
    document.write('</a>');
    return;
  }
  
  a = a.replace(/\|/g, '.');
  b = b.replace(/\|/g, '.');
  
  if (c === false)
    document.write('<a href="mailto:'+a+'@'+b+'">');
  else
    document.write(a+'@'+b);
}

// Utility functions for cookies from quirksmode.org, modified by Per Eckerdal
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; 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;
}

// Dynamic script loading technique stolen from
// http://cain.supersized.org/archives/2-Dynamic-loading-of-external-JavaScript-.js-files.html
// Modified by Per Eckerdal
function loadScript(file, cacheBuster) {
  if (window.XMLHttpRequest) // Gecko and alike
    var req = new XMLHttpRequest();
  else if (window.ActiveXObject) // IE
    var req = new ActiveXObject("Msxml2.XMLHTTP");
  
  if (cacheBuster) // Hack used when debugging
    file += "?rand="+parseInt(Math.random()*99999999);
  
  if (req) {
    req.open("GET", file, false);
    req.send(null);
    
    if (req.status==200)
      window.eval(req.responseText);
  }
}

function loadAdminInterface() {
  if (Prototype.Browser.IE || Prototype.Browser.Opera) {
    alert("Administrationsgränssnittet fungerar inte i Internet Explorer eller Opera. Använd till exempel Firefox istället.");
    return;
  }
  
  loadScript('/js/admin.js');
  adminScriptLoaded(readCookie('user'));
}


Event.observe(document, 'dom:loaded', function() {
  // Things for the admin interface
  var l = document.getElementById('admin_login_link');
  l.onclick = loadAdminInterface;
  if (readCookie('user')) loadAdminInterface();
  
  $$("#menu img").each(function(img) {
    var src = img.src;
    var hsrc = src.replace(/.jpg$/, '_h.jpg');
    Event.observe(img, 'mouseover', function() { img.src = hsrc; });
    Event.observe(img, 'mouseout', function() { img.src = src; });
  });
});
