// javaScript for hansnieswandt.de

function getCanvasHeight() {
	// returns height of canvas 
	// (drawable rect of window)
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	} else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		} else {
			if (document.body && document.body.clientHeight) {
				windowHeight=document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

function getCanvasWidth() {
	// returns width of canvas 
	// (drawable rect of window)
	var windowWidth = 0;
	if (typeof(window.innerWidth) == 'number') {
		windowWidth=window.innerWidth;
	} else {
		if (document.documentElement && document.documentElement.clientWidth) {
			windowWidth = document.documentElement.clientWidth;
		} else {
			if (document.body && document.body.clientWidth) {
				windowWidth=document.body.clientWidth;
			}
		}
	}
	return windowWidth;
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}


function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}


function layoutFrontPageElements()
{
	var canvasHeight = getCanvasHeight();
	var canvasWidth = getCanvasWidth();
	
	// gigs
	var gigsBlock = document.getElementById("gigsBlock");
	var gigsContent = document.getElementById("gigsContent");
	if (canvasWidth <= 600)
	{
		gigsBlock.style.width = "400px";
		gigsBlock.style.left = "0px";
		gigsBlock.style.marginLeft = "0px";
		gigsContent.style.height = "auto";
		gigsContent.style.overflow = "visible";
	}
	else
	{
		gigsBlock.style.width = "400px";
		gigsBlock.style.left = "50%";
		gigsBlock.style.marginLeft = "-200px";
		gigsContent.style.overflow = "auto";
	}
	gigsBlock.style.visibility = "visible";
	var gigsBlockTop = parseInt(getStyle(gigsBlock, "top"), 10);
	var gigsBlockHeight = parseInt(getStyle(gigsBlock, "height"), 10);
	
	// news
	var newsBlock = document.getElementById("newsBlock");
	var newsMessage = document.getElementById("newsMessage");
	var newsHeadline = document.getElementById("newsHeadline");
	if (isNaN(gigsBlockHeight))
	{
		gigsBlockHeight = 300;
	}
	newsBlock.style.top = gigsBlockTop + gigsBlockHeight + 40 + "px";
	if (canvasWidth <= 600)
	{
		newsBlock.style.width = (canvasWidth - 40) + "px";
		newsBlock.style.left = "0px";
		newsBlock.style.marginLeft = "20px";
		newsBlock.style.marginRight = "20px";
		newsMessage.style.height = "auto";
		newsMessage.style.overflow = "visible";
	}
	else
	{
		newsBlock.style.width = "600px";
		newsBlock.style.left = ((canvasWidth / 2.0) - 300) + "px";
		newsBlock.style.marginRight = 0;
		newsBlock.style.marginLeft = 0;
		var newsHeadlineHeight = parseInt(getStyle(newsHeadline, "height"), 10);
		var newsMessageHeight = canvasHeight - parseInt(newsBlock.style.top) - newsHeadlineHeight;
		newsMessageHeight = newsMessageHeight - 60;
		if (newsMessageHeight > 300)
		{
			newsMessage.style.overflow = "auto";
			newsMessage.style.height = newsMessageHeight + "px";
		}
		else
		{
			newsMessage.style.height = "auto";
			newsMessage.style.overflow = "visible";
		}
	}
	newsBlock.style.visibility = "visible";
	
	// texts
	var textsBlock = document.getElementById("textsBlock");
	var textsContent = document.getElementById("textsContent");
	if (canvasWidth > 1480)
	{
		textsBlock.style.top = "40px";
		textsBlock.style.left = "40px";
		textsBlock.style.width = "400px";
		textsBlock.style.height = "240px";
		textsContent.style.width = textsBlock.style.width;
		textsContent.style.height = "240px";
		textsContent.style.overflow = "auto";
	}
	else
	{
		var newsBlockTop = parseInt(getStyle(newsBlock, "top"), 10);
		var newsBlockLeft = parseInt(getStyle(newsBlock, "left"), 10);
		var newsBlockWidth = parseInt(getStyle(newsBlock, "width"), 10);
		var newsBlockHeight = parseInt(getStyle(newsBlock, "height"), 10);
		if (isNaN(newsBlockHeight))
		{
			newsBlockHeight = 700;
		}
		if (isNaN(newsBlockWidth))
		{
			newsBlockWidth = 600;
		}
		textsBlock.style.left = newsBlockLeft + "px";
		textsBlock.style.top = newsBlockTop + newsBlockHeight + 20 + "px";
		if (canvasWidth <= 600)
		{
			textsBlock.style.width = "400px";
			textsBlock.style.height = "180px";
			textsContent.style.height = "auto";
			textsContent.style.overflow = "visible";
		}
		else
		{
			// (canvasWidth <= 1480)
			textsBlock.style.width = newsBlockWidth + "px";
			textsBlock.style.height = "180px";
			textsContent.style.width = newsBlockWidth + "px";
			textsContent.style.height = "165px";
			textsContent.style.overflow = "auto";
		}
	}
	textsBlock.style.visibility = "visible";

	// records
	var recordsBlock = document.getElementById("recordsBlock");
	if (canvasWidth > 1200)
	{
		recordsBlock.style.left = (canvasWidth - 40 - 250) + "px";
		recordsBlock.style.top = "55px";
	}
	else
	{
		var textsBlockTop = parseInt(getStyle(textsBlock, "top"), 10);
		var textsContentHeight = parseInt(getStyle(textsContent, "height"), 10);
		var newsBlockLeft = parseInt(getStyle(newsBlock, "left"), 10);
		var newsBlockWidth = parseInt(getStyle(newsBlock, "width"), 10);
		recordsBlock.style.left = (newsBlockLeft + newsBlockWidth - 250) + "px";
		recordsBlock.style.top = textsBlockTop + textsContentHeight + 60 + "px";
	}
	recordsBlock.style.visibility = "visible";
	
	// books
	var booksBlock = document.getElementById("booksBlock");
	if (canvasWidth > 1200)
	{
		booksBlock.style.left = (canvasWidth - 40 - 250) + "px";
		booksBlock.style.top = "360px";
	}
	else
	{
		var textsBlockTop = parseInt(getStyle(textsBlock, "top"), 10);
		var textsContentHeight = parseInt(getStyle(textsContent, "height"), 10);
		var newsBlockWidth = parseInt(getStyle(newsBlock, "width"), 10);
		booksBlock.style.left = newsBlockLeft + "px";
		booksBlock.style.top = textsBlockTop + textsContentHeight + 60 + "px";
	}
	booksBlock.style.visibility = "visible";
	
	// bio
	var bioBlock = document.getElementById("bioBlock");
	if (canvasWidth > 1050)
	{
		bioBlock.style.top = "310px";
		bioBlock.style.left = "40px";
	}
	else
	{
		var booksBlockTop = parseInt(getStyle(booksBlock, "top"), 10);
		var booksBlockHeight = parseInt(getStyle(booksBlock, "height"), 10);
		var booksBlockLeft = parseInt(getStyle(booksBlock, "left"), 10);
		if (isNaN(booksBlockHeight))
		{
			booksBlockHeight = 440;
		}
		bioBlock.style.top = booksBlockTop + booksBlockHeight + 20 + "px";
		bioBlock.style.left = booksBlockLeft + "px";
	}
	bioBlock.style.visibility = "visible";
	
	// press
	var pressBlock = document.getElementById("pressBlock");
	if (canvasWidth > 1050)
	{
		pressBlock.style.top = "620px";
		pressBlock.style.left = "40px";
	}
	else
	{
		var bioBlockTop = parseInt(getStyle(bioBlock, "top"), 10);
		var bioBlockHeight = parseInt(getStyle(bioBlock, "height"), 10);
		var bioBlockLeft = parseInt(getStyle(bioBlock, "left"), 10);
		if (isNaN(bioBlockHeight))
		{
			bioBlockHeight = 260;
		}
		pressBlock.style.top = bioBlockTop + bioBlockHeight + 20 + "px";
		pressBlock.style.left = bioBlockLeft + "px";
	}
	pressBlock.style.visibility = "visible";
	
	// links
	var linksBlock = document.getElementById("linksBlock");
	linksBlock.style.top = parseInt(pressBlock.style.top) + 40 + "px";
	linksBlock.style.visibility = "visible";
	
	// contact
	var contactBlock = document.getElementById("contactBlock");
	var linksBlockHeight = parseInt(getStyle(linksBlock, "height"), 10);
	if (isNaN(linksBlockHeight))
	{
		linksBlockHeight = 300;
	}
	contactBlock.style.top = parseInt(linksBlock.style.top) + linksBlockHeight + 40 + "px";
	contactBlock.style.visibility = "visible";
}
