﻿/*
	Image rotater script
	Programmed by Tad Renstrom
	Thirdwave, LLC
	For use by AIGA
	version:0.2.5
**************************************************************

	(This image rotate comes with preloading of images.
	 However if the images are overly large in size, and the rotate time is set to be relatively fast
	 the images may not load correctly)
	
**************************************************************
*/

/*
	Additional variables (General)

*/

//if order slide start number (0 is first Array element)
	var slideShowStart=0;

//if slide show should be random or in order (true for random, false for ordered/if slideshow set to false, this must be set to true to have random image on load or it will just desplay the first or what ever number is set in slideShowStart)
	var slideShowRandom=false;

//get array length and subtract 1 (***Do not Change***)
	var adjArLen=PicAr.length-1;
	
//Select random number (***Do not Change***)
	var randomnumber=Math.floor(Math.random()*adjArLen);
	
//Set pre load object (***Do not Change***)
	var getImg=new Image();

//if random
	if(slideShowRandom==true){
		//preload image
		getImg.src=pathToImg+PicAr[randomnumber];
	}else{
		//preload image
		getImg.src=pathToImg+PicAr[slideShowStart];
	}

/*
	Additional variables (for secondary images)
*/

//run secondary images once (false secondary images will rotate as well, true runs once only)
	var secImgsOnce=true;

//Secondary image preload array (***Do not Change***)
	var preLoadAr=new Array();
	
//Secondary image randomizer array (***Do not Change***)
	var secRandomAr=new Array();

//determine small image items (***Do not Change***)
	if(PicSmAr.length>0){
		for(y=0;y<PicSmAr.length;y++){
			//get length of array and subtract one
			setLength=PicSmAr[y].length-1;
			//randomize
			secRandomAr[y]=Math.floor(Math.random()*setLength);
			//preload the images from the array
			preLoadAr[y]=new Image();
			//set preload action
			preLoadAr[y].src=PicSmAr[y][secRandomAr[y]];
		}	
	}
	
	//run once canceler (if secondary images only run once cancel after ***do not change***)
	var secCXL=false;
	//repopulate var img if repeated (***do not change***)
	var repopulate=false;

/* 
	Functions to operate slide show
*/
	
//continues if slide show
	function continueSlides(){
		if(slideShowRandom==true){
			//select new random number
			randomnumber=Math.floor(Math.random()*adjArLen);
			//set path to random image
			randIMG=pathToImg+PicAr[randomnumber];
			//check to see if random image is same as current image
			if(document.getElementById("mainImg").src != randIMG){
				//if different image preload the image
				getImg.src=randIMG;
			}else{
				//if is the same image re-run function
				continueSlides();
			}
		}else{
			//set next image in array unless at end then goto begining
			if(slideShowStart<adjArLen){
				//add one to slide number
				slideShowStart++;
				
			}else{
				//reset to 0
				slideShowStart=0;
				
			}
			//preload image
			getImg.src=pathToImg+PicAr[slideShowStart];
		}
		
		//if run secondary images more than once
		if(secImgsOnce==false){
			if(PicSmAr.length>0){
				for(y=0;y<PicSmAr.length;y++){
					//get length of array and subtract one
					setLength=PicSmAr[y].length-1;
					//randomize
					secRandomAr[y]=Math.floor(Math.random()*setLength);
					//preload the images from the array
					preLoadAr[y]=new Image();
					//set preload action
					preLoadAr[y].src=PicSmAr[y][secRandomAr[y]];
				}	
			}
		
		}
		
		
		//set time amount
		setTimeLimit=timeSet+"000";
		//set the timeout for showing image
		setTimeout("intLoad()",setTimeLimit);
	}

//load the first image
	function intLoad(){
		document.getElementById(mainImgID).src=getImg.src;
		//if is slideshow start countdown
		if(slideShow==true){
			continueSlides();
		}
		//logic to run seconday images only once
		if(secCXL==false){
			//if secondary images to be run more than once clear out div
			if(secImgsOnce==false && repopulate==true){
				//replace image source with new image source for secondary images if secCXL=false
				for(yz=0;yz<preLoadAr.length;yz++){
					document.getElementById("secD"+yz).src=preLoadAr[yz].src;
				}
				
			}else{
				//create image and populate div
				//this runs only once on page load
				for(yz=0;yz<preLoadAr.length;yz++){
					//creates image tag object
					imgCreation=document.createElement("img");
					//set source for created image object
					imgCreation.setAttribute("src", preLoadAr[yz].src);
					//sets alt for created image object to blank
					imgCreation.setAttribute("alt", "");
					//sets id of created image objdect
					imgCreation.setAttribute("id", "secD"+yz);
					//adds created image to container id in variable on page script
					document.getElementById(secondaryImgToPop).appendChild(imgCreation);
				}
			}
			//if only run once cancel it before next run of function
			if(secImgsOnce==true){
				//keep secondary images from rotating
				secCXL=true;
			}else{
				//if secondary images are to rotate keeps them going
				repopulate=true;
			}
		
		}
		
	}

/*
	Function to run on window load
*/

//on load function 
window.onload=function(){
	intLoad();
}