var slideshow = null;

//Pinned thumbnails
var imagesCounter = 0;
$("thumbs").addClassName("visible");

$$(".thumbs a").each(function(element){
	if (imagesCounter < 5) {
		element.addClassName("visible");
		imagesCounter++;
	}
	
	$(element).down("img").observe('click', changeImages);
});

//Uwaga - musi by� nieparzysta ilo�� element�w!!
function changeImages(event, element) {
	if (!element && event)
	{
		element = Event.element(event);
	}
	else if (!element)
	{
		return;
	}
	
	var lastImageIndex = galleryCount + 1;
	
	var visibleElements = 5;
	var middleIndex = Math.floor(visibleElements / 2) + visibleElements % 2;
	var actualIndex = parseInt(element.getAttribute("ref"));
	var from = 1;
	
	if (actualIndex > middleIndex && actualIndex < (lastImageIndex - middleIndex))
	{
		from = actualIndex - middleIndex + 1;
		to = actualIndex + middleIndex - 1;
	}
	else if (actualIndex < middleIndex)
	{
		from = 0;
		to = visibleElements;
	}
	else if (actualIndex > (lastImageIndex - middleIndex - 1))
	{
		from = lastImageIndex - middleIndex - 1;
		to = lastImageIndex;
	}
	
	$$(".thumbs a").each(function(element){
		var imageIndex = element.down("img").readAttribute("ref");
		if (imageIndex >= from && imageIndex <= to)
		{
			element.down("img").up("a").addClassName("visible");
		}
		else
		{
			element.down("img").up("a").removeClassName("visible");
		}
	});
	
	$$(".thumbs a").each(function(item){
		item.removeClassName("active");
	});
	
	$$(".thumbs a img[ref="+ actualIndex +"]").each(function(item){
		item.up("a").addClassName("active");
	});
}

function next()
{
	var element = null;
	$$(".thumbs a img[ref="+ slideshow.temp +"]").each(function(item){
		element = item;
	});
	
	changeImages(null, element);
}

function previous()
{
	var element = null;
	$$(".thumbs a img[ref="+ slideshow.temp +"]").each(function(item){
		element = item;
	});
	
	changeImages(null, element);
}

$$(".previous").each(function(item){
	item.observe("click", function(event) { slideshow.butPrev(); event.stop(); });
});

$$(".next").each(function(item){
	item.observe("click", function(event) { slideshow.butNext(); event.stop(); });
});

$("autoSlideShow").observe("click", function() {
	slideshow.toggleshow();
});

slideshow = {
	ile : galleryCount,
	start : 1,
	temp : 1,
	slidet: 3,
	running: null,
	duration: 0.2,
	old: null,
	dirs: '',
	clicked: false,
	
	start: function()
	{
		this.startshow();
	},
	
	startshow: function()
	{
		this.running = true;
		
		this.slideS = new PeriodicalExecuter(function(pex){
			slideshow.butNext(null, null, true);
		}, this.slidet);
	},
	
	stopshow: function()
	{
		this.running = false;
		if(this.slideS)
		{
			this.slideS.stop();
		}
	},
	
	stopshowf: function()
	{
		this.stopshow();
	},

	butPrev : function(jump, element)
	{
		if(this.clicked == true)
		{
			return false;
		}
		
		this.clicked = true;
		this.stopshowf();
		this.old = this.temp;
		
		if (jump && jump != 0)
		{
			slideshow.temp = jump;
		}
		else slideshow.temp--;
		
		if (this.temp < 1)
		{
			this.temp = this.ile + 1;
		}
		
		new Effect.Fade('whole'+this.old, { queue: {scope: 'schenck', position: "end", limit: 2}, duration:this.duration, afterFinish: function() {} });
		new Effect.Appear('whole'+this.temp, { queue: {scope: 'schenck', position: "end", limit: 2}, duration:this.duration, afterFinish: function() { slideshow.clicked = false; previous(); } });

		var image = document.getElementById("galleryImage" + slideshow.temp);
		if (image)
		{
		    image.style.display = "inline";
		    image.src = image.getAttribute("rel");
		}
	},
	
	butNext : function (jump, element, auto) 
	{
		if(this.clicked==true)
		{
			return false;
		}
		
		if (!auto)
		{
			this.clicked = true;
			this.stopshowf();
		}
		this.old = this.temp;
		
		if (jump && jump != 0)
		{
			slideshow.temp = jump;
		}
		else slideshow.temp++;
		
		if (this.temp > this.ile + 1) 
		{
		    this.temp = 1;
		}
		
		new Effect.Fade('whole'+this.old, { queue: {scope: 'schenck', position: "end"}, duration:this.duration, afterFinish: function() { } });
		new Effect.Appear('whole'+this.temp, { queue: {scope: 'schenck', position: "end"}, duration:this.duration, afterFinish: function() { slideshow.clicked = false; next(); }});
		
		var image = document.getElementById("galleryImage" + slideshow.temp);
		if (image)
		{
		    image.style.display = "inline";
		    image.src = image.getAttribute("rel");
		}
	},
	
	toggleshow: function()
	{
		if (!this.running)
		{
			$("autoSlideShow").innerHTML = "Wyłącz automatyczny pokaz zdjęć";
			this.startshow();
		}
		else
		{
			$("autoSlideShow").innerHTML = "Włącz automatyczny pokaz zdjęć";
			this.stopshow();
		}
	},
	
	toggleshowbut: function()
	{
			if (this.running)
			{
				this.stopshowf();
			}
			else
			{
				this.startshow();
			}
	},
	
	togglelight : function()
	{
		if (this.running)
		{
			if (this.slideS)
			{
				this.slideS.stop();
			}
		}
	},
	
	togglelightclose : function()
	{
		if (!this.running)
		{
			if (this.slideS)
			{
				this.startshow();
			}
		}
	}
};

