// JavaScript Document
<!--

var loadWall = new Class({

	Implements: [Options, Events],

	options: {
		container: undefined,
		rows: 2,
		columns: 4,
		labelImage: 'wall',
		extImage: 'gif',
		emptyImage: 'wall_empty.gif',
		maxSteps: 5,
		sPathImgWall: 'images/wall/',
		arAllImages: {},
		arLinkImages: {},
		arLinkImagesExt: {},
		arLink: {},/*
		arLinkExt: {},*/
		stepDuration: 200 //aumentando questo valore l'effetto risulta pių lento
	},

	initialize: function(options){
		this.setOptions(options);
		
		/* Setting global variables */
		this.allSetting = true;
		this.options.container = $(this.options.container);
		this.curStep = 0;
		this.stepsCount = 0;
		this.stepBlockImg = 2;
		this.stepsBlockImgCount = 0;
		this.stop = false;	
		this.stopAllDelay = false;
		this.arAllImg = this.options.arAllImages; //new Array();
		this.arAllImgLink = this.options.arLinkImages; //new Array();
		this.arImgLinkExt = this.options.arLinkImagesExt; //new Array();
		//this.arImgLinkExtBig = this.options.arLinkImagesExtBig; //new Array();
		this.arBoxWall = new Array();
				
		this.arAllImgTemp = new Array(this.arAllImg.length);
		this.arBoxWallTemp = new Array();

		/* Get ID by all wall's images */
		(this.options.container).getElements('img').each(function(item, index){
			this.arBoxWall[index] = item.getProperty('id');
			this.arBoxWallTemp[index] = item.getProperty('id');
		}, this);
		
		/*if(this.arLinkImages.length <= ((this.options.rows * this.options.columns)/2).round()) {
			var i;
			var arTempRow = new Array();
			for(i=0; i<this.options.rows
		}*/
		
		
		/* Create copy of array arAllImg and arBoxWall*/
		this.arAllImg.each(function(item, index){
			this.arAllImgTemp[index] = item;
		}, this);
		//this.arAllImgTemp = $A(this.arAllImg);
		//this.arBoxWallTemp = $A(this.arBoxWall);
		
		this.delayImgInBlock = parseInt(this.options.maxSteps / 2);
		this.delayImgBlocked = parseInt(this.options.maxSteps / 2);
		
	},
	
	/* 
	Questa funzione esegue tutte le operazioni.
	Per prima cosa Esegue il conteggio dei vari step e il conteggio degli step necessari per bloccare l'immagine sul muro.
	Successivamente recupera un blocco immagine casuale dal muro "var box = Math.floor($random(0, this.arBoxWallTemp.length-1))"
	e un immagine tra tutte quelle che abbiamo a disposizione "var img = Math.floor($random(0, this.arAllImgTemp.length-1))".
	L'immagine viene associata al blocco selezionato "$(this.arBoxWallTemp[box]).setProperty('src', this.options.sPathImgWall + this.arAllImgTemp[img])"
	e sia l'immagine che il blocco vengono esclusi dai rispettivi array:
	this.arBoxWallTemp.erase(this.arBoxWallTemp[box]);
	this.arAllImgTemp.erase(this.arAllImgTemp[img]);
	Prima di questa operazione viene effettuato un controllo per verificare che l'immagine non sia da associare in modo definitivo
	a tale blocco "if(this.stepsBlockImgCount < this.stepBlockImg) {". Questo perchč dopo un certo numero di steps, indicati dalla 
	variabile "stepBlockImg", l'immagine selezionata deve essere associata in modo definitivo al relativo blocco selezionato.
	Se cosė non fosse viene eseguita una funzione che permette dopo un certo "delay" di ripristinare il tutto, inserendo quindi 
	un'immagine vuota al blocco selezionato "$(boxIdTemp).setProperty('src', this.options.sPathImgWall + this.options.emptyImage);"
	e reinserendo nei rispettivi array sia il blocco che l'immagine:
	this.arBoxWallTemp.include(boxIdTemp);
	this.arAllImgTemp.include(imgTemp);
	*/
	executeStep: function() {
		this.stepsCount++;
		this.stepsBlockImgCount++;
		//alert(this.stepsCount);
		
		if (!this.stop) {
			var box = Math.floor($random(0, this.arBoxWallTemp.length-1));
			var img = Math.floor($random(0, this.arAllImgTemp.length-1));
			$(this.arBoxWallTemp[box]).setProperty('src', this.options.sPathImgWall + this.arAllImgTemp[img]);			
			
			if(this.stepsBlockImgCount < this.stepBlockImg) {
				var boxIdTemp = this.arBoxWallTemp[box];
				var imgTemp = this.arAllImgTemp[img];
				
				(
					function() { 
						if (!this.stopAllDelay) {  
							$(boxIdTemp).setProperty('src', this.options.sPathImgWall + this.options.emptyImage);
						}
						this.arBoxWallTemp.include(boxIdTemp);
						this.arAllImgTemp.include(imgTemp);
					}
				).delay(this.delayImgInBlock, this); //.delay(2000, this);
			}
			else {
				var boxIdTemp = this.arBoxWallTemp[box];
				var imgTemp = this.arAllImgTemp[img];
				
				(
					function(){ 
						if (!this.stopAllDelay) {  
							$(boxIdTemp).setProperty('src', this.options.sPathImgWall + this.options.emptyImage);
						}
						this.arBoxWallTemp.include(boxIdTemp);
						this.arAllImgTemp.include(imgTemp);
					}
				).delay(this.delayImgBlocked, this); //.delay(4000, this);
				//alert(this.arBoxWallTemp[box] +'--'+ this.arAllImgTemp[img]);
				this.stepsBlockImgCount = 0;
			}
			
			this.arBoxWallTemp.erase(this.arBoxWallTemp[box]);
			this.arAllImgTemp.erase(this.arAllImgTemp[img]);
		}	
		
		this.stepCompleted();
	},
	
	
	finalStep: function() {
		
//alert('finalStep');

		this.stopAllDelay = true;
		var i,j;
		var stepAdvancePeriodical;
		this.arIndexRow = new Array();
		
		this.arWall = new Array(new Array(this.options.rows)); //, new Array(this.options.columns));
		
		/*alert('this.arBoxWall.length::'+ this.arBoxWall.length);
		alert('this.arBoxWallTemp.length::'+ this.arBoxWallTemp.length);
		
		alert('this.arAllImg.length::'+ this.arAllImg.length);
		alert('this.arAllImgTemp.length::'+ this.arAllImgTemp.length);*/
		
		
		for (i=0; i<this.options.rows; i++) {
			this.arWall[i] = new Array(this.options.columns);
			for (j=0; j<this.options.columns; j++) {
				this.arWall[i][j] = j;
			}
		}
		
		//Calcola la frequenza di step con la quale inserire le immagini con i collegamenti alle pagine interne
		var stepsForImgLink = (this.arBoxWall.length / this.arAllImgLink.length).round(1);
		var iStepForImgLinkCount = stepsForImgLink;

		//Questa variabile č utilizzata per l'inserimento delle immagini per i collegamenti esterni.
		//Queste immagini saranno sempre di numero inferiore alle img con i collegamenti interni.
		var bShowImgExt = false;

		var iRowCount = 0;
		this.stepsCount = 0;

		var fncCreateArrayIndexRow = function(row) {
			var arReturn = new Array();
			for (i=0; i<row; i++) {
				arReturn[i] = i;
			}
			return arReturn;
		}

//alert('this.options.rows * this.options.columns::'+ (this.options.rows * this.options.columns));
		//var stepAdvance = function(fncToStopping){ 
		var stepAdvance = function(){ 
		
//alert('stepsCount::'+ this.stepsCount);
			//alert(this.stepsCount);
			if(iRowCount == 0) {
				//alert('create array arIndexRow');
				this.arIndexRow = fncCreateArrayIndexRow(this.options.rows);
			}
			
			this.stepsCount++; 
			iRowCount++;
			
			//alert('this.arIndexRow.length::'+ this.arIndexRow.length);
			
			var randomRow = Math.floor($random(0, this.arIndexRow.length-1));
			var rowIndex = this.arIndexRow[randomRow];
			//alert('rowIndex::'+ rowIndex);
			
			//alert('this.arWall[rowIndex].length::'+ this.arWall[rowIndex].length);
			var arColumn = this.arWall[rowIndex].filter(function(item, index){
				return item > -1;
			});
			//alert('arColumn.length::'+ arColumn.length);
			var randomColumn = Math.floor($random(0, arColumn.length-1));
			var columnIndex = arColumn[randomColumn];
			//alert('columnIndex::'+ columnIndex);
			
			this.arWall[rowIndex][columnIndex] = -1;
			this.arIndexRow.erase(rowIndex);
			
			if(iStepForImgLinkCount <= this.stepsCount || (this.arAllImgLink.length>0 && this.stepsCount==(this.options.rows * this.options.columns)))  {
				iStepForImgLinkCount+=stepsForImgLink;
				var img = Math.floor($random(0, this.arAllImgLink.length-1));
				$(this.arBoxWall[(rowIndex * this.options.columns) + columnIndex]).set('src', this.options.sPathImgWall + this.arAllImgLink[img]);
				//$(this.arBoxWall[(rowIndex * this.options.columns) + columnIndex]).getParent().setProperty('href', this.options.arLink[img]);
				$(this.arBoxWall[(rowIndex * this.options.columns) + columnIndex]).getParent().addClass('lnk');
				this.arAllImgLink.erase(this.arAllImgLink[img]);
				this.options.arLink.erase(this.options.arLink[img]);
				bShowImgExt = true;
			}
			/*else if(bShowImgExt && this.arImgLinkExt.length>0)  {
				var img = Math.floor($random(0, this.arImgLinkExt.length-1));
				//alert(this.options.sPathImgWall + this.arImgLinkExt[img] +'.'+ this.extImage);
				$(this.arBoxWall[(rowIndex * this.options.columns) + columnIndex]).setProperty('src', this.options.sPathImgWall + this.arImgLinkExt[img] +'.'+ this.options.extImage);
				$(this.arBoxWall[(rowIndex * this.options.columns) + columnIndex]).getParent().addClass('extlnk');
				$(this.arBoxWall[(rowIndex * this.options.columns) + columnIndex]).getParent().set('id', this.arImgLinkExt[img]);
				$(this.arBoxWall[(rowIndex * this.options.columns) + columnIndex]).getParent().set('href', this.options.arLinkExt[img]);
				$(this.arBoxWall[(rowIndex * this.options.columns) + columnIndex]).getParent().set('target', '_blank');
				this.arImgLinkExt.erase(this.arImgLinkExt[img]);
				this.options.arLinkExt.erase(this.options.arLinkExt[img]);
				bShowImgExt = false;
			}*/
			else { 	
				/*var img = Math.floor($random(0, this.arAllImg.length-1));
				$(this.arBoxWall[(rowIndex * this.options.columns) + columnIndex]).setProperty('src', this.options.sPathImgWall + this.arAllImg[img]);			
				this.arAllImg.erase(this.arAllImg[img]);*/
//alert('this.arAllImgTemp.length::'+ this.arAllImgTemp.length);
//alert('this.arBoxWall.length::'+ this.arBoxWall.length);
				var img = Math.floor($random(0, this.arAllImgTemp.length-1));
				$(this.arBoxWall[(rowIndex * this.options.columns) + columnIndex]).setProperty('src', this.options.sPathImgWall + this.arAllImgTemp[img]);			
				this.arAllImgTemp.erase(this.arAllImgTemp[img]);
			}
			
			
			if(iRowCount == this.options.rows) {
				iRowCount = 0;
			}
			
			if(this.stepsCount==(this.options.rows * this.options.columns)) {
				//$clear(stepAdvancePeriodical);
				//alert('stopp');
				this.finish();
			}
			else {
				//if(confirm('vai avanti')) stepAdvance();
				stepAdvance.delay(this.options.stepDuration);
			}
		}.bind(this);
		
		//stepAdvancePeriodical = stepAdvance.periodical(1000, this, stepAdvancePeriodical);
		stepAdvance();
		
	},
	
	
	start: function(steps){
		//this.cancel();
		this.curStep = 0;
		this.stepsCount = 0;
		this.stop = false;		
		
		if(this.allSetting) 
			this.executeStep();
		else 
			alert('Impossibile proseguire. Verificare le impostazioni iniziali definite.');
	},
	
	stepCompleted: function() {
		this.curStep++;
		
		//this.fireEvent('onStep', this.curStep);
		
		if (this.stepsCount < this.options.maxSteps) {
			this.executeStep.delay(this.options.stepDuration, this);
		}
		else {
			this.finalStep();
			//this.finish();
		}
	},

	finish: function() {
		this.stop = true;
		//this.back1Fx.cancel();		
		//this.back2Fx.cancel();
	
		/***
		For internal box link
		***/
		/*$$('a.lnk').each(function(item, index){
			var wmC = new classForInternalLink({
				objectParent: item,
				objectLnk: $('box_works')
			});
		});*/

		
		/***
		For external box link
		***/
		/*var arCls = new Array();
		
		// tooltips 
		var tips = new Tips();
		var tipURLs = [];
		var delayPeriodicalFunc = 2000;
		
		// grab all complete linked anchors 
		$$('a.extlnk').each(function(a, index) {
			var href = a.get('href');
			var id = a.get('id');
			if(!href.contains(window.location.host) && !tipURLs.contains(href)) {
				tipURLs.push(href);
				var url = href.replace(window.location.protocol + '//','');
				a.store('tip:title','');
				a.store('tip:text','<img src="images/wall/'+ id +'_big.gif" />');
				//a.addClass('screenshot');
				tips.attach(a);
			}
			
			delayPeriodicalFunc += 1000;
			arCls[index] = new classForExternalLnk(a, {startDelay:delayPeriodicalFunc});
			
			a.addEvents({
				'mouseenter': function(){
					this[index].finish();
				}.bind(arCls, index),
				'mouseleave': function(){
					this[index].start();
				}.bind(arCls, index)
			});

		});*/
		
		/*
		Metodo dichiarato e implementato esternamente
		*/
		this.loadingWallComplete();

	},	

	loadingWallComplete: function() {
		/*Questo metodo dovrebbe essere implementato esternamente e contiene 
		le istruzioni da eseguire alla fine di tutto il processo della Classe*/
	}
});


//-->