if (document.observe) document.observe("dom:loaded", function() {
  /* Initialize login form */
  initLoginForm();

  /* Set active menu */
  setActiveMenu();

  /* Hide focus on menu links */
  hideFocus("menu");

  /* Replace titles */
  $$("#layoutContent h2").each(function(element) {
    setPNGbackground(element, getTitleURL("h2", element));
    Element.setStyle(element, { textIndent: "-10000px" });
  });
  $$("#layoutContent h3").each(function(element) {
    setPNGbackground(element, getTitleURL("h3", element));
    Element.setStyle(element, { textIndent: "-10000px" });
  });

  if ($("menuHighlights")) document.scrollerHighlights = new HM.NewsScroller("menuHighlights", {
    direction: "horizontal",
    interval: 7500
  });

  //if ($("divFadeTopFoto")) $("divFadeTopFoto").fadedelay = 7500;
});

function setActiveMenu() {
  var uri = window.location.pathname.match(/[^\/]+$/);
  var menuElement = $$("ul#menu li.menuitem ul a[href="+uri+"]").first();
  if (!menuElement) return;
  menuElement.up("li").addClassName("submenuitem_active");
  menuElement.up("li.menuitem").addClassName("menuitem_active");
}

function writeEmail(a) {
  var h=location.host.match(/[^\.]+\.[^\.]+$/);
  var e=a+'&#X40;'+h;
  document.write('<a href="mailto:'+e+'">'+e+'</a>');
}


function hideBackground(element) {
  $(element).setStyle({backgroundPosition: "1000px"});
}
function showBackground(element) {
  $(element).setStyle({backgroundPosition: "left top"});
}


function initLoginForm() {

  $A([$("auth_user"), $("auth_pass")]).each(function(element) {
    if (!element) return;
    if (element.value) {
      hideBackground(element);
    }
    element.observe("focus", function() {
      if (!this.value) {
        hideBackground(element);
      }else{
        this.select();
      }
    });
    element.observe("blur", function() {
      if (!this.value) showBackground(element);
    });
  });

}

function fnLogin() {
  $("frmLogin").submit();
  return false;
}

function hideFocus(root) {
  var isMSIE = /*@cc_on!@*/false;
  if (isMSIE) Element.select(($(root) || document), "a").each( function(element) {
    element.hideFocus = true;
    //element.style.outline = "none";
  } );
}

function getTitleURL(type, element) {
  var text = element.innerHTML;
  text = text.replace( /\?/g, "%3F" );
  //var url = "http://www2.maasrbm.nl" + "/images/"+type+"/"+escape(text)+".png";
  var url = "/images/"+type+"/"+escape(text)+".png";
  return url;
}

/* Set a PNG as a background images */
function setPNGbackground(element, url) {
  var isIE = false; /*@cc_on isIE = true; @*/
  if (isIE) {
    Element.setStyle( element, { filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+url+"', sizingMethod='crop')" } );
  }else{
    Element.setStyle( element, { backgroundImage: "url("+url+")" } );
  }
}