// SlideShow() Class

// Dependencies: httpRequest.class.js

var SlideShow = function()
{
	/* Private Properties */
	var holderId = "";
	var holderObj = null;
	var slides = new Array();
	var imgDelay = 8000;
	var trnSpeed = 0.01;
	var fadeTimer = null;
	var transitionTimer = null;
	var currentImg = null;
	var nextImg = null;
	var imgPointer = 0;
	
	/* Public Methods */
	SlideShow.prototype.setHolderId = function(div_id)
	{
		if(typeof(document.getElementById(div_id) != "undefined"))
		{
			holderId = div_id;
		}
		else
		{
			alert("ERROR: SlideShow() Class method setHolderId() was given an invalid holder ID.");
		}
	}
	
	SlideShow.prototype.addImage = function(imagePath)
	{
		slides.push(imagePath);
	}
	
	SlideShow.prototype.setDelay = function(int_seconds)
	{
		if(typeof(int_seconds) == "number" && int_seconds >= 1 && int_seconds <= 300)
		{
			imgDelay = Math.round(int_seconds) * 1000;
		}
		
	}
	
	SlideShow.prototype.setTransitionSpeed = function(int_1_to_10)
	{
		if(typeof(int_1_to_10) == "number" && int_1_to_10 >= 1 && int_1_to_10 <= 10)
		{
			trnSpeed = Math.round(int_1_to_10) / 100;
		}
	}
	
	SlideShow.prototype.start = function()
	{
		holderObj = document.getElementById(holderId);
		holderObj.style.position = "relative";
		imgPointer = 0;
		nextImg = loadImg(slides[imgPointer]);
		updateImg(nextImg);
	}
	
	/* Private Methods */
	
	var loadImg = function(imgPath)
	{
		var img = new Image();
		img.width = holderObj.clientWidth;
		img.height = holderObj.clientHeight;
		img.src = imgPath;
		img.style.position = "absolute";
		img.style.top = "0px";
		img.style.left = "0px";
		return img;
	}
	
	var startTransitionTimer = function()
	{
		if(fadeTimer != null)
		{
			window.clearInterval(fadeTimer);
		}
		if(slides.length > 1)
		{	
			transitionTimer = window.setTimeout(function()
			{
				updateImg(nextImg);
				
			},imgDelay);
		}
	}
	
	var updateImg = function(imgObj)
	{
		if(transitionTimer != null)
		{
			window.clearTimeout(transitionTimer);
		}
		imgObj.style.opacity = 0;
		imgObj.style.filter = 'alpha(opacity = 0)';
		holderObj.appendChild(imgObj);
		
		var opacity = 0;
		var oldIE = 0;
		
		fadeTimer = window.setInterval(function()
		{
			opacity += trnSpeed;
			oldIE += (trnSpeed * 100);
			
			imgObj.style.opacity = opacity;
			imgObj.style.filter = 'alpha(opacity = ' + oldIE + ')';
			
			if(opacity >= 1)
			{
				if(currentImg != null)
				{
					holderObj.removeChild(currentImg);
				}
				currentImg = imgObj;
				
				imgPointer++;
				if(imgPointer >= slides.length)
				{
					imgPointer = 0;
				}
				nextImg = loadImg(slides[imgPointer]);
				
				startTransitionTimer();
			}
		},10);
	}

}
