/*************************
 * FONCTIONS PRINCIPALES *
 *************************/ 
 
// Largeur de la page
var document_width = 0;
// Hauteur de la page
var document_height = 0;

/*
** Récupère un élément via son id
*/
function getEnvObject(id) {

	if (document.getElementById) {
		return document.getElementById(id);
	} else if (document.layers) {
		return document.layers[id];
	} else if (document.all) {
		return document.all[id];
	} else {
		return false;
	}
	
}

/*
** Ajoute une fonction à exécuter lorsque la page est chargée
*/
function addLoadListener(func) {
	
	if (window.addEventListener) {
		window.addEventListener("load", func, false);
	} else if (document.addEventListener) {
		document.addEventListener("load", func, false);
	} else if (window.attachEvent) {
		window.attachEvent("onload", func);
	} else if (typeof window.onload != "function") {
		window.onload = func;
	} else {
		var oldonload = window.onload;
		window.onload = function() {
			oldonload();
			func();
		};
	}
	
}

/*
** Ajoute une fonction à exécuter lorsque la fenêtre est redimensionnée
*/
function addResizeListener(func) {
	
	if (window.addEventListener) {
		window.addEventListener("resize", func, false);
	} else if (document.addEventListener) {
		document.addEventListener("resize", func, false);
	} else if (window.attachEvent) {
		window.attachEvent("onresize", func);
	} else if (typeof window.onresize != "function") {
		window.onresize = func;
	} else {
		var oldonresize = window.onresize;
		window.onresize = function() {
			oldonresize();
			func();
		};
	}	
	
}

/*
** Ajoute une fonction à exécuter lorsque la souris est relachée
*/
function addMouseUpListener(func) {
	
	if (document.addEventListener) {
		document.addEventListener("mouseup", func, false);
	} else if (document.attachEvent) {
		document.attachEvent("onmouseup", func);
	} else if (typeof document.onmouseup != "function") {
		document.onmouseup = func;
	} else {
		var oldonmouseup = document.onmouseup;
		document.onmouseup = function() {
			oldonmouseup();
			func();
		};
	}
	
}

/*
** Affiche le message du site en cours de construction
*/
function showBuilding() {
	
	_fog 			= getEnvObject('fog');
	_building = getEnvObject('building');
	_envoi 		= getEnvObject('envoi');
	_nom 			= getEnvObject('nom_msg');
	_prenom 	= getEnvObject('prenom_msg');
	_societe	= getEnvObject('societe_msg');
	_message 	= getEnvObject('message_msg');
	_result 	= getEnvObject('msg_result');
	
	if (_nom && _prenom && _societe && _message && _fog && _building && _envoi) {
		_fog.style.display 			= 'inline';
		_building.style.display = 'inline';
		_envoi.style.display 		= 'inline';
		_result.style.display 	= 'none';
		_nom.disabled = "";
		_prenom.disabled = "";
		_societe.disabled = "";
		_message.disabled = "";
		_nom.value = "";
		_prenom.value = "";
		_societe.value = "";
		_message.value = "";
	}
	
}

/*
** Ferme le message du site en cours de construction
*/
function closeBuilding() {
	
	_fog 			= getEnvObject('fog');
	_building = getEnvObject('building');
	
	if (_fog && _building) {
		_fog.style.display 			= 'none';
		_building.style.display = 'none';
	}
	
}

/*
** L'envoi s'est correctement déroulé
*/
function envoiValid(msg) {

	if (msg.length > 0) {

		_envoi 		= getEnvObject('envoi');
		_nom 			= getEnvObject('nom_msg');
		_prenom 	= getEnvObject('prenom_msg');
		_societe	= getEnvObject('societe_msg');
		_message 	= getEnvObject('message_msg');
		_result 	= getEnvObject('msg_result');
		
		if (_nom && _prenom && _societe && _message && _envoi) {
			_nom.disabled 				= "disabled";
			_prenom.disabled 			= "disabled";
			_societe.disabled 		= "disabled";
			_message.disabled 		= "disabled";
			_result.innerHTML			= msg;
			_result.style.display = 'inline';
			_envoi.style.display 	= 'none';
		}
		
	}
	
}

/*
** L'envoi est en erreur
*/
function envoiError() {
	
}

/*
** Envoi le message
*/
function envoiMessage() {

	_envoi 		= getEnvObject('envoi');
	_nom 			= getEnvObject('nom_msg');
	_prenom 	= getEnvObject('prenom_msg');
	_societe	= getEnvObject('societe_msg');
	_message 	= getEnvObject('message_msg');
	
	
	if (_nom && _prenom && _societe && _message && _envoi) {
		sendData('building.php', 'nom='+_nom+'&prenom='+_prenom+'&societe='+_societe+'&message='+_message, 'POST', envoiValid, envoiError);
	}

}

/*
** Récupère et mémorise les dimensions du document 
*/
function getDocumentSize() {
	
	if( typeof( window.innerWidth ) == 'number' ) {
		//Pas-IE
		document_width = window.innerWidth;
		document_height = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6 et standards 
		document_width = document.documentElement.clientWidth;
		document_height = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 et compatibles
		document_width = document.body.clientWidth;
		document_height = document.body.clientHeight;
	}
	
	return true;
	
}

/*
** Positionne la zone de connexion
*/ 
function redimBody() {
	
	_fog   		= getEnvObject('fog');
	_building = getEnvObject('building');
	_head 		= getEnvObject('head');
	_body 		= getEnvObject('body');
	_foot 		= getEnvObject('foot');
	_foot_bg	= getEnvObject('foot_bg');
	
	if (_fog && _building && _head && _body && _foot && _foot_bg) {
		
		_fog.style.width			= parseInt(_foot_bg.offsetWidth) +'px';
		_fog.style.height			= (parseInt(_head.offsetHeight) + parseInt(_body.offsetHeight) + parseInt(_foot.offsetHeight)) +'px';
		_building.style.left	= (document_width / 2 - 420) + 'px';
		_building.style.top		= (document_height / 2 - 240) + 'px';
		
		if (_fog.offsetHeight < document_height) _fog.style.height = document_height;
		_head.style.left			= ((document_width  - parseInt(_head.offsetWidth))  / 2) +'px';
		_body.style.left			= ((document_width  - parseInt(_body.offsetWidth))  / 2) +'px';
		_foot.style.left			= ((document_width  - parseInt(_foot.offsetWidth))  / 2) +'px';
		if ((document_height - parseInt(_foot_bg.offsetHeight)) > (parseInt(_head.offsetHeight) + parseInt(_body.offsetHeight))) {
			_foot.style.top			= (document_height - parseInt(_foot.offsetHeight)) +'px';
			_foot_bg.style.top	= (document_height - parseInt(_foot_bg.offsetHeight)) +'px';
		} else {
			_foot.style.top			= (parseInt(_head.offsetHeight) + parseInt(_body.offsetHeight)) +'px';
			_foot_bg.style.top	= (parseInt(_head.offsetHeight) + parseInt(_body.offsetHeight)) +'px';
		}
		
	}
	
}

bandeau_pos   = null;
bandeau_nb 	  = null;
bandeau_sp 	  = 10;
bandeau_time  = null;
bandeau_delai = 5000;
bandeau_width = 930;

/*
** Fais circuler le bandeau vers la droite
*/
function nextBandeau() {
	
	if (bandeau_time == null) return false;
	
	slideBandeau(bandeau_sp, 0, 1);
	
}

/*
** Fais circuler le bandeau vers la droite
*/
function prevBandeau() {
	
	if (bandeau_time == null) return false;
	
	slideBandeau(-1 * bandeau_sp, 0, 1);
	
}

/*
** Exécute régulièrement le slide
*/
function timeBandeau() {
			
	if (bandeau_pos >= bandeau_nb) moveBandeau(1);
	else nextBandeau();
	
}

/*
** Initialise les bandeaux
*/
function bandeau_init() {
	
		_bx = getEnvObject('bandeaux');
		
		if (_bx && bandeau_pos == null) {
			
			bandeau_pos = 1;
			bandeau_nb = _bx.childNodes.length;
			pos = 0;
		
			for(i = 1; i <= bandeau_nb; i++) {
				
				_b = getEnvObject('bandeau'+i);
				if (_b) {
					_b.style.left = pos + 'px';
					pos += bandeau_width;
				}
				
			}
			
			bandeau_time = setTimeout('timeBandeau()', bandeau_delai);
			
		}
			
}

/*
** Fait circuler le bandeau
*/
function slideBandeau(dep, tot, nb) {
	
	bandeau_init();
	if (bandeau_time != null) {
		clearTimeout(bandeau_time);
		bandeau_time = null;
	}
		
	if (tot < bandeau_width * nb && -1 * tot < bandeau_width * nb) {
		
		tot -= dep;		
		for(i = 1; i <= bandeau_nb; i++) {
			
			_b = getEnvObject('bandeau'+i);
			if (_b) _b.style.left = (parseInt(_b.offsetLeft) - dep) + 'px';
			
		}
		
		setTimeout('slideBandeau('+dep+', '+tot+', '+nb+')', 1);

	} else {
		
		last_pos = bandeau_pos;
		bandeau_pos += (dep > 0) ? nb : -nb;
		_bt_left  = getEnvObject('left');
		_bt_right = getEnvObject('right');
		if (bandeau_pos == 1) {
			
			_bt_left.style.display = 'none';
			
		} else {
			
			_bt_left.style.display = 'inline';
			
		}
		if (bandeau_pos == bandeau_nb) {
			
			_bt_right.style.display = 'none';
			bandeau_pos
		} else {
			
			_bt_right.style.display = 'inline';
			
		}
				
		_b1  = getEnvObject('bandeausel' + last_pos);
		_b1.src = 'img/select_off.png';
		_b2  = getEnvObject('bandeausel' + bandeau_pos);
		_b2.src = 'img/select_on.png';
		
		bandeau_time = setTimeout('timeBandeau()', bandeau_delai);
		
	}
	
}

/*
** Déplace le bandeau jusqu'à la position souhaitée
*/
function moveBandeau(pos) {
	
	bandeau_init();	
	if (bandeau_time == null) return false;
	pos = parseInt(pos);
	if (bandeau_pos < pos && pos <= bandeau_nb) slideBandeau(bandeau_sp, 0, pos - bandeau_pos);
	if (bandeau_pos > pos && pos >= 1) slideBandeau(-1 * bandeau_sp, 0, bandeau_pos - pos);
		
}

/*
** Redimensionne et positionne les éléments structurants
*/
function resize() {

	// Récupère et mémorise les dimensions du document
	getDocumentSize();
	// Déplacement de la zone de connexion
	redimBody();

	return true;
	
}

/*
** Initialise ma page
*/
function init() {
	
		// Redimensionne le body
		resize();
		
		// Initialise le bandeau
		bandeau_init();

		return true;
	
}

// Récupère et mémorise les dimensions du document
getDocumentSize();
// Lorsque la page est chargée
addLoadListener(init);
// Lorsque la fenêtre est redimensionnée
addResizeListener(resize);
