/*
	Autore: Luca Piccinelli www.lucapiccinelli.com
	Codice rilasciato sotto licenza GPL http://www.gnu.org/licenses/gpl.html
	http://katolaz.homeunix.net/gplv3/gplv3-it-final.html (italiano)
*/

function Gallery(displayId, linkClass, preloadClass,linkPage)
{
		this.imgIndex = 0;
		this.display = displayId;
		this.links = $(linkClass).get();
		this.linkp = $(linkPage).get();
		this.imgNumber = this.links.length;
		this.slideShow = null;
		this.speed=5000;
		this.fadespeed=1000;
		
		if(preloadClass)
			$(preloadClass).css({display: "none"});
		
		this.showImg = function(_index, fade)
		{
			if(!_index)
				index = this.imgIndex;
			else
				index = _index;
			
			var link = this.links[index];
			var linkp = this.linkp[index];
			
			if($("#displayImg"))
			{
					$("#displayImg").remove();
			}
			$("<a href='" + link.href + "'><img id='displayImg' class='fade' src='" + linkp.href + "'/></a>").appendTo(this.display);
			$("#displayImg").css(
				{
					//position: "absolute",
					//top: $(this.display).offset().top + 1,
					//left: $(this.display).offset().left + 1,
					//zIndex: "0"
				}
			);
			if(fade)
				$(".fade").css({display: "none"}).fadeIn(this.fadespeed);
		}
		
		this.ReloadIndex = function()
		{
			this.links = $(linkClass).get();
			this.linkp = $(linkPage).get();
		}
		
		this.goToPage = function(index)
		{
			var link = this.links[index];
			document.location.href=link.href;
		}
		
		this.setIndex = function(index)
		{
			this.imgIndex = index;
		}
		
		this.showNext = function(fade)
		{
			this.imgIndex = ++this.imgIndex % this.imgNumber;
			this.showImg(null, fade);
		}
		
		this.showPrev = function(fade)
		{
			this.imgIndex = --this.imgIndex;
			if(this.imgIndex < 0) this.imgIndex = this.imgNumber - 1;
			this.showImg(null, fade);
		}
		
		this.load = function(path)
		{
			var fileName = this.links[this.imgIndex].href.split("/").pop();
			var top = $(this.display).offset().top + ($(this.display).height() / 2);
			var left = $(this.display).offset().left + ($(this.display).width() / 2);
			
			$("<img id='indicator' src='script/img/indicator_medium.gif' alt='ajax indicator'/>").css(
				{
					position: "absolute",
					top: top,
					left: left
				}
			).appendTo(this.display);
			return $("<div id='loadReturn'></div>").load(path + "fileData.php?filename=" + fileName, function()
				{
					$("#indicator").remove();
				}
			);
		}
		
		this.createStatusBar = function(content, height)
		{
			h = height
			if(!height) h = 20
			/* Creazione della statusBar */
			$("<div id='statusBar'></div>").appendTo(this.display)
			$("#statusBar").append(content);
			$("#statusBar").css(
				{
					position: "absolute",
					top: $(this.display).offset().top + 1,
					left: $(this.display).offset().left + 1,
					zIndex: "1",
					width: $(this.display).width() - 2,
					display: "none",
					height: h,
					border: "1px solid",
					background: "#000000",
					color: "#FFFFFF",
					fontSize: "10px",
					opacity: "0.50"
				}
			).slideDown(700);
		}
		
		this.destroyStatusBar = function()
		{
			$("#statusBar").slideUp(700, 
			function()
				{
					$("#statusBar").remove();
				}
			);
		}
		
		this.start = function(_obj)
		{
			obj = _obj;
			obj.destroyStatusBar();
			obj.showNext(true);
			//setTimeout("obj.createStatusBar(obj.load('imgs/'))", 1000);
			this.slideShow = setTimeout("obj.start(obj)", this.speed);
		}
		
		this.stop = function()
		{
			clearTimeout(this.slideShow);
		}
}
