// JavaScript Document
var timerSquare= new Array(3);
var timerFade= new Array(3);;
var oldSquare= new Array(3);
var duration = 0;
var durationLow = 2;
var durationHigh = 8;

var totalImages = 34;
var totalWords = 25;
var favorImagesPercentage = 85;

function switchImage(whichSquare) 
{
	var randomNumber;
	clearTimeout(timerSquare[whichSquare]);
	
	// Choose if image or word
	var randomSwitch = Math.floor(Math.random()*(100))+1;  // number from 0-100
	if (oldSquare[whichSquare]==0)
		randomSwitch = 0; // don't allow words on first round 
		
	if (randomSwitch < favorImagesPercentage) 
	{	
		// Choose random Image
		do 
		{
			randomNumber = Math.floor(Math.random()*(totalImages))+1;
		}
		while (randomNumber==oldSquare[whichSquare]);		
		var newFilename = "Images/homeSquares/homeImage_" + randomNumber + ".jpg";
	 } 
	else
	{
		//Choose random Word
		do 
		{
			randomNumber = Math.floor(Math.random()*(totalWords))+1;
		}
		while (randomNumber==oldSquare[whichSquare]);		
		var newFilename = "Images/homeSquares/homeWord_" + randomNumber + ".gif";
	}
	
	// Display new image or word
	
		/*if (oldSquare[whichSquare]!=0)
			setOpacity(0, whichSquare);  // Don't do on first round */
		
	if (oldSquare[whichSquare]!=0)
		document["square" + whichSquare].src = newFilename;
	
		/*if (oldSquare[whichSquare]!=0)
			fadeIn(whichSquare);  // Don't do on first round */
		
	// Calculate the duration 
	randomNumber = Math.floor(Math.random()*(durationHigh)) + 1 + durationLow;
	if (oldSquare[whichSquare]==0) randomNumber = Math.floor(randomNumber/1.5); // Shorter time for the first round
	duration = randomNumber;

	// remember which number is displayed in current square
	oldSquare[whichSquare] = randomNumber;
	
	// set new timeout
	timerSquare[whichSquare] = setTimeout("switchImage(" + whichSquare + ")", duration*1000);
}

function fadeIn(whichSquare) {
	//clearTimeout(timerFade[whichSquare]);
	for (var i=1;i<11;i++) {
		f = 'setOpacity(' + i + ',' + whichSquare + ')';
		timerFade[whichSquare] = setTimeout(f, 30*i);
	}
	return false;
}

function setOpacity(value, whichSquare)
{
	document["square" + whichSquare].style.opacity = value/10;
	document["square" + whichSquare].style.filter = 'alpha(opacity=' + value*10 + ')';
}


function initializeSquares()
{ 
	// preload Images
	var s="";
	for (i=1; i<=totalImages; i++)
	{
		if (i > 1) s = s + ",";
		s = s + "'Images/homeSquares/homeImage_" + i + ".jpg'";
	}
	for (i=1; i<=totalWords; i++)
	{
		s = s + ",'Images/homeSquares/homeWord_" + i + ".jpg'";
	}

	MM_preloadImages(s);


	// Initialize Squares
	oldSquare[1]=0;
	oldSquare[2]=0;
	oldSquare[3]=0;
	oldSquare[4]=0;
	
	switchImage(1);
	switchImage(2);
	switchImage(3);
	switchImage(4);
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; 
	for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

