$().ready(function(){
	if ($('a.deletePage').length > 0)
	{
		$('a.deletePage').each(function(){
			$(this).click(function(){
				if (confirm('Potvrďte prosím odstranění stránky'))
				{
					href = $(this).attr('href');
					window.location.href = href + '&isConfirmed';
					return false;
				}
				else
				{
					return false;
				}
			});
		});
	}

	if ($('#photoList a').length > 0)
	{
		$('#photoList a').fancybox({
			'hideOnContentClick': true,
			'overlayShow': true,
			'padding': 1,
			'titleShow': false
		});
		$('#photoList.editMode a').each(function(){
			$(this).click(function(e){
				if (e.ctrlKey)
				{
					$.fancybox.cancel();
					photoId = $('img', this).attr('alt');
					window.location = window.location.pathname + '?photoId=' + photoId + '&action=edit';
					e.preventDefault();
				}
			});
		});
	}

	if ($('#mvphoto').length > 0)
	{
		_size = parseInt($('input[name="cropSize"]').val());
		_x1 = parseInt($('input[name="cropX"]').val());
		_y1 = parseInt($('input[name="cropY"]').val());
		_x2 = _x1+_size;
		_y2 = _y1+_size;
		$('#mvphoto').imgAreaSelect({
			x1: _x1,
			y1: _y1,
			x2: _x2,
			y2: _y2,
			minWidth: 200,
			minHeight: 200,
			aspectRatio: '1:1',
			handles: true,
			onSelectChange: updateCropData
		});
		$('body').dblclick(function(){
			$('form').submit();
		});
	}

	function updateCropData(img, selection)
	{
		if (!isNaN(selection.x1) && !isNaN(selection.y1) && !isNaN(selection.width))
		{
			$('input[name="cropX"]').val(selection.x1);
			$('input[name="cropY"]').val(selection.y1);
			$('input[name="cropSize"]').val(selection.width);
		}
	}

	if ($('.PhotoRetouchSingle').length > 0)
	{
		$('.PhotoRetouchSingle').each(function(){
			fader = $('.PhotoRetouchFader', $(this));
			width = 0;
			height = 0;
			currentPhoto = 0;
			countPhotos = 0;
			$('.PhotoRetouchFile', $(this)).each(function(i){
				if (i == 0)
				{
					width = $(this).width();
					height = $(this).height();
				}
				else
				{
					$(this).hide();
				}
				countPhotos++;
			});
			$(this).click(function(){
				changePhoto(this);
			});
			$(this).css('width', width + 'px');
			$(this).css('height', height + 'px');
			updatePhotoInfo(this);
		});
	}
});

function updatePhotoInfo(l)
{
	$('.PhotoInfo', l).text(currentPhoto+1);
}

function changePhoto(l)
{
	currentPhoto++;
	if (countPhotos <= currentPhoto)
	{
		currentPhoto = 0;
	}
	$('.PhotoRetouchFile', l).each(function(i){
		if (i == currentPhoto)
		{
			$(this).show();
		}
		else
		{
			$(this).hide();
		}
	});
	updatePhotoInfo(l);
}
