$(function() {
	$(window).load(updateBackgroundContainer).resize(updateBackgroundContainer);

	$('a.rolloverimage').each(function() {
		
		$([$('img', this).attr('src')]).preload();
		
		if (!$(this).hasClass('active')) { return; };
		
		var imagePathInfo = $('img', this).attr('src').replace('_over', '').split('.');
		var imagePath = imagePathInfo.slice(0, -1).join('.');
		var imageExtension = imagePathInfo[imagePathInfo.length - 1];
		
		$('img', this).attr('src', imagePath + '_over' + '.' + imageExtension);
		
	});
	
	$('a.rolloverimage').mouseenter(function() {
		
		var imagePathInfo = $('img', this).attr('src').replace('_over', '').split('.');
		var imagePath = imagePathInfo.slice(0, -1).join('.');
		var imageExtension = imagePathInfo[imagePathInfo.length - 1];
	
		$('img', this).attr('src', imagePath + '_over' + '.' + imageExtension);
		
	}).mouseleave(function() {
		
		if ($(this).hasClass('active')) { return; };
		
		var imagePathInfo = $('img', this).attr('src').replace('_over', '').split('.');
		var imagePath = imagePathInfo.slice(0, -1).join('.');
		var imageExtension = imagePathInfo[imagePathInfo.length - 1];
	
		$('img', this).attr('src', imagePath + '.' + imageExtension);
		
	});
	
	$('.imagemodule a').fancybox({
		overlayColor: '#333'
	});
	
	$('.teamcontent a').fancybox({
		overlayColor: '#333'
	});
});

function updateBackgroundContainer() {
	var backgroundcontainerHeight = $(window).height();
	var maincontainerHeight = $('#maincontainer').css('height', 'auto').height() + 325;
	
	if (backgroundcontainerHeight > maincontainerHeight) {
		$('#backgroundcontainer').height(backgroundcontainerHeight - 187);
		$('#maincontainer').height(backgroundcontainerHeight);
	} else {
		$('#backgroundcontainer').height(maincontainerHeight - 187);
		$('#maincontainer').height(maincontainerHeight);
	}
	
}

function gotoSection(sectionname) {
	var duration = 500;
	
	$('html, body').animate({
			scrollTop: $('#contentmodule_'+sectionname).offset().top
		},
		duration,
		'easeOutQuad'
	);
}

$.fn.preload = function() {
    this.each(function() {
        (new Image()).src = this;
    });
}
