function rollOnglet(id,mode) {
	if(mode == "on") {
		document.getElementById("home_onglet_g_"+id).className = "home_onglet_g_hover";
		document.getElementById("home_onglet_fond_"+id).className = "home_onglet_fond_hover";
		document.getElementById("home_onglet_d_"+id).className = "home_onglet_d_hover";
		document.getElementById("home_titre_onglet_"+id).className = "home_titre_onglet_hover";
	}
	else {
		document.getElementById("home_onglet_g_"+id).className = "home_onglet_g";
		document.getElementById("home_onglet_fond_"+id).className = "home_onglet_fond";
		document.getElementById("home_onglet_d_"+id).className = "home_onglet_d";
		document.getElementById("home_titre_onglet_"+id).className = "home_titre_onglet";
	}
}

function selectBlocAnimation(id,disable) {
	var text = document.getElementById('content_animation_text_' + id).value;
	var visuel = document.getElementById('content_animation_visuel_' + id).value;
	var linkType = document.getElementById('content_animation_linkType_' + id).value;
	var link = "http://" + document.getElementById('content_animation_link_' + id).value;
	var type = document.getElementById('content_animation_type_' + id).value;
	var textDisplay = "";

	if(trim(link) == "") {
		document.getElementById('dynamic_animation').onclick = function(){};
	}
	else {
		document.getElementById('dynamic_animation').onclick = function(){
			if(linkType == "self") {
				document.location = link;
			}
			else {
				window.open(link);
			}
		};
	}
			
	if(trim(text) != "") {
		textDisplay += "<div style='position:absolute;z-index:100'><table style='width:100%;height:295px'>";
			textDisplay += "<tr>";
				textDisplay += "<td style='padding-left:35px;vertical-align:bottom'>";
					textDisplay += "<table>";
						textDisplay += "<tr>";
							textDisplay += "<td style='width:10px;background-color:#f8982a'><td>";
							textDisplay += "<td class='text_dynamic_animation' style='width:455px;padding-bottom:10px;'>";
								textDisplay += text;
							textDisplay += "<td>";
						textDisplay += "</tr>";
					textDisplay += "</table>";
				textDisplay += "</td>";
			textDisplay += "</tr>";
		textDisplay += "</table></div>";
	}
	
	switch(type) {
		case "Visuel":
			document.getElementById('dynamic_animation').style.background = 'url(' + visuel.replace("../","./") + ') no-repeat';
			break;
		case "Diaporama":
			var diapos = document.getElementById('content_animation_diapo_' + id).value;
			var tabDiapos = diapos.split(";");

			textDisplay += '<div style="position:absolute;z-index:10"><div id="rotator">';
			for(var diapo in tabDiapos) {
				textDisplay += '<img src="' + tabDiapos[diapo] + '" alt="" />';
			}
			textDisplay += '</div></div>';
		
			document.getElementById('dynamic_animation').style.background = "";
			break;
		case "Animation flash":
			break;
	}

	document.getElementById('dynamic_animation').innerHTML = textDisplay;
	
	if(type == "Diaporama" && !timeoutLaunch) {
		so_init();
	}
	else if(type != "Diaporama") {
		timeoutLaunch = false;
	}
	
	var cpt=0;
	
	while(document.getElementById("bloc_animation_" + cpt)) {
		if((cpt != id) || !disable) {
			changeBlocAnimation(cpt,"off");
			
			document.getElementById("bloc_animation_" + cpt).onmouseover = function(){
				var temp = this.id.split("_");
				changeBlocAnimation(temp[2],"on");
			};
			document.getElementById("bloc_animation_" + cpt).onmouseout = function(){
				var temp = this.id.split("_");
				changeBlocAnimation(temp[2],"off");
			};
		}
		cpt++;
	}
	
	if(disable) {
	document.getElementById("bloc_animation_" + id).onmouseover = function(){}
	document.getElementById("bloc_animation_" + id).onmouseout = function(){}
	}
}

function changeBlocAnimation(id,mode) {
	if(mode == "on") {
		if(id == "0") {
			document.getElementById("bloc_animation_" + id).className = "animation_element_first_hover";
		}
		else {
			document.getElementById("bloc_animation_" + id).className = "animation_element_hover";
		}
		document.getElementById("bloc_animation_titre_" + id).className = "animation_element_titre_hover";
		document.getElementById("bloc_animation_texte_" + id).className = "animation_element_texte_hover";
	}
	else {
		if(id == "0") {
			document.getElementById("bloc_animation_" + id).className = "animation_element_first";
		}
		else {
			document.getElementById("bloc_animation_" + id).className = "animation_element";
		}
		
		document.getElementById("bloc_animation_titre_" + id).className = "animation_element_titre";
		document.getElementById("bloc_animation_texte_" + id).className = "animation_element_texte";
	}
}

function trim(myString)
{
	return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')
}

function emailValid(email)
{
	var place = email.indexOf("@",1);
	var point = email.indexOf(".",place+1);
	
	if ((place > -1)&&(email.length >2)&&(point > 1)) {
		return true;
	}
	else {
		return false;
	}
}

function ajaxLoader() {
	this.actualActuBloc = 1;
	this.actualBureau = 1;
}

ajaxLoader.prototype.loadNextBureau = function(lang) {
	this.actualBureau++;
	this.callAjax("getBureau",lang);
}

ajaxLoader.prototype.loadPreviousBureau = function(lang) {
	this.actualBureau--;
	this.callAjax("getBureau",lang);
}

ajaxLoader.prototype.loadBloc = function(pageBloc, lang) {
	document.getElementById("home_onglet_g_" + this.actualActuBloc).className = "home_onglet_g";
	document.getElementById("home_onglet_fond_" + this.actualActuBloc).className = "home_onglet_fond";
	document.getElementById("home_onglet_d_" + this.actualActuBloc).className = "home_onglet_d";
	document.getElementById("home_titre_onglet_" + this.actualActuBloc).className = "home_titre_onglet";
	document.getElementById("bloc_home_id_" + this.actualActuBloc).className = "home_onglet_ajax";
	document.getElementById('bloc_home_id_' + this.actualActuBloc).onmouseover = function(){
		var tabId = this.id.split("_");
		rollOnglet(tabId[3],'on');
	};
	document.getElementById('bloc_home_id_' + this.actualActuBloc).onmouseout = function(){
		var tabId = this.id.split("_");
		rollOnglet(tabId[3],'off');
	};

	this.actualActuBloc = pageBloc;
	
	document.getElementById("home_onglet_g_" + this.actualActuBloc).className = "home_onglet_g_hover";
	document.getElementById("home_onglet_fond_" + this.actualActuBloc).className = "home_onglet_fond_hover";
	document.getElementById("home_onglet_d_" + this.actualActuBloc).className = "home_onglet_d_hover";
	document.getElementById("home_titre_onglet_" + this.actualActuBloc).className = "home_titre_onglet_hover";
	document.getElementById("bloc_home_id_" + this.actualActuBloc).className = "home_onglet_ajax_hover";

	document.getElementById('bloc_home_id_' + this.actualActuBloc).onmouseover = function(){};
	document.getElementById('bloc_home_id_' + this.actualActuBloc).onmouseout = function(){};
		
	this.callAjax("getBloc",lang);
}

ajaxLoader.prototype.callAjax = function(mode,lang) { 
	var httpObject = getXMLHTTP();
	
	if(httpObject)
	{
		switch(mode) {
			case "getBloc":
				httpObject.open("POST","include/ajaxModules/" + mode + ".php?id=" + this.actualActuBloc + "&lang=" + lang, true);	
				break;
			case "getBureau":
				httpObject.open("POST","include/ajaxModules/" + mode + ".php?page=" + this.actualBureau + "&lang=" + lang, true);	
				break;
		}
	}

	httpObject.onreadystatechange = function()
	{
		/*  When call is finished */
		if (httpObject.readyState==4 && httpObject.responseText)
		{	
			parent.theAjaxLoader.generateJson(httpObject, mode);		
		}
	};
	
	httpObject.send(null)
}

ajaxLoader.prototype.generateJson = function(httpObject, mode) { 
	var toDisplay = "";
	var JSON = eval('(' + httpObject.responseText + ')'); 

	switch(mode) {
		case "getBloc":
			switch(this.actualActuBloc) {
				case "1":
					document.getElementById("home_content_left_bloc").innerHTML = JSON.contenu;
					break;
				case "2":
					toDisplay += "<div style='float:left;width:160px;text-align:justify'>";
						toDisplay += JSON.contenu
					toDisplay += "</div>";
					toDisplay += "<div style='padding-left:22px;float:left;width:100px'>";
						toDisplay += "<div id='bloc_recruteur_rh'></div>";
						toDisplay += "<div id='bloc_recruteur_pme'></div>";
						toDisplay += "<div id='bloc_recruteur_tpe'></div>";
					toDisplay += "</div>";
					toDisplay += "<div class='spacer'></div>"
					
					document.getElementById("home_content_left_bloc").innerHTML = toDisplay;
								
					so_init(JSON.widthFirstPhoto,JSON.heightFirstPhoto);
					break;
				case "3":
					//toDisplay += "<div class='titre_bloc'>" + JSON.bloc + "</div>";
				
					for(var aActu in JSON.actus) {
						toDisplay += "<div class='bloc_actu'>";
							toDisplay += "<div class='date_bloc_actu'>";
								toDisplay += JSON.actus[aActu].date;
							toDisplay += "</div>";
							toDisplay += "<div class='titre_orange'>";
								toDisplay += JSON.actus[aActu].titre;
							toDisplay += "</div>";
							toDisplay += "<div>";
								toDisplay += JSON.actus[aActu].contenu;
							toDisplay += "</div>";
						toDisplay += "</div>";
						
						toDisplay += "<div class='bloc_separator'></div>";
					}
					
					toDisplay += "<div id='bloc_lien_actus'><a href='" + JSON.link + "'>" + JSON.linkText + "</a></div>";
					
					document.getElementById("home_content_left_bloc").innerHTML = toDisplay;
					break;
			}
			break;
		case "getBureau":
			for(var aBureau in JSON.bureaux) {
				document.getElementById("element_office_id_" + aBureau).innerHTML = JSON.bureaux[aBureau].lieu;
				document.getElementById("element_office_id_parent_" + aBureau).style.visibility = "visible";
			}
			for(var i=parseInt(aBureau)+1;i<10;i++) {
				document.getElementById("element_office_id_parent_" + i).style.visibility = "hidden";
			}
			
			if(JSON.actualPage > 1) {
				document.getElementById("fleche_previous_bureau").style.display = "block";
			}
			else {
				document.getElementById("fleche_previous_bureau").style.display = "none";
			}
			
			if(JSON.actualPage == JSON.nbPages) {
				document.getElementById("fleche_next_bureau").style.display = "none";
			}
			else {
				document.getElementById("fleche_next_bureau").style.display = "block";
			}
			
			break;
	}
}

/*
 * +-------------------------------------+
 * Number.prototype.nombreFormate
 * +-------------------------------------+
 * Params (facultatifs):
 * - Int decimales: nombre de decimales (exemple: 2)
 * - String signe: le signe precedent les decimales (exemple: "," ou ".")
 * - String separateurMilliers: comme son nom l'indique
 * Returns:
 * - String chaine formatee
 */
Number.prototype.nombreFormate = function (decimales, signe, separateurMilliers) {
	var _sNombre = String(this), i, _sRetour = "", _sDecimales = "";
	if (decimales == undefined) decimales = 2;
	if (signe == undefined) signe = '.';
	if (separateurMilliers == undefined) separateurMilliers = ' ';

	function separeMilliers (sNombre) {
		var sRetour = "";
		while (sNombre.length % 3 != 0) {
			sNombre = "0"+sNombre;
		}
		for (i = 0; i < sNombre.length; i += 3) {
			if (i == sNombre.length-1) separateurMilliers = '';
			sRetour += sNombre.substr(i, 3)+separateurMilliers;
		}
		while (sRetour.substr(0, 1) == "0") {
			sRetour = sRetour.substr(1);
		}
		return sRetour.substr(0, sRetour.lastIndexOf(separateurMilliers));
	}
	if (_sNombre.indexOf('.') == -1) {
		for (i = 0; i < decimales; i++) {
			_sDecimales += "0";
		}
		_sRetour = separeMilliers(_sNombre)+signe+_sDecimales;
	} else {
		var sDecimalesTmp = (_sNombre.substr(_sNombre.indexOf('.')+1));
		_sDecimales = sDecimalesTmp;
		if (sDecimalesTmp.length > decimales) {
			var nDecimalesManquantes = sDecimalesTmp.length - decimales;
			var nDiv = 1;
			for (i = 0; i < nDecimalesManquantes; i++) {
				nDiv *= 10;
			}
			_sDecimales = Math.round(Number(sDecimalesTmp) / nDiv);
		}
		_sRetour = separeMilliers(_sNombre.substr(0, _sNombre.indexOf('.')))+String(signe)+_sDecimales;
	}
	return _sRetour;
}
