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)
{	
	$('html').css('overflow', 'hidden');
	$('div#mask').show();
	$('div#foto-bg .next').hide();
	$('div#foto-bg .prev').hide();
	$('div#foto-full').hide();
	$(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() + 57) / 2)+$('html').scrollTop());
	$('div#foto-bg').fadeIn(50);
	
	var foto = $('#f' + num);
	
	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 = {};

	$('body').everyTime(100, function ()
	{
		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);
			}
			else
			{			
				$('div#foto-full').width(obj.width).height(obj.height);
				$('div#foto-bg').animate(
				{
					width: obj.width,
					height: obj.height + 35,
					marginTop: (-(obj.height + 57) / 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);
				});
			}
		}
	});

	$(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#foto-full').unbind('click').click(function ()
	{
		$(document).unbind('keydown');
		$('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()
{
	$('#left li.head div a').click(function ()
	{
		var item = $(this).parents('li.head').find('ul.items');
		$('#left li.head ul.items:visible').slideUp(100);

		if (item.css('display') == 'none')
		{
			item.slideDown(100);
		}

		return false;
	});

	$('a[rel=ext]').live('click', function ()
	{
		this.target = "_blank";
	});
	
	var offer_id = $('input[name=current-offer]').val();
	
	if (offer_id > 0)
	{
		var item = $('a[rel=' + offer_id + ']').parents('ul.items');
		
		item.show();
	}
	
	$('a.addrs').nospam({replaceText: true});
	
	var thumbs = $('.thumb').length;
	
	$('.thumb a').click(function ()
	{		
		if ($(this).attr('href').substr(-4) == 'null')
			return false;

		show_full_image($(this).attr('rel'), thumbs);
		
		return false;
	});
	
	$('form#kontakt .button-mask').click(function()
	{
		form_kontakt();
	});
	
	$('form#kontakt input').keypress(function(e)
	{
		if (e.which == 13) form_kontakt();
	});
	
	$('form#szukaj .button-mask').click(function()
	{
		$('form#szukaj').submit();
	});
			
	$('form#szukaj input').keypress(function(e)
	{
		if (e.which == 13) $('form#szukaj').submit();
	});
});
