function refreshElements()
{

	// Updating background size - fitting in page
	
	var wh = $(window).innerHeight();
	var ww = $(window).innerWidth();
	var nh = ww * h / w;
	
	if(nh > wh)
	{
		$('#back img').css({ width : ww + 'px', height : nh + 'px' });
	} else
	{
		var nw = wh * w / h;
		$('#back img').css({ width : nw + 'px', height : wh + 'px', marginLeft : Math.round((ww - nw)/2) + 'px' });
	}
	
	$('#back').show();
	
	
	// Updating content position - centering by vertical
	var ch = $('.content').height();
	var emptyHeight = wh - 185 /* Menu + logo */ - 40 /* Footer */;
	
	$('.content').css('margin-top', ch < emptyHeight ? Math.round((emptyHeight - ch) / 3) : 0);
	
}


var maxPage = 1;
var curPage = 1;
var pageWidth = 850+30;

$(document).ready(function(e) {

	refreshElements();
	
	if($('.pager a').length > 1)
	{
		
		maxPage = $('.pager a').length;
		
		if(window.location.hash.substr(1)) curPage = parseInt(window.location.hash.substr(1));

		updateScrollable(curPage);

		// Prev page
		$('a.go-prev').click(function(e) {
			e.preventDefault();

			updateScrollable(curPage-1);
		});

		// Next page
		$('a.go-next').click(function(e) {
			e.preventDefault();

			updateScrollable(curPage+1);
		});

		// Page navigation
		$('.pager a').click(function(e) {
			e.preventDefault();

			updateScrollable($(this).attr('rel'));
		});
		
		// Mouse scroll
		$('.content-items').mousewheel(function(event, delta) {
			updateScrollable(curPage + (delta > 0 ? -1 : 1));
			return false;
		});
		
	}

	if($('.item-preview').length > 0)
	{
		$('.item-preview a').click(function(e) {
			e.preventDefault();
			
			showFullPhoto(parseInt($(this).attr('rel')));
		});
		
	}

	$('.item-full div.photo, .item-full div.photo a.close').click(function(e) {
		
		e.preventDefault();
		
		$('.item-full').hide();
		
		$('.item-preview').removeClass('preview-full');
		$('.item-preview span').css('top', 28);
		
	});
	
	$('.item-full div.photo div').mousemove(function(e) {
		
		// Moving background image if it's neccessary

		var of = $(this).offset();
		var y = (e.pageY - of.top) / $(this).height() * ($(this).attr('ih') - $(this).height());
		
		$('.item-full .photo div').css('background-position', 'center -'+y+'px');

	});
	
	$('.collection-items a.img, .item-photo a').click(function(e) {
		
		e.preventDefault();
		
		$('.item-full .photo div').css({
			'background-image' : 'url('+$(this).attr('href')+')',
			'background-position' : $(this).attr('ih') > $('.item-full div.photo div').height() ? 'center top' : 'center center'
		}).attr('ih', $(this).attr('ih'));
		
		$('.item-full').show();
		
	});
	
});

$(window).resize(function(e) {

	refreshElements();
	
});


function updateScrollable(page)
{
	$('.item-full').hide();
	
	if(!(1 <= page && page <= maxPage)) return false;
	
	$('.content-items').animate({ left : -(page - 1) * pageWidth}, 300, function() {
		$('.page a').removeClass('cur');
		$('.page a[rel='+page+']').addClass('cur');
		
		curPage = parseInt(page);
		
		if(curPage > 1) window.location.hash = curPage; else window.location.hash = '';

		if(page == 1) $('a.go-prev').css('visibility', 'hidden'); else  $('a.go-prev').css('visibility', 'visible'); 
		if(page == maxPage) $('a.go-next').css('visibility', 'hidden'); else  $('a.go-next').css('visibility', 'visible');
	});
}


function showFullPhoto(num)
{
	
	if($('.item-preview').length > 0)
	{
		
		$('.item-preview').addClass('preview-full');
		
		var esrc = $('.item-preview a[rel="'+num+'"]');
		
		$('.item-full .photo div').css({
			'background-image' : 'url('+esrc.attr('href')+')',
			'background-position' : esrc.attr('ih') > $('.item-full div.photo div').height() ? 'center top' : 'center center'
		}).attr('ih', esrc.attr('ih'));
		
		$('.item-full').show();
		
	}
	
}

