// JavaScript Document
$(document).ready(function(){
	function megaHoverOver(){
	
		$(this).find(".sub").stop().fadeTo('fast', 1).show();
			
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).outerWidth(); 
				});	
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".subMenu").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});
			$(this).find(".subMenu").css({'width' : rowWidth});
			
		}
	}
	
	function megaHoverOut(){ 
		$(this).find(".sub").stop().fadeTo('fast', 0, function() {
			$(this).hide(); 
		});
	}
	
	function megaHoverLoginOut(){ 
		if(!$('ul.mgl li .sub ul li input.focus').length){
			$('ul.mgl li').find(".sub").stop().fadeTo('fast', 0, function() {
				$('ul.mgl li .sub').hide(); 
			});
		}
	}

	config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 100, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 500, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};
	
	configLogin = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 100, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 500, // number = milliseconds delay before onMouseOut    
		 out: megaHoverLoginOut // function = onMouseOut callback (REQUIRED)    
	};

	$("ul.l li div.sub").css({'opacity':'0', 'display': 'none'});
	$("ul.l li").hoverIntent(config);
	$("ul.u li div.sub").css({'opacity':'0', 'display': 'none'});
	$("ul.u li").hoverIntent(config);
	$("ul.vk li div.sub").css({'opacity':'0', 'display': 'none'});
	$("ul.vk li").hoverIntent(config);
	$("ul.zvk li div.sub").css({'opacity':'0', 'display': 'none'});
	$("ul.zvk li").hoverIntent(config);
	$("ul.mgl li div.sub").css({'opacity':'0', 'display': 'none'});
	$("ul.mgl li").hoverIntent(configLogin);
	
	$('#header .sub .bridge').each(function(){
		var width = $(this).parents('li').find('a.subS').outerWidth();
		if($(this).parents('.secondline').length){
			width -= 1;
			$(this).parent().find('.subMenu').after('<div class="bridge" style="width: '+width+'px"></div>');
			//$(this).css('width', width);
			$(this).remove();
		} else {
			$(this).css('width', width);
		}
	});
	
		$('a.bubble').hover(function(){		
			$(this).children().css('display', 'none');
			$(this).children().stop(false, false).fadeTo(500, 1);
		}, function(){
			$(this).children().stop(false, false).fadeOut();
		});
		
		$("a.fancybox").fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	500, 
			'speedOut'		:	350,
			'titlePosition'	:	'inside'
		});
		
		$("a.objects").fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	500, 
			'speedOut'		:	350,
			'titlePosition'	:	'inside',
			'titleFormat' : function(title, currentArray, currentIndex, currentOpts) {
			if (title!='undefined') {
return '<span id="fancybox-title-inside"><div style="float:left"><strong>Bild ' + (currentIndex + 1) + ' / ' + currentArray.length + '</strong></div><div style="float:right">' + title + '</div></span>'; }
}
		});
		
		$('a[href^="#popup"]').fancybox({
			/*'width'			:	400,
			'height'		:	300,*/
			'scrolling'		:	'yes',
			'autoDimensions': 	false,
			'onComplete'	:	function() {
				$('body').addClass('printable');
			},
			'onCleanup'	:	function() {
				$('body').removeClass('printable');
			}
		});
		
		$('a[href^="#belegsatz"]').fancybox({
			'width'			:	730,
			'height'		:	300,
			'scrolling'		:	'yes',
			'autoDimensions': 	false,
			'onComplete'	:	function() {
				$('body').addClass('printable');
				$('#belegsatz form input').attr('disabled', 'disabled');
				$('#belegsatz form select').attr('disabled', 'disabled');
			},
			'onCleanup'	:	function() {
				$('body').removeClass('printable');
			}
		});
		
		$('.pictureCarousel li a img').each(function(){
			$(this).css('width', '100px').css('height', '100px');
		});
		
		jQuery('.pictureCarousel').jcarousel();
		
		$('label[for=zugang]').css('width', '100%').css('font-weight', 'bold');
		
		
		$('div.menu ul.mgl li div.sub input').click(function(){
			$(this).addClass('focus');
		});
		
		$(document).click(function(e) {
			if (!$(e.target).parents().andSelf().is('div.menu ul.mgl li div.sub input')) {
				$('div.menu ul.mgl li div.sub input').removeClass('focus');
				megaHoverLoginOut();
			}
		});
		
		if ( $.browser.msie ) {
			if($('#col2 table.formated').length){
				$('#col2 table.formated tr:nth-child(2n)').addClass('highlight');
			}
		 }
		
		if($('ul.sitemap')){
			$('ul.sitemap > li > ul > li:nth-child(2n)').css('margin-left', '20px');
		}
		
	});
