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');
  }

  $('main').getElements('h1').each(function(heading, numHeading) {
    //heading.set('html', heading.get('html') + "&reg;");
    //console.log(numHeading + ' ' + heading.get('html').charCodeAt(10));
    //console.log(heading.get('html').indexOf(String.fromCharCode(174)));
    heading.set('html', heading.get('html').replace(String.fromCharCode(174), "<sup>&reg;</sup>"));
  });

  $$('a[href^=http]').set('target', '_blank'); 
  
  // 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 = new Element('li').addClass('AspNet-Menu-Leaf').adopt(linkToCopy);
    newLi.inject(ul,'top');    
    i++;
  }

  // create a scrollbar when content exceeds 555px
  var article = $('article').getElement('.Article0');
  // downloads in article div
  if($$('#article .downloads')[0]) {
    $$('#article .downloads')[0].inject($$('#article .Article0')[0])
  }
  
  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');
        
        // 2010-10-22 - GG - Dikke uitzondering
        // [START BAH]
        var p
        if (Browser.Engine.trident) { // IE
            if (Browser.Engine.version < 6) { // IE7
                if (location.pathname == '/accueil/contactez-nous/default.aspx') { // FR
                    p = '<p><br /></p><p><br /></p><p><br /></p><p><br /></p><p><br /></p><p><br /></p><p><br /></p><p><br /></p><p style="margin-bottom: 55px;"><br /></p>';
                } else if (location.pathname == '/home/contact-us/default.aspx') { // EN
                    p = '<p><br /></p><p><br /></p><p><br /></p><p><br /></p><p><br /></p><p><br /></p>';
                } else if (location.pathname == '/startpagina/contact/default.aspx') { // NL
                    p = '<p><br /></p><p><br /></p><p><br /></p><p><br /></p><p><br /></p><p><br /></p><p><br /></p><p><br /></p><p style="margin-bottom: 45px;"><br /></p>';
                } else { // REST
                    p = '<p><br /></p><p><br /></p><p><br /></p><p><br /></p><p><br /></p><p><br /></p><p><br /></p><p><br /></p><p style="margin-bottom: 35px;"><br /></p>';
                }
            } else { // IE OVERIGE VERSIES
                if (location.pathname == '/home/contact-us/default.aspx') { // EN
                    p = '<p><br /></p><p><br /></p><p style="margin-bottom: 35px;"><br /></p>';
                } else if (location.pathname == '/inicio/contactenos/default.aspx') { // ES
                    p = '<p><br /></p><p><br /></p><p><br /></p><p><br /></p><p style="margin-bottom: 45px;"><br /></p>';
                } else { // REST
                    p = '<p><br /></p><p><br /></p><p><br /></p><p><br /></p><p style="margin-bottom: 45px;"><br /></p>';
                }
            }
        } else { // NIET IE
            if (location.pathname == '/home/contact-us/default.aspx') { // EN
                p = '<p><br /></p><p><br /></p><p><br /></p>';
            } else { // REST
                p = '<p><br /></p><p><br /></p><p><br /></p><p><br /></p><p><br /></p>';
            }
        }
        
        switch (location.pathname) {
            case '/home/contact-us/default.aspx' :
                new Element('div', {
                    html : p
                }).inject($('article'));
                break;
            case '/startseite/so-erreichen-sie-uns/default.aspx' :
                new Element('div', {
                    html : p
                }).inject($('article'));
                break;
            case '/startpagina/contact/default.aspx' :
                new Element('div', {
                    html : p
                }).inject($('article'));
                break;
            case '/inicio/contactenos/default.aspx' :
                new Element('div', {
                    html : p
                }).inject($('article'));
                break;
            case '/accueil/contactez-nous/default.aspx' :
                new Element('div', {
                    html : p
                }).inject($('article'));
                break;
            case '/products/pink-verify/default.aspx' :

                if (Browser.Engine.trident && Browser.Engine.version < 6) { // IE
                    p = '<p style="margin-top: 20px;"><br /></p>'
                } else {
                    p = '<p style="margin-top: 70px;"><br /></p>'
                }

                new Element('div', {
                    html : p
                }).inject($('article'));
                break; 
        }
        // [END BAH]


	  }
	}

  // 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').setStyle('display', 'block').inject($('aside'));
  }*/
	if ($('mp_cphAside_Logos')) {
	  $('mp_cphAside_Logos').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/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);
    });
  });
  
  // PartnerMap
  if($$('.PartnerMap')[0]) {
    $$('.PartnerMap')[0].getParent().inject($('mp_cphArticle_ArticlePanel0'));
    
  }
  if($$('.divNearestPartner')) {
    //$$('.divNearestPartner').setStyle('behavior','url(/_scripts/PIE.htc)');
  }
  // Nearest partner
  /*if($$('.divNearestPartner')[0] && $('mp_cphArticle_ArticlePanel1')) {
    $$('.divNearestPartner')[0].setStyle('margin-top','0px').inject($('mp_cphArticle_ArticlePanel1'));
  }*/
  
  // Twitter Ticker
  $('tweets-here').set('html','');
    //get information
    var myTwitterGitter = new TwitterGitter('Mproof',{
      count: 10,
      onComplete: function(tweets,user) {
        tweets.each(function(tweet,i) {
          new Element('div',{
            html: '<a href="http://twitter.com/Mproof" target="_blank" title="Mproof Twitter"><img src="/_images/twitter-icon.png" align="left" alt="' + user.name + '" /></a>' + tweet.text ,
            'class': 'tweet clear'
          }).inject('tweets-here');
        });
      }
    }).retrieve();
	
  var vert = new SwTicker('tweets-here');
  
});

// 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'
    });
  });
}
