/*var tilesH = $H({'tiles' :{
					1:[{'colour':'black','src':'/images/2009/home_tiles/After-Dark-Tickets.gif'}],
					2:[{'colour':'blue','src':'/images/2009/home_tiles/Animals-in-Action.gif'},
					   {'colour':'orange','src':'/images/2009/home_tiles/Animals-in-Action_2.gif'}],
					3:[{'colour':'red','src':'/images/2009/home_tiles/Be-a-Show-Friend.gif'}],
					4:[{'colour':'red','src':'/images/2009/home_tiles/Farm_Discovery_Trail.gif'},
					   {'colour':'green','src':'/images/2009/home_tiles/Farm_Discovery_Trail_2.gif'}],
					5:[{'colour':'orange','src':'/images/2009/home_tiles/free-activities.jpg'}],
					6:[{'colour':'green','src':'/images/2009/home_tiles/Great-Tasting-Experience-Stage.gif'},
					   {'colour':'orange','src':'/images/2009/home_tiles/Great-Tasting-Experience-Stage_2.gif'}],
					7:[{'colour':'orange','src':'/images/2009/home_tiles/Live-Music.gif'},
					   {'colour':'yellow','src':'/images/2009/home_tiles/Live-Music_2.gif'}],
					8:[{'colour':'orange','src':'/images/2009/home_tiles/Official-Show-Merchandise.gif'},
					   {'colour':'yellow','src':'/images/2009/home_tiles/Official-Show-Merchandise_2.gif'}]
				}
});

	*/		
			
			
	function dbug(inVal){
	
		var ttt = "inspect = " + Object.inspect(inVal) + "\n" +
					"isArray = " + Object.isArray(inVal) + "\n" +
					"isElement = " + Object.isElement(inVal) + "\n" +
					"isFunction = " + Object.isFunction(inVal) + "\n" +
					"isHash = " + Object.isHash(inVal) + "\n" +
					"isNumber = " + Object.isNumber(inVal) + "\n" +
					"isString = " + Object.isString(inVal) + "\n" +
					"isUndefined = " + Object.isUndefined(inVal) + "\n" +
					"keys = " + Object.keys(inVal) + "\n" +
					"toHTML = " + Object.toHTML(inVal) + "\n" +
					"toJSON = " + Object.toJSON(inVal) + "\n" +
					"toQueryString = " + Object.toQueryString(inVal) + "\n" +
					"values = " + Object.values(inVal) + "\n" 
		alert(ttt)
		}			
	
			
var clsTile = Class.create({   
	initialize: function(L, A, I, C) {     
		this.pLink = L;     
		this.pAlt = A;   
		this.pImage = I;
		this.pColour = C;
		this.pIndex = 0;
	},   
	isColourDifferent: function(col) {     
		if (Object.isArray(this.pColour)){
			for (i=0;i<this.pColour.length;i++){
				if (this.pColour[i] != col){
					this.pIndex = i;
					return true;
				}
			}
		}else{
			if (this.pColour != col){
				this.pIndex = 0;
				return true;
			}
		}
	},
	colour: function(){
		if (Object.isArray(this.pColour)){
			return this.pColour[this.pIndex];
		}else{
			return this.pColour;
		}
	},
	image: function(){
		if (Object.isArray(this.pImage)){
			return this.pImage[this.pIndex];
		}else{
			return this.pImage;
		}
	},
	toHTML: function(){
		if (this.pLink == 'http://www.rasv.com.au/membership.asp')
		{
			return '<a href="' + this.pLink + '" target="_blank"><img src="' + this.image() + '" alt="' + this.pAlt + '" /></a>';
		}
		else
		{
			return '<a href="' + this.pLink + '"><img src="' + this.image() + '" alt="' + this.pAlt + '" /></a>';		
		}
	}


}); 		

/*var  = function(i,col){
	var tile = tilesArr.get('root')[i];
	if (Object.isArray(tile['colour']))

	{
	}
	if (Object.IsArray(tile['colour'])){
		

	}
	//return true;
}*/

function populateRandomArray(){

	//randomArr.clear();

	if (randomArr.size() == 6){
		randomArr.shift();
		randomArr.shift();
		randomArr.shift();
		//randomArr.shift();
	}
	var randomNum;
	do
	{
		randomNum = Math.floor(Math.random() * tilesArr.size())
		if (!randomArr.any(function(n){
			return (randomNum == n)
			}) && tilesArr[randomNum].isColourDifferent(lastCol)){
			randomArr.push(randomNum);
			lastCol = tilesArr[randomNum].colour();
		}else{
			
		}

	}
	while (randomArr.size() < 6);
}




//populateRandomArray();


function changeTiles(){
//alert(randomArr.join());

	if(randomArr.size() == 3){
		populateRandomArray();
		populateRandomArray();
	};

	var el = $$('.tileBox')[index];
	new Effect.Fade(el,{
			duration : 0.3,
			from : 1,
			to : 0.2,
			afterFinish : function(){
				el.update(tilesArr[randomArr[index]].toHTML());
				new Effect.Appear(el, {
					duration : 0.8,
					from : 0.2,
					to : 1
				});
			}
		});

	index++;
	if (index >= 3){
		index = 0;
		populateRandomArray();
		//alert(randomArr.join());
	}

}

new PeriodicalExecuter(changeTiles, 2);