var detailBoxOpen = false;
var maxPosition = 1416;
var nowPosition = 0;
var singleImageWidth = 354;

var counter = 0;
var aktiv;

var oldWidth;
var oldHeight;


$(document).ready(function() {	
	
	$("#jsWarning").css({display:"none"});
	
	reFrame();
	
	oldWidth = $(window).width();
	oldHeight = $(window).height();
	aktiv = setInterval( "checkForResize()", 250);
    
    if (gup('bert') != '') {
    	loadContent(gup('bert'))
    }
     
});

function checkForResize() {
	reFrame();
	currentWidth = $(window).width();
	currentHeight = $(window).height();
	if (currentWidth != oldWidth || currentHeight != oldHeight) {
		rePosition();
	}
	oldWidth = currentWidth;
	oldHeight = currentHeight;
}

/*
	Reposition detailBox in the second row
*/
function rePosition() 
{
	
	tempNode = $("#detailBox");
	tempNode2 = $("#detailBox2");
	$("#detailBox").remove();
	$("#detailBox2").remove();
	listenElemente = $("#contentBox > .box");
	for(var i = 0; i < listenElemente.length; i++) {
		tempOffset = $(listenElemente[i]).offset();
		if (tempOffset.top == 206) {
			$(listenElemente[i]).before(tempNode);
			$("#detailBox").after(tempNode2);
			return;
		}
		//$("#debug").html($("#debug").html() + counter++ +  ' ');
	}
	$(listenElemente[listenElemente.length - 1]).after(tempNode);
	$("#detailBox").after(tempNode2);
	return;
}

/*
	Install a listener to close the detailBox
*/
function closeContent() {
	if (detailBoxOpen == true) {
		$("#detailBox").animate({
			width: "0px",
			height: "0px"
		}, 1500, "swing", function() {
			$(this).css({display:"none"})
		});
		$("#detailBox2").animate({
			width: "0px",
			height: "0px"
		}, 1500, "swing", function() {
			$(this).css({display:"none"})
		});
		detailBoxOpen = false;
	}
}

/*
	Prevents the viewport to be bigger as necessary and selts a minimum viewport as well
*/
function reFrame() 
{
	// get screen width
	screenWidth = $(window).width();
	// get max number of columns excluding the navigation column
	spalten = (screenWidth - 212) / 197;
	spalten = Math.floor(spalten);
	// calculate width space consumption of all columns
	myWidth = spalten * 197;
	// calculate minimum space consumption
	minWidth = 197 * 2;
	if (myWidth < minWidth) myWidth = minWidth;
	// set width
	$("#contentBox").css({width:myWidth+"px"});
	allContentWidth = myWidth + 188;
	$("#allContent").css({width:allContentWidth+"px"});
	return true;
}

/*
    Scroll the reference images
*/
function scrollImage(direction) 
{
	bannerWidth = $("#big_image").width();
	if (direction == 'left') {
    	nowPosition = nowPosition + singleImageWidth;
    	//if (nowPosition > 0) nowPosition = -1062;
    	if (nowPosition > 0) nowPosition = (bannerWidth - singleImageWidth) * -1;
    	$("#big_image").animate({left: nowPosition+"px"}, 1500, "swing");
    }
    else if (direction == 'right') {
    	nowPosition = nowPosition - singleImageWidth;
    	//if (nowPosition < -1062) nowPosition = 0;
    	if (nowPosition < (bannerWidth - singleImageWidth) * -1) nowPosition = 0;
    	$("#big_image").animate({left: nowPosition+"px"}, 1500, "swing");
    }
}

/*
	Load content from a specific page
*/
function gup( name )
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null ) {
		return "";
	}
	else {
		return results[1];
	}
}