/* Image Slide Show general include */

/* Description: get the transition code to set an HTML Image object's style.filter value to
   Inputs: duration: the duration in seconds of the transition between images in the slideshow
		   type: the number of the type of transition to perform; 1=fade, 2=disolve, 3=wipe(left to right)
   Output: string of the function call for the specified image transition */
function getTransTypeText(duration,type)
{
	var transTypes = new Array();
	
	transTypes[0] = new String();
	transTypes[0].value = "blendTrans(duration=" + duration + ")"; //Fade
	transTypes[1] = new String();
	transTypes[1].value = "revealTrans(duration=" + duration + ", transition=12)"; //Dissolve
    transTypes[2] = new String();
	transTypes[2].value = "progid:DXImageTransform.Microsoft.gradientWipe(GradientSize=0.5,wipeStyle=0,motion=forward,duration=" + duration + ")"; //Gradient Wipe: Left to Right
	return transTypes[type-1].value;
}

/* Description: Called once for each slideshow on an html page to get the first image to display
				and schedule the next image transition.  Only call this once!
   Inputs: slideShowNumber: the ID of the slideshow to init; valid value [1-n]
   Output: fully qualified path of the first image to display in the slideshow */
function getImageInit(slideShowNumber)
{
	//alert('WHER ARE IN');
	var image = getImage(slideShowNumber, true);
	timeoutIds[(slideShowNumber-1)] = setTimeout('runSlideShow('+slideShowNumber+')', (image.showDuration*1000));
	//alert(image.path);
	return image.path;
}

/* Description: Get a random number within the range specified
   Inputs: range: the integer representing the top end of the range to select a
				  random number from within; valid imputs [2-n]
   Output: a random integer between 1-(range), inclusive */
function getRandom(range)
{
	if (range > 1)
	{
		var n = Math.floor(Math.random() * 10) % range;
		return n + 1;
	}
	else
	{
		return 1;
	}
}

/* Description: Refreshes the slideshows in the CM edit view.  Not used for the user site
   Inputs: slideShowNumber: the ID of the slideshow to refresh; valid value [1-n]
   Output: None */
function refreshForCM(slideShowNumber)
{
	slideShowCollection[slideShowNumber][0].currImage = 999;
	clearTimeout(timeoutIds[(slideShowNumber-1)]);
	timeoutIds[(slideShowNumber-1)] = setTimeout('runSlideShow('+slideShowNumber+',true)', 1);
}

/* Description: Get the next image that is different than the current image from 
				the specified slideshow, respecting the random or sequential order
				property of this slideshow.
   Inputs: slideShowNumber: the ID of the slideshow to init; valid value [1-n]
		   isFirstImage: pass any value to get the first image returned, regardless of if it is cached
   Output: the image object of the next image to show */
function getImage(slideShowNumber, isFirstImage)
{
	isSameImage[slideShowNumber] = false;
	var newImageIndex;
	var isCached = false;
	
	if ((slideShowCollection[slideShowNumber][0].random) == true)
	{
		newImageIndex = getRandom(slideShowCollection[slideShowNumber].length - 1);
		var tries = 0;
		while (isFirstImage == null && tries < slideShowCollection[slideShowNumber].length && isCached == false)
		{
			while (newImageIndex == slideShowCollection[slideShowNumber][0].currImage && slideShowCollection[slideShowNumber].length > 2)
			{
				newImageIndex = getRandom(slideShowCollection[slideShowNumber].length - 1);
			}
			if (cachedImages[slideShowNumber][newImageIndex] != null && cachedImages[slideShowNumber][newImageIndex].complete == true) {isCached = true;}
			tries++;
		}
	}
	else
	{
		newImageIndex = (slideShowCollection[slideShowNumber][0].currImage) + 1;
		if (newImageIndex > (slideShowCollection[slideShowNumber].length - 1))
		{
			newImageIndex = 1;
		}
		var tryAgain = true;
		while (isFirstImage == null && tryAgain == true)
		{
			if (cachedImages[slideShowNumber][newImageIndex] != null && cachedImages[slideShowNumber][newImageIndex].complete == true)
			{
				isCached = true;
				tryAgain = false;
			}
			else
			{
				if (newImageIndex == 1)
				{
					isCached = false;
					tryAgain = false;
				}
				else
				{
					newImageIndex = 1;
				}
			}
		}
	}
	
	if (isCached == true || isFirstImage != null)
	{
		slideShowCollection[slideShowNumber][0].currImage = newImageIndex;
		return slideShowCollection[slideShowNumber][newImageIndex];
	}
	else
	{
		isSameImage[slideShowNumber] = true;
		return slideShowCollection[slideShowNumber][(slideShowCollection[slideShowNumber][0].currImage)];
	}
}

/* Description: switches out the image and link for the specified slideshow
				and schedules the next image transition.  MSIE browser see the
				fancy image transitions, and Gecko users just see the new image.
   Inputs: slideShowNumber: the ID of the slideshow; valid value [1-n]
		   isCMRefresh: to skip the IE specific image transition pass any value in, otherwise don't pass a value
   Output: void */
function runSlideShow(slideShowNumber, isCMRefresh)
{
	var agt=navigator.userAgent.toLowerCase();
	
	var image;
	if (isCMRefresh == null) {image = getImage(slideShowNumber);}
	else {image = getImage(slideShowNumber, true);}
	
	if (isSameImage[slideShowNumber] == false)
	{
		// IE only filters
		if (document.all && isCMRefresh == null) {
			document.getElementsByName('SlideShow'+slideShowNumber)[0].style.filter = getTransTypeText(slideShowCollection[0].transitionDuration, slideShowCollection[0].transitionType);
			// Initiate the transition
			document.getElementsByName('SlideShow'+slideShowNumber)[0].filters[0].Apply();
		}
		
		// switch the link URL and image; applies to both NS + IE
		document.getElementsByName('SlideShow'+slideShowNumber)[0].src = image.path;

		// IE only filters
		if (document.all && isCMRefresh == null) {
			// Play the transition
			document.getElementsByName('SlideShow'+slideShowNumber)[0].filters[0].Play();
		}
	}
		
	if (agt.indexOf('msie') != -1 && isCMRefresh == null) //if Windows IE browser
	{
		timeoutIds[(slideShowNumber-1)] = setTimeout('runSlideShow('+slideShowNumber+')', ((image.showDuration*1000)+(slideShowCollection[0].transitionDuration*1000)) );
	} 
	else // if (agt.indexOf('gecko') != -1) or any other non-IE browser
	{
		timeoutIds[(slideShowNumber-1)] = setTimeout('runSlideShow('+slideShowNumber+')', (image.showDuration * 1000));
	}

}

/* Description: Handles the onClick event for the specified slideshow link.  The
				user is directed to the link associated with the currently displayed
				image.  If the current image does not have a link, then then no link
				is followed.
   Inputs: slideShowNumber: the ID of the slideshow; valid value [1-n]
   Output: void */
function slideShowLink(slideShowNumber)
{	
	var link = slideShowCollection[slideShowNumber][(slideShowCollection[slideShowNumber][0].currImage)].targetUrl;
	if (link == '') {return false;}
	else {document.getElementById('SlideShowLink'+slideShowNumber).href = link;}
}