window.addEventListener ? window.addEventListener('load', $GestionnaireEvenement, false) : window.attachEvent('onload', $GestionnaireEvenement);

//Gestionnaire evenement window
function $GestionnaireEvenement(){
    window.addEventListener ? window.addEventListener('resize', $FilterPopup, false) : window.attachEvent('onresize', $FilterPopup);
    window.addEventListener ? window.addEventListener('scroll', $FilterPopup, false) : window.attachEvent('onscroll', $FilterPopup); // IE - FIREFOX >= 2
    if(window.addEventListener){window.addEventListener('DOMMouseScroll', $FilterPopup, false);} // FIREFOX < 2
}

// Variables globales
var TsDocRef = '';
var TsAfficheFiltre = false;
var Tsnavigateur = navigator.appName.toLowerCase();
var TsPlatform   = navigator.platform.toLowerCase();
var Tsversion    = navigator.appVersion.toLowerCase();
var Tslargeur = 0;
var Tshauteur = 0;
var TsFilterName = 'filter';
var TsPopupAltimaName = 'popup_altima';

// Version de Internet Explorer
function $MonNavigateur(){
	if(Tsnavigateur != 'microsoft internet explorer'){return false;}

	var Tsversion_deb = Tsversion.indexOf('(') + 1;
	var Tsversion_fin = Tsversion.indexOf(')');
	Tsversion = Tsversion.substring(Tsversion_deb, Tsversion_fin);

	var TstabTmp = Tsversion.split(';');
	for(i in TstabTmp){
		if(TstabTmp[i].indexOf('msie') > -1){Tsversion = parseInt(TstabTmp[i].replace('msie', 'IE'));break;}
	}
}

function $MonDocRef(){
   document.documentElement ? TsDocRef = document.documentElement : TsDocRef = document.body;
}

// Gestion du filtre et du positionnement popup
function $FilterPopup(event){
	event = event || window.event;

	if(!TsAfficheFiltre){return false;}

	$ShowFilter();
	$ShowPopupAltima();
	
	if(event.preventDefault){
		event.preventDefault();
		event.stopPropagation();
	}else{
		event.returnValue = false;
		event.cancelBubble = true;
	}
}

function $Contains(t, elt) {
	for (var i=0;i<t.length;i++) if (t[i]==elt) return true;
	return false;
}

function $n(o) {
	return (typeof o=='string') ? o=document.getElementById(o) : o;
}

function $t(o) {
	return $n(o).tagName ? $n(o).tagName.toLowerCase() : '';
}

function $c(o,t) {
	if (!$n(o).childNodes) {return null;}

	var at = t ? t.toLowerCase().split('|') : null;
	var a=new Array;
	for (var i=0;i<$n(o).childNodes.length;i++) {
		if (!at) { if ($n(o).childNodes[i].tagName) a[a.length]=$n(o).childNodes[i]; }
		else if ($Contains(at,$t($n(o).childNodes[i]))) a[a.length]=$n(o).childNodes[i];
	}
	return a;
}

function $p(o,t) {
	if (!o) return null;
	
	var at = t ? t.toLowerCase().split('|') : null;
	return $t(o)=='body' ? null : ($Contains(at,$t(o)) ? $n(o) : $p($n(o).parentNode, t));
}

function $changeOpacity(TsOpacity, TsId){
	if(!$n(TsId)){return false;}
	
	var TsObj = $n(TsId).style;
	with(TsObj){
		opacity = (TsOpacity / 100);
		MozOpacity = (TsOpacity / 100);
		KhtmlOpacity = (TsOpacity / 100);
		filter = "alpha(opacity=" + TsOpacity + ")";
	}
}

// Affiche le filtre
function $ShowFilter(TsIdFilter, TsBackgroundColor){
	// Arguments par defaut
	TsIdFilter = TsIdFilter || TsFilterName;
	TsBackgroundColor = TsBackgroundColor || '#fff';
	
	// regarde si element pour affichage filtre existe
	var Tsblock = document.getElementById(TsIdFilter);
	var Tsblock00 = $ElementBody();
	if(Tsblock && Tsblock00){
		Tslargeur = parseInt(Tsblock00.offsetWidth);
		Tshauteur = parseInt(Tsblock00.offsetHeight);
		
		// Si hauteur-largeur du body inferieur a la hauteur du navigateur
		$MonDocRef();
		if( Tslargeur < parseInt(TsDocRef.clientWidth) ) {Tslargeur = parseInt(TsDocRef.clientWidth);}
		if( Tshauteur < parseInt(TsDocRef.clientHeight) ){Tshauteur = parseInt(TsDocRef.clientHeight);}
		
		Tsblock.style.width = Tslargeur + 'px';
		Tsblock.style.height = Tshauteur + 'px';
		
		if(!TsAfficheFiltre){
			Tsblock.style.display = 'block';
			TsAfficheFiltre = true;
		}
	}else{ // Creation des elements pour affichage filtre
		$CreateElement($ElementBody(), 'div', 'id='+TsIdFilter+'|style=position:absolute;top:0;left:0;z-index:9998;display:none;width:0px;height:0px;background:'+TsBackgroundColor+';')
		$changeOpacity(50, TsIdFilter);
		setTimeout('$ShowFilter("'+TsIdFilter+'", "'+TsBackgroundColor+'")', 100);
	}
}

// Cache le filtre
function $HideFilter(TsIdFilter){
	// Arguments par defaut
	TsIdFilter = TsIdFilter || TsFilterName;

	// regarde si element existe
	var Tsblock = document.getElementById(TsIdFilter);
	if(!Tsblock){return false;}

	Tsblock.style.display = 'none';
	TsAfficheFiltre = false;
}

// Affiche la popup Altima
function $ShowPopupAltima(TsIdPopup){
	TsIdPopup = TsIdPopup || TsPopupAltimaName;

	var Tsblock = document.getElementById(TsIdPopup);
	if(!Tsblock){
		$CreateElement($ElementBody(), 'div', 'id='+TsIdPopup+'|style=position:absolute;top:0;left:0;z-index:9999;display:none;width:100px;height:50px;background:red;');
		$ShowPopupAltima(TsIdPopup);
		return false;
	}

	$ShowFilter(); // Affiche le filtre
	Tsblock.style.display = 'block'; // Affiche la Popup Altima
	$PositionPopupAltima(TsIdPopup); // Positionne la Popup Altima
	setTimeout('$HidePopupAltima()', 3000);
}

// Cache la popup Altima
function $HidePopupAltima(TsIdPopup){
	TsIdPopup = TsIdPopup || TsPopupAltimaName;

	var Tsblock = document.getElementById(TsIdPopup);
	if(!Tsblock){return false;}

	Tsblock.style.display = 'none';
	$HideFilter();
}

// Calcule la position de la Popup Altima
function $PositionPopupAltima(TsIdPopup){
	TsIdPopup = TsIdPopup || TsPopupAltimaName;

	var Tsblock = document.getElementById(TsIdPopup);
	if(Tsblock && ( Tsblock.style.display == 'block' ) ){
		$MonDocRef();
		var TsScrollTop = 0;
		var TsScrollLeft = 0;
		window.pageXOffset ? TsScrollLeft = window.pageXOffset : TsScrollLeft = TsDocRef.scrollLeft;
		window.pageYOffset ? TsScrollTop = window.pageYOffset : TsScrollTop = TsDocRef.scrollTop;
		
		var TsblockTop = (parseInt(TsDocRef.clientHeight) - parseInt(Tsblock.offsetHeight)) /2;
		Tsblock.style.top = TsScrollTop + parseInt(TsblockTop) + 'px';
		var TsblockLeft = (parseInt(TsDocRef.clientWidth) - parseInt(Tsblock.offsetWidth)) /2;
		Tsblock.style.left = TsScrollLeft + parseInt(TsblockLeft) + 'px';
	}
}

// Creation de div a la ricine de la page HTML
function $CreateElement(TsObj, TsTagElement, TsAttributes){
	if(!TsObj){return null;}
	var TsElement = document.createElement(TsTagElement);

	// Creation des attributs de la balise
	var TsTabAttributes = TsAttributes.toLowerCase().split('|');
	for (var i in TsTabAttributes){
		TsAttribute = TsTabAttributes[i].split('=');
		if(TsAttribute.length != 2){continue} // Format : attribut=valeur

		// attribut ID
		if(TsTabAttributes[i].indexOf('id=') > -1){
			TsElement.setAttribute('id', TsAttribute[1]);
		}

		// attribut Class
		if(TsTabAttributes[i].indexOf('class=') > -1){
			TsElement.setAttribute('class', TsAttribute[1]);
		}

		// attribut STYLE
		if(TsTabAttributes[i].indexOf('style=') > -1){
			TsElement.setAttribute('cssText', TsAttribute[1]); // Ie
			TsElement.setAttribute('style', TsAttribute[1]); // Autres
		}
	}
	
	TsObj.appendChild(TsElement);
	return true;
}

// Recherche le BODY de la page
function $ElementBody(){
	var TsObj = document.getElementsByTagName("body");
	if(!TsObj[0]){return null;}
	return TsObj[0];
}