var submenuOffset, origSubnavMarginTop;


$(document).ready(function(e) {
    resetFooter();
	
	$(window).resize(onResizeWindow);
	
	initSubnav();
	initAnchorSliding();
	initHomeTeaser();
	initLeistungenTeaser();
	initArtOnBodyTeaser();
	initCEImageTextBlocks();
	initHomeTeaserImageLinks();
	initPageGalleries();
	
	$('a').focus(function(){$(this).blur();})
});


function initPageGalleries()
{
	$('div.gallery').each(
		function(divIndex, divElm)
		{
			$(divElm).find('img').each(
				function(index, elm)
				{
					console.log(elm);
					if ( index != 0 && (index+1) % 3 == 0 )
					{
						$(elm).css('marginRight', 0);
					}
				}
			);
		}
	);
	$('div.gallery a').fancybox({
		'titlePosition'  : 'over',
		'overlayOpacity': .88,
		'overlayColor': '#000'
	});
}

function initHomeTeaserImageLinks()
{
	$('body.home #content div.teaser').each(
		function(index, elm)
		{
			$(elm).children('img').click(function(){window.location = $(elm).find('a:first').attr('href');}).hover(
				function()
				{
					$(this).css('cursor', 'pointer');
				},
				function()
				{
					$(this).css('cursor', 'normal');
				}
			);
		}
	)
}

function initCEImageTextBlocks()
{
	$('div.ce_image_text img').each(
		function(index, elm)
		{
			$(elm).parent().children('h3').after(elm);
		}
	)
}

function onResizeWindow(e)
{
	resetFooter();
}

function resetFooter()
{
	var winH = $(window).height();
	var wrapperH = $('#footer').offset().top;
	var diffH = winH - wrapperH - $('#footer').height();
	
	if ( diffH > 0 )
	{
		$('#footer').css({marginTop:diffH});
	}
}

function initSubnavAutoposition()
{
	var subNav = $('#subnav');
}

function goToByScrollAnchor(anchorElm)
{
     	$('html,body').animate({scrollTop: anchorElm.offset().top},'slow');
		
		var mtb = $('#subnav').css('margin-top');
		$('#subnav').css({marginTop: 0});
		
		var offset = anchorElm.offset().top - $('#subnav').offset().top;
		if ( offset < 25 )
		{
			offset = 25;
		}
		$('#subnav').css({marginTop: mtb});
		$('#subnav').animate({marginTop: offset}, 'slow');
		$('#subnav li').removeClass('active');
}

function initAnchorSliding()
{
	$('.contact #subnav li:first').addClass('active');
	$('a[href*=#]').click(
		function()
		{
			goToByScrollAnchor($('a[name='+$(this).attr('href').split('#')[1]+']'));
			$(this).parent('li').addClass('active');
			return false;
		}
	);
	if ( $('body.contact').length > 0 || $('body.leistungen').length > 0 || $('body.art_on_body').length )
	{
		$(window).unbind('scroll');
	}
}

function initLeistungenTeaser()
{
	$('body.leistungen div.teaser').hover(
		function(e)
		{
			$(this).children().stop(true,true).animate({opacity:1}, 500);
			$(this).css('cursor', 'pointer');
			if ( e.originalEvent )
			{  
				$("#subnav li a[href='"+$(this).find('a:first').attr('href')+"']").parent('li').trigger('mouseenter');
			}
		},
		function(e)
		{
			$(this).children().stop(true,true).animate({opacity:.6}, 500);
			$(this).css('cursor', 'auto');
			if ( e.originalEvent )
			{
				$("#subnav li a[href='"+$(this).find('a:first').attr('href')+"']").parent('li').trigger('mouseleave');
			}
		}
	);
	
	$('body.leistungen div.teaser').trigger('mouseout');
	
	$('body.leistungen div.teaser').click(function()
		{
			window.location = $(this).find("a:first").attr('href');
		}
	);
	
	$('#subnav li').hover(
		function(e)
		{
			if ( e.originalEvent )
			{
				$("body.leistungen a[href='"+$(this).find('a').attr('href')+"']").parents('div.teaser').trigger('mouseenter');
			}
		},
		function(e)
		{
			if ( e.originalEvent )
			{
				$("body.leistungen a[href='"+$(this).find('a').attr('href')+"']").parents('div.teaser').trigger('mouseleave');
			}
		}
	);
}

function initArtOnBodyTeaser()
{
	$('body.art_on_body div.teaser').hover(
		function(e)
		{
			$(this).children().stop(true,true).animate({opacity:1}, 500);
			$(this).css('cursor', 'pointer');
		},
		function(e)
		{
			$(this).children().stop(true,true).animate({opacity:.6}, 500);
			$(this).css('cursor', 'auto');
		}
	);
	
	$('body.art_on_body div.teaser').trigger('mouseout');
	
	$('body.art_on_body div.teaser:last').css({'margin-right':0});
	
	$('body.art_on_body div.teaser a').fancybox({
		'titlePosition'  : 'over',
		'overlayOpacity': .88,
		'overlayColor': '#000'
	});
	
	$('body.art_on_body div.teaser').click(
		function(e)
		{
			if ( e.originalEvent )
			{
				$(this).children('a:first').trigger('click');
			}
		}
	);
}

function initSubnav()
{
	$('#subnav li').hover(
		function()
		{
			$(this).addClass('hover');
		},
		function()
		{
			$(this).removeClass('hover');
		}
	);
	
	submenuOffset =  $('#subnav').offset();
	origSubnavMarginTop = $('#subnav').css('margin-top');
	
	if ( $('#subnav').length > 0 )
	{
		$(window).scroll(onScrollWithSubmenu);
	}
}

function onScrollWithSubmenu(e)
{
	var pageScrollTop = $(document).scrollTop();
	
	if ( pageScrollTop - submenuOffset.top > 0 ) 
	{
		$('#subnav').stop(true, true).animate({marginTop: pageScrollTop - submenuOffset.top}, 500);
	}
	else
	{
		$('#subnav').stop(true, true).animate({marginTop: origSubnavMarginTop}, 500);
	}
}

function initHomeTeaser()
{
	$('body.home div.teaser').each(
		function(index, teaser)
		{
			if ( index != 0 && (index+1) % 3 == 0 )
			{
				$(teaser).css('margin-right', 0);
			}
		}
	);
}
