var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		pre_bt_print_over = newImage('images_simulateur/bt_print-over.gif');
		pre_bt_export_over = newImage('images_simulateur/bt_export_over.gif');
		pre_arrow_left_over = newImage('images_simulateur/arrow_left_over.gif');
		pre_arrow_right_over = newImage('images_simulateur/arrow_right_over.gif');
		preloadFlag = true;
	}
}

var curMaisonId=0;
var curMaison="";
var curTeinte="";
var curBump="";
var curTeinteName="";
var curBumpName="";
var curLight="";
var Gi, Gid, Gr, Gl;
function simuFull(){
	waiton();
	window.setTimeout("simuFull2();",100);
	return false;
}
function simuFull2(){
	var oldMaison="", oldTeinte="", oldBump="";
	j=document.simuF;
	if (j){
		/*
		if (oldMaison!=curMaison){
			j.setMaison(curMaison);
			oldMaison=curMaison;
		}
		*/
		if (oldTeinte!=curTeinte){
			j.setTeinte(curTeinte);
			oldTeinte=curTeinte;
		}
		if (oldBump!=curBump){
			j.setBump(curBump);
			oldBump=curBump;
		}
	}
	waitoff();
	return false;
}
function simuLight(i){
	waiton();
	Gi=i;
	window.setTimeout("simuLight2();",100);
	return false;
}
function simuLight2(){
	i=Gi;
	j=document.simuF;
	if (j){
		if (i!=curLight){
			j.setLight(""+i);
			curLight=i;
		}
	}
	waitoff();
	return false;
}
var winExport=null;
function exportImage(){
	waiton();
	winExport=window.open('exportView.htm', 'parexport',"width=520,height=390,scrollbars,resizable=1");
	winExport.focus();
	window.setTimeout("exportImageDo();",800);
	return false;
}
function exportImageDo(){
	a='px'+Math.round(100000+899999*Math.random());
	j=document.simuF;
	if (j) j.exportImage('exports/'+a+'.txt');
	winExport.location.href='exportView.php?img='+a;
	winExport.focus();
	waitoff();
}
function imprimeImage(){
	waiton();
	a='exports/px'+Math.round(100000+899999*Math.random())+'.txt';
	// w=window.open('exportView.php?img='+a, 'parexport',"width=492,height=350,toolbar,location,directories,status,menubar,scrollbars,resizable=1");
	w=window.open('exportView.htm', 'parexport',"width=520,height=390,scrollbars,resizable=1");
	w.focus();
	window.setTimeout("imprimeImage2();",800);
	return false;
}
function imprimeImage2(){
	j=document.simuF;
	if (j) j.exportImage(a);
	// tm[i]="Exemple1";tt[i]="Bâtiment individuel";to[i]="NORD";tr[i]="Rhône-Alpes";tp[i]="exe
	w.location.href='exportPrint.php?m='+tm[curMaisonId]+'&t='+curTeinte+'&tn='+curTeinteName+'&f='+curBump+'&fn='+curBumpName+'&r='+tr[curMaisonId]+'&o='+to[curMaisonId]+'&img='+a;
	waitoff();
}
function u(o){
	j=document.simu;
	if (j) j.setImage(o);
}
function v(o){
	j=document.simu;
	// window.alert("v j="+j+" o="+o);
	if (j) j.setBump(o);
}
var olda=0;
function selTeinte(i,id,r,l){
	waiton();
	// window.alert("selTeinte("+i+","+id+","+r+","+l+")");
	Gi=i; Gid=id; Gr=r; Gl=l;
	window.setTimeout("selTeinte2();",100);
	return false;
}
function selTeinte2(){
	i=Gi; id=Gid; r=Gr; l=Gl;
	// if (olda!=null)
	document.getElementById('plic'+olda).style.backgroundColor='white';
	a=document.getElementById('plic'+i);
	// window.alert(a);
	a.style.backgroundColor='black';
	olda=i;
	a=document.getElementById('ploc');
	// window.alert(a);
	a.innerHTML=r+' '+l;
	curTeinteName=r+'-'+l;
	curTeinte='../upload/simuCouleur/Photo/'+id+'/'+r+'.jpg';
	u(curTeinte);
	waitoff();
}
var oldb=0;
function selFinition(i,id,r,l){
	waiton();
	// window.alert("selFinition("+i+","+id+","+r+","+l+")");
	Gi=i; Gid=id; Gr=r; Gl=l;
	window.setTimeout("selFinition2();",100);
	return false;
}
function selFinition2(){
	i=Gi; id=Gid; r=Gr; l=Gl;
	// if (oldb!=null)
	document.getElementById('plaf'+oldb).style.backgroundColor='white';
	a=document.getElementById('plaf'+i);
	// window.alert(a);
	a.style.backgroundColor='black';
	oldb=i;
	a=document.getElementById('pluc');
	// window.alert(a);
	a.innerHTML=l;
	curBumpName=l;
	curBump='../upload/simuFinition/Photo/'+id+'/'+r;
	v(curBump);
	waitoff();
	return false;
}

var nbMatch=0;
var match=Array();
var ticoM=Array();
var iDecal=0;
function ini(){
	// waiton();
	document.getElementById('divRegion').innerHTML=tr[0];
	document.getElementById('divOrient').innerHTML=to[0];
	Matching();
	// waitoff();
}
function Matching(){	// remplit match avec ttes les maisons ki matchent
	nbMatch=0;
	f=document.forms['f'];
	sr=f.selRegion.value;
	st=f.selType.value;
	so=f.selOrient.value;
	for(i=0;i<nb;i++){
		match[i]=false;
		if (sr!="" && sr!=tr[i]) continue;
		if (st!="" && st!=tt[i]) continue;
		if (so!="" && so!=to[i]) continue;
		match[i]=true;
		ticoM[nbMatch]=i;
		nbMatch++;
	}
	iDecal=0;
	affiMaisons();
	// window.alert("nbMatch:"+nbMatch+"/"+nb);
}
function calcMaisons(){
	if (nbMatch<=4){ // no scroll
		document.images["arrow_left"].src='images_simulateur/arrow_left.gif';
		document.images["arrow_right"].src='images_simulateur/arrow_right.gif';
	}
	else{
		if (iDecal>0) document.images["arrow_left"].src='images_simulateur/arrow_left_over.gif';
		else document.images["arrow_left"].src='images_simulateur/arrow_left.gif';
		if (iDecal+4<nbMatch) document.images["arrow_right"].src='images_simulateur/arrow_right_over.gif';
		else document.images["arrow_right"].src='images_simulateur/arrow_right.gif';
	}
}
function affiMaisons(){
	waiton();
	calcMaisons();
	for(i=0;i<4;i++){
		ii=i+iDecal;
		if (i<nbMatch){
			iMaison=ticoM[ii];
			document.images["m"+(i+1)].src='/upload/simuMaison/Photo/'+ti[iMaison]+'/'+tp[iMaison];
		}
		else{
			document.images["m"+(i+1)].src='images_simulateur/nofoto.jpg';
		}
	}
	waitoff();
}
function g(){
	waiton();
	if (iDecal>0){
		iDecal--;
		affiMaisons();
	}
	waitoff();
	return false;
}
function d(){
	waiton();
	if (iDecal+4<nbMatch){
		iDecal++;
		affiMaisons();
	}
	waitoff();
	return false;
}
function c(a){
	waiton();
	Gi=a;
	window.setTimeout("c2();",100);
	return false;
}
function c2(){
	a=Gi;
	a--;
	a+=iDecal;
	if (a>=nbMatch){
		window.alert(a+" trop grand >"+nbMatch);
		return false;
	}
	iMaison=ticoM[a];
	maison='../upload/simuMaison/Photo/'+ti[iMaison]+'/'+tp[iMaison];
	maison=maison.replace('_THUMB','');
	masque=maison.replace('.jpg','.msk');
	// window.alert(maison+" - "+masque);
	j=document.simuF;
	if (j){
		if (maison!=curMaison){
			j.setMaison(maison, masque, to[iMaison]);
			curMaison=maison;
			curMaisonId=iMaison;
		}
	}
	document.getElementById('divRegion').innerHTML=tr[iMaison];
	document.getElementById('divOrient').innerHTML=to[iMaison];
	// window.alert("clic sur"+iMaison+" "+tm[iMaison]);
	waitoff();
}
function mous(e){
	window.status=""+e.x+","+e.y;
	return true;
}
function waiton(){
	document.getElementById("ledivwait").style.visibility="visible";
	// window.alert("waiton");
	document.getElementById("lebody").style.cursor="wait";
	// window.status="please wait....";
}
function waitoff(){
	document.getElementById("ledivwait").style.visibility="hidden";
	document.getElementById("lebody").style.cursor="default";
	// window.status="";
}
