$(document).ready(function()
{
	$('#overlayBox').hide();
	$('.galleryBox ul li').each(function(i, item)
	{
		$(item).click(function(){ previewGalleryImage($(this).children('img')); });
	});
	$('#overlayBox .closeBtn').click(function(){ $('#overlayBox').hide('slow'); });

	$('#aranzacje .fold').hide();
	$('#aranzacje .unfold').click(function(){ $('#main').css('overflow','hidden'); unfoldGalleryBox(this); $('#main').css('overflow','auto'); });
	$('#aranzacje .fold').click(function(){ $('#main').css('overflow','hidden'); foldGalleryBox(this); $('#main').css('overflow','auto'); });
	
	$('#elementy .fold').hide();
	$('#elementy .unfold').click(function(){ $('#main').css('overflow','hidden'); unfoldGalleryBox(this); $('#main').css('overflow','auto'); } );
	$('#elementy .fold').click(function(){ $('#main').css('overflow','hidden'); foldGalleryBox(this); $('#main').css('overflow','auto'); } );
	

	$('#elementy li, #aranzacje li').click(function(){ 
		$('#main').css('overflow','hidden'); 
		var c = this.parentNode.parentNode.firstChild.nextSibling;
		if (!$(c).is(":hidden")) unfoldGalleryBox(c); 
		$('#main').css('overflow','auto'); 
	} );

	$('#elementy, #aranzacje').addClass('initialStyle');
	
	$('#contentBackgroundImages').cycle({ fx: 'scrollDown', speed: 1000, timeout: 5000 });
});

function previewGalleryImage(obj)
{
	if (!$('#overlayBox').is(':hidden')){ $('#overlayBox').hide('slow', function(){ previewGalleryImage(obj); }); return; }
	
	$('#overlayBox #previewImage').attr('src','/img/'+obj.attr('alt'));
	$('#overlayBox #previewImage + p').html(obj.attr('title'));
	$('#overlayBox #previewImage').load(function(){
		$('#overlayBox').show('slow');
	});
	$('#overlayBox #downloadButton').attr('href', '/downloadGalleryBoxImage.php?id='+obj.attr('id'));
	
}

function unfoldGalleryBox(obj, dontToggleOther)
{
	obj = $(obj);
	id = obj.parent().attr('id');

	if (!dontToggleOther){ $('.galleryBox .fold').each(function(i, item){ foldGalleryBox(item, true); }); }

	$(obj).parent().removeClass('initialStyle');
	obj.parent().children('ul').slideDown()
	obj.parent().children('.fold').show();
	obj.parent().children('.unfold').hide();
	
	return true;
}

function foldGalleryBox(obj, dontToggleOther)
{
	obj = $(obj);
	id = obj.parent().attr('id');

	if (!dontToggleOther){ $('.galleryBox .unfold').each(function(i, item){ unfoldGalleryBox(item, true); }); }

	obj = $(obj);
	obj.parent().children('ul').slideUp()
	obj.parent().children('.unfold').show();
	obj.parent().children('.fold').hide();
	
	return true;
}


function switchWoj(woj)
{
	woj = 'page.php?cat=7&eq[wojewodztwo]='+woj;
	$('#overlayBox').css('overflow', 'auto');
	$('#overlayBox').hide('slow', function(){ $('#overlayBox').load(woj,'',function(responseText, textStatus, XMLHttpRequest){ if (responseText!=''){ $('#overlayBox').show('slow'); } }); });
}
