//Rotates the images on the Independent pages

//Pad the numbers with leading zeros
	function zeroPad(num,count) {
		var numZeropad = num + '';
		while(numZeropad.length < count) {
			numZeropad = "0" + numZeropad;
		}
		//alert (numZeropad);
		return numZeropad;
	}

	
//Remove path from current filename
	function zeroPath(imgNamePath) {
		var lastPathDelimiter = imgNamePath.lastIndexOf("/");
		var fileNameOnly = imgNamePath.substring(lastPathDelimiter+1); //Get the filename
		fileNameOnly = fileNameOnly.replace('la.jpg', ''); //Remove the suffix
		return fileNameOnly;
	}	

//Cache the up-coming large images
	var x;
	function cacheImage(fileNameOnly, imgCount) {
		for (x=1; x<3; x++) {
			var imgNameLength = fileNameOnly.length;	//How long is the string 
	   		var stemHyphenLeft = fileNameOnly.substring(0,fileNameOnly.indexOf("_")+1); //Get string left of and including the hyphen
			var imgNum = fileNameOnly.substring(fileNameOnly.indexOf("_")+1,imgNameLength); // Get string right of and excluding the hyphen
		
			var imgNumLength = imgNum.length; // Get length of the string suffix that is the number of the image - needed for adding padding zeros
			var imgNumNew = parseFloat(imgNum) + x; // Add i to the current image number
			if (imgNumNew == (imgCount + 1)) { //If we get to the last image reset the counter
				imgNumNew = 1;
			}		
			var imgNewNumStr = imgNumNew+''; // Turn the number back into a string
			var imgNewNumLength = imgNewNumStr.length; // How long is the string of the new number
			var leadingZeros = imgNumLength-imgNewNumLength; // Substract new length from the original lengh of the suffix to see how many zeors we need.
			var result = zeroPad(imgNumNew,leadingZeros + 1); // Pad the number with zeros using the zeroPad function and add 1
	
			var newIMG = stemHyphenLeft + result;
			
			//Preload Main image
			var cacherA = new Image();
			cacherA.src = 'img_db/' + newIMG +'la.jpg';
		}
	}

//Change images for products when available
	function imageNum(count, imgCount) {//imgCount=number of images on the page
	
		var imgNamePath = (document.getElementById('imgID_' + count).src); //Get the current image
		var fileNameOnly = zeroPath(imgNamePath);

		var imgNameLength = fileNameOnly.length;	//How long is the string 
   		var stemHyphenLeft = fileNameOnly.substring(0,fileNameOnly.indexOf("_")+1); //Get string left of and including the hyphen
		var imgNum = fileNameOnly.substring(fileNameOnly.indexOf("_")+1,imgNameLength); // Get string right of and excluding the hyphen
	
		var imgNumLength = imgNum.length; // Get length of the string suffix that is the number of the image - needed for adding padding zeros
		var imgNumNew = parseFloat(imgNum) + 1; // Add 1 to the current image number
		if (imgNumNew == (imgCount + 1)) { //If we get to the last image reset the counter
			imgNumNew = 1;
		}		
		var imgNewNumStr = imgNumNew+''; // Turn the number back into a string
		var imgNewNumLength = imgNewNumStr.length; // How long is the string of the new number
		var leadingZeros = imgNumLength-imgNewNumLength; // Substract new length from the original lengh of the suffix to see how many zeors we need.
		var result = zeroPad(imgNumNew,4); // Pad the number with zeros using the zeroPad

		var newIMG = stemHyphenLeft + result;
		
		//Preload and cache the up-coming Main image
		cacheImage(fileNameOnly, imgCount);
		
		
		//Change the main image
		//alert (newIMG);
	    document.getElementById('imgID_' + count).src = 'img_db/' + newIMG +'la.jpg'; // Change the Main Image to the new Main image
	    
	    var i;
	    for (i=1;i<imgCount;i++) { //Move the small images around

			var imgNumNew = parseFloat(imgNum) + i +1; // Add i to the current image number
			//alert ('The image num ' + (imgNumNew));
			if (imgNumNew > (imgCount)) { //If we get to the last image reset the counter
				imgNumNew = (imgNumNew-imgCount);			
			}
			//alert ("Div Id: " + i + " Image Number: " + imgNumNew );
			var imgNewNumStr = imgNumNew+''; // Turn the number back into a string
			var imgNewNumLength = imgNewNumStr.length; // How long is the string of the new number
			var leadingZeros = imgNumLength-imgNewNumLength; // Substract new length from the original lengh of the suffix to see how many zeors we need.
			var result = zeroPad(imgNumNew,4); // Pad the number with zeros using the zeroPad
			
			//alert (result);
			var newIMG = stemHyphenLeft + result;

			//Preload the next image
			var cacherNext = new Image();
			cacherNext.src = 'img_db/' + newIMG +'la.jpg';
			
			//Change the image
		    document.getElementById('imgID_' + i).src = 'img_db/' + newIMG +'sm.jpg'; // Change the Main Image to the new Main image	
	   	}	
	} 