
window.onload 			= initialize;

// perform onLoad actions
function initialize() { 
	redirectBrowsers();				
	positionToolbar();			 
	setNaviBg(); 	
	stretchColumnSeparator(); 	
	setStyleSheet();	
	setSubMenuHeight();			
	showSearchOnSearchPage();			
	getPopupLink();		
	initMap();		
	initVideo();	
}

function initVideo(){
	if (getElement('clipNameMetaTag')){
		getElement('videoObject').Source = getElement('clipNameMetaTag').content;
		getElement('videoObject').DoPlay();
	}
}



//Browseres older than ie5.5 and from other distributores than Ms is redirected	
function redirectBrowsers(){
//|| navigator.userAgent.indexOf("5.01") > 0
	if(navigator.userAgent.indexOf("MSIE") <= 0 ){
		var sLang = document.location.toString()
		if (sLang.indexOf('/da-DK/') > 0) {
			sUrl = '/da-DK/BrowserMessage'
		}else if (sLang.indexOf('/en-GB/') > 0) {
			sUrl = '/en-GB/BrowserMessage'
		}
			location.href = sUrl;
	}
}
//Initializes map om WhereToFindUs page
function initMap(){
	if(getElement('mapLeft')){
		getLang()
		getDataMap()
		window.w_division = getElement('headElements_danexportDivision').content.toLowerCase();
		window.w_map = oWorld.continent[ 0 ].id;
		renderMap ();	
		renderSelectArea ();
		renderHeadoffice ();
	}
}


function NewWindow (mypage, myname, w, h) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+', scrollbars=no, resizable=yes'
	if (myname == ''){ myname = '_blank'}
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}



// converts targets on videolinks 	
function getPopupLink(){
	var i;
	var videoLink;
	var currUrl;
	if(getElement('mainSection')){
		var elementA = getElement('mainSection').getElementsByTagName('a');
		for (i=0; i < elementA.length; i++ ){
			if (getElement('mainSection').getElementsByTagName('a')[i].target == 'video'){
				videoLink = getElement('mainSection').getElementsByTagName('a')[i]
				currUrl = videoLink.href;
				videoLink.attachEvent("onclick", new Function ( 'NewWindow ("' + currUrl + '", "video", 690, 288)')); 
			}
		}
	}
}	
//Displays Productimages on mouseover 
function showImage(URL){
	var imageContainer = getElement('imageContainer');
	var topSectionHeight = getElement('mainTop').offsetHeight + 190;
	var imageContainerPosition = imageContainer.offsetTop
	var WindowTop = document.body.scrollTop
	var productImage = document.createElement('img');
	
	while ( imageContainer.hasChildNodes() ) imageContainer.removeChild ( imageContainer.firstChild );
	imageContainer.style.width = 245 + 'px';
	if ( WindowTop > topSectionHeight ){
		imageContainer.style.position = 'absolute'
		imageContainer.style.top = WindowTop + 90 + 'px'
	}else{
		imageContainer.style.top = topSectionHeight + 'px'
	}
	
	
	productImage.src = URL;
	imageContainer.appendChild(productImage);
}

//Sets submenu background image
function setNaviBg(){
	if(getElement('leftNavigation') || getElement('leftNavigationContainer')){
		window.w_division = getElement('headElements_danexportDivision').content.toLowerCase();
		document.body.className = 'catalog' + w_division;	
	}
}




//Sets submenuHeight
function setSubMenuHeight(){
	if(getElement('submenu')){
		var menuHeight = getElement('submenu').offsetHeight;
		var maxElements = 7;
		var elementHeight = 0;
		var countChildNodes = getElement('submenu').getElementsByTagName('a').length
		var aElementHeight = new Array()
		if (countChildNodes > 0){
			var countVisibleItems = (countChildNodes < maxElements)? countChildNodes : maxElements; 
			for(var i = 0; i < countChildNodes;i++) aElementHeight[i] = getElement('submenu').getElementsByTagName('a')[i].offsetHeight 
			for ( j=0; j < countVisibleItems; j++ ) elementHeight += (aElementHeight[j]+1)
			getElement('submenu').style.height = (countChildNodes <= maxElements) ?  elementHeight : elementHeight; + 'px';					
		}
		else{
			getElement('submenu').style.display = 'none';
		}
	}
}

//displays columnseparator in full length
function stretchColumnSeparator () {
	if (getElement ("columnSeparator") && getElement('mainSection')){	
		var mainHeight = getElement('mainSection').offsetHeight
		getElement ("columnSeparator").style.height = mainHeight + 100 + "px";
	}	
}
//moves toolbar to placeholder inside MainStage
function positionToolbar(){
	if (getElement('toolbarContainer') && getElement ("toolBarDestination") ){
		var oToolbarContainer = getElement('toolbarContainer').firstChild.cloneNode( true )
		getElement('toolBarDestination').appendChild(oToolbarContainer)
	}
}
//Change stylesheet on reload		
function setStyleSheet() {
	if (getWindowWidth () < 400 || navigator.userAgent.indexOf("Netscape")>= 0){
		for ( i = 0; i < document.styleSheets.length; i++ ) {
			if ( document.styleSheets[i].title ) {
				document.styleSheets[i].disabled = true;
			}
		}
	}
	else{ document.styleSheets[0].disabled = false;}
}
//Selectbox visibility on show/hide layers.
function displaySelectBox(sState){
	window.selectBoxes = getElement("mainStage").getElementsByTagName ( "select" );
	for ( i=0; i<selectBoxes.length; i++ ) {
		selectBoxes [i].style.visibility = (sState == true ) ? "hidden" : "visible";
	}
}
//moves searchControl to contentarea on searchpage.
function showSearchOnSearchPage(){
	if (getElement('searchResult')){
		getElement('searchDestination').appendChild(getElement('searchContainer').firstChild)
	}	
}
//Searchlayer visibility, when toolbar is clicked
function showSearch(){
	var isSearchPage = (getElement('searchResult')) ? getElement('searchResult').content : null;
	if(getElement('searchContainer')){
		var oSearchContainer = getElement('searchContainer');
		if(oSearchContainer.style.display == 'block'){
			oSearchContainer.style.display = 'none';			
			displaySelectBox(false)
		}else{
			oSearchContainer.style.position = 'absolute';
			oSearchContainer.style.top = 150 + 'px';
			oSearchContainer.style.left = 458 + 'px';					
			oSearchContainer.style.display = 'block';
			displaySelectBox(true)
		}
	}

}
//table visibility on ProteinMyProducts
function collapseMyProducts(oContainer, element){
	element.src = (element.src.indexOf("Plus") > 0) ? "/ui/images/icoMinus.gif" : "/ui/images/icoPlus.gif";  
	oContainer.style.display = (!oContainer.style.display || oContainer.style.display == 'none' ) ? 'block' : 'none' ;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Interface tools //////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function NewWindow (mypage, myname, w, h) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+', scrollbars=yes, resizable=yes'
	if (myname == ''){ myname = '_blank'}
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}
function getWindowWidth () { // returns the width of the window
		return window.innerWidth ? window.innerWidth : document.body.clientWidth;
}
function getWindowHeight () { // returns the width of the window
		return window.innerHeight ? window.innerHeight : document.body.clientHeight;
}
function getElement ( element ) { // return object reference
	if ( document.all ) return document.all [element];
	else return document.getElementById (element);
}
function getTags ( tagType ) { // return element collection
	if ( document.getElementsByTagName ) return document.getElementsByTagName ( tagType );
	if ( document.all && !document.getElementsByTagName ) return document.all.tags ( tagType );
}
function getStyle ( element ) { // similar functions; trendyer syntax
	return Element (element).style;
}
function getParent ( object ) { // return object parentNode
	return document.all ? object.offsetParent : object.parentNode;
}