$(function(){
	
	/* Apply fancybox to multiple items */

	$("a.lightbox").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut':	'elastic',
		'speedIn'		:	600,
		'speedOut'		:	200,
		'overlayShow'	:	true,
		'swf' : {
			'wmode'  : 'transparent',
			'allowfullscreen' : 'true'
				}
	});

	$('body').append('<div id="bg" style=\"display:none\"><img class="img" /><img class="hidden" /></div>');
	
	$(window).resize(function(){
		winResize();
	});

});

window.onload = function() {
	
	var _bg = $('body').css('backgroundImage');
		 _bg = _bg.replace(/^url|[\(\)]/g, '');
		 _bg = _bg.replace('"', '');
		 _bg = _bg.replace('"', '');

	$('#bg img').attr('src', _bg);
	$('#bg').css('display', 'block');
	winResize();

}

function winResize() {

	var _img = $('#bg .img');

	var okno = $('#bg').width() / $('#bg').height();
	var obraz = parseInt($('#bg .hidden').width()) / parseInt($('#bg .hidden').height());

	if( okno > obraz) {
		_img.css({
				'width': $('#bg').width() + 'px',
				'height': ( $('#bg').width() / obraz ) + 'px'
				});
	}
	else {
		_img.css({
				'height': $('#bg').height() + 'px',
				'width': ( $('#bg').height() * obraz ) + 'px'
				});
	}
		
}
