// Java Document
//
// This is a simple java slide show.
// It use the HTML Elements DIV and IMG to create a window to display an array of slides.
// To keep it simple, the following restriction must be followed:
// 		The DIV and IMG dimentions must be the same size to keep the div background from showing.
//		The DIV and IMG must have a element ID that is passed to idenify the two elements.
//		The all the images should be the must size as the img to prevent image distortion.
//      The image file names must be sequential in order and last image file passed on the intialize call.
//
// Has a temporary tracking cookie support to allow the slides to continue on new web page.
// The text of the current slide name is optional.
// Pause and Play controls are provide.
//
// For IE, the IE tranform function are used to transistion the slides. 
// For Mozilla browsers, the DIV background is for the next image and the IMG is faded out to display it.  
//
// 07/25/2011 - Updated for Chrome and fixed some minor bugs. 
// 05/01/2011 - Added the Play controls.
//
// Designed by the IHN Tech Team for use by Non-profits.
//
var BrowserType  = 0;							// Browser type
var NextSlide    = -1;
var DisplayTime  = 4500;
var FadeStepTime = 25;
var Timer;
var SlideImage,   DivImage, SlideText, FormatPad;
var PlaySlides = 1;

function initSlides(DivId, ImageId, LastImage,TextId ) {
	DivImage     = document.getElementById(DivId);	
	SlideImage   = document.getElementById(ImageId);
	SlideText    = document.getElementById(TextId);	
	LastSlide    = LastImage;

	// The LastImage String ("Slides/IhnHomePage-60.jpg") is parsed path and last slide
	PrefixIx     = LastImage.lastIndexOf("_");              		// Where dash(-) is  
	TypeIx       = LastImage.lastIndexOf(".");		    			// Where delimiter(.) is
	SlideImgPath = LastImage.substring(0,PrefixIx+1);				// Extract Path
	LastSlide    = parseInt(LastImage.substring(PrefixIx+1,TypeIx));//  Last File Number string
	FileType     = LastImage.substring(TypeIx);						//  Type type(jpg)
	
	// Figure out how many digits to format slidename.
	FormatPad = TypeIx - PrefixIx - 1;   
	
	// Try to get the index from a Cookie
	//NextSlide = readCookie("IhnSlideIndex");		// Save Index. 
	if(NextSlide == -1) NextSlide    = LastSlide;	//  Init to last slide to force reset to start

	// Initalized the Img and Div opacity
	SlideImage.style.opacity = 1.0;
	DivImage.style.opacity   = 1.0;

	//BrowserType = browserIndex();
	var UserAgent = navigator.userAgent;
	if     (UserAgent.indexOf("MSIE") >= 0)   BrowserType = 1;	// InternetExplorer
	else if(UserAgent.indexOf("Mozilla") >= 0)BrowserType = 2;	// Firefox, Safari, Chrome
	else                                      BrowserType = 0;	// Others.
	
	DisplayTime = 4000;		// Default time of 4.5 seconds + .5 second transition.
							// Start or restart the slide show.
							
	PlaySlides = 0;	// Display msg first.
	Timer = 0;
	//NextSlide = 68;

//	if(BrowserType == 1) {	// MSIE 
//		Timer = setTimeout("transform()",0500);	
//	}
//	else { 					// Others
//		DivImage.style.backgroundImage =  "url("+getNextSlide()+")"; 	// New background Image
//		Timer = setTimeout("fadeOutLoop()",0500);						// Wait second before the show starts.
//	}
}
// Increment to the next slide.
// Handles File name range 1 to 999 depending on Range
// Format name with leading 0's	
function getNextSlide() {
	if(++NextSlide >= LastSlide) NextSlide = 1;			// Reset Slide Increment

	SlideString = NextSlide.toString();
	SlideNumString = ('000' + SlideString).slice(-FormatPad);
	SlideName = SlideImgPath+SlideNumString+FileType;
	
	//writeCookie("IhnSlideIndex",NextSlide);		// Save Index if tracking between pages. 

	if(SlideText != undefined) {					// Optional update Slide name on HTML Element
		SlideRootIx  = SlideName.lastIndexOf("/");
		SlideText.innerHTML = SlideName.substring(SlideRootIx+1);
	}
	return SlideName;
}

// Slide Controls.
// Toggles the timer and changes the text on the control button. 
// The button is defined as  <button onclick="slidecontrol(this)">Pause</button>
function slidecontrol(buttonObject) {
	if (PlaySlides > 0 ) {
		PlaySlides = 0;
		buttonObject.innerHTML = "Play";
	}
	else {
		PlaySlides = 1;
		buttonObject.innerHTML = "Pause";				
		if(Timer == 0) {							// Is timer running?
			if(BrowserType == 1)  transform();
			else                  fadeNextImage();
		}
	}
}

// This Function supports is on IE6 and greater to transform filter to display the images.
// Some transform have two different actions an even and odd tranform selection is use.
// The different transfroms are cycle every 8 slides
var FilterArray =  ["Fade()",
					"Fade()",
					"GradientWipe(GradientSize=0.25,motion=forward,wipestyle=0)",
					"GradientWipe(GradientSize=0.25,motion=reverse,wipestyle=0)",
 					"Iris(irisStyle=CIRCLE, motion=out)",
					"Iris(irisStyle=CIRCLE, motion=out)",
					"Stretch(stretchStyle='push')",
					"Stretch(stretchStyle='push')",
					"Slide(bands=1, slideStyle=HIDE)",
					"Slide(bands=1, slideStyle=HIDE)"];

var SlideCount  = 0;
var FilterIndex = 0;

function transform(){
	Timer = 0;								// Clear timer object
	if(PlaySlides == 0) return;				// Paused Slide Show>
	
	// Queue up the next slide and cycle through the different transistions. 
	++SlideCount;						// Slide Counter
	if((SlideCount % 8) == 0) {			// switch Transistion type every 8th picture
		FilterIndex += 2;				// Increment to next transformation
		if(FilterIndex >= FilterArray.length) {
			FilterIndex = 0;
			SlideCount  = 0;
		}
	}	
	Index = FilterIndex + (SlideCount % 2)			
	FilterString = "progid:DXImageTransform.Microsoft."+FilterArray[Index];
	
	SlideImage.style.filter = FilterString;
	SlideImage.filters.item(0).Apply();					// Get Ready
	SlideImage.src = getNextSlide();					// New Image
	SlideImage.filters.item(0).Play(1.0); 				// Transition time

	Timer = setTimeout("transform()",DisplayTime);		// restart Timer
}

// For Firefox, Safari and Chrome (Mozilla browsers)
// Update Div Image and then fade out IMG.
// Fixed rounding error on FadeLevel to 2 decimal places.
var FadeType = 0;				// 0 = FadeOut, 1 = FadeIn

// Set the Div Background image and start fading out the IMG. 
function fadeNextImage() {
	Timer = 0;													// Reset Timer
	if(PlaySlides ==  0) return;								// Slides playing?
	
	else {
		SlideImage.src = getNextSlide();						// Set IMG Element for next slide.
		Timer = setTimeout("fadeNextImage()",DisplayTime);		// Queue Up Display Timer. 
	}
}


