function imgGallerySlideShow(slideShowID, images, sDirection, pAlign, imgShown, sbars, auto, autoTime)
{
	var ssID = slideShowID;
	var imgGallerySS = this;
	var imgRecordSet = images;
	var maxImgContainerWidth = 0;
	var maxImgContainerHeight = 0;
	var imgsContainerWidth = 0;
	var imgsContainerHeight = 0;


	var totalWidth = 0;
	var totalHeight = 0;
	var imgCount = 0;

	var margin = 20;
	var marginSide = 4;
	var scrollbarWidth = 18;


	var moveToLock = false;
	var moveToTimeout = null;
	var rotateToLock = false;
	var targetX = 0;
	var targetY = 0;
	var startTargetX = 0;
	var startTargetY = 0;
	var maxTargetX = 0;
	var maxTargetY = 0;

	var scrollDirection = sDirection;
	var pageAlignment = pAlign;
	var imagesShown = imgShown;
	var scrollbars = sbars;
	var autoRotate = auto;
	var autoRotateTimespan = autoTime;
	var autoRotateTimeout = null;
	var autoRotateStatus = "play";
	var blindSpeed = 300;


	if (autoRotate && autoRotateTimespan.length > 0)
	{
		autoRotateTimespan = 1000 * Number(autoRotateTimespan);	
	}


	this.setupPostRegionLoad = function()
	{
		imgCount = imgRecordSet.getRowCount();

		setSizes();
		setInitialViewerSize();
		setImagesContainerSize();
		alignImages();
		setTimeout(setImagesShown, 40);
	}


	this.nextPicture = function()
	{
		var container = YAHOO.util.Dom.get("imgsContainer");
		var tmpTargetX = null;
		var tmpTargetY = null;

		if (scrollDirection == "horizontal")
		{
			if (targetX != maxTargetX)
			{
				tmpTargetX = targetX - maxImgContainerWidth;
				
				if (tmpTargetX >= maxTargetX)
				{
					targetX -= maxImgContainerWidth;
				}
	
				tmpTargetX -= margin;
	
				if (tmpTargetX >= maxTargetX)
				{
					targetX -= margin;
				}
			}
			else if (autoRotate)
			{
				targetX = startTargetX;
			}
		}
		else
		{
			tmpTargetY = targetY - maxImgContainerHeight;

			if (tmpTargetY >= maxTargetY)
			{
				targetY = targetY - maxImgContainerHeight;
			}

			tmpTargetY -= margin;

			if (tmpTargetY >= maxTargetY)
			{
				targetY = targetY - margin;
			}
		}

		if (!moveToLock && !autoRotate)
		{
			moveToLock = true;
			moveContainerTo();
		}
		else if (!rotateToLock && autoRotate)
		{
			rotateToLock = true;
			rotateContainerTo();
		}
	}


	this.prevPicture = function()
	{
		var container = YAHOO.util.Dom.get("imgsContainer");
		var tmpTargetX = null;
		var tmpTargetY = null;

		if (scrollDirection == "horizontal")
		{
			if (targetX != startTargetX)
			{
				tmpTargetX = targetX + maxImgContainerWidth;
	
				if (tmpTargetX <= startTargetX)
				{
					targetX += maxImgContainerWidth;
				}
	
				tmpTargetX += margin;
	
				if (tmpTargetX <= startTargetX)
				{
					targetX += margin;
				}
			}
			else if (autoRotate)
			{
				targetX = maxTargetX;
			}
		}
		else
		{
			tmpTargetY = targetY + maxImgContainerHeight;
			
			if (tmpTargetY <= startTargetY)
			{
				targetY += maxImgContainerHeight;
			}

			tmpTargetY += margin;

			if (tmpTargetY <= startTargetY)
			{
				targetY += margin;
			}
		}

		if (!moveToLock && !autoRotate)
		{
			moveToLock = true;
			moveContainerTo();
		}
		else if (!rotateToLock && autoRotate)
		{
			rotateToLock = true;
			rotateContainerTo();
		}
	}

	this.togglePlayMode = function()
	{
		if (autoRotateStatus == "play")
		{
			clearTimeout(autoRotateTimeout);
			autoRotateStatus = "pause";
			disableButton("buttonPlay");
		}
		else
		{
			autoRotateStatus = "play";
			autoRotateTimeout = setTimeout(imgGallerySS.nextPicture, autoRotateTimespan);
			enableButton("buttonPlay");
		}
	}

	this.pausePlayMode = function()
	{
		if (autoRotateStatus == "play")
		{
			clearTimeout(autoRotateTimeout);
			autoRotateStatus = "pause";
			disableButton("buttonPlay");
		}
	}





	function setSizes()
	{
		var row = null;
		var img = null;
		var imgContainer = null;
		
		for (var i = 0; i < imgCount; i++)
		{
			row = imgRecordSet.getRowByRowNumber(i);
			img = YAHOO.util.Dom.get("imgID_" + row["@imgid"]);
			
			img.style.width = row["listImgWidth"] + "px";
			img.style.height = row["listImgHeight"] + "px";

			if (img.offsetWidth > maxImgContainerWidth)
			{
				maxImgContainerWidth = img.offsetWidth;
			}

			if (img.offsetHeight > maxImgContainerHeight)
			{
				maxImgContainerHeight = img.offsetHeight;
			}
		}


		for (var i = 0; i < imgCount; i++)
		{
			row = imgRecordSet.getRowByRowNumber(i);
			imgContainer = YAHOO.util.Dom.get("containerImgID_" + row["@imgid"]);

			totalWidth += maxImgContainerWidth;
			totalHeight += maxImgContainerHeight;

			imgContainer.style.width = maxImgContainerWidth + "px";
			imgContainer.style.height = maxImgContainerHeight + "px";

			if (scrollDirection == "horizontal")
			{
				imgContainer.style.marginTop = marginSide + "px";
				imgContainer.style.marginBottom = marginSide + "px";
				imgContainer.style.marginRight = margin + "px";
			}
			else
			{
				imgContainer.style.marginLeft = marginSide + "px";
				imgContainer.style.marginRight = marginSide + "px";
				imgContainer.style.marginBottom = margin + "px";
			}
		}
	}


	function setInitialViewerSize()
	{
		var viewer = YAHOO.util.Dom.get("imgViewer");
		var width = maxImgContainerWidth;
		var height = maxImgContainerHeight;

		if (imgCount > imagesShown && scrollbars)
		{
			if (scrollDirection == "horizontal")
			{
				height += scrollbarWidth;
			}
			else
			{
				width += scrollbarWidth;
			}
		}

		if (!scrollbars)
		{
			viewer.style.overflow = "hidden";
		}
		else
		{
			viewer.style.overflow = "auto";
		}

		viewer.style.width = width + "px";
		viewer.style.height = height + "px";
	}


	function setImagesContainerSize()
	{
		var imgsContainer = YAHOO.util.Dom.get("imgsContainer");
		var imgFirst = imgRecordSet.getRowByRowNumber(0);
		var imgFirstContainer = YAHOO.util.Dom.get("containerImgID_" + imgFirst["@imgid"]);
		var imgLast = imgRecordSet.getRowByRowNumber(imgRecordSet.getRowCount() - 1);
		var imgLastContainer = YAHOO.util.Dom.get("containerImgID_" + imgLast["@imgid"]);


		if (scrollDirection == "horizontal")
		{
			imgFirstContainer.style.marginLeft = "0px";
			imgLastContainer.style.marginRight = "0px";
			imgsContainer.style.width = totalWidth + "px";
			imgsContainerWidth = (maxImgContainerWidth * imgCount) + (margin * (imgCount - 1));
			imgsContainerHeight = maxImgContainerHeight + marginSide * 2;
		}
		else
		{
			imgsContainerHeight = (maxImgContainerHeight * imgCount) + (margin * (imgCount - 1));
			imgsContainerWidth = maxImgContainerWidth + marginSide * 2;
			imgFirstContainer.style.marginTop = "0px";
			imgLastContainer.style.marginBottom = "0px";
			imgsContainer.style.height = totalHeight + "px";
		}


		if (imgCount > 1)
		{
			//startTargetX = YAHOO.util.Dom.getX(imgsContainer);
			//startTargetY = YAHOO.util.Dom.getY(imgsContainer);
			//targetX = startTargetX;
			//targetY = startTargetY;
			
			if (scrollDirection == "horizontal")
			{
				//imgsContainerWidth = (maxImgContainerWidth * imgCount) + (margin * (imgCount - 1));
				//imgsContainerHeight = maxImgContainerHeight + marginSide * 2;
				imgsContainer.style.width = imgsContainerWidth + "px";
				imgsContainer.style.height = imgsContainerHeight + "px";
				maxTargetX = startTargetX - imgsContainerWidth + (maxImgContainerWidth * imagesShown) + (margin * (imagesShown - 1));
				maxTargetY = startTargetY;
			}
			else
			{
				//imgsContainerHeight = (maxImgContainerHeight * imgCount) + (margin * (imgCount - 1));
				//imgsContainerWidth = maxImgContainerWidth + marginSide * 2;
				imgsContainer.style.width = imgsContainerWidth + "px";
				imgsContainer.style.height = imgsContainerHeight + "px";
				maxTargetX = startTargetX;
				maxTargetY = startTargetY - imgsContainerHeight + (maxImgContainerHeight * imagesShown) + (margin * (imagesShown - 1));
			}

			imgsContainer.style.left = "0px";
		}
	}


	function alignImages()
	{
		var row = null;
		var container = null;

		var height = 0;
		var spacerHeight = 0;
		var marginHeight = 0;

		
		for (var i = 0; i < imgCount; i++)
		{
			marginHeight = 0;

			row = imgRecordSet.getRowByRowNumber(i);
			img = YAHOO.util.Dom.get("imgID_" + row["@imgid"]);

			height = Number(row["listImgHeight"]);

			if (height < maxImgContainerHeight - 2)
			{
				spacerHeight = maxImgContainerHeight - height;
				marginHeight = Math.floor(spacerHeight / 2);
			}
			
			img.style.marginTop = marginHeight + "px";
		}
	}

	function setImagesShown()
	{
		if (imagesShown > 0)
		{
			var viewer = YAHOO.util.Dom.get("imgViewer");
			var controller = YAHOO.util.Dom.get("imgViewerController");
			var slideShow = YAHOO.util.Dom.get("imgGallerySlideShow");

			if (scrollDirection == "horizontal")
			{
				var width = (maxImgContainerWidth * imagesShown) + (margin * (imagesShown - 1));
				var height = imgsContainerHeight;

				if (imgCount > imagesShown && scrollbars)
				{
					height += scrollbarWidth;
				}

				viewer.style.width = width + "px";
				viewer.style.height = height + "px";
			}
			else
			{
				var height = (maxImgContainerHeight * imagesShown) + (margin * (imagesShown - 1));
				var width = imgsContainerWidth;

				if (imgCount > imagesShown && scrollbars)
				{
					width += scrollbarWidth;
				}

				viewer.style.height = height + "px";
				viewer.style.width = width + "px";
			}
		}

		if (!scrollbars)
		{
			setController();	
		}
	}

	function setController()
	{
		var slideshow = YAHOO.util.Dom.get(ssID);
		var viewer = YAHOO.util.Dom.get("imgViewer");
		var controller = YAHOO.util.Dom.get("imgViewerController");
		var fbutton = YAHOO.util.Dom.get("buttonForward");
		var rbutton = YAHOO.util.Dom.get("buttonReverse");

		var buttonWidth = fbutton.offsetWidth;
		var buttonHeight = fbutton.offsetHeight;

		if (scrollDirection == "horizontal")
		{
			controller.style.height = buttonHeight + "px";
			controller.style.width = viewer.style.width;

			var margin = viewer.style.width.substr(0, viewer.style.width.length - 2) - (2 * buttonWidth);
			
			if (autoRotate)
			{
				var pbutton = YAHOO.util.Dom.get("buttonPlay");
				var pbuttonWidth = pbutton.offsetWidth;
				var pbuttonMargin = Math.round(margin / 2) - Math.round(pbuttonWidth / 2);
				pbutton.style.marginLeft = pbuttonMargin + "px";
				
				margin = margin - pbuttonMargin - pbuttonWidth;
			}

			fbutton.style.marginLeft = margin + "px";
		}
		else
		{
			controller.style.height = viewer.style.height;
			controller.style.width = buttonWidth + "px";

			var slideshowWidth = controller.offsetWidth + viewer.offsetWidth;
			var margin = viewer.style.height.substr(0, viewer.style.height.length - 2) - (2 * buttonHeight);
			slideshow.style.width = slideshowWidth + "px";

			fbutton.style.marginTop = margin + "px";
		}


		if (imgCount > 1)
		{
			enableButton(fbutton.id);
	
			if (autoRotate)
			{
				autoRotateTimeout = setTimeout(imgGallerySS.nextPicture, autoRotateTimespan);
				enableButton("buttonPlay");
				enableButton(rbutton.id);
			}
			else
			{
				disableButton(rbutton.id);
			}
		}
		else
		{
			disableButton(fbutton.id);
			disableButton(rbutton.id);
		}
	}

	function moveContainerTo()
	{
		var container = YAHOO.util.Dom.get("imgsContainer");
		var splitAmount = 8;


		if (targetX == maxTargetX && maxTargetX != 0 || targetY == maxTargetY && maxTargetY != 0)
		{
			disableButton("buttonForward");
		}
		else
		{
			enableButton("buttonForward");
		}

		if (targetX == 0 && maxTargetX != 0 || targetY == 0 && maxTargetY != 0)
		{
			disableButton("buttonReverse");
		}
		else
		{
			enableButton("buttonReverse");
		}


		var tX = targetX;
		var tY = targetY;
		//var cX = YAHOO.util.Dom.getX(container);
		//var cY = YAHOO.util.Dom.getY(container);
		var cX = Number(container.style.left.substr(0, container.style.left.length - 2));
		var cY = Number(container.style.top.substr(0, container.style.top.length - 2));


		if (cX != tX)
		{
			var distance = Math.abs(tX - cX);
			var movement = Math.ceil(distance / splitAmount);

			if (cX < tX)
			{
				nX = cX + movement;
			}
			else
			{
				nX = cX - movement;
			}

			//YAHOO.util.Dom.setX(container, nX);
			container.style.left = nX + "px";
			cX = nX;
		}


		if (cY != tY)
		{
			var distance = Math.abs(tY - cY);
			var movement = Math.ceil(distance / splitAmount);

			if (cY < tY)
			{
				nY = cY + movement;
			}
			else
			{
				nY = cY - movement;
			}

			//YAHOO.util.Dom.setY(container, nY);
			container.style.top = nY + "px";
			cY = nY;
		}
		
		if (cX != tX || cY != tY)
		{
			clearTimeout(moveToTimeout);
			moveToTimeout = setTimeout(moveContainerTo, 20);
		}
		else
		{
			clearTimeout(moveToTimeout);
			moveToLock = false;
		}
	}

	function rotateContainerTo()
	{
		var viewerCover = YAHOO.util.Dom.get("imgViewerCover");
		var viewer = YAHOO.util.Dom.get("imgViewer");

		viewerCover.display = "block";
		viewerCover.style.width = "100%";
		viewerCover.style.top = "-" + viewer.offsetHeight + "px";
		rotateContainerToPosition();
		//var slider = new Spry.Effect.Blind("imgViewerCover", {duration: blindSpeed, from: "0px", to: viewer.offsetHeight + "px", finish: rotateContainerToPosition, transition: Spry.linearTransition});
		//slider.start();
	}
	
	function rotateContainerToPosition()
	{
		var container = YAHOO.util.Dom.get("imgsContainer");
		var viewer = YAHOO.util.Dom.get("imgViewer");
		var tX = targetX;
		var tY = targetY;
		var cX = Number(container.style.left.substr(0, container.style.left.length - 2));
		var cY = Number(container.style.top.substr(0, container.style.top.length - 2));


		if (cX != tX)
		{
			container.style.left = tX + "px";
		}

		if (cY != tY)
		{
			container.style.top = tY + "px";
		}

		rotateContainerToFinish();
		//var slider = new Spry.Effect.Blind("imgViewerCover", {duration: blindSpeed, from: viewer.offsetHeight + "px", to: "0px", finish: rotateContainerToFinish, transition: Spry.linearTransition});
		//slider.start();
	}

	function rotateContainerToFinish()
	{
		rotateToLock = false;
		if (autoRotateStatus == "play")
		{
			autoRotateTimeout = setTimeout(imgGallerySS.nextPicture, autoRotateTimespan);
		}
	}

	function enableButton(buttonName)
	{
		var button = YAHOO.util.Dom.get(buttonName);
		var listeners = YAHOO.util.Event.getListeners(button, "click");
		
		if (YAHOO.util.Dom.hasClass(button, "disabled"))
		{
			YAHOO.util.Dom.removeClass(button, "disabled");
		}

		if (listeners == null)
		{
			if (button.id == "buttonForward")
			{
				YAHOO.util.Event.addListener(button, "click", imgGallerySS.nextPicture);
				if (autoRotate)
				{
					YAHOO.util.Event.addListener(button, "click", imgGallerySS.pausePlayMode);
				}
			}
			else if (button.id == "buttonReverse")
			{
				YAHOO.util.Event.addListener(button, "click", imgGallerySS.prevPicture);
				if (autoRotate)
				{
					YAHOO.util.Event.addListener(button, "click", imgGallerySS.pausePlayMode);
				}
			}
			else if (button.id == "buttonPlay")
			{
				YAHOO.util.Event.addListener(button, "click", imgGallerySS.togglePlayMode);
				
				for (var i = 0; i < imgCount; i++)
				{
					row = imgRecordSet.getRowByRowNumber(i);
					img = YAHOO.util.Dom.get("imgID_" + row["@imgid"]);
					
					YAHOO.util.Event.addListener(img, "click", imgGallerySS.pausePlayMode);
				}
			}
		}
	}

	function disableButton(buttonName)
	{
		var button = YAHOO.util.Dom.get(buttonName);
		var listeners = YAHOO.util.Event.getListeners(button, "click");

		if (!YAHOO.util.Dom.hasClass(button, "disabled"))
		{
			YAHOO.util.Dom.addClass(button, "disabled");
		}

		if (listeners != null && buttonName != "buttonPlay")
		{
			YAHOO.util.Event.removeListener(button, "click");	
		}
	}
}
