var thisURL = location.href,
	thisRef = document.referrer,
	fadeInHero = true;

if (thisURL.indexOf("referenzen") != -1 && thisRef.indexOf("referenzen") != -1)
	{
	document.write('<sty' + 'le>#theHero { opacity:1.0; filter:alpha(opacity=100); }</st' + 'yle>');
	fadeInHero = false;
	}
	
	
//-------------------------------------------------------------------------------------------------------------


function get_scroll(obj)
	{
	if (!obj) return "";
	var pageScroll = obj.pageYOffset;
	if (typeof pageScroll == "undefined") pageScroll = obj.scrollTop;
	if (typeof pageScroll == "undefined") return 0;
	else return pageScroll;
	}
	
//-------------------------------------------------------------------------------------------------------------

	
function init_page()
	{
	// Prevent links from displaying a border
	allLinks = document.getElementsByTagName("a");
	linkCount = allLinks.length;
	for (i=0; i<linkCount; i++)
		{
		//allLinks[i].onmouseup = function() { this.blur(); };
		}
	}
	
	
//-------------------------------------------------------------------------------------------------------------


function set_dynamic_styles(styleString)
	{
	/*
	Looks for a style tag with id="dynamicStyles"
	If not found this tag will be created
	The style information (styleString) will then be inserted into this tag
	*/
	var ds = dom_element("dynamicStyles");
	if (ds == null)
		{
		ds = document.createElement('style');
		ds.setAttribute("type", "text/css");
		ds.setAttribute("id", "dynamicStyles");
		var hd = document.getElementsByTagName('head')[0];
		hd.appendChild(ds);
		}
	if(ds.styleSheet) ds.styleSheet.cssText = styleString;
	else ds.textContent = styleString;
	}
	
	
function adjust_page_layout()
	{
	// Get the size of the inner browser window
	if (document.body) { visibleAreaX = document.body.offsetWidth; visibleAreaY = document.body.offsetHeight; } // Should work on all browsers
	else if (window.innerWidth) { visibleAreaX = window.innerWidth; visibleAreaY = window.innerHeight; } // just to make sure
	else return false;
	
	dynStyles = "";	
	
	// Header and footer not fixed on tablets and smartphones
	if ($user.smartphone || $user.iphone)
		{
		dynStyles += "#theHeader { position:absolute; } ";
		dynStyles += "#theFooter { position:absolute; top:" + object("thePage").offsetHeight + "px; } ";
		}
	// Adjust the page margins
	marginX = Math.floor(visibleAreaX * 0.03);
	marginBottom = Math.floor(visibleAreaX * 0.04);
	dynStyles += "#theContent { margin: " + marginX  + "px 0 " + marginBottom + "px " + marginX  + "px; } ";
	dynStyles += "#headerLeft { padding-left:" + marginX  + "px; } ";
	dynStyles += "#headerRight { padding-right:" + marginX  + "px; } ";
	dynStyles += "#footerLeft { padding-left:" + marginX  + "px; } ";
	dynStyles += "#footerRight { padding-right:" + marginX  + "px; } ";
	
	// Adjust font sizes and layout on large and small screens
	scaleFactor = 1;
	if (visibleAreaX >= 1200) scaleFactor = Math.min(visibleAreaX,1600) / 1200;
	else if (visibleAreaX < 980) scaleFactor = visibleAreaX / 980;
		
	// Fonts
	var fontsizeStandard = Math.floor(scaleFactor * 15);
	var lineheightStandard = Math.floor(scaleFactor * 20);
	var fontsizeTopic = Math.floor(scaleFactor * 35);
	var lineheightTopic = Math.floor(scaleFactor * 35);
	dynStyles += "body, table, div, p, textarea, input { font-size:" + fontsizeStandard + "px; line-height:" + lineheightStandard + "px; }";
	dynStyles += "h1.pagetitle, h1.teamName { font-size:" + fontsizeTopic + "px; line-height:" + lineheightTopic + "px; }";
	
	// Header & footer
	headerHeight = Math.floor(scaleFactor * 70);
	footerHeight = Math.floor(scaleFactor * 70);
	var logoWidth = Math.floor(scaleFactor * 300);
	dynStyles += "#theHeader, #theHeader table { height:" + headerHeight + "px; }";
	dynStyles += "#theFooter, #theFooter table { height:" + footerHeight + "px; }";
	dynStyles += "#theLogo { width:" + logoWidth + "px; }";
	
	// Google Map
	mapHeight = visibleAreaY - headerHeight - footerHeight - 60;
	dynStyles += "#googleMap { height:" + mapHeight + "px; }";
	
	return true;
	}


//-------------------------------------------------------------------------------------------------------------


function show_hero() // fades in image of Friedrich after loading the page
	{
	sa = $("theHero");
	if (!sa) return;
	
	delayInit = 400;
	delayStep = 40;
	alphaNow = 0;
	alphaEnd = 100;
	alphaStep = 5;
	setTimeout("hero = setInterval('set_hero()', delayStep);", delayInit);
	
	if (thisURL.indexOf("kontakt.php") != -1) //setTimeout("kontakt = setInterval('set_kontakt()', delayStep);", delayInit);
		{
		$("mmKontakt").style.visibility = "hidden";
		$("mmKontaktShadow").style.visibility = "hidden";
		}
	}
	
	
//-------------------------------------------------------------------------------------------------------------


function set_hero() // sets alpha of Friedrich, used by show_hero()
	{
	if (!sa) return;
	// set the transparency
	alphaNow += alphaStep;
	if (alphaStep >= 0 && alphaNow > alphaEnd) alphaNow = alphaEnd;
	else if (alphaStep < 0 && alphaNow < 0) alphaNow = 0;
	var alphaSet = Math.round(alphaNow);
	sa.style.filter = "alpha(opacity=" + alphaSet + ")";
	sa.style.opacity = alphaSet / 100;
	if (alphaNow == alphaEnd) clearInterval(hero);
	}
	
	
//-------------------------------------------------------------------------------------------------------------


function change_language(newLang)
	{
	// check for valid parameter
	if (newLang != "de" && newLang != "en" && newLang != "ru") return;
	// create the replace strings
	var newString1 = "-" + newLang + ".php";
	var newString2 = "_" + newLang + "/";
	// get current URL
	var thisURL = this.location.href;
	// don't do anything if we are in the desired language version already
	if (thisURL.indexOf(newString1) != -1) return;
	// Check special cases: Products and projects. In case of detail page go to overview
	if (thisURL.indexOf("product-de.php") != -1 || thisURL.indexOf("product-en.php") != -1 || thisURL.indexOf("product-ru.php") != -1) thisURL = "products" + newString1;
	else if (thisURL.indexOf("project-de.php") != -1 || thisURL.indexOf("project-en.php") != -1 || thisURL.indexOf("project-ru.php") != -1) thisURL = "projects" + newString1;
	// modify URL and switch to new language
	else
		{
		thisURL = thisURL.replace(/-(de|en|ru)\.php/, newString1);
		thisURL = thisURL.replace(/_(de|en|ru)\//, newString2);
		}
	top.location.href = thisURL;
	}
	
	
//-------------------------------------------------------------------------------------------------------------



