$(document).ready(function(){
	$window = $(window);

	var window_size = Array($window.width(), $window.height());
	
	// ## Setup
	$('section#blogg').css('top', (window_size[1] - 130));
	
	// ## Mainmenu
	$('ul#mainmenu > li > a').hover(function () {
		$(this).animate({
			backgroundPosition: '0 -36px'
		}, 200);
	}, function () {
		$(this).animate({
			backgroundPosition: '0 0'
		}, 200);
	});
	
	
	// ## Background-image
	$('#background-image').css({
		'width' : window_size[0],
		'height' : window_size[1],
		'overflow' : 'hidden'
	});
	
	$('#background-image > img').each(function (element, index) {
		if (window_size[0] > window_size[1])
		{
			$(this).width(window_size[0]);
		}
		else
		{
			$(this).height(window_size[1]);
		}
	
		var image_position_y = (parseFloat($(this).height()) - parseFloat(window_size[1]))/2;
		var image_position_x = (parseFloat($(this).width()) - parseFloat(window_size[0]))/2;
		$(this).css('margin-top', '-' + image_position_y + 'px');
		$(this).css('margin-left', '-' + image_position_x + 'px');
		$(this).hide();
	});
	
	$('#background-image > img:first').show().addClass('active');
	
	
	$window.resize(function () {
		window_size = Array($window.width(), $window.height());
		$('section#blogg').css('top', (window_size[1] - 130));
		
		$('#background-image').css({
			'width' : window_size[0],
			'height' : window_size[1],
			'overflow' : 'hidden'
		});
		
		$('#background-image > img').each(function (element, index) {
			if (window_size[0] > window_size[1])
			{
				$(this).width(window_size[0]);
				$(this).height('auto');
			}
			else
			{
				$(this).width('auto');
				$(this).height(window_size[1]);
			}
			
			var image_position_y = (parseFloat($(this).height()) - parseFloat(window_size[1]))/2;
			var image_position_x = (parseFloat($(this).width()) - parseFloat(window_size[0]))/2;
			$(this).css('margin-top', '-' + image_position_y + 'px');
			$(this).css('margin-left', '-' + image_position_x + 'px');
		});
	});
	
	$window.scroll(function () {
		var scrollpos = $(this).scrollTop();
		
		$('#background-image').css('margin-top', '-' + (scrollpos/8) + 'px');
	});
	
	$('section#blogg.small').masonry({
		// options
		itemSelector : '.post',
		columnWidth : 10
	});
	
	$('#main').fadeIn();
	
	$('.social-link#buy').click(function () {
		$('#buy-lightbox').fadeIn();
		
		$('body').click(function () {
			$('#buy-lightbox').fadeOut();
		});
		
		return false;
	});
});

swapme = setInterval('swapBackground()', 15000);

function swapBackground() {
	var next = ($('#background-image > img.active').next('img').length > 0) ? $('#background-image > img.active').next('img') : $('#background-image > img:first');
	
	$('#background-image > img.active').fadeOut('slow', function () {
		$('#background-image > img.active').removeClass('active');
		next.fadeIn('slow').addClass('active');
	});
}
