// JavaScript Document
var logo;
var action=false;
var bar;
var widthScreen;
var nav="";
var prodMachine;
var height;
var ajax;
var iframe;
var fadeNext = false;

//variable necessaire au fonctionnement de la transition des maximes
var maximeTab;				//contient les maximes
var maximeDiv;				//conteneur des maximes
var maximeBool;				//boolean qui dit si on doit effacer la maxime ( au passage vers l'index 2)
var maximeIntervalle;		//variables qui donne les limites du cadre d'intro ou les maxime ne doivent pas s'afficher


//<![CDATA[	
function createRequestObject(){
    var tp = null;
    if (window.XMLHttpRequest){ // Mozilla, Safari, IE7 ...
        tp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject){ // Internet Explorer 6
        tp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return tp;
}


function resizeBar(){
	if(!bar){
		bar = document.getElementById('loadingIndex');
	}
	bar.style.width=document.body.clientWidth+"px";
}

function prechargementPage(){
	if (ajax.readyState > 0 ){
		if(action==false){
			action=true;
			//à la connexion avec le serveur on debute le chargement de la barre
			loadingBarIndex();	
		}
	}
	if (ajax.readyState == 4 && ajax.status != 200){
		alert('probleme de chargement');	
	}
	if (ajax.readyState == 4 && ajax.status == 200){
		logo.style.visibility="visible";
		prodMachine.style.visibility="visible";
		setTimeout("fadeIndex()",300);
	}
}

function findContainer(){
	prodMachine=document.getElementById('prodMachineIndex');
	widthScreen=document.body.clientWidth;
	bar=document.getElementById('loadingIndex');
	logo=document.getElementById('aLogoIndex');
	iframe=document.getElementById('accueil');
	maximeDiv=document.getElementById('maxime');
}

function onLoadScript(){
	
	//tableau contenant les maximes
	//maximeTab = new Array("Quand la fabrication passe par les sens,<br/>elle devient un plaisir");
	//maximeTab = new Array('<img src="http://www.prodmachine.fr/Design/citation.jpg" alt="Prodmachine - Bureau de fabrication">');
	maximeTab = new Array('<img src="http://www.prodmachine.fr/Design/transp.gif" alt="Prodmachine - Bureau de fabrication">');
	maximeBool = true;
	
	maximeIntervalle = new Array();

	//intervalle pour calcul position aleatoire
	/*maximeIntervalle.push((document.documentElement.clientWidth-(((30*document.documentElement.clientWidth)/100)+500))-200);
	maximeIntervalle.push(maximeIntervalle[0]+500+200);
	maximeIntervalle.push(100);
	maximeIntervalle.push(500);*/
	
	ajax = createRequestObject();
	findContainer();
	if(ajax!=null){
		ajax.open('GET', 'index2.php' , true);
			
		ajax.onreadystatechange = prechargementPage;
		
		ajax.send(null);
	}
	//si on est pas en mode chargement ajax on demarre la progressbar
	else{
		loadingBarIndex();
	}
	
	setTimeout("maxime(1)",10);
}
//fonction qui gere tout l'affichage des maxime ( calcul positionnement, et transition )
function maxime(state){
	//tant que il n'y a pas une action en cour on effectue les apparition et disparition de maxime
	if(action==false){
		//tant que l'on ne fait pas le fondu avec la page d'apres
		switch (state){
		//calcul du positionnement
		case 1:
		//posiionnement aleatoire
			/*var x=maximeIntervalle[0]+1;
			var y=maximeIntervalle[2]+1;
			
			//tant que l'on est dans le cadre interdit on recalcule les variable aleatoire de positionnement
			while(x>maximeIntervalle[0]){	
				x = Math.floor(Math.random()*(document.documentElement.clientWidth-200));
				y = Math.floor(Math.random()*(document.documentElement.clientHeight-100));
			}*/
			
			maximeDiv.style.left = 100+"px";
			maximeDiv.style.top = 220+"px";

			//maximeDiv.innerHTML = maximeTab[Math.floor(Math.random()*(maximeTab.length-1))];
			maximeDiv.innerHTML = maximeTab[0];

			setTimeout("maxime(2)",10);
		
			break;
		//fait apparaitre le bloc
		case 2:
			if(nav!='IE'){
				if(maximeDiv.style.opacity <0.8){
					maximeDiv.style.opacity=parseFloat(maximeDiv.style.opacity)+0.05;
					setTimeout("maxime(2)",10);
				}
			}
			else{
				if(maximeDiv.filters.alpha.opacity < 80){
					maximeDiv.filters.alpha.opacity=parseInt(maximeDiv.filters.alpha.opacity)+5;
					setTimeout("maxime(2)",10);			
				}
			}
			break;		
		}
	}
	else{
		setTimeout("maxime("+state+")",120);	
	}
}

function loadingBarIndex(){
	widthScreen=document.body.clientWidth;
	if(ajax!=null){
		var x = parseInt(Math.random()*(ajax.readyState*10));
		var maxX = (widthScreen*ajax.readyState)/4;
	}
	//si on est pas en mode chargement ajax on ne s'occupe pas de l'avancement du chargement
	else{
		var x = parseInt(Math.random()*40);
		var maxX = widthScreen;	
	}
	
	if((parseInt(bar.style.width)+x)<widthScreen && (parseInt(bar.style.width)+x)<maxX){
		bar.style.width=(parseInt(bar.style.width)+x)+"px";
		setTimeout("loadingBarIndex()",2);
	}
	else{
		if(parseInt(bar.style.width)+1<widthScreen && (parseInt(bar.style.width)+1)<maxX){
			bar.style.width=(parseInt(bar.style.width)+1)+"px";
			setTimeout("loadingBarIndex()",2);
		}
		else
			if((parseInt(bar.style.width)+1)>=maxX){
				setTimeout("loadingBarIndex()",50);
			}
			else{
			var strChUserAgent = navigator.userAgent;
			var intSplitStart = strChUserAgent.indexOf("(",0);
			var intSplitEnd = strChUserAgent.indexOf(")",0);
			var strChStart = strChUserAgent.substring(0,intSplitStart);
			var strChMid = strChUserAgent.substring(intSplitStart, intSplitEnd);
			var strChEnd = strChUserAgent.substring(strChEnd);
			if(strChMid.indexOf("MSIE") != -1)
				nav = "IE";
				
			//si on est pas en mode chgarement ajax on affiche le logo
			if(ajax==null){
				setTimeout("fadeIndex()",300);	
			}
		}
	}
}


function fadeIndex(){
	if(nav!='IE'){
		if(logo.style.opacity < 1){
			logo.style.opacity=parseFloat(logo.style.opacity)+0.02;
			prodMachine.style.opacity=parseFloat(prodMachine.style.opacity)+0.02;
			setTimeout("fadeIndex()",10);
		}
		else{
			action=false;	
		}
	}
	else{
		if(logo.filters.alpha.opacity < 100){
			logo.filters.alpha.opacity=parseInt(logo.filters.alpha.opacity)+2;
			prodMachine.filters.alpha.opacity=parseInt(prodMachine.filters.alpha.opacity)+2;
			setTimeout("fadeIndex()",10);			
		}
		else{
			action=false;
		}
	}
}

function openLogo(){
	//si il n'y a aucune action ou si on ferme le menu
	if(action==2 || action==false){
		action=1;
		changeSizeLogo(1);
	}
}

function closeLogo(){
	//si il n'y a aucune action ou si on ouvre le menu
	if(action==1 || action==false){
		action=2;
		changeSizeLogo(2);
	}
}

function changeSizeLogo(state){
	if(state==1 && action==1){
		if(parseInt(logo.style.width)<300){
			logo.style.width = (parseInt(logo.style.width)+10)+"px";
			logo.style.height = (parseInt(logo.style.height)+10)+"px";
			setTimeout("changeSizeLogo(1)",15);
		}
		else{
			action=false;	
		}
	}
	if(state==2 && action==2){
		if(parseInt(logo.style.width)>100){
			logo.style.width = (parseInt(logo.style.width)-10)+"px";
			logo.style.height = (parseInt(logo.style.height)-10)+"px";
			setTimeout("changeSizeLogo(2)",15);
		}
		else{
			action=false;	
		}
	}
}

function startOpenMenu(){
	//si aucune action n'est en route, on ouvre le menu sur la seconde page, et on arrete les apparition de maxime 
	if(action==false){
		if(ajax!=null){
			action=true;
			maximeBool=false;
			openMenu();
		}
		else{
			window.location.replace("http://www.prodmachine.fr/index3.php");
		}
	}
}

function openMenu(state){
	if(state==null){
		if(nav!='IE'){
			if(logo.style.opacity >0){
				logo.style.opacity=parseFloat(logo.style.opacity)-0.04;
				prodMachine.style.opacity=parseFloat(prodMachine.style.opacity)-0.04;
				bar.style.opacity=parseFloat(prodMachine.style.opacity)-0.04;
				setTimeout("openMenu()",10);
			}
			else{
				iframe.style.display="none";
				iframe=document.getElementById('fenetreSuivante');
				
				iframe.innerHTML = ajax.responseText;
				
				var allscript = iframe.getElementsByTagName('script');
				for(var i=0;i< allscript.length;i++){
					eval(allscript[i].text);
				}
				
				setTimeout("openMenu(1)",10);
			}
		}
		else{
			if(logo.filters.alpha.opacity > 0){
				logo.filters.alpha.opacity=parseInt(logo.filters.alpha.opacity)-4;
				prodMachine.filters.alpha.opacity=parseInt(prodMachine.filters.alpha.opacity)-4;
				bar.filters.alpha.opacity=parseInt(bar.filters.alpha.opacity)-4;
				setTimeout("openMenu()",10);			
			}
			else{
				iframe.style.display="none";
				logo.style.display="none";
				prodMachine.style.display="none";
				bar.style.display="none";
				iframe=document.getElementById('fenetreSuivante');
				
				iframe.innerHTML = ajax.responseText;
				
				var allscript = iframe.getElementsByTagName('script');
				for(var i=0;i< allscript.length;i++){
					eval(allscript[i].text);
				}				
				setTimeout("openMenu(1)",10);
			}
		}
	}
	if(state==1){
		if(nav!='IE'){
			if(iframe.style.opacity <1){
				iframe.style.opacity=parseFloat(iframe.style.opacity)+0.05;
				setTimeout("openMenu(1)",20);
			}
			else{
				action=false;
				//ouvrture du menu à l'affichage de la page
				//slideMenu(1) ->ouvrir menu
				//slideMenu(2) -> fermer menu
				slideMenu(1);
			}
		}
		else{
			if(iframe.filters.alpha.opacity<100){
				iframe.filters.alpha.opacity=parseFloat(iframe.filters.alpha.opacity)+5;
				setTimeout("openMenu(1)",20);			
			}
			else{
				action=false;
				//ouvrture du menu à l'affichage de la page
				slideMenu(1);
			}
		}
	}
}


//]]>