/*
 * -------------------------------------------------------------------------------------------------------------------------------------
 * Fumktion zum aufrufen der Galerie Details in einen neuen Fenster
 * -------------------------------------------------------------------------------------------------------------------------------------
*/

/*global $, window */

function popup (url) {
	 var fenster = window.open(url, "Information", "width=700,height=600,resizable=yes,scrollbars=yes");
	 fenster.focus();
	 return false;
}

function openWindow(id) {
	$("#hidden" + id).removeClass('hidden');
}

function closeWindow(id) {
	$("#hidden" + id).addClass('hidden');
}

function resizeBackground(scrolling) {
	var height = 0;
	var headerHeight     = $("#header").height();
	var menuHeight       = $("#main_menu").height();
	var subMenuHeight    = ($("#line").is(":visible")) ? $("#line").height() : 0;
	var subSubMenuHeight = ($("#line2").is(":visible")) ? $("#line2").height() : 0;
	
	if (($('#contentArea').height() + subMenuHeight + subSubMenuHeight) < $('#gb_listing').height()) {
		$('#contentArea').height($('#gb_listing').height() - subMenuHeight - subSubMenuHeight);
	}

	height =  headerHeight + menuHeight + subMenuHeight + subSubMenuHeight + $('#contentArea').height();
	
	if (height > $('#gb_listing').height()) {
		$('#gb_listing').height($('#contentArea').height() + subMenuHeight + subSubMenuHeight);
	}
	
	if (!scrolling ) {
		if ($('.referenceItem').is('div') && (!$('#left_home').is('div'))) {
			$('#contentArea').height($('#contentArea').height());
		}
	}
	
	if (!scrolling && height !== 0 && (!$('#left_home').is('div'))) {
		
		if (height < $('#into').outerHeight()) {
			height = height - $('#contentArea').height() + $('#into').outerHeight();
		} 

		if (height < $('#gb_listing').height()) {
			height = height - $('#contentArea').height() - subMenuHeight - subSubMenuHeight + $('#gb_listing').height();
		} else  {
			$('#gb_listing').height($('#contentArea').height() + subMenuHeight + subSubMenuHeight);
		}
		
		if (height < 600 ) {
			height = 600 ;
			$('#contentArea').height(600 - (headerHeight + menuHeight + subMenuHeight + subSubMenuHeight) + 'px' );
		}
		
		if ($('#gb_listing').is('div')) {
			$('#gb_listing').height(Math.max($('#into').outerHeight(), $('#contentArea').height()) + subMenuHeight + subSubMenuHeight);
		}
		
		$(".outer").height(height + 65 + 'px');
		$("#footer").css({'top': height + 'px'});
	} else if ($('#left_home').is('div')) {
		if (!scrolling && (height > $('#contentArea').height())) {
			$(".outer").height(height + 65 + 'px');
			$("#footer").css({'top':height + 250 + 'px'});
		} else {
			if (height > $('#contentArea').height() ){
				$(".outer").height(height + 65 + 'px');
				$("#footer").css({'top':height + 'px'});
			}
		}
		
		if ($('#gb_listing').height() > $('#contentArea').height()) {
			if (!scrolling) {
				$(".outer").height(height + headerHeight + menuHeight + subMenuHeight + subSubMenuHeight + 65 + 'px');
				$("#footer").css({'top':height + headerHeight + menuHeight + subMenuHeight + subSubMenuHeight + 'px'});
				$('#contentArea').height($('#gb_listing').height());
			} 
		}
	}
}

$(document).ready(function() { 
	//return;
	if($("#mailform").is('form')) {
		$(".csc-mailform-label:last").css({"margin":"40px 0"});
		resizeBackground(false);
	}
	
	$('.headerNav > a').each(function(i,e) {
		$('.caption' + i).html('');
		$('#img' + i).wrap('<a href="' + $(this).attr('href') + '" />');
		$(this).parent().removeClass('headerNav');
		$(this).parent().addClass('pseudo'+ i);
		$(this).parent().children().addClass('qseudo'+ i);
		$(this).parent().addClass('headerNav');
		$(this).detach().appendTo($('.caption' + i));
	});
	
	$('.headerNav > .gbNavSub').each(function() {
		if ($(this).hasClass('qseudo0')) {
			$(this).detach().appendTo($('#gbNavSub0')).children().addClass('subLink');
		}
		
		if ($(this).hasClass('qseudo1')) {
			$(this).detach().appendTo($('#gbNavSub1')).children().addClass('subLink');
		}
		
		if ($(this).hasClass('qseudo2')) {
			$(this).detach().appendTo($('#gbNavSub2')).children().addClass('subLink');
		}
	});
	
	if ($("#gbNavContainer").is("div"))
	{
		$("#gbNavContainer").detach().insertAfter($("#into").children(":first"));
	}

	$('.vorlage').remove();
	
	$('.footerNav').each(function(i, e) {
		if ($(this).text() == $('.navButtonC').text()) {
			$(this).parent().addClass('navButtonC');
		}
	});
	
	$('.csc-frame-rulerAfter .external-link-new-window ').each(function(i, e) {
		$(this).css({'top':$(this).position().top + ($(this).parentsUntil('.csc-frame-rulerAfter').prev().height()) + 'px'});
	});
	
	if ($('.navLayer').is("div") || $('.navLayerC').is("div")) {
		if (level[0] !== '') {
			$(".navButton:contains('" + level[0] + "')").removeClass('navButtonCur').addClass('navButtonC');
		}
		
		if (level[1] !== '') {
			$(".navLayer:contains('" + level[1] + "')").removeClass('navLayerCur').addClass('navLayerC');
		} 

		if ($('.navL').is("div")) {
			$('.navL').detach().appendTo($('#innerLine'));
			$("#contentArea").css({'top':'210px'});
			$('#line').show();
		}
		
		if ($('.nav2').is("div")) {
			$('.nav2').detach().appendTo($('#innerLine2'));
			$("#contentArea").css({'top':'232px'});
			$('#line2').show();
		}
		
		var tempWidth = 0;
		$('.nav2').each(function(){tempWidth += $(this).width();});
	
		if (tempWidth > $('#center').width()) {
			$("#contentArea").css({'top':'250px'});
		}
	
		$('#lineMenu2').show();
	}
	
	var widthL = 0;
	var potPos = 0;

	if ($('.navL').is("div")) {
		widthL = 0;
		
		$('.navL').each(function(index) {
			widthL += $(this).width() + 10;
		});
	
		potPos = $('.navButtonC').position().left + ($('.navButtonC').width()/2) - ($('#innerLine').width() / 2);
		
		if (20 > potPos) {
			potPos = 20;
		}

		$('#innerLine').css({'left':potPos});
	}

	if ($('.nav2').is('div')) {
		widthL = 0;
		
		$('.nav2').each(function(index) {
			widthL += $(this).width() + 5;
		});
		
		potPos = $('#innerLine').position().left + $('.navLayerC').position().left - widthL/2;

		if (20 > potPos) {
			potPos = 20;
		}
		
		$('#innerLine2').css({'position':'absolute','left':potPos});
	}

	resizeBackground(false);
	
	$(window).scroll(function() { resizeBackground(true); });
	$(window).resize(function() { resizeBackground(true); });
});
