/* The "Small Ads Player" */
/* Global variables (mostly arrays) used by the "Small Ads Player" program:

"smallAdHolder" - an array(6) of DIVs that display the "current" small ad (current is relative as there are 6 DIVs cycling the same images)
"smallAdFaderInner" - an array(6) of DIVs that fade in over the top of their respective "smallAdHolder"
"smallAdFadeLevel" - an array(6) of integers used by the "smallAdFadeIn" function to track and set fade levels
"currentSmallAd" - an array(6) of integers to keep track of which DIV is currently displaying which ad
"nextSmallAd" - an integer, used to keep track of which image is next
"smallAd[]" - an array that holds the URLs of all "small ad" images that PHP found on the server
"timerVariance" - the maximum number of milliseconds to be randomly added to the wait time before displaying the next ad image
"timerMinimum" - the amount of time (in milliseconds) to wait before loading the new ad images */

	var smallAdHolder = new Array();
	var smallAdFaderInner = new Array();
	var smallAdFadeLevel = new Array();
	var currentSmallAd = new Array();
	var nextSmallAd = 6;
	
	var timerVariance = 2500;
	var timerMinimum = 7000;
	
		
// A for loop (1 to 6) to set all global arrays to appropriate initial values

	for (var i=1; i<=6; i++)
		{
			smallAdHolder[i] = document.getElementById('smallAd'+i); // Set "smallAdHolder[1]" to reference the "currentSmallAd1" and so on...
			smallAdFaderInner[i] = document.getElementById('smallAdFader'+i); // Set "smallAdFaderInner[1]" to reference the "smallAdFader1" and so on...
			smallAdFadeLevel[i] = 0;
			currentSmallAd[i] = i-1; // currentSmallAd[1] is 0, currentSmallAd[2] is 1, currentSmallAd[3] is 2, etc.
			setOpacity(smallAdFaderInner[i], 0); // Hide all of the "smallAdFaderInner" DIVs
			smallAdHolder[i].style.backgroundImage = smallAd[i-1]; // Display small ads 0, 1, 2 etc. on their respective holders
		}
		



/* The "smallAdFadeIn" function: */
/* Once called, the "smallAdFadeIn()" function is called repeatedly (every 50 milliseconds) by the "showNextSmallAd" function
It will "loop" until the corresponding "smallAdTransition(1, 2, 3 etc.)" interval is cleared
Each time the function runs it will do one OR two things, based on the requested (whichHolder) "smallAdFadeLevel" array index
If the "smallAdFadeLevel[whichHolder]" index is less than 100, it will increase the corresponding "smallAdFadeLevel" by 10,
and set the opacity of the corresponding "smallAdFaderInner" to match
When "smallAdFadeLevel[whichHolder]" reaches 100, it will prepare to display the next image, and stop the "loop" */

		function smallAdFadeIn(whichHolder)
			{
				if (smallAdFadeLevel[whichHolder] < 100)
					{
						smallAdFadeLevel[whichHolder] += 7;
						(smallAdFadeLevel[whichHolder] > 100) ? smallAdFadeLevel[whichHolder] = 100 : null;
						setOpacity(smallAdFaderInner[whichHolder], smallAdFadeLevel[whichHolder]);
					}
				
				if (smallAdFadeLevel[whichHolder] == 100)
					{
						smallAdHolder[whichHolder].style.backgroundImage = smallAd[currentSmallAd[whichHolder]];
						setOpacity(smallAdFaderInner[whichHolder], 0);
						smallAdFadeLevel[whichHolder] = 0;
						window.clearInterval(window['smallAdTransition' + whichHolder]);
					}
			}

		
		function showNextSmallAd(whichHolder)
			{
			// Call the "smallAdFadeIn" function with a global interval variable named "smallAdTransition(1, 2, 3 etc.)"
				window['smallAdTransition' + whichHolder] = window.setInterval("smallAdFadeIn("+whichHolder+")", 41);
			}
			

		function loadNewAds()
			{
			// A for loop (1 to 6) that loads the new "smallAdFaderInner" (invisible) images
				for(var i=1; i<=6; i++)
					{
						smallAdFaderInner[i].style.backgroundImage = smallAd[nextSmallAd];
						
					/* Set each "currentSmallAd" array element to the "nextSmallAd" value BEFORE advancing the "nextSmallAd" variable
					This way the program can "remember" which ad is currently displayed in each "smallAdHolder" DIV after transition*/
						currentSmallAd[i] = nextSmallAd;
						nextSmallAd = (nextSmallAd + 1) % smallAd.length;
						
					/* Call the "showNextSmallAd" variable once for each "smallAdHolder" DIV,
					with an added delay of (timerVariance) milliseconds
					The "interval" variables are dynamically named by the "showNextSmallAd" function */						
						window['smallAdQued' + i] = window.setTimeout("showNextSmallAd("+i+")", Math.round(Math.random() * timerVariance));
					}
					
			}
