function load_image(i)
{			
	var img = $('<img />');

	img.load(function()
	{
		$(this).addClass('ready');
	});
	
	img.attr('src', i.img);
	return img;
}

function show_full_image(num, all)
{	
	var foto = $('#f' + num);

	if (foto.attr('href') == '')
	{
		$('div#foto-full').click();
		return;
	}
	
	$('html').css('overflow', 'hidden');
	$('div#mask').show();
	$('div#foto-bg .next').hide();
	$('div#foto-bg .prev').hide();
	$('div#foto-full').hide();
	$('div#foto-desc p').hide().html('');
	$(document).unbind('keydown');

	$('div#foto-bg').css('background', 'url(pub/img/loading.gif) center no-repeat');
	$('div#foto-bg').css('margin-top', (-($('div#foto-bg').height()) / 2)+$('html').scrollTop());
	$('div#foto-bg').fadeIn(50);
	
	
	var desc = $('#d' + num).html();
	$('div#foto-desc p').html(desc);
	
	if (foto.attr('href').substr(-4) == 'null')
	{
		if (num < all)
		{
			show_full_image(++num, all)
		}
		else if (num == all)
		{
			//img.remove();
			$(document).unbind('keydown');
			$('div#foto-bg').css('background-image', '').hide();
			$('div#foto-full').css('background-image', '').fadeOut(50);
			$('div#mask').hide();
			$('html').css('overflow', '');
		}
			
		return false;
	}	

	var img = load_image({img:foto.attr('href')});
	var obj = {};
	var load_time = 0;
	
	$('body').everyTime(100, function ()
	{
		load_time++;
		
		if (load_time > 30000)
		{
			$('body').stopTime();
			$(img).remove();
			$('div#foto-full').click();
		}
		
		if ($(img).attr('class') == 'ready')
		{
			$('body').stopTime();
			
			obj = {src: $(img).attr('src'), width: $(img).attr('width'), height: $(img).attr('height')};

			$(img).remove();
			
			if ($('div#foto-full').width() == obj.width && $('div#foto-full').height() == obj.height)
			{
				$('div#foto-bg').css('background', '');
				
				if (num < all)
				{
					$('div#foto-bg .next').fadeIn(300);
				}

				if (num > 1)
				{
					$('div#foto-bg .prev').fadeIn(300);
				}
				
				$('div#foto-full').css('background', 'url(' + obj.src + ') center no-repeat');
				$('div#foto-full').fadeIn(300, function()
				{
					$(this).pngFix({blankgif:'pub/img/blank.gif'});
					$('div#close').show();
					$('div#foto-desc p').show();
					
				});
			}
			else
			{
				$('div#foto-full').width(obj.width).height(obj.height);
				$('div#foto-bg').animate(
				{
					width: obj.width,
					height: obj.height+45,
					marginTop: (-(obj.height+45) / 2)+$('html').scrollTop(),
					marginLeft: -(obj.width) / 2
				}, 400, function ()
				{
					$('div#foto-bg').css('background', '');
					
					if (num < all)
					{
						$('div#foto-bg .next').fadeIn(300);
					}

					if (num > 1)
					{
						$('div#foto-bg .prev').fadeIn(300);
					}
				
					$('div#foto-full').css('background', 'url(' + obj.src + ') center no-repeat');
					$('div#foto-full').fadeIn(300, function()
					{
						$(this).pngFix({blankgif:'pub/img/blank.gif'});
						$('div#close').show();
						$('div#foto-desc p').show();
					});
				});
			}
		}
	});

	$(document).keydown(function (e)
	{
		$(img).remove();
		
		switch (e.keyCode)
		{
			case 27:
				$('div#foto-full').click();
				break;

			case 37:
				if (num > 1) $('div#foto-bg .prev').click();
				break;

			case 39:
				if (num < all) $('div#foto-bg .next').click();
				break;		
		}
	});
	
	$('div#close').unbind('click').click(function ()
	{
		$('div#foto-full').click();
	});

	$('div#foto-full').unbind('click').click(function ()
	{
		$(document).unbind('keydown');
		$('div#close').hide();
		$('div#foto-bg').css('background-image', '').hide();
		$('div#foto-full').css('background-image', '').fadeOut(50);
		$('div#mask').hide();
		$('html').css('overflow', '');
	});
	
	$('div#foto-bg .prev').unbind('click').click(function ()
			{
				$('div#foto-full').css('background-image', '').fadeOut(50);
				show_full_image(--num, all);
			});

			$('div#foto-bg .next').unbind('click').click(function ()
			{
				$('div#foto-full').css('background-image', '').fadeOut(50);
				show_full_image(++num, all);
			});
}

function form_kontakt()
{
	var $alert = false;
	
	$('form#kontakt .req').each(function ()
	{
		if ($(this).val() == '') $alert = true;
	});
	
	if ($alert)
		alert('Należy wypełnić wszystkie pola oznaczone *.');
	else
		$('form#kontakt').submit();
}

$(document).ready(function()
{
	$('a[rel=ext]').click(function ()
	{
		this.target = "_blank";
	});
	
	/*$('.gallery #f').parents('.gallery div').remove();*/
	$('.gallery a[href=""]').css('cursor', 'auto');
	
	var thumbs = $('.gallery div').length;
	
	$('.gallery a').click(function ()
	{		
		if ($(this).attr('href').substr(-4) == 'null')
			return false;

		show_full_image($(this).attr('rel'), thumbs);
		
		return false;
	});

	$('a.addrs').nospam({replaceText: true});
	
	$('form#kontakt input[type=button]').click(function()
	{
		form_kontakt();
	});
	
	$('form#kontakt input').keypress(function(e)
	{
		if (e.which == 13) form_kontakt();
	});
	
	$('#cat-body div a').hover(function (e)
	{
		var klasa = $(this).parents('div').attr('class');
		$('#cat-header .' + klasa).css('background-color', '#9b1c1c');
	}, function (e)
	{
		var klasa = $(this).parents('div').attr('class');
		$('#cat-header .' + klasa).css('background-color', '');
	});
});
