window.addEvent('domready', function() {
  // CSS3 hack for IE6 and IE7
  if (Browser.Engine.trident) {
    $$('div.AspNet-Menu-Horizontal li:last-child').setStyle('border-right','none');

    if ($('language')) $('language').getElement('li:last-child').setStyle('border-right','none');
  }
  
  // Correct submenu placement (in all browsers!)
  setMenuPlacementTrident();
  
  // Shadowbox
  Shadowbox.init();

  // Add copy of main menu items in submenu
  var allLi = $$('#nav ul.AspNet-Menu')[0].getChildren();
  var i = 0;
  var linkToCopy;
  var ul;
  var newLi;  
  while(i < allLi.length) {
    linkToCopy = allLi[i].getFirst().clone();
    linkToCopy.removeClass('AspNet-Menu-ChildSelected');
    ul = allLi[i].getChildren('ul')[0];
    newLi = new Element('li',{
      'class':'AspNet-Menu-Leaf'
    }).adopt(linkToCopy);
    newLi.inject(ul,'top');    
    i++;
  }


  // create a scrollbar when content exceeds 555px
  var article = $('article').getElement('.Article0');
  if (article) {
    var minHeight = 605;
    if (article.getSize().y > minHeight) {
	  $('article').setStyle('padding-left', '77px');	  
	  $('article').setStyle('padding-bottom', '0px');	
	  $('article').setStyle('padding-right', '308px');  
	  $('article').getElement('.Article0').addClass('scroll');
	} else if (article.getSize().y < minHeight) {
	  var bottom = 88 + (minHeight - article.getSize().y);
	  if (bottom > 150) bottom = 150;

	  if ($('mp_cphArticle_AsideLinks')) {
	    $('mp_cphArticle_AsideLinks').setStyle('bottom', bottom + 'px');
      $('article').setStyle('min-height', minHeight +'px');
	  }
	}
  // check height when article is missing
  } else {
    if ($('article').getSize().y < 555) $('article').setStyle('height', '555px');
  }


  if ($('main').getElement('.bannerPanel')) {
    if ($('main').getElement('.bannerPanel').getElement('.content').getElement('blockquote')) {
      var el = $('main').getElement('.bannerPanel').getElement('.content').getElement('blockquote');
      el.set('html', '<img id="quotestart" src="/_images/quote_start.png" alt="" />' + el.get('html'));
      el.set('html', el.get('html') + '<img id="quoteeind" src="/_images/quote_eind.png" alt="" />');
    }
  }

  if ($('mp_cphArticle_Logos')) {
	  $('mp_cphArticle_Logos').inject($('aside')).setStyle('display', 'block');
  }
	
  if ($('main').getElement('img[src=/_images/products-banner.jpg]')) {
	  $('main').getElement('img[src=/_images/products-banner.jpg]').set('usemap', '#product_logos');

	  var usemap = new Element('map', {
		'name' : 'product_logos'
	  }).inject($('main'));

	  new Element('area', {
		'shape'  : 'rect',
		'coords' : '326,70,442,185',
		'href'   : '/products/clientele-itsm/default.aspx'
	  }).inject(usemap);

	  new Element('area', {
		'shape'  : 'rect',
		'coords' : '443,59,514,149',
		'href'   : '/products/clientele-itsm/portal/default.aspx'
	  }).inject(usemap);

	  new Element('area', {
		'shape'  : 'rect',
		'coords' : '516,50,576,126',
		'href'   : '/products/clientele-itsm/performance-intelligence/default.aspx'
	  }).inject(usemap);
  }

  var loc = document.location + '';
  loc = loc.replace('http://nieuw.mproof.nl', '').replace('default.aspx', '');
  var re = new RegExp('http://(www|nl|fr|es|de)\.mproof\.com');
  loc = loc.replace(re, '');
  re = new RegExp('http://mproofint(nl|fr|es|de)?\.online\.lan');
  loc = loc.replace(re, '');
  
  switch (loc) {
	case '/home/contact-us/' :
		$('article').setStyle('min-height', '520px');
		$('mp_cphArticle_AsideLinks').setStyle('bottom', '65px');
		break;
	case '/partners-customers/' :
		//$('article').getElement('div.ExtraLogos').setStyle('bottom', '90px');
		break;
	case '/support/','/soporte/' :
		//$('article').getElement('.ExtraLogos').setStyle('bottom', '100px');
		$('article').getElement('.Article1').getElement('h1').setStyle('visibility', 'hidden');
		break;
  }
  
  // expand label
  if ($('mp_cphArticle_moreinfo_0')) $('mp_cphArticle_moreinfo_0').getNext('label').setStyle('width', '200px');
  
  // replace all rel=external links with target=_blank
  $$('a[rel=external]').setProperty('target', '_blank');
  // Links to external domain
  $$('a[href^=https://]').setProperty('target', '_blank');

  // replace all rel=mediaitem links (media related to page) with target=blank
  $$('a[rel=mediaitem]').setProperty('target', '_blank');
  
  // Searchbox label in searchbox  
  if ($$('.search')) {
    var label = $$('.search').getElement('label').get('text');
    var box = $$('.search').getElement('.searchBox');
    if(box.get('value') == '') box.set('value',label);
    box.addEvent('click',function() {this.set('value','');});
  }
  
  // create a delay when a menu is closing
  $('nav').getElements('li.AspNet-Menu-WithChildren').each(function(nav) {
    nav.addEvent('mouseenter', function() {
      openNavigation.bind(this).apply();
    });

    nav.addEvent('mouseleave', function() {
      this.timer = closeNavigation.delay(250, this);
    });
  });
  
});

// open a submenu
var openNavigation = function() {
  $clear(this.timer);
  
  this.addClass('open');
  this.current = true;
}

// close a submenu
var closeNavigation = function() {
  this.removeClass('open');
  this.current = false;
}

var setMenuPlacementTrident = function() {
  var n;
  $('nav').getElements('.AspNet-Menu > .AspNet-Menu-WithChildren').each(function(nav, index) {
    n = nav.getPosition($('body'));
    nav.getElement('ul').setStyles({
      'margin-left': '0px',
      'left': n.x + 3 + 'px'
    });
  });
}