$(function() {
	$('#bg img').each(function() {
	  	var imageWidth	= $(this).width();
		var imageHeight	= $(this).height();

		var windowWidth = $(window).width();
		var windowHeight = 540;

		var percent = Math.round( (windowWidth/imageWidth)*100 );
		var newHeight = (imageHeight*percent)/100;
		
		if (newHeight < 540) newHeight = 540;
		
		var marginTop = -(newHeight/4);

		if (marginTop+newHeight < 540) marginTop = 0;

		$(this).css({
			'width' : '100%',
			'height' : newHeight,
			'margin-top': marginTop
		}).fadeIn().attr({'title' : '', 'alt' : ''});
	});

	$('a.print').bind('click', function() {
		window.print();
		return false;
	});

	$('span.epostPlaceholder').html('<a href="mailto:mail@vassbakk.no">mail@vassbakk.no</a>');

	// Rullerende featured-boks på hovedsiden
	$('#featured').cycle({
		timeout: 6000,
		next: '#featuredNext',
		prev: '#featuredPrev',
		pager: '#featuredNav',
		pause: 1,
		fx:     'scrollHorz'
	});

	// Navigere med venstre og høyre piltast på hovedsiden
	$('body.home').keydown(function(e){
		if(e.keyCode == 37){ 
			$('#featured').cycle('prev');
		}
		if(e.keyCode == 39){ 
			$('#featured').cycle('next');
		}
	});

	// Erstatte logo med bildefil på mobil
	var bodyWidth = $('body').width();
	if(bodyWidth <= 480){
		$('#logo').css('background', 'none');
		$('#logo a')
			.css({
				'background' : 'none',
				'text-indent' : 0
			})
			.html('<img src="/wp-content/themes/vassbakk-mobil/images/logo-vs.png" alt="" />');
	}
});
