var App = {
	transDuration: 500,
	transDelay: 300,
	transDelayLong: 450,
	restoreDelayTime: 250,
	hoverDuration: 450,
	hoverDurationShort: 250,
	overlayOpacity: 0.7,
	ssOverlayOpacity: 0.5,
	overlay: null,
	cookieName: 'stay',
	cookieDuration: 30, //days
	transEffect: Fx.Transitions.Sine.easeOut,
	initialMenuItemSet: false,	
	initialMenuItemSetItem: null,
	printLink: '/cmspages/dhv/printimage.aspx?url=',
	mooblade: null,

	boot: function() {
		Element.implement({
			showHide: function(recurse) {
				if(!this.hasClass('clear')){
					var children = this.getChildren();
					if(recurse) {
						children.showHide();
					}
					else{
						var visibility = this.getStyle('visibility') == 'hidden' ? 'visible' : 'hidden';
						this.setStyle('visibility',visibility);
					}
				}
			}
		});
		//if(Browser.Engine.trident) this.fixIE();
		this.setFonts();
		this.fixGridBackground();		
		this.buildMainMenu();
		this.buildSiteselectMenu();
		this.buildNavigation();
		this.startResize();
		this.sideMenuFoldables();
		this.recentProjectsNavigation();
		this.extendeeTitleExpand();
		this.minMax();
		this.tickBoxes();
		this.fixRelatedImages();
		this.searchBoxBehavior();
		this.mapFlashHovers();
		this.setSubjects();
		this.comHome();
		this.searchButtonEvents();
		this.foldingSubjects();
	},
	
	comHome: function(){
		if($('blades') == null) {
			return false;
		}
		App.mooblade = new MooBladeGallery({'bladesElement':'blades', 'scrollElement':'scroll'});
		
		var maxHeight = 0;
		$each($$('div.comhome-bottom'), function(obj){
			if(obj.getSize().y > maxHeight) maxHeight = obj.getSize().y;
		});	
		$each($$('div.comhome-bottom'), function(obj){
			obj.setStyle('height', maxHeight);
		});		
		
		
		var bottomPosition = function(){
			if($chk($$('div.comhome-bottom')[0]) && $chk($('vspacer'))){
				var vspacer = $('vspacer');
				var blockHeight = $$('div.comhome-bottom')[0].getSize().y + 28;
				var screenHeight = document.html.clientHeight;
				var vspaceY = vspacer.getPosition().y;
				if(screenHeight <= ((vspaceY+9) + (3*blockHeight) - 0.5*blockHeight)){
					if($chk($('clear1'))) $('clear1').removeClass('clear');
					if($chk($('clear2'))) $('clear2').removeClass('clear');
					if($chk($('clear3'))) $('clear3').removeClass('clear');
				}
				else{
					if(screenHeight > ((vspaceY+9) + (3*blockHeight)  - 0.5*blockHeight) && screenHeight <= ((vspaceY+9) + (4 *blockHeight) - 0.5*blockHeight)){
						if($chk($('clear1'))) $('clear1').addClass('clear');
						if($chk($('clear2'))) $('clear2').removeClass('clear');
						if($chk($('clear3'))) $('clear3').removeClass('clear');
					}
					else{
						if(screenHeight > ((vspaceY+9) + (4*blockHeight) - 0.5*blockHeight) && screenHeight <= ((vspaceY+9) + (5 *blockHeight) - 0.5*blockHeight)){
							if($chk($('clear1'))) $('clear1').addClass('clear');
							if($chk($('clear2'))) $('clear2').addClass('clear');
							if($chk($('clear3'))) $('clear3').removeClass('clear');
						}
						else{
							if(screenHeight > ((vspaceY+9) + (5 *blockHeight) - 0.5*blockHeight)){
								if($chk($('clear1'))) $('clear1').addClass('clear');
								if($chk($('clear2'))) $('clear2').addClass('clear');
								if($chk($('clear3'))) $('clear3').addClass('clear');							
							}
							else{
								console.log('oops!');
							}
						}
					}
				}
				
				var bottomContentHeight = $('bottom-holder').getSize().y - 16;
				var vspaceHeight = screenHeight - vspaceY - bottomContentHeight;
				if(vspaceHeight < 9 ) vspaceHeight=9;
				vspacer.setStyle('height', vspaceHeight);
			}
		};
		bottomPosition();
		window.addEvent('resize', bottomPosition.bind(this));
	},
	
	setSubjects: function (){
		var subjects = $('sidemenu-subjects-block');
		if (subjects != null){
			$$('div#sidemenu-subjects-block ul li:last-child').setStyle('border-bottom', 'none');
		}
	},
	
	setContentAnchors: function(){
		var anchors = [$$('div.content div.left a')].flatten();
		$each(anchors, function(anchor){
			if(anchor.get('class') == '') anchor.addClass('icon').addClass('icon-link');
		});
	},
	
	mapFlashHovers: function(){
	
		$each($$('a.maphover'), function(mapHover, index){
			mapHover.addEvent('mouseenter', function(){
				if($('sidemenu_minimap_flashcontent_content') != null && $('sidemenu_minimap_flashcontent_content').flashHover != null){
					$('sidemenu_minimap_flashcontent_content').flashHover(this.get('id'), 'in');
				}
				if($('grid-flashcontent_content') != null && $('grid-flashcontent_content').flashHover != null){
					$('grid-flashcontent_content').flashHover(this.get('id'), 'in');
				}				
			}.bind(mapHover));
			mapHover.addEvent('mouseleave', function(){
				if($('sidemenu_minimap_flashcontent_content') != null && $('sidemenu_minimap_flashcontent_content').flashHover != null){
					$('sidemenu_minimap_flashcontent_content').flashHover(this.get('id'), 'out');
				}
				if($('grid-flashcontent_content') != null && $('grid-flashcontent_content').flashHover != null){
					$('grid-flashcontent_content').flashHover(this.get('id'), 'out');
				}					
			}.bind(mapHover));			
		});
	},
	
	mapHover: function(id, direction){
		if($(id)!= null){
			if(direction == 'in'){
				$(id).getParent().addClass('hover');
			}
			else{
				$(id).getParent().removeClass('hover');
			}
		}
	},		
	
	fixIE: function(){
		App.transDuration = 0;
		App.transDelay = 30;
		App.transDelayLong = 0;
		App.restoreDelayTime = 0
		App.hoverDuration = 50;
		App.hoverDurationShort = 0;
	},
	
    fixGridBackground: function() {
        var gridRows = $$('div.grid-a div.horizontal:nth-child(last)');

        $each(gridRows, function(gridRow, index) {
            if (gridRow.getChildren().length == 1)
                gridRow.addClass('single-item');
        });
    },	
	
	searchBoxBehavior: function(){
		var searchBox = $('search-input');
		var searchWrapper = $('search-wrapper');
		if(searchBox != null && searchWrapper != null){
			searchBox.addEvent('focus', function(){
				searchWrapper.addClass('search-focused');
				searchWrapper.setStyle('background-image','url(/App_Themes/Dhv/Img/navigation-bar-search-bg-focus.gif)');
			});
			searchBox.addEvent('blur', function(){
				searchWrapper.removeClass('search-focused');
				searchWrapper.setStyle('background-image','url(/App_Themes/Dhv/Img/navigation-bar-search-bg.gif)');				
			});
		}
		var searchWrapperTitle = $('search-wrapper-title');
		if(searchWrapperTitle != null){
			var searchBoxTitle = searchWrapperTitle.getChildren('input')[0];
			searchBoxTitle.addEvent('focus', function(){
				searchWrapperTitle.addClass('search-focused');
				searchWrapperTitle.setStyle('background-image','url(/App_Themes/Dhv/Img/navigation-bar-search-bg-focus.gif)');
			});
			searchBoxTitle.addEvent('blur', function(){
				searchWrapperTitle.removeClass('search-focused');
				searchWrapperTitle.setStyle('background-image','url(/App_Themes/Dhv/Img/navigation-bar-search-bg.gif)');				
			});
		}			
	},
	
	setFonts: function(){
		//Cufon.replace('div#language div span, div#language div a', { fontFamily: 'TheMix', hover: true });
		//Cufon.replace('div#header-menu ul li div.submenu ul li a', { fontFamily: 'TheMix', hover: true });
		
		Cufon.replace('div#navigation div#navigation-bar div#navigation-search a', { fontFamily: 'TheSans', hover: true });
		Cufon.replace('div#navigation div#navigation-bar div#navigation-items a.main', { fontFamily: 'TheSans', hover: true });
		Cufon.replace('div#navigation div#navigation-bar div#navigation-items a.sub', { fontFamily: 'TheSans', hover: true });
		
		Cufon.replace('div.shadowwrapper-comhome-search-width div#navigation-search a', { fontFamily: 'TheSans', hover: true });
		
		Cufon.replace('div#pagetitle-holder h1', { fontFamily: 'TheMix' });
		
		Cufon.replace('div.grid-a-item-table-body h4 a', { fontFamily: 'TheMix' });
		Cufon.replace('h4', { fontFamily: 'TheMix' });
		Cufon.replace('div.grid-fullwidth div.grid-fullwidth-item-content-body h2', { fontFamily: 'TheMix' });
		Cufon.replace('div.grid-a div.grid-a-item h2', { fontFamily: 'TheMix' });
		Cufon.replace('div.grid-d div.grid-d-item h2', { fontFamily: 'TheMix' });
		Cufon.replace('div.article-types h5', { fontFamily: 'TheSans' });
		Cufon.replace('div.search-results-container div.left h5', { fontFamily: 'TheSans' });
		Cufon.replace('div.grid-c div.article-summary h2', { fontFamily: 'TheMix' });
		Cufon.replace('div.head h5, div.grid-head h1, div.world_continents-map-head h1, div.region-map-head h1, div.country-map-head h1, div.office-map-head h1', { fontFamily: 'TheSans' });
		Cufon.replace('div.news-head h1', { fontFamily: 'TheSans' });
		Cufon.replace('div.comhome-news-themes-head h1', { fontFamily: 'TheSans' });
		Cufon.replace('div.comhome-bottom-head h1', { fontFamily: 'TheSans' });
		Cufon.replace('div.recent-projects h2 a', { fontFamily: 'TheMix', hover: true });
		Cufon.replace('div.search-results-container div.search-results ul li h3 a', { fontFamily: 'TheSans', hover: true });		
		Cufon.replace('div.pagetitle-search a.search-button', { fontFamily: 'TheSans' });
		Cufon.replace('div#sidemenu-holder div.sidemenu-item div.sidemenu-moreinformation-block div a.request-button', { fontFamily: 'TheSans' });
		Cufon.replace('div#blades div.article h1', { fontFamily: 'TheSans', hover: true });
		Cufon.replace('div#blades div.article div.details h3', { fontFamily: 'TheSans', hover: true });
		
		Cufon.replace('div#comhome div#blades div.blade div.image-button-quote div.button-quote div.quote span.quote', { fontFamily: 'TheSans', hover: true });
		Cufon.replace('div#comhome div#blades div.blade div.image-button-quote div.button-quote div.quote span.name', { fontFamily: 'TheSans', hover: true });
		Cufon.replace('div#comhome div#blades div.blade div.image-button-quote div.button-quote div.quote span.readmore a', { fontFamily: 'TheSans', hover: true });
		
		
		Cufon.now();
	},
	
	extendeeTitleExpand: function(){
		var readmore_links = $$('a.more-title-lnks');
		$each(readmore_links, function(obj, index){
			obj.addEvent('click', function(){
				
				$$('span.read-more-link').setStyle('display', 'none');
				$$('span.read-more').setStyle('display', 'inline');
			
				obj.setStyle('display', 'none');
			}.bind(obj));
		});
	},
	
    fixRelatedImages: function() {
        var relatedImages = $$('div.article-types div.related-images a');
        $each(relatedImages, function(relatedImage, index) {
            if (index % 3 == 0) {
                relatedImage.setStyle('padding-left', '0');
                relatedImage.setStyle('clear', 'both');
            }
            if (relatedImages.length - index <= 3) relatedImage.setStyle('padding-bottom', '2px');
        });
    },
	
	tickBoxes: function(){
		var tickBoxes = $$('div.filters input');
		$each(tickBoxes, function(tickBox, index){
			if(tickBox.get('type') == 'checkbox'){
				var tickBoxImage = new Element('img');
				if(tickBox.checked) tickBoxImage.set('src', '/App_Themes/Dhv/Img/tickBox-searchfilter-on.png');
				else tickBoxImage.set('src', '/App_Themes/Dhv/Img/tickBox-searchfilter-off.png');
				tickBox.parentNode.insertBefore(tickBoxImage, tickBox);
				tickBox.setStyle('display','none');
				tickBoxImage.addEvent('click', function(e){
					e.stop();
					if(tickBox.checked){
						this.set('src', '/App_Themes/Dhv/Img/tickBox-searchfilter-off.png');
						tickBox.checked = false;
					}
					else{
						this.set('src', '/App_Themes/Dhv/Img/tickBox-searchfilter-on.png');
						tickBox.checked = true;						
					}
				});
			}
		});
	
		var tickBoxes = $$('div.sidemenu-item input');
		$each(tickBoxes, function(tickBox, index){
			if(tickBox.get('type') == 'checkbox'){
				var tickBoxImage = new Element('img');
				if(tickBox.checked) tickBoxImage.set('src', '/App_Themes/Dhv/Img/tickBox-on.png');
				else tickBoxImage.set('src', '/App_Themes/Dhv/Img/tickBox-off.png');
				tickBox.parentNode.insertBefore(tickBoxImage, tickBox);
				tickBox.setStyle('display','none');
				tickBoxImage.addEvent('click', function(e){
					e.stop();
					if(tickBox.checked){
						this.set('src', '/App_Themes/Dhv/Img/tickBox-off.png');
						tickBox.checked = false;
					}
					else{
						this.set('src', '/App_Themes/Dhv/Img/tickBox-on.png');
						tickBox.checked = true;						
					}
					//Has Children?
					if(tickBox.getParent('li').getChildren('ul')[0] != null){
						var childLis = tickBox.getParent('li').getChildren('ul')[0].getChildren('li');
						$each(childLis, function(childLi, index){
							var childTickBox = childLi.getChildren('input')[0];
							var childTickBoxImage = childLi.getChildren('img')[0];
							if(!tickBox.checked){
								childTickBoxImage.set('src', '/App_Themes/Dhv/Img/tickBox-off.png');
								childTickBox.checked = false;
							}
							else{
								childTickBoxImage.set('src', '/App_Themes/Dhv/Img/tickBox-on.png');
								childTickBox.checked = true;						
							}							
						});
					}
					//Has Parent?
					try{
						var parentLi = tickBox.getParent('ul').getParent('ul').getChildren('li');
					}
					catch(e){
						return false;
					}
					if(parentLi != null){
						var childLis = parentLi.getChildren('ul')[0].getChildren('li')[0];
						var allChildsChecked = true;
						var allChildsUnChecked = true;
						
						//check parent children
						$each(childLis, function(childLi, index){
							var childTickBox = childLi.getChildren('input')[0];
							if(!childTickBox.checked) allChildsChecked = false;
							else allChildsUnChecked = false;							
						});
						
						if(allChildsChecked){
							parentLi.getChildren('img')[0].set('src', '/App_Themes/Dhv/Img/tickBox-on.png');
							parentLi.getChildren('input')[0][0].checked = true;						
						}
						if(!allChildsChecked && parentLi.getChildren('input')[0][0].checked == true){
							parentLi.getChildren('img')[0].set('src', '/App_Themes/Dhv/Img/tickBox-off.png');
							parentLi.getChildren('input')[0].checked = false;							
						}
						if(allChildsUnChecked){
							parentLi.getChildren('img')[0].set('src', '/App_Themes/Dhv/Img/tickBox-off.png');
							parentLi.getChildren('input')[0][0].checked = false;						
						}
					}
					eval(tickBox.get('onclick'));
				});
			}
		});
	
	},
	
	minMax: function(){
		var minMaxButtons = $$('a.mix-max');
		var tempInitialItem = null;
		var pageTitleHolder = $('pagetitle-holder');
		$each(minMaxButtons, function(minMaxButton, index){
			var minMaxButtonsInner = $$('a.mix-max');
			minMaxButton.addEvent('click', function(e){
				e.stop();
				$each(minMaxButtonsInner, function(minMaxButtonInner, index){
					if(minMaxButtonInner.hasClass('minimized')){
						if($chk(minMaxButtonInner.getParent().getSiblings('div')[0])){
							minMaxButtonInner.getParent().getSiblings('div')[0].showHide(true);
						}
						if(minMaxButtonInner.getParent().hasClass('grid-head') ||
							minMaxButtonInner.getParent().hasClass('news-head') ||
							minMaxButtonInner.getParent().hasClass('world_continents-map-head') ||
							minMaxButtonInner.getParent().hasClass('region-map-head') ||
							minMaxButtonInner.getParent().hasClass('office-map-head') ||
							minMaxButtonInner.getParent().hasClass('comhome-search-head') ||
							minMaxButtonInner.getParent().hasClass('comhome-news-themes-head') ||
							minMaxButtonInner.getParent().hasClass('comhome-bottom-head')
							){
							
							minMaxButtonInner.getSiblings().setStyle('visibility','visible');
							if(minMaxButtonInner.getParent().hasClass('comhome-news-themes-head')){
								minMaxButtonInner.getParent().setStyle('background-image', 'url(/App_Themes/Dhv/Img/comhome-news-theme-header-background.gif)');
							}
							else{
								minMaxButtonInner.getParent().setStyle('background-image', 'url(/App_Themes/Dhv/Img/item-bg.jpg)');
							}
							
							if(!minMaxButtonInner.getParent().hasClass('comhome-search-head') && !minMaxButtonInner.getParent().hasClass('comhome-news-themes-head')){
								minMaxButtonInner.getParent().setStyle('border-bottom-width', '1px');
								minMaxButtonInner.getParent().setStyle('padding-bottom', '0px');
							}
							if(minMaxButtonInner.getParent().hasClass('comhome-news-themes-head')){
								minMaxButtonInner.getParent().setStyle('height', 'auto');
								minMaxButtonInner.getParent().setStyle('background-repeat', 'repeat-x');
								minMaxButtonInner.getParent().setStyle('background-position', 'top left');								
							}
							else{
								minMaxButtonInner.getParent().setStyle('background-repeat', 'repeat-x');
								minMaxButtonInner.getParent().setStyle('background-position', 'bottom left');							
								minMaxButtonInner.getParent().setStyle('height', '22px');
							}							
						}
						else {
							minMaxButtonInner.getParent().setStyle('background-image', 'none');
							minMaxButtonInner.getParent().setStyle('height', 'auto');
						}
						
						//hide the minmax background
						minMaxButtonInner.getParent().getParent().getParent().setStyle('padding-right', '7px');
						minMaxButtonInner.getParent().getParent().setStyle('width', minMaxButtonInner.getParent().getParent().getParent().getWidth()-13);
						minMaxButtonInner.getParent().getParent().setStyle('height', 'auto');
						//minMaxButtonInner.getParent().getParent().setStyle('overflow', 'visible');
						minMaxButtonInner.getParent().getSiblings().setStyle('overflow', 'visible');
						minMaxButtonInner.setStyle('margin-right', '0');
						
						minMaxButtonInner.getParent().getParent('div').setStyle('background-color', '#fff');
						minMaxButtonInner.getParent().getSiblings('div').setStyle('background-color', '#fff');
						
						minMaxButtonInner.getParent().getParent().getParent().setStyle('background-image', 'url(/App_Themes/Dhv/Img/shadow-right.png)');
						minMaxButtonInner.getParent().getParent().getParent().getSiblings()[0].getChildren()[0].setStyle('background-image', 'url(/App_Themes/Dhv/Img/shadow-bottom-left.png)');
						minMaxButtonInner.getParent().getParent().getParent().getSiblings()[0].getChildren()[1].setStyle('background-image', 'url(/App_Themes/Dhv/Img/shadow-bottom-center.png)');
						minMaxButtonInner.getParent().getParent().getParent().getSiblings()[0].getChildren()[2].setStyle('background-image', 'url(/App_Themes/Dhv/Img/shadow-bottom-right.png)');
						
						minMaxButtonInner.removeClass('minimized');
						
						//show title
						if(pageTitleHolder != null){
							pageTitleHolder.setStyle('visibility', 'visible');
						}						
						
						//show sidemenu foldables
						var siteMenuItems = $$('div.sidemenu-item');
						$each(siteMenuItems, function(sideMenuItem, index){
							if(sideMenuItem.get('foldedRemember') != 'true'){
								App.showHideSideMenuFoldable(sideMenuItem, 'down');
							}
						});						
					}
					else{
						if($chk(minMaxButtonInner.getParent().getSiblings('div')[0])){
							minMaxButtonInner.getParent().getSiblings('div')[0].showHide(true);
						}
						
						if(minMaxButtonInner.getParent().hasClass('grid-head') ||
							minMaxButtonInner.getParent().hasClass('news-head') ||
							minMaxButtonInner.getParent().hasClass('world_continents-map-head') ||
							minMaxButtonInner.getParent().hasClass('region-map-head') ||
							minMaxButtonInner.getParent().hasClass('office-map-head') ||
							minMaxButtonInner.getParent().hasClass('comhome-search-head') ||
							minMaxButtonInner.getParent().hasClass('comhome-news-themes-head') ||
							minMaxButtonInner.getParent().hasClass('comhome-bottom-head')					
							){
							//hide the background for headers
							minMaxButtonInner.getSiblings().setStyle('visibility','hidden');
							minMaxButtonInner.getParent().setStyle('background-image', 'none');
							if(!minMaxButtonInner.getParent().hasClass('comhome-search-head') && !minMaxButtonInner.getParent().hasClass('comhome-news-themes-head')){
								minMaxButtonInner.getParent().setStyle('border-bottom-width', '0px');
								minMaxButtonInner.getParent().setStyle('padding-bottom', '1px');
							}
						}

						//Show the minmax background
						minMaxButtonInner.getParent().getParent().getParent().setStyle('padding-right', '0px');
						minMaxButtonInner.getParent().getParent().setStyle('width', minMaxButtonInner.getParent().getParent().getParent().getWidth()+6);
						minMaxButtonInner.getParent().getParent().setStyle('height', minMaxButtonInner.getParent().getParent().getHeight());
						//minMaxButtonInner.getParent().getParent().setStyle('overflow', 'hidden');
						minMaxButtonInner.getParent().getSiblings().setStyle('overflow', 'hidden');
						minMaxButtonInner.setStyle('margin-right', '6px')
						minMaxButtonInner.getParent().setStyle('background-image', 'url(/App_Themes/Dhv/Img/button-minmax-background.png)');
						minMaxButtonInner.getParent().setStyle('background-repeat', 'no-repeat');
						minMaxButtonInner.getParent().setStyle('background-position', 'top right');
						minMaxButtonInner.getParent().setStyle('height', '28px');
					
						minMaxButtonInner.getParent().getParent('div').setStyle('background-color', 'transparent');
						minMaxButtonInner.getParent().getSiblings('div').setStyle('background-color', 'transparent');
						
						minMaxButtonInner.getParent().getParent().getParent().setStyle('background-image', 'none');
						$each(minMaxButtonInner.getParent().getParent().getParent().getSiblings()[0].getChildren(), function(obj, index){
							obj.setStyle('background-image', 'none');
						});
						minMaxButtonInner.addClass('minimized');
						
						//hide title
						if(pageTitleHolder != null){
							pageTitleHolder.setStyle('visibility', 'hidden');
						}
						
						//hide sidemenu foldables
						var sideMenuItems = $$('div.sidemenu-item');
						$each(sideMenuItems, function(sideMenuItem, index){
							App.showHideSideMenuFoldable(sideMenuItem, 'up');
						});

						if(App.initialMenuItemSetItem != null){
							App.initialMenuItemSet = false;							
							App.initialMenuItemSetItem.fireEvent('mouseleave');
						}
					}
				});
			});
			App.startResize();
		});
	},
	
	buildMainMenu: function(){
		var menuItems = $$('div#header-menu ul li.main');
		$each(menuItems, function(menuItem, index){
			if(menuItem.getChildren()[1] == null){
				menuItem.getChildren()[0].setStyle('background', 'none');
				menuItem.getChildren()[0].setStyle('padding-right', '0px');
			}
			menuItem.addEvent('mouseenter', function(e){
				e.stop();
				this.setStyle('background', 'transparent url(/App_Themes/Dhv/Img/header-menu-background-nosub-hover.png) top left no-repeat');
				this.getNext().setStyle('background', 'transparent url(..//App_Themes/Dhv/Img/header-menu-background-nosub-right.png) top left no-repeat');
				if(this.getChildren()[1] != null){
					this.getChildren()[0].addClass('collapsed');
					this.getChildren()[0].getNext('div.submenu').setStyle('display', 'block');
				}
				else{
					this.getChildren()[0].addClass('hovered');
				}
				App.updateMainMenuFont();
			});
			menuItem.addEvent('mouseleave', function(e){
				e.stop();
				this.setStyle('background', 'transparent url(/App_Themes/Dhv/Img/header-menu-background-nosub.png) top left no-repeat');
				this.getNext().setStyle('background', 'transparent url(..//App_Themes/Dhv/Img/header-menu-background-nosub.png) top left no-repeat');
				if(this.getChildren()[1] != null){
					this.getChildren()[0].removeClass('collapsed');
					this.getChildren()[0].getNext('div.submenu').setStyle('display', 'none');
				}
				else{
					this.getChildren()[0].removeClass('hovered');
				}				
				App.updateMainMenuFont();
			});			
		});
		var extraItem = new Element('li');
		extraItem.set('class', 'main');
		$$('div#header-menu ul')[0].appendChild(extraItem);
		App.updateMainMenuFont();
	},

	buildSiteselectMenu: function(){
		var menuItems = $$('div#siteselect div.selector ul li.main');
		$each(menuItems, function(menuItem, index){
			menuItem.addEvent('mouseenter', function(e){
				e.stop();
				this.setStyle('background', 'transparent url(/App_Themes/Dhv/Img/header-menu-background-nosub-hover.png) top left no-repeat');
				this.getChildren()[0].addClass('collapsed');
				this.getChildren()[0].getNext('div.submenu').setStyle('display', 'block');
			});
			menuItem.addEvent('mouseleave', function(e){
				e.stop();
				this.setStyle('background', 'transparent none');
				this.getChildren()[0].removeClass('collapsed');
				this.getChildren()[0].getNext('div.submenu').setStyle('display', 'none');
			});			
		});
	},	
	
	sideMenuFoldables: function() {
		var sideMenuItems = $$('div.sidemenu-item');
		$each(sideMenuItems, function(sideMenuItem, index){
			sideMenuItem.getChildren('div.head')[0].getChildren('a')[0].addEvent('click', function(){
				if(sideMenuItem.get('folded') == 'false'){
					sideMenuItem.set('foldedRemember', 'true');
					App.showHideSideMenuFoldable(this, 'up');
				}
				else{
					sideMenuItem.set('foldedRemember', 'false');
					App.showHideSideMenuFoldable(this, 'down');
				}
			}.bind(sideMenuItem));
			sideMenuItem.set('folded', 'false');
			sideMenuItem.set('foldedRemember', 'false');
		});
	},
	
	showHideSideMenuFoldable: function(sideMenuItem, direction){
		var arrowImageAnchor = sideMenuItem.getChildren('div.head')[0].getChildren('a')[0];	
		var arrowImage = arrowImageAnchor.getStyle('background-image');
		if(direction == 'up'){
			arrowImageAnchor.addClass('down');
			$each(sideMenuItem.getChildren('div'), function(contentItem, index){
				if(!contentItem.hasClass('head') && !contentItem.hasClass('sidemenu-item-spacer')){
					contentItem.setStyle('display', 'none');
				}
				if(contentItem.hasClass('sidemenu-item-spacer')){
					contentItem.setStyle('display', 'block');
				}	
			});	
			sideMenuItem.set('folded', 'true');
		}
		if(direction == 'down'){
			arrowImageAnchor.removeClass('down');
			$each(sideMenuItem.getChildren('div'), function(contentItem, index){
				if(!contentItem.hasClass('head') && !contentItem.hasClass('sidemenu-item-spacer')){
					contentItem.setStyle('display', 'block');
				}
				if(contentItem.hasClass('sidemenu-item-spacer')){
					contentItem.setStyle('display', 'none');
				}	
				
			});
			sideMenuItem.set('folded', 'false');	
		}
		App.startResize();	
	},
	
	recentProjectsNavigation: function(){
		var recentProjectsContainer = $$('div.content div.recent-projects')[0];
		if(recentProjectsContainer != null){
			var nextButton = $$('div.content div.recent-projects a.next-lnk')[0];
			var previousButton = $$('div.content div.recent-projects a.previous-lnk')[0];
			var currentPageSpan = $('currentProject');
			var currentProject = 1;
			var recentProjets = $$('div.content div.recent-projects ul li');
			var totalProjects = recentProjets.length;
			nextButton.addEvent('click', function(e){
				e.stop();
				currentProject++;
				currentProject > totalProjects ? currentProject = 1 : null;
				$each(recentProjets, function(recentProject){
					recentProject.setStyle('display', 'none');
				});
				recentProjets[currentProject-1].setStyle('display', 'block');
				currentPageSpan.set('text', currentProject);
			});
			previousButton.addEvent('click', function(e){
				e.stop();
				currentProject--;
				currentProject <= 0 ? currentProject = totalProjects : null;			
				$each(recentProjets, function(recentProject){
					recentProject.setStyle('display', 'none');
				});
				recentProjets[currentProject-1].setStyle('display', 'block');
				currentPageSpan.set('text', currentProject);
			});
		}
	},
	
	updateMainMenuFont: function() {
		//Cufon.replace('div#header-menu ul li a.main', { fontFamily: 'TheMix', hover: false });
	},
	
	updateNavigationFont: function() {
		Cufon.replace('div#navigation div#navigation-bar div#navigation-items a.sub', { fontFamily: 'TheSans', hover: true });
	},	
	
	siteSelect: function(country, url, website){
		
		this.overlay = new Element('div', { 'id':'overlay','styles':{ 'opacity':0,'visibility':'visible','height':0,'overflow':'hidden', 'z-index':'800' }}).inject($(document.body));
		this.overlay.setStyles({ 'top': -$(window).getScroll().y,'height':$(window).getScrollSize().y+$(window).getScroll().y });
		this.overlay.get('tween').addEvent('onComplete', function(e){
			e.stop();
			var ssCenter = new Element('div', {'id':'ssCenter', 'styles':{'width':406,'height':137,'marginLeft':-(398/2)-8, 'marginTop':-(130/2)-7, 'z-index':'850'}}).inject($(document.body));
			var ssCanvas = new Element('div', {'id':'ssCanvas'}).inject(ssCenter);

			var ssTitle = new Element('h1', {'id':'ssTitle'}).inject(ssCanvas);
			var ssClose = new Element('div', {'id':'ssClose'}).inject(ssCanvas);
			var ssInfo = new Element('div', {'id':'ssInfo'}).inject(ssCanvas);
			
			var ssStay = new Element('div', {'id':'ssStay'}).inject(ssCanvas);
			var ssInputStay = new Element('input', {'id':'ssInputStay', 'type':'radio', 'name':'ssChoice', 'value':'stay', 'checked':'true'}).inject(ssStay);
			var ssLabelStay = new Element('label', {'id':'ssLabelStay', 'for':'ssInputStay'}).inject(ssStay);
			
			var ssGo = new Element('div', {'id':'ssGo'}).inject(ssCanvas);
			var ssInputGo = new Element('input', {'id':'ssInputGo', 'type':'radio', 'name':'ssChoice', 'value':'go'}).inject(ssGo);
			var ssLabelGo = new Element('label', {'id':'ssLabelGo', 'for':'ssInputGo'}).inject(ssGo);
			
			var ssRemember = new Element('div', {'id':'ssRemember'}).inject(ssCanvas);
			var ssInputRemember = new Element('input', {'id':'ssInputRemember', 'type':'checkbox', 'name':'ssChoice', 'value':'go'}).inject(ssRemember);
			var ssLabelRemember = new Element('label', {'id':'ssLabelRemember', 'for':'ssInputRemember'}).inject(ssRemember);
			
			var ssContinue = new Element('div', {'id':'ssContinue'}).inject(ssCanvas);
			var ssContinueLink = new Element('a', {'id':'ssContinueLink'}).inject(ssContinue);
			
			ssTitle.set('html', 'Information');
			ssClose.set('html', 'x');
			ssInfo.set('html', 'We have noticed you are visiting DHV.com from '+country+'.<br />Do you want to change to DHV.nl for more specific content?');
			ssLabelStay.set('html', 'Stay at DHV.com');
			ssLabelGo.set('html', 'Goto '+website);
			ssLabelRemember.set('html', 'Remember my choice');
			ssContinueLink.set('html', 'Continue');
			
			Cufon.replace('div#ssCanvas h1#ssTitle', { fontFamily: 'TheSans', hover: true });
			Cufon.replace('div#ssCanvas div#ssContinue a', { fontFamily: 'TheSans', hover: true });

			//Click event for close button
			ssClose.addEvent('click', function(){
				this.overlay.dispose();
				ssCenter.dispose();
			}.bindWithEvent(this));
			
			//Click event for continue button
			ssContinueLink.addEvent('click', function(){
				if(ssInputStay.checked == true && ssInputRemember.checked == true){
					var dhvCookie  = Cookie.write(App.cookieName, 'true', {duration: App.cookieDuration});
				}
				if(ssInputGo.checked == true && ssInputRemember.checked == true){
					document.location.href = url;
					var dhvCookie  = Cookie.write(App.cookieName, 'false', {duration: App.cookieDuration});				
				}
				if(ssInputGo.checked == true && ssInputRemember.checked == false){
					document.location.href = url;				
				}				
				this.overlay.dispose();
				ssCenter.dispose();
			}.bindWithEvent(this));
		}.bindWithEvent(this));
		
		timeOut = function(){
			this.overlay.tween('opacity',this.ssOverlayOpacity);
		}.bind(this).delay(App.transDelay);
		
		window.addEvent('resize',function(){
			if(this.overlay.getStyle('opacity') == 0){ return; };//resize only if visible
			var scrollSize = $(window).getScrollSize().y;
			var scrollTop = $(window).getScroll().y;
			this.overlay.setStyles({ 'height':scrollSize+scrollTop,'top':-scrollTop });
		}.bindWithEvent(this));		
	},
	
	enableOverlay: function(fnComplete){
		if(!$chk(this.overlay)){
			this.overlay = new Element('div', { 'id':'overlay','styles':{ 'opacity':0,'visibility':'visible','height':0,'overflow':'hidden' }}).inject($(document.body));
			window.addEvent('resize',function(){
				if(this.overlay.getStyle('opacity') == 0){ return; };//resize only if visible
				var scrollSize = $(window).getScrollSize().y;
				var scrollTop = $(window).getScroll().y;
				this.overlay.setStyles({ 'height':scrollSize+scrollTop,'top':-scrollTop });
			}.bindWithEvent(this));
		}
		
		this.overlay.setStyles({ 'top': -$(window).getScroll().y,'height':$(window).getScrollSize().y+$(window).getScroll().y });
		this.overlay.tween('opacity',this.overlayOpacity).addEvent('onComplete', fnComplete);		
	},
	
	disableOverlay: function(){
		if($chk(this.overlay)){
			this.overlay.tween('opacity',0).addEvent('onComplete', function(){
				this.overlay.destroy();
				this.overlay = null;
			}.bindWithEvent(this));
		}
	},
	
	buildNavigation: function(){
		if($('wrapper') == null) return false;
		var menuItems = $$('div#navigation div#navigation-bar div#navigation-items ul li.main');
		var saveWrapperOriginalPosition = $('wrapper').getStyle('top').toInt();
		var saveWrapperPosition = null;
		var subSubFolded = false;
		
		$each(menuItems, function(menuItem, index){
			if(menuItem.getChildren('div.navigation-sub-items') != null && menuItem.getChildren('div.navigation-sub-items').length > 0){
				var subMenuDiv = menuItem.getChildren('div.navigation-sub-items')[0];
				var saveSubMenuDivMargin = null;
				subMenuDiv.set('id', 'subMenuDiv'+index);
				var timeOut = null;
				var subMenuAs =$$('div#subMenuDiv'+index+' a.sub');
				var subTimeout = null;
				var subSubDivs = $$('div#subMenuDiv'+index+' div.navigation-sub-sub-items');
				var subItems = $$('div#subMenuDiv'+index+' ul li.sub');
				
				//COLUMN LAYOUT
				if(subItems.length % 5 == 1) {
					//let's fix the lonelyness
					$each($$('div#subMenuDiv'+index+' ul li.sub:nth-child(5n)'), function(subItem5n){
						var emptySubItem = new Element('li');
						subItem5n.setStyle('clear','left');
						subItem5n.getParent().insertBefore(emptySubItem, subItem5n);
					});
					$each(subItems, function(subItem){
						var columnWidth = Math.floor(subItem.getWidth().toInt()*5/4) - subItem.getStyle('padding-right').toInt()
						subItem.setStyle('width', columnWidth);
					});
				}
				$each($$('li.subsub'), function(obj){
					obj.setStyle('width', obj.getParent().getParent().getParent().getSize().x - 16);
				});

				//SLIDING FUNCTIONALITY
				//Create the slider object
				var subMenuSlider = new Fx.Slide('subMenuDiv'+index, {
						mode: 'vertical',
						link: 'cancel',
						hideOverflow : true,
						duration: App.transDuration,
						transition: App.transEffect
				});
				
				//Correct submenu the position
				subMenuDiv.getParent().setStyles({'left':(menuItem.getPosition(menuItem.getParent()).x*-1), 'position': 'relative'});
				
				//Mouse enter/leave events (with delay)
				menuItem.addEvent('mouseenter', function(e){
					e.stop();
					//Save the margin, so that it can be restored later (when the subsub menu slides back, the sumenudiv margin top is set to high by the slider object).
					if(saveSubMenuDivMargin == null) saveSubMenuDivMargin = subMenuDiv.getStyle('margin-top');
					//delay the slidein
					timeOut = function(){
						//hide the initial item before entering the currently hovered item (because the App.initialMenuItemSet item never fired the mouseleave event)
						if(App.initialMenuItemSet == true && App.initialMenuItemSetItem != null && App.initialMenuItemSetItem != menuItem){
							App.initialMenuItemSet = false;
							App.initialMenuItemSetItem.fireEvent('mouseleave');
							var tempTimeOut = function(){
								subMenuSlider.slideIn();
								menuItem.setStyle('background-color', '#fff');
								menuItem.setStyle('background-image', 'url(/App_Themes/Dhv/Img/navigation-bar-devider-hover.png)');
								if(index > 0) menuItem.getChildren('a')[0].setStyle('background-image', 'none');
							}.delay(App.transDelay);
						}
						else{
							//show the submenu
							subMenuSlider.slideIn();
							menuItem.setStyle('background-color', '#fff');
							menuItem.setStyle('background-image', 'url(/App_Themes/Dhv/Img/navigation-bar-devider-hover.png)');
							if(index > 0) menuItem.getChildren('a')[0].setStyle('background-image', 'none');
						}
						//use delay
					}.delay(App.transDelay);
				});
				menuItem.addEvent('mouseleave', function(e){
					//clear the timeout (and thus never slide the item in if the delay time hasn't been reached)
					if(timeOut != null){
						$clear(timeOut);
						timeOut = null;
					}
					if(App.initialMenuItemSet == true && menuItem == App.initialMenuItemSetItem) return true;
					App.disableOverlay();
					var restoreDelay = function(){
						//restore the navigation and wrapper positions (used for sliding back when the subsubmenu is open)
						//$('navigation-wrapper').setStyle('left', '0px');
						$('navigation-wrapper').getChildren().setStyle('position', 'relative');
						$('wrapper').setStyle('top', saveWrapperOriginalPosition);
					}.delay(App.restoreDelayTime);
					
					var hoverDelay = function(){
						menuItem.setStyle('background-color', 'transparent');
						if(index > 0) menuItem.getChildren('a')[0].setStyle('background-image', 'url(/App_Themes/Dhv/Img/navigation-bar-devider-anchor.png)');						
					}.delay(App.hoverDuration);					
					
					//Slide out .. and...
					subMenuSlider.slideOut().chain(function(){
						//Hide the subsub items
						$each($$('div.navigation-sub-sub-items'), function(obj){
							obj.setStyle('display', 'none');
						});
						//Restore the link color
						$each(subMenuAs, function(obj){
							obj.removeClass('dhv-black-important');
						});	
						App.updateNavigationFont();
						
						//Restore the top margin (when the subsub menu slides back, the sumenudiv margin top is set to high bij de slider object).
						subMenuDiv.setStyle('margin-top', saveSubMenuDivMargin);
					});
				});

				//sub-sub items
				if(subSubDivs != null && subSubDivs.length > 0){
					var subSubMenuSliderRegister = new Array;
					$each(subSubDivs, function(subSubDiv, index){
						subMenuItem = subSubDiv.getSiblings('a')[0];
						subMenuItem.addEvent('mouseenter', function(e){
							e.stop();
							subTimeout = function(){
								App.initialMenuItemSet = false;
								var tempWrapperPosition = $('wrapper').getPosition().y;
								$('wrapper').setStyle('top', tempWrapperPosition);
								
								//$('navigation-wrapper').setStyle('left', $('navigation-wrapper').getPosition().x + 1);
								$('navigation-wrapper').getChildren().setStyle('position', 'absolute');
							
								App.enableOverlay();
								$each(subSubDivs, function(obj){
									obj.setStyle('display', 'block');
								});								
								App.updateNavigationFont();
								subMenuDiv.setStyle('padding-bottom','10px');
								subMenuSlider.slideIn();
							}.delay(App.transDelayLong);
						});
						subMenuItem.addEvent('mouseleave', function(e){
							e.stop();
							if(subTimeout != null){
								$clear(subTimeout);
								subTimeout = null;
								subMenuDiv.setStyle('padding-bottom','0');
							}						
						});
					});
				}
				
				//Hide all sub items
				subMenuSlider.hide();
				
				//Show the active item
				if(menuItem.hasClass('active')){
					App.initialMenuItemSet = true;
					App.initialMenuItemSetItem = menuItem;
					if(saveSubMenuDivMargin == null) saveSubMenuDivMargin = subMenuDiv.getStyle('margin-top');
					menuItem.setStyle('background-color', '#fff');
					menuItem.setStyle('background-image', 'url(/App_Themes/Dhv/Img/navigation-bar-devider-hover.png)');
					if(index > 0) menuItem.getChildren('a')[0].setStyle('background-image', 'none');					
					subMenuSlider.show();
				}
				
			}
			else{
				//hover the menuitems without subitems
				menuItem.addEvent('mouseenter', function(e){
					e.stop();
					timeOut = function(){
						if(App.initialMenuItemSet == true && App.initialMenuItemSetItem != null && App.initialMenuItemSetItem != menuItem){
							App.initialMenuItemSet = false;
							App.initialMenuItemSetItem.fireEvent('mouseleave');
							var tempTimeOut = function(){
								menuItem.setStyle('background-color', '#fff');
								menuItem.setStyle('background-image', 'url(/App_Themes/Dhv/Img/navigation-bar-devider-hover.png)');
								if(index > 0) menuItem.getChildren('a')[0].setStyle('background-image', 'none');
							}.delay(App.transDelay);
						}
						else{
							menuItem.setStyle('background-color', '#fff');
							menuItem.setStyle('background-image', 'url(/App_Themes/Dhv/Img/navigation-bar-devider-hover.png)');
							if(index > 0) menuItem.getChildren('a')[0].setStyle('background-image', 'none');
						}
					}.delay(App.transDelay);
				});
				menuItem.addEvent('mouseleave', function(e){
					e.stop();
					//clear the timeout (and thus never slide the item in if the delay time hasn't been reached)
					if(timeOut != null){
						$clear(timeOut);
						timeOut = null;
					}
					if(App.initialMenuItemSet == true && menuItem == App.initialMenuItemSetItem) return true;
					var hoverDelay = function(){
						menuItem.setStyle('background-color', 'transparent');
						if(index > 0) menuItem.getChildren('a')[0].setStyle('background-image', 'url(/App_Themes/Dhv/Img/navigation-bar-devider-anchor.png)');						
					}.delay(App.hoverDurationShort);	
				});		
			}
		});
		$('navigation-items').addEvent('mouseleave', function(e){
			e.stop();
			App.disableOverlay();
		});
	},
	
	foldingSubjects: function(){
		var timeOut = null;
		var subjectitmes = $$('div.sidemenu-subjects-block li');
		
		$each(subjectitmes, function(subjectitem){
			if(subjectitem.getChildren('div').length){
	
				//on mouse enter...
				subjectitem.addEvent('mouseenter', function(e){
					e.stop();
					timeOut = function(){
						subjectitem.getChildren('div').setStyle('display', 'block');
					//use delay
					}.delay(App.transDelay);
				});
				//on mouse leave
				subjectitem.addEvent('mouseleave', function(e){
					e.stop();
					if(timeOut != null){
						$clear(timeOut);
						timeOut = null;
					}	
					subjectitem.getChildren('div').setStyle('display', 'none');
				});	
			}
		});
	},
	
	startResize: function(){
		if($('supersize') == null) return false;
		moosizer = new mooSizer({ bgElement:'supersize' });
	},

	submitSearch: function(){
		window.location.href = "/search.aspx?q=" + $('search-input').value;
	},
	
	searchButtonEvents: function(){
		if($('search-input') != null){
			$('search-input').addEvent('keydown', function(e){
				if(e.key == 'enter') {
					this.submitSearch();
					e.preventDefault();
				}
			}.bindWithEvent(this));
		}
		if($('plc_lt_content_pageplaceholder_pageplaceholder_lt_zoneMain_DhvSearch_searchTextBox') != null){
			$('plc_lt_content_pageplaceholder_pageplaceholder_lt_zoneMain_DhvSearch_searchTextBox').addEvent('keydown', function(e){
				if(e.key == 'enter') $('form1').submit();
			}.bindWithEvent(this));
		}
	},
	
	searchFilter: function(filterValue){
		$each($$('div.pagetitle-search div.filters ul li input'), function (tickBox, index){
			var tickBoxImage = tickBox.getSiblings('img')[0];
			if(filterValue == 0){
				tickBoxImage.set('src', '/App_Themes/Dhv/Img/tickBox-searchfilter-off.png');
				tickBox.checked = false;
			}
			else{
				tickBoxImage.set('src', '/App_Themes/Dhv/Img/tickBox-searchfilter-on.png');
				tickBox.checked = true;						
			}			
		});
	}
}

if(console == null){
	var console = {
		log: function(value){
			//alert(value);
		}
	}
}