var nowloading = false;
var ss = false;

$(document).ready(
	function (){
	
		$('.slideshow').click(function(e){
		
			if (ss == true)
			{
				stopSS();
			}
			else
			{
				
			
				ss = true;
				$('.head, .m_menu, .foot, .galcontrol a.th, .galcontrol a.nextfot, .galcontrol a.prevfot').fadeTo('fast',0.1);
				
				if ((CUR == FOTS.length-1))
				{
					$(this).oneTime('3s', 'SS', function(i) {
						openimageID(1);
					});
				}
				else
				{
					$(this).oneTime('3s', 'SS', function(i) {
						openimageID(CUR+1);
					});
				}
			}
			
			return false;
		});

		$(".wait").spriteAnimation({
                numberOfLoops: -1,
                direction: 'ltr',
                startFrame: 0,
                endFrame: 36,
                interval: 20
		});
	
		$(".galcontrol .th").click(function(e){
			if (ss == true) stopSS();
			
			var imgid = parseInt($(this).attr('class').split(' ')[0].substr(1));
			var imgsrc = FOTS[imgid];
			
			
			if (imgid == 1) {pierwszy();}
			if (imgid == FOTS.length-1) {ostatni();}
			if (!(imgid == FOTS.length-1 || imgid == 1)) {srodkowy();}
			
			// $('.m_fot img').attr('src', imgsrc);
			if (!nowloading)
			{
				if (!$(this).hasClass('active')) 
					openimageID(imgid);
				
				$(".galcontrol .th").each(function(e){
					$(this).removeClass('active');
				});
				
				$(this).addClass('active');
			}
			return false;
		});
		
		$(".galcontrol .nextfot, .galcontrol .prevfot").click(function()
		{
			if (ss == true) stopSS();
			
			if (!nowloading)
			if (typeof $(this).attr('href') !== 'undefined')
			{
				if ($(this).hasClass('nextfot'))
				{
					openimageID(parseInt(CUR)+1);
				}
				else
				{
					openimageID(parseInt(CUR)-1);
				}
			}
			return false;
		});
		
	}
)

function pierwszy()
{
	$('.prevfot').removeClass('active').removeAttr('href');
	$('.nextfot').addClass('active').attr('href','?nastepne');
}

function srodkowy()
{
	$('.prevfot').addClass('active').attr('href','?poprzednie');
	$('.nextfot').addClass('active').attr('href','?nastepne');
}

function ostatni()
{
	$('.prevfot').addClass('active').attr('href','?poprzednie');
	$('.nextfot').removeClass('active').removeAttr('href');
}


function openimageID(iid)
{
	openimage(FOTS[iid],iid);
}

function openimage(imgsrc,iid)
{
	var nimg = new Image();
	var jesimag = false;	
	
	nowloading = true;	
	
	$('.wait').show();		
	$('.m_fot img').each(function(){
		$(this).hide();
		
		if ($(this).attr('src') == imgsrc)
		{
			$('.wait').hide();
			jesimag = true;
			$(this).fadeIn('slow');
			nowloading = false;
			$('.ic').css('width',$(this).width()+'px').css('height',$(this).height()+'px');
			CUR = iid;
			
			upCUR();
		}
	});
	
	if (!jesimag)
	{
		$(nimg).attr('src',imgsrc).load(function () {
			$('.wait').hide();
			$('.m_fot div.ic').append(this);
			$(this).hide();
			$(this).fadeIn('slow');
			nowloading = false;
			
			CUR = iid;
			$('.ic').css('width',$(this).width()+'px').css('height',$(this).height()+'px');
			
			upCUR();
		});	
	}
	
	/*
	if (CUR == 1) {pierwszy();}
			else if (CUR == FOTS.length-1) {ostatni();}
			else {srodkowy();}
			
			$('.th').removeClass('active');
			$('.i'+(parseInt(CUR)+1)+'.th').addClass('active');
*/
}

function upCUR()
{
	if (CUR == 1) {pierwszy();} 
	else if (CUR == FOTS.length-1) {ostatni();}
	else {srodkowy();}
	
	$('.th').removeClass('active');
	$('.i'+CUR+'.th').addClass('active');
	
	
	if (!(CUR == FOTS.length-1))
	{
		if (ss == true)
		{
			$('.slideshow').oneTime('3s', 'SS', function(i) {
					openimageID(CUR+1);
			});
		}
	}
	else
	{
		stopSS();
	}
	
}

function stopSS()
{
	$('.slideshow').stopTime('SS');
	ss = false;
	$('.head, .m_menu, .foot, .galcontrol a.th, .galcontrol a.nextfot, .galcontrol a.prevfot').fadeTo('fast',1);
}
