/**********************************************************/
/* 					Fonction récurente  				  */
/**********************************************************/

function $E(id_bloc) {
	return document.getElementById(id_bloc);
}

function ajout_event(blocId, evt, fct){
	 if(blocId.attachEvent) blocId.attachEvent('on'+evt, fct); // IE
	 else blocId.addEventListener(evt, fct, true); // Compagnie
}
	
function supprim_event(blocId, evt, fct){
	 if(blocId.attachEvent) blocId.detachEvent('on'+evt, fct); // IE
	 else blocId.removeEventListener(evt, fct, true); // Compagnie
}

function getXHR() {
	var xhr = null;
	if (window.XMLHttpRequest || window.ActiveXObject) {
		if (window.ActiveXObject) {
			try {
				xhr = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
		} else {
			xhr = new XMLHttpRequest(); 
		}
	} 
	else {
		alert("Votre navigateur ne semble pas prendre en compte l'objet XMLHTTPRequest !");
		return null;
	}
	return xhr;
}

/**********************************************************/
/* 						  Menu  				  		  */
/**********************************************************/

function changeSelect(boxTexteOnglet, out) {
	var textOnglet = boxTexteOnglet.innerHTML;
	var decalageMax = 9;
	var sensDecal = 3;
	var d=0;
	if(!out) {
		var index = 50;
	}
	else {
		var index = '';
		boxTexteOnglet.style.zIndex = index;
	}
	if(!out) boxTexteOnglet.style.background = 'url("./images/bg_menu.gif") bottom right';

	function activeDecal() {
		d += sensDecal;
		
		if(d>=decalageMax) {
			sensDecal=-3;
			boxTexteOnglet.style.zIndex = index;
			if(out) boxTexteOnglet.style.background = 'url("./images/bg_menu.gif") bottom left';
		}
		
		if(d<0) {
			boxTexteOnglet.style.left = '0px';
			clearInterval(decalActiver);
		}
		else
			boxTexteOnglet.style.left = -d+'px';
	}
	var decalActiver = setInterval(function(){activeDecal()}, 40);
}

/**********************************************************/
/* 						Box Options  				  	  */
/**********************************************************/

function boxOptions(nbreBouton) {
	this.lance_openOptions;
	this.leftBox;
	this.sens;
	this.timer;
	this.timerAnim;
	this.decalage = 0;
	this.nbreBt = nbreBouton;
	this.angle = 360 / this.nbreBt;		
	this.posX;
	this.posY;
	this.rayon = 300;
	this.centreX = $E("pointCentre").offsetLeft;
	this.centreY = $E("pointCentre").offsetTop;
	this.lCentre = 50;
	this.lItem = 70;
	this.bouton = new Array();
		for(var i=0; i<this.nbreBt; i++) {
			var newBouton = document.getElementById("option" + i);
			this.bouton.push(newBouton);
			ajout_event(newBouton, 'mouseover', function(){boxOpt.stopOptions()});
			ajout_event(newBouton, 'mouseout', function(){boxOpt.playOptions()});
		}
}


function openOptions() {
	this.leftBox = parseInt($E('box_options').offsetLeft);
	if(this.leftBox == -640) {
		clearInterval(this.timer);
		this.sens = -4;
	}
	else {
		this.sens = 4;
		this.timer = setInterval('boxOpt.animOpt()',60);
	}
	this.lance_openOptions = setInterval('boxOpt.bougeBoxOptions('+this.sens+')', 30);
}
boxOptions.prototype.openOptions = openOptions;


function bougeBoxOptions(s) {
	var widthActuel = parseInt($E('box_options').offsetLeft)+s;
	if(this.sens>0 && widthActuel >= -640) {
		$E('box_options').style.left = '-640px';
		clearInterval(this.lance_openOptions);
	}
	else if(this.sens<0 && widthActuel <= -760) {
		$E('box_options').style.left = '-760px';
		clearInterval(this.timer);
		clearInterval(this.lance_openOptions);
	}
	else
		$E('box_options').style.left = widthActuel+'px';
}
boxOptions.prototype.bougeBoxOptions = bougeBoxOptions;
	
	
function animOpt() {
	this.decalage++;
	var numBouton=0;
	for(var b=-90; b < 270; b=b + this.angle)
	{
		var r = b + this.decalage;		
		this.posX = this.centreX + 25 + this.rayon * Math.cos(r*Math.PI / 180);
		this.posY = this.centreY + 25 + this.rayon * Math.sin(r*Math.PI / 180); 

		this.bouton[numBouton].style.top = (this.posY - this.lItem / 2) + "px"; 
		this.bouton[numBouton].style.left = (this.posX - this.lItem / 2) + "px";  
		numBouton++; 
	}	
}
boxOptions.prototype.animOpt = animOpt;
	
	
function playOptions() {
	this.timer = setInterval('boxOpt.animOpt()',60);
}
boxOptions.prototype.playOptions = playOptions;
	
	
function stopOptions() {
	clearInterval(this.timer);	   
}
boxOptions.prototype.stopOptions = stopOptions;

/**********************************************************/
/* 							Pop-in  				  	  */
/**********************************************************/

function creatpopin(optionChoisit, lienRetour, result_form)
{
	this.option = optionChoisit;
	this.lien_retour = lienRetour;
	if(result_form) this.resultForm = result_form;
	
	var bgConteneur = '<div id="bgPopin"></div>';
	$E('boxPourOptions').innerHTML = bgConteneur;
	
	this.boxBgPop = $E('bgPopin');
	this.newOpIE;
	if(this.newOpIE==null) {
		this.newOpIE = 0;
		this.bg_pop = setInterval(function(){ this.appliqBgPopin(this.lien_retour) }, 40);
	}
	else {
		this.boxBgPop.style.filter='alpha(opacity=60)';
		this.boxBgPop.style.opacity=0.6;
		this.contentPopin(this.lien_retour);
	}
}

function appliqBgPopin() {
	if (this.boxBgPop.style.filter!=null) {
		this.newOpIE += 5;
		this.boxBgPop.style.filter = 'alpha(opacity='+ newOpIE +')';
	}
	else {
		var newOp = this.boxBgPop.style.opacity - (-4 / 100);
		this.boxBgPop.style.opacity = newOp;
	}
	
	if(newOp > 0.6 || newOpIE>=60) {
		this.newOpIE = null; // pour un fondu à chaque ouverture
		this.boxBgPop.style.opacity=0.6;
		clearInterval(this.bg_pop);
		this.contentPopin(this.lien_retour);
	}
}
creatpopin.prototype.appliqBgPopin = appliqBgPopin;


function contentPopin() {
	var conteneur = '<div id="contenu_popin"></div>';
	$E('boxPourOptions').innerHTML += conteneur;
	var xhr = new getXHR();
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && xhr.status == 200){
			$E('contenu_popin').innerHTML = '<div id="croix_popin" class="cursor" onclick="closePopin(); "></div>'+ xhr.responseText +'';
		} 
		else if (xhr.readyState < 3){
			$E('contenu_popin').innerHTML = '<p style="color:#FFF;">En cours de chargement ...</p>';
		}
	}
		
	xhr.open("POST", "./include/popoptions.inc.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	if(this.resultForm) 
		var data_popin="option="+this.option+"&lien_retour="+this.lien_retour+"&result_form="+this.resultForm+"";
	else
		var data_popin="option="+this.option+"&lien_retour="+this.lien_retour+"";
    xhr.send(data_popin);
}
creatpopin.prototype.contentPopin = contentPopin;


function closePopin() {
	$E('boxPourOptions').innerHTML = '';
	boxOpt.openOptions();
}

/**********************************************************/
/* 					Dossier - fichier 				  	  */
/**********************************************************/

function openClose(blocDiv)
{
	// Hauteur standard 28px
	var elt = blocDiv.parentNode.parentNode;
	
	if(parseInt(elt.offsetHeight) == '28') {
		elt.style.height = 'auto';
		blocDiv.style.background = '#d7d7d7';		
	}
	else {
		elt.style.height = '28px';	
	}
}


function apercuKit(choice) {
	var boxApercuKit = $E('forApercuKit');

	if(!choice) {
		boxApercuKit.style.background = 'url("images/bg_apercu_kit.jpg")';
		boxApercuKit.innerHTML = '<p>Survolez une image pour voir son aperçu agrandit</p>';
	}
	else {
		boxApercuKit.innerHTML = '';
		boxApercuKit.style.background = 'url("./images/allapercu_futura_classic.jpg")';
	}
}

/**************************************************/
/*			Animation de Contact Rapid			  */
/**************************************************/

function AnimContactRapid() { $("#contactRapid").animate({height: "168px"}, 500, function(){AnimTitreCR();}); }
function AnimTitreCR() { $("#titreCR").animate({opacity: 1}, 500, function(){AnimTextCR();}); }
function AnimTextCR() {	$("#textCRun").animate({marginLeft:"15px", opacity:1} , 500); $("#textCRdeux").animate({marginLeft:"15px", opacity:1} , 500,function(){AnimFormCR();}); }
function AnimFormCR() { $("#form_contactRapid").animate({opacity:1}, 500); $("#coordon").animate({width:"206px"}, 500); }

function envoieCoordon() {
	var coord=$E('coordon').value;	
	var reg=new RegExp('^[a-z0-9@?\. -]+$', 'i');
	if(reg.test(coord.toLowerCase())) {
		var xhr = new getXHR();
		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4 && xhr.status == 200){ jAlert(xhr.responseText); }
		}
		xhr.open("POST", "./script_js/ajax/envoieCoord.php", true);
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xhr.send("coord="+coord+"");
	}
}