// filename: merz.js
// functions for the main navigation and other pages
// version 2.16 - 2004-10-13
// Distributed under the terms of the GNU Library General Public License
// author: martin dot kliehm at gpm dot de

//window.onerror = null


/////////////////////////
// page initialization //
/////////////////////////

function pageInit(section) {
	// initialize browsercheck library
	is = new BrowserCheck();
	// get screen attributes
	fenster = new screenObject();
	// initialize reload
	reloadPage(true);
	// center page
	if (is.dom && is.opera == false) centerPage();
	// set footer
	// if (is.dom && is.opera == false) setFooter();
	isHome = section;
	
	if((document.location.href.indexOf('merz-at') == -1) && (document.location.href.indexOf('co.at') == -1)){
		// init fontsize-change	
		if (is.dom) {		
			initFontchange("mainContent");
			// checks if there is a fontcookie and sets individual fontsize
			checkFontCookie();
		}
	}
	
	// hack to fix to IE6 text selection bug
	/*
	if (is.ie6up) {
		var containsNoForm = true;
		for (var i = 0; i < document.forms.length; i++) {
			if (document.forms[i].parentNode.id == 'mainContent') containsNoForm = false;
		}
		if (containsNoForm) {
			for (var i = 0; i < document.images.length; i++) {
				document.images[i].unselectable = "on";
				document.images[i].contentEditable = false;
			}
			for (var i = 0; i < document.anchors.length; i++) {
				document.anchors[i].contentEditable = false;
			}
			document.onmousedown = fixIE6Text;
			if (containsNoForm) document.getElementById('mainContent').contentEditable = true;
		}		
	}
	*/
	
	// fix for mac / mainnavigation
	if ((is.mac)) {
		if(document.getElementById('navHauptWorld')){
			document.getElementById('navHauptWorld').style.fontSize = 10+"px";
		}
		if(document.getElementById('navHauptLangLink')){
			document.getElementById('navHauptLangLink').style.fontSize = 10+"px";
		}
		for(var i = 0; i < 10; i++){
			if(document.getElementById('navHaupt'+i)){
				document.getElementById('navHaupt'+i).style.fontSize = 10+"px";
			}
		}
	}
	
	// mark current category
	compareURL('langAnchor', 'navSubLink bold')
	compareURL('subNavAnchor2', 'navSubLinkActive')
	compareURL('subNavAnchor3', 'navSubLinkActive')
	compareURL('subNavAnchor4', 'navSubLinkActive')
	
	// display Netscape 4 disclaimer
	if (section == 'home' && is.ns4) windowOpener('meta/netscape4/index.html',350,200,'netscape4',100,100,'yes','no','no','no','yes','no','no','no','no');
}
var isHome;

function copyText() {
	if (is.ie6up) {
		var range = document.body.createTextRange();
		var footerRange = document.body.createTextRange();
		
		footerRange.moveToElementText(document.getElementById('footerDiv'));
		range.moveToElementText(document.getElementById('mainContent'));
		
		range.setEndPoint("EndToStart", footerRange);
		range.execCommand("Copy", false, false);
		//alert(range.text);
	}
	/*
	else if (document.createRange) {
		var range = document.createRange();
		range.selectNode(document.getElementById('mainContent'));
		range.setEndBefore(document.getElementById('footerDiv'));
		document.getElementById('copyFrame').contentDocument.designMode = "on";
		document.getElementById('copyFrame').contentDocument.innerHTML = range;
		document.getElementById('copyFrame').contentDocument.execCommand("selectall", false, false);
		document.getElementById('copyFrame').contentDocument.execCommand("copy", false, false);
	}
	*/
}


//////////////////////////////////////
// positioning of right column and  //
// marking of navigational elements //
//////////////////////////////////////

// center page
function centerPage() {
	var leftPos = parseInt((fenster.visWidth - parentLayerWidth) / 2);
	if (leftPos <= 15) leftPos = 15;
	document.getElementById('parentLayer').style.left = leftPos + 'px';
	document.getElementById('parentLayer').style.marginLeft = '0px';
	// adjust top position of navigation in WinIE6 // changed pixel values 2003-10-01 by mak
	if (is.win) {
		if (is.ie6up) { // changed 2004-06-08 by mak
			document.getElementById('navHaupt').style.top = '78px';
			//document.getElementById('navHauptLang').style.top = '3px';
			}
		else if (is.ie55) { // changed 2004-06-08 by mak
			document.getElementById('navHaupt').style.top = '82px';
			//document.getElementById('navHauptLang').style.top = '-1px';
			}
		else if (is.ie5) {
			document.getElementById('navHaupt').style.top = '84px';
			//document.getElementById('navHauptLang').style.top = '-3px';
			}
	}
}
var parentLayerWidth = 770;

// set footer
function setFooter() {
	// get content height
	var contentHeight = (is.ie5up) ? document.getElementById('mainContent').offsetHeight : parseInt(getComputedStyle(document.getElementById('mainContent'), null).getPropertyValue('height'));
	// get height of left column
	var leftHeight = (is.ie5up) ? document.getElementById('navSub').offsetHeight : parseInt(getComputedStyle(document.getElementById('navSub'), null).getPropertyValue('height'));
	
	// compare height of content and left column; add 55 for difference of y-pos
	if (contentHeight + 55 >= leftHeight) {
		var objHeight = contentHeight;	// set object height
		var offsetHeight = (is.ie5up) ? document.getElementById('mainContent').offsetTop : parseInt(getComputedStyle(document.getElementById('mainContent'), null).getPropertyValue('top')); // get offset height (top position of element)
		if (offsetHeight + objHeight < footerMinYPos) objHeight = footerMinYPos - offsetHeight; // set minimum height to prevent collision with left column
		document.getElementById('navSub').style.height = (is.ie5up) ? offsetHeight - document.getElementById('navSub').offsetTop + objHeight - 25 : (offsetHeight - parseInt(getComputedStyle(document.getElementById('navSub'), null).getPropertyValue('top')) + objHeight - 25) + 'px'; // resize left column to total size of content; take top-padding (25px) into account
	}
	else {
		var objHeight = (is.ie5up) ? leftHeight : leftHeight + 25; // set object height
		var offsetHeight = (is.ie5up) ? document.getElementById('navSub').offsetTop : parseInt(getComputedStyle(document.getElementById('navSub'), null).getPropertyValue('top')); // get offset height (top position of element)
		if (offsetHeight + objHeight < footerMinYPos) objHeight = footerMinYPos - offsetHeight; // set minimum height to prevent collision with left column
	}
	// resize right line
	document.getElementById('rightLine').style.height = (is.ie5up) ? offsetHeight - document.getElementById('rightLine').offsetTop + objHeight + 2 : (offsetHeight - parseInt(getComputedStyle(document.getElementById('rightLine'), null).getPropertyValue('top')) + objHeight + 2) + 'px';
	// position and show lower bar
	document.getElementById('footerDiv').style.top = (is.ie5up) ? parseInt(document.getElementById('footerDiv').offsetTop) + offsetHeight + objHeight + 2 : (offsetHeight + objHeight + 2) + 'px';
	document.getElementById('footerDiv').style.visibility = 'visible';
	//document.getElementById('footerDiv').style.display = 'block';
}
var footerMinYPos = 450;


// compareURL() compares the actual location URL with navigation link URLs to mark them // added parameters 2003-10-01 by mak
function compareURL(anchorName, className) {
	var navMarker = false
	var navMarkerCorrection = 0
	
	// compare main navigation links in DOM browsers
	if (is.dom || is.ie4up) {
		// parse all links
		for (var j = 0; j < document.anchors.length; j++) {
			// ignore anchors without proper name or if current level has been reached  // added navMarkerStop 2003-12-03 by mak
			if (navMarkerStop == false && document.anchors[j].id.indexOf(anchorName) != -1) {
				// alert(document.anchors[j].id)
				// navMarkerCorrection++;
				// compare link URL with location URL, get number of matching element
				comparedHref = (document.location.search != '') ? document.location.href.substr(0,document.location.href.indexOf('?')) + document.location.search.substr(document.location.search.indexOf('=') + 1, document.location.search.length) + '/' : document.location.href;
				if (comparedHref.indexOf(document.anchors[j].href) != -1) {
					navMarker = document.anchors[j].id // j + 1 - navMarkerCorrection;
					// set stop-marker if current level has been reached, search no further // added 2003-12-03 by mak
					if (comparedHref == document.anchors[j].href) navMarkerStop = true
					break;
				}
			}
		}
		// alert(navMarker);
		// mark matched link item
		if (navMarker) {
			if (is.ie) document.getElementById(navMarker).className = className // msover(navMarker,'nav');
			else document.getElementById(navMarker).setAttribute('class', className) // msover(navMarker,'nav');
			// mark match link's parent list item // added 2003-12-03 by mak
			if (document.getElementById(navMarker).parentNode) {
				// get parentNode object
				var parent = document.getElementById(navMarker).parentNode;
				// list item is anchor's grandparent
				var parentLI = parent.parentNode;
				// get list item's class attribute
				var parentClass = (is.ie) ? parentLI.className : parentLI.getAttribute('class');
				// change only list item which's class includes listArrowS
				if (parentClass.indexOf('listArrowS') != -1) {
					var classStringBefore = parentClass.substring(0, parentClass.indexOf('listArrowS'));
					var classStringAfter = parentClass.substring(parentClass.indexOf('listArrowS') + 10, parentClass.length);
					// set list item's class attribute
					if (is.ie) parentLI.className = classStringBefore + 'listArrowSAct' + classStringAfter;
					else parentLI.setAttribute('class', classStringBefore + 'listArrowSAct' + classStringAfter);
				}
			}
		}
	}
}
var navMarker = false
var navMarkerCorrection = 0
var navMarkerStop = false

// kill select lists in IE6 // added by mak 2004-06-18
// replaces select lists with input fields which are not in front of the the DHTML menues
function killSelectLists() {
	if (is.ie) {
		selects = document.getElementsByTagName('select');
		// hack for height bug on homepage
		var selectAdjustment = (document.location.href.indexOf('merz.de') != -1);
		for (var i = 0; i < selects.length; i++) {
			var obj = selects[i];
			// get option text, width, height, and class name
			var value = obj.options[obj.selectedIndex].innerHTML;
			// hack for height bug on homepage
			var width = (selectAdjustment) ? obj.offsetWidth : obj.offsetWidth;
			var height = (selectAdjustment) ? obj.offsetHeight : obj.offsetHeight;
			var className = obj.className;
			
			// create input text field
			selectInput = document.createElement('input');
			selectInput.type = 'text';
			selectInput.name = 'dummy' + i;
			selectInput.id = 'select' + i;
			// use select list attributes
			selectInput.style.width = (is.ie6up) ? width  : width;
			selectInput.style.height = (is.ie6up) ? height - 1 : height - 1;
			selectInput.style.padding = (is.ie6up) ? '2px 4px 0 4px' : '1px 3px 0 3px';
			selectInput.style.marginTop = '-1px';
			// if (isHome) selectInput.style.marginBottom = '4px';
			if (is.ie6up) selectInput.style.borderColor = '#7f9db9';
			else {
				selectInput.style.borderStyle = 'inset';
				selectInput.style.borderWidth = '2px 2px 1px 2px';
			}
			selectInput.className = className;
			selectInput.value = value;
			
			// hide select list
			obj.style.display = 'none';
			// append text input
			myParent = obj.parentNode;
			myParent.appendChild(selectInput, obj);
		}
	}
}
// show select lists again
function showSelectLists() {
	if (is.ie) {
		selects = document.getElementsByTagName('select');
		for (var i = 0; i < selects.length; i++) {
			obj = selects[i];
			myParent = obj.parentNode;
			if (document.getElementById('select' + i)) {
				// remove text input fields
				throwawayNode = myParent.removeChild(document.getElementById('select' + i));
				// display select lists
				obj.style.display = 'inline';
			}
		}
	}
}


///////////////
// ROLLOVERS //
///////////////

// mouseOver above select lists, display submenu // added positioning of dynamic subnavigation 2003-10-23 by mak
function msOverNav(subnav, staywhereyouare) {
	var hauptnav = 'navHaupt' + subnav.substr(subnav.length - 1, 1)
	if (staywhereyouare) var subnavLeft = 0; // added 2003-12-16 by mak
	else if (document.getElementById(hauptnav)) {
		var subnavLeft = parseInt(document.getElementById('navHaupt').offsetLeft) + parseInt(document.getElementById('navHauptNav').offsetLeft) + parseInt(document.getElementById(hauptnav).offsetLeft) - 6 // 7px is the width of the pipe character plus space, 1px is the space left of the pipe character
	}
	if (is.ie && document.all[subnav]) {
		document.all[subnav].style.visibility = "visible";
		document.all[subnav].style.left = subnavLeft + 'px';
	}
	else if (is.dom && document.getElementById(subnav)) {
		document.getElementById(subnav).style.visibility = "visible"
		document.getElementById(subnav).style.left = subnavLeft + 'px'
	}
	else if (is.ns4 && document[subnav]) document[subnav].visibility = "visible"
	
	// kill all select lists in IE
	if (is.ie && !is.mac) killSelectLists();
}

// mouseOut (click anywhere on the page), hide all submenus
function msOutNav() {
	// hide submenus
	for (var i = 0; i < 10; i++) {
		if (is.ie && document.all["navDyn" + i]) document.all["navDyn" + i].style.visibility = "hidden"
		else if (is.dom && document.getElementById("navDyn" + i)) document.getElementById("navDyn" + i).style.visibility = "hidden"
		else if (is.ns4 && document["navDyn" + i]) document["navDyn" + i].visibility = "hidden"
	}

	// show all select lists in IE
	if (is.ie && !is.mac) showSelectLists();
}

// hide all subnavigation layers on a click anywhere on the page
if (is.ns4) document.captureEvents(Event.MOUSEUP)
if (msOutNav) document.onmouseup = msOutNav


/////////////////////
// image functions //
/////////////////////

// preload images
if (document.images) {
	var normal = new makeArray(4);
	var over = new makeArray(4);
	
	//normal[1].src = 'skin_img/gen_icon_fontsize_larger_no.gif';
	//normal[2].src = 'skin_img/gen_icon_fontsize_smaller_no.gif';
	//normal[3].src = 'skin_img/gen_icon_print_no.gif';
	//normal[4].src = 'skin_img/gen_icon_mailtofriend_no.gif';
	
	//over[1].src = 'skin_img/gen_icon_fontsize_larger_ms.gif';
	//over[2].src = 'skin_img/gen_icon_fontsize_smaller_ms.gif';
	//over[3].src = 'skin_img/gen_icon_print_ms.gif';
	//over[4].src = 'skin_img/gen_icon_mailtofriend_ms.gif';
}
	
// rollover functions
function msover(imgNum,obj) {
	if (document.images && is.min) {
		if (is.dom) document.getElementById(obj + imgNum).src = over[imgNum].src;
	}
}
function msout(imgNum,obj) {
	if (document.images && is.min && imgNum != navMarker) {
		if (is.dom) document.getElementById(obj + imgNum).src = normal[imgNum].src;
	}
}
	
// makeArray() - make new arrays with image objects
function makeArray(n) {
	this.length = n + 1;
	for (var i = 1; i<=n; i++) {
		this[i] = new Image();
		}
	return this;
}


//////////////////////////////////////
// go to page of products - merz.de //
/////////////////////////////////////

function gotoProductpage(){

top.location.href = document.getElementById("productlistselect").options[document.getElementById("productlistselect").selectedIndex].value;
//alert(document.getElementById("productlistselect").options[document.getElementById("productlistselect").selectedIndex].value)

}

/////////////////////////////////////////////
////////// functions for font-change ////////
/////////////////////////////////////////////

// global vars for changing of fontsize
var globalFontchange = 0; // difference to default
var contentObj; // contentlayer obj
var ratiofontsize = new Array();
var fontTag = new Array();
	fontTag[0]="p"; ratiofontsize[0]=18/12;
	fontTag[1]="h1"; ratiofontsize[1]=21/18;
	fontTag[2]="h2"; ratiofontsize[2]=18/13;
	fontTag[3]="h3"; ratiofontsize[3]=18/13;
	fontTag[4]="a"; ratiofontsize[4]=18/12;
	fontTag[5]="li"; ratiofontsize[5]=18/12;
	fontTag[6]="address"; ratiofontsize[6]=18/12;
	fontTag[7]="div"; ratiofontsize[7]=18/12;
	
function initFontchange(){
	contentObj = document.getElementById("mainContent");
}
	
function changeFontsize(num){

	if (!is.opera && !is.safari) { // so far not working on safari and opera
		globalFontchange+=num;
		
		if (globalFontchange < -5) {globalFontchange = -5; num = 0;}
		else if (globalFontchange >10) {globalFontchange = 10; num = 0;}
		
		var newfontsize=0;
		var helpobj;
		for (var i=0; i < fontTag.length; i++){
			for (var j=0; j < contentObj.getElementsByTagName(fontTag[i]).length; j++){
				if (is.ie) newfontsize = parseInt(contentObj.getElementsByTagName(fontTag[i])[j].currentStyle.fontSize);
				else newfontsize = parseInt(document.defaultView.getComputedStyle(contentObj.getElementsByTagName(fontTag[i])[j],"").getPropertyValue("font-size"));
				newfontsize+=num;
				contentObj.getElementsByTagName(fontTag[i])[j].style.fontSize = newfontsize+"px";// set new fontsize
				contentObj.getElementsByTagName(fontTag[i])[j].style.lineHeight = Math.round(newfontsize*ratiofontsize[i])+"px";// set new line-height
				
				
			}
		}
		SetCookie("merzfontsize",globalFontchange,"/");
	}
}

function checkFontCookie(){
	if (document.cookie) {
		var userfontsize = GetCookie("merzfontsize");
		if ((isNaN(userfontsize) != true) && (userfontsize != null)) {
			userfontsize = Math.round(userfontsize);
			changeFontsize(userfontsize);
		}
	}	
	if ((!document.cookie) ||(isNaN(userfontsize) == true) || (userfontsize == null)) {
			userfontsize=0;
			SetCookie("merzfontsize",userfontsize,"/");
			changeFontsize(userfontsize);
		}
}

function clearFontsize(){
	changeFontsize( -1 * globalFontchange);
}
	
	

	
// ############# cookie-handling ############

var today = new Date();
var expiry = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000);

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) {endstr = document.cookie.length; }
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) {
			return getCookieVal (j);
			}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}

function DeleteCookie (name,path,domain) {
	if (GetCookie(name)) {
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-80 00:00:01 GMT";
	}
}

function SetCookie (name,value,path,expires,domain,secure) {
	//Heute plus ein jahr
	var expireNextYear = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000);
	
	document.cookie = name + "=" + escape (value) +
		
		//cookie laeuft in genau einem Jahr ab
		"; expires=" + expireNextYear +
		
		//Originalangabe die angeblich auf dem uebergebenen expires Wert basiert
		//((expires) ? "; expires=" + expires.toGMTString() : "") +
		
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure=" + secure : "");
}

// ##########################################
	
	
/////////////////////////////
// other helpful functions //
/////////////////////////////

function showlayerbyid(derlayer) {
	if (document.getElementById) {
		document.getElementById(derlayer).style.display = "block";
	}
}

function hidelayerbyid(derlayer) {
	if (document.getElementById) {
		document.getElementById(derlayer).style.display = "none";
	}
}



// change the URL of the link for new user registration // added 2004-02-10 by mak
function changeRegLink(type) {
	// check if link with ID exists
	if (document.getElementById('regLink')) {
		// remember original link
		origRegLink = (changeRegLinkCall == 0) ? document.getElementById('regLink').href : origRegLink;
		// get document language
		var docLanguage = document.getElementsByTagName('html')[0].lang;
		switch(type) {
			// Merz registration
			case 'merz':
				document.getElementById('regLink').href = origRegLink;
				document.getElementById('regLink').target = "_self";
			break;
			
			// DocCheck registration
			case 'doccheck':
				switch(docLanguage) {
					case 'en': document.getElementById('regLink').href = 'https://www.doccheck.com/password/'; break;
					default: document.getElementById('regLink').href = 'http://www2.doccheck.com/de/passwort/antrag/alle.php'; break;
				}
				document.getElementById('regLink').target = "_blank";
			break;
		}
		changeRegLinkCall++;
	}
}
var origRegLink = false;
var changeRegLinkCall = 0;

// reloadPage() causes NS4 to reload on resize due to known Netscape resize bug,
// positions bottom bar on the bottom screen edge in NS6 and IE4+
function reloadPage(init) {
	// initialize function on first call
	if (init == true) {
		// set resize event
		window.onresize = reloadPage;}

	// do something if window size has changed
	else if (window.innerWidth != fenster.visWidth || window.innerHeight != fenster.visHeight) {
		// re-center
		if (is.dom) {
			fenster = new screenObject();
			centerPage();}
		// reload page in NS4
		else if (is.ns4) {
			window.location.reload();}
	}
}
	
// screenObject() creates universally accessible screen attribute objects
function screenObject() {
	// bottom edge
	if (window.pageYOffset) {
		this.bottom = window.pageYOffset;}
	else if (document.body && document.body.scrollHeight) {
		this.bottom = document.body.scrollHeight;}
	else {this.bottom = false;}

	// height	
	if (window.outerHeight) {
		this.height = window.outerHeight;}
	else if (document.body && document.body.offsetHeight) {
		this.height = document.body.offsetHeight;}
	else {this.height = false;}
		
	// visible height
	if (window.innerHeight) {
		this.visHeight = window.innerHeight;}
	else if (document.body && document.body.clientHeight) {
		this.visHeight = document.body.clientHeight;}
	else {this.visHeight = false;}
		
	// width
	if (window.outerWidth) {
		this.width = window.outerWidth;}
	else if (document.body && document.body.offsetWidth) {
		this.width = document.body.offsetWidth;}
	else {this.width = false;}
		
	// visible width
	if (window.innerWidth) {
		this.visWidth = window.innerWidth;}
	else if (document.body && document.body.clientWidth) {
		this.visWidth = document.body.clientWidth;}
	else {this.visWidth = false;}
		
	// scroll position
	if (window.pageYOffset) {
		this.scrollTop = window.pageYOffset;}
	else if (document.body && document.body.scrollTop) {
		this.scrollTop = document.body.scrollTop;}
	else {this.scrollTop = 0}
}

// seite drucken
function printWindow() {
	if (window.print) window.print();
}

// open pop-up window
function windowOpener(winUrl,winWidth,winHeight,winName,winX,winY,winScrollbars,winLocation,winStatus,winPersonalbar,winResizable,winToolbar,winMenubar,winDependent,winDirectories) {
	var windowName = ""
	// set defaults
	if (!winWidth) winWidth = 770
	if (!winHeight) winHeight = 550
	if (!winName) winName = 'popupWindow'
	if (!winX) winX = 10
	if (!winY) winY = 10
	if (!winScrollbars) winScrollbars = "yes"
	if (!winLocation) winLocation = "no"
	if (!winStatus) winStatus = "no"
	if (!winPersonalbar) winPersonalbar = "no"
	if (!winResizable) winResizable = "no"
	if (!winToolbar) winToolbar = "no"
	if (!winMenubar) winMenubar = "yes"
	if (!winDependent) winDependent = "no"
	if (!winDirectories) winDirectories = "no"
	// open pop-up window
	eval(winName +  '= window.open(winUrl,winName,"width=' + winWidth + ",height=" + winHeight + ",left=" + winX + ",top=" + winY + ",screenX=" + winX + ",screenY=" + winY + ",scrollbars=" + winScrollbars + ",location=" + winLocation + ",status=" + winStatus + ",personalbar=" + winPersonalbar + ",resizable=" + winResizable + ",toolbar=" + winToolbar + ",menubar=" + winMenubar + ",dependent=" + winDependent + ",directories=" + winDirectories + '")')
	eval(winName).focus()
}
