//window.onload = function(){nc_diapo.construct();};
addLoadEvent(function() {rounded_init()	} )
addLoadEvent(function(){nc_diapo.construct() } )


var nc_diapo = {
	
	/* variable d'initialisation */
	nbrImg:'',
	pulse:'',
	/*****************************/
	
	img : '',
	imgSize : 164,
	step : 30,
	left : 0,
	off : 0,
	diff : 0,
	animCtr : 0,
	/* tableau d'images */
	tabImg : new Array(),
	
	/* tableau des urls d'images à afficher */
	urlTab : new Array (
	 	'img/accueil/diapo01.jpg',
		'img/accueil/diapo02.jpg',
		'img/accueil/diapo03.jpg',
		'img/accueil/diapo04.jpg',
		'img/accueil/diapo05.jpg',
		'img/accueil/diapo06.jpg',
		'img/accueil/diapo07.jpg',
		'img/accueil/diapo08.jpg',
		'img/accueil/diapo09.jpg',
		'img/accueil/diapo10.jpg',
		'img/accueil/diapo11.jpg',
		'img/accueil/diapo12.jpg',
		'img/accueil/diapo13.jpg',
		'img/accueil/diapo14.jpg',
		'img/accueil/diapo15.jpg',
		'img/accueil/diapo16.jpg',
		'img/accueil/diapo17.jpg',
		'img/accueil/diapo18.jpg'
	),
	
	construct : function(){
		nc_diapo.tabImgInit();
		nc_diapo.matrixInit();
		nc_diapo.fadeInit();
	},	
	
	tabImgInit : function(){
		var img;
		for(i=0; i<nc_diapo.urlTab.length; i++){
			img = new Image();
			img.src = nc_diapo.urlTab[i];
			nc_diapo.tabImg[i] = { 'id' : 'img'+i, 'img' :	img, 'src' : img.src, 'state' : 0	};
			}	
			
	},
	
	random : function (state, tabName){
		var ctr = 0;
		var rand = 0;
		do{
			ctr++;
			rand = Math.floor((tabName.length)*Math.random());
			}while(tabName[rand]['state']!=state || ctr == 50);
		return rand;
	},
	
	matrixInit : function(){
		for (i=1; i<nc_diapo.nbrImg; i++){
		var rand = nc_diapo.random(0,nc_diapo.tabImg);
		if(i==2 || i==4 || i==6 || i==8){
			nc_diapo.tabImg[rand]['state'] = 1
		}else nc_diapo.tabImg[rand]['state'] = -1;
		document.getElementById('matrix'+i).innerHTML = '<img id="'+nc_diapo.tabImg[rand]['id']+'" src="'+nc_diapo.tabImg[rand]['src']+'" width="164" height="164" alt="" />';
		}
	},
	
	
	fadeInit : function(){
		for (i=2; i<nc_diapo.nbrImg; i=i+2){
		setTimeout('x_fade("matrix'+i+'","20:100:25")',i*250/2);
		}
		setTimeout('nc_diapo.switchImg()',3000);
		setTimeout('text_move()',3000);
	},
	
	switchImg : function(){
		var rand;
		var diff;
		//nc_diapo.check();
		if(nc_diapo.img == ''){
		rand =  nc_diapo.random(1,nc_diapo.tabImg);
		nc_diapo.tabImg[rand]['state'] = 4;
		nc_diapo.img = document.getElementById(nc_diapo.tabImg[rand]['id']);
		nc_diapo.img.style.position = "absolute"; /* en fait utile que pour ie8 qui apparement a due mal avec l'opacity du parent si l'enfant est positionner !!! */
		}
		
		if(nc_diapo.off == 0){
			if(nc_diapo.img.width - nc_diapo.step >=0 ){
			nc_diapo.img.width -= nc_diapo.step ;
			nc_diapo.left += nc_diapo.step/2;
			nc_diapo.img.style.left = nc_diapo.left+'px';
			if(nc_diapo.img.width > nc_diapo.imgSize*1/4){setTimeout('nc_diapo.switchImg()',60);}
			else{setTimeout('nc_diapo.switchImg()',60);}
			} else {
			nc_diapo.diff = nc_diapo.img.width;
			nc_diapo.img.width -= nc_diapo.diff; 
			nc_diapo.left += (nc_diapo.diff)/2;
			nc_diapo.img.style.left = nc_diapo.left+'px';
			rand =  nc_diapo.random(0,nc_diapo.tabImg);
			nc_diapo.tabImg[rand]['state'] = 5;
			nc_diapo.img.id = nc_diapo.tabImg[rand]['id'];
			nc_diapo.img.src = nc_diapo.tabImg[rand]['src'];
			nc_diapo.off = 1;
			setTimeout('nc_diapo.switchImg()',100);
			}
		}else{
			if(nc_diapo.diff != 0 ){
				nc_diapo.img.width +=  nc_diapo.diff;
				nc_diapo.left -=  nc_diapo.diff/2;
				nc_diapo.diff = 0;
				setTimeout('nc_diapo.switchImg()',10);
			}else{
				if(nc_diapo.img.width + nc_diapo.step <= nc_diapo.imgSize ){
				nc_diapo.left -= nc_diapo.step/2;
				nc_diapo.img.style.left = nc_diapo.left+'px';
				nc_diapo.img.width += nc_diapo.step;
				if(nc_diapo.img.width < nc_diapo.imgSize*3/4 ){setTimeout('nc_diapo.switchImg()',30);}
				else{setTimeout('nc_diapo.switchImg()',3);}
				}else{
					nc_diapo.off = 0; 
					nc_diapo.img = ''; 
					if(nc_diapo.animCtr < nc_diapo.pulse){
						nc_diapo.animCtr ++;
						setTimeout('nc_diapo.switchImg()',10); 
					}else{
						nc_diapo.animCtr = 0;
						setTimeout('nc_diapo.switchImg()',3000); 
						nc_diapo.initState();
					//	nc_diapo.check();
					}
				}
			}
		}
		
	},
	
	initState : function(){
		for(i=0; i<nc_diapo.tabImg.length; i++){
			if(nc_diapo.tabImg[i]['state'] == 2) nc_diapo.tabImg[i]['state'] = 0;
			if(nc_diapo.tabImg[i]['state'] == 3) nc_diapo.tabImg[i]['state'] = 1;
		}
		for(i=0; i<nc_diapo.tabImg.length; i++){
			if(nc_diapo.tabImg[i]['state'] == 4) nc_diapo.tabImg[i]['state'] = 2;
			if(nc_diapo.tabImg[i]['state'] == 5) nc_diapo.tabImg[i]['state'] = 3;
		}
	},
	
	check :function(){
		var str;
		for (i=1; i<nc_diapo.tabImg.length; i++){
			str += '\n'+nc_diapo.tabImg[i]['state']+'\n';
		}
		alert(str);
	}
}

// -------- Textes
var grille=new Array(-10,-1,-10,-1,-10,-1,-10,-1,-10),
	couleur=new Array('ffffff','ffffff','ffffff','ffffff','ffffff','ffffff','ffffff','ffffff','ffffff'),
	news=new Array(
		'<b>Conseil et§courtage</b>§en fichiers BtoB§Français et§internationaux'
		,'<b>Gestion</b>§de bases§de données'
		,'<b>Audit</b>§et connaissance§client'
		,'<b>Bienvenue</b>§Welcome§Willkommen§Bienvenida§Benvenuto'
	),
	i,x,y,n,flying=-1,
	max=2,			// nb de textes allumés simultanément
	delay=1600,		// delai entre 2 actions
	step=8,			// step alpha
	textes_init=false


// Affichage aléatoire des textes.
// Cette fonction et lancée à la fin de l'initialisation des photos
function text_move() {
	var t,i,j,todel

	if(!textes_init) {
		// génération des éléments HTML
		// Ils sont placés dans le conteneur 'textes-container' de la page HTML afin d'être positionnés au bon endroit
		textes_init=true
		var html='',container=document.getElementById('textes-container')
		if(container) {
			for(i in grille) {
				if(grille[i]!=-10) {
					y=Math.floor(i/3)
					x=i-3*y
					html+= '<table border="0" cellpadding="0" cellspacing="0" width="167" '
						+ 'id="flying-container-'+i+'" style="z-index:1;position:absolute;left:'+(11+x*175)+'px;top:'+(7+y*175)+'px"><tr>'
						+ '<td id="flying-'+i+'" style="text-align:center;vertical-align:middle;width:167px;height:167px;color:#'+couleur[i]
						+ ';font-size:17px">&nbsp;</td></tr></table>'
				}
			}
			container.innerHTML=html
		}
	}

	// détermination du texte à allumer
	flying++;if(flying==news.length) flying=0		// texte à afficher
	do { t=Math.floor((grille.length)*Math.random());if(t==grille.length) t-- } while(grille[t]>-1 || grille[t]==-10)	// endroit où le placer

	// détermination du texte à éteindre, s'il existe
	todel=flying-max; if(todel<0) todel+=news.length
	for(i in grille) if(grille[i]==todel) {
		x_fade('flying-container-'+i,'100:0:'+step)
		grille[i]=-1
	}

	// affichage du nouveau
	grille[t]=flying
	document.getElementById('flying-'+t).innerHTML=news[flying].replace(/§/g,'<br />')
	x_fade('flying-container-'+t,'0:100:'+step)	
	setTimeout("text_move()",delay)
}


