////////////////////////////////////////////////////////////////////////////
//
// © PMP CONCEPT 2007 :: FENETRE DE DIALOGUE
//
////////////////////////////////////////////////////////////////////////////

function Popup()
{
	this.content = new Array();
	this.currentIndex = 0;
	
	this.addContent = function addContent(content)
	{
		this.content.push(content.toString());
		
	}
	
	this.addURL = function addURL(url)
	{
		if( url==null || typeof(url)=='undefined' )
			return;
		
		// récupération du contenu de la page en ajax
		var xhr_object = null;
	
		if(!xhr_object && window.XMLHttpRequest) // Firefox
			xhr_object = new XMLHttpRequest();
		
		if(!xhr_object && window.ActiveXObject) // Internet Explorer
			xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
		
		if(!xhr_object && window.ActiveXObject) // Internet Explorer
			xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
			
		if(!xhr_object)
		{ // XMLHttpRequest non supporté par le navigateur
			alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
			return;
		}
		
		
		xhr_object.open("GET", url, false);
		xhr_object.send(null);
		
		if(xhr_object.readyState != 4)
			return;
			
		if(xhr_object.status != 200)
			alert('ERREUR : ' + xhr_object.status);
		
		var tabUrl = new Array();
		tabUrl['url'] = url;
		tabUrl['content'] = xhr_object.responseText;
		
		this.content.push(tabUrl);
		//alert(tabUrl['url'] + '\n' + tabUrl['content']);
	}
	
	/**
	 * Met à jour le contenu de la page index en appelant l'url url
	 * @param {int} index
	 * @param {String} url
	 */
	this.updateURL = function updateURL(index, url)
	{
		var xhr_object = this.getXHR_Object();
		
		if(url == null || url == "undefined")
		{
			url = this.content[index]['url'];
		}
		
		xhr_object.open("GET", url, false);
		xhr_object.send(null);
		
		if(xhr_object.readyState != 4)
			return;
			
		if(xhr_object.status != 200)
		{
			alert('ERREUR : ' + xhr_object.status);
			return;
		}
		
		this.content[index]['content'] = xhr_object.responseText;
	}
	
	this.getXHR_Object = function getXHR_Object(){
		// récupération du contenu de la page en ajax
		var xhr_object = null;
	
		if(!xhr_object && window.XMLHttpRequest) // Firefox
			xhr_object = new XMLHttpRequest();
		
		if(!xhr_object && window.ActiveXObject) // Internet Explorer
			xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
		
		if(!xhr_object && window.ActiveXObject) // Internet Explorer
			xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
			
		if(!xhr_object)
		{ // XMLHttpRequest non supporté par le navigateur
			alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
			return;
		}
		
		return xhr_object;
	}
	
	this.show = function show()
	{
		this.viewContent(0);
	}
	
	this.hide = function hide()
	{
		window.parent.hideLayer();
	}
	
	this.prev = function prev()
	{
		this.viewContent(this.currentIndex - 1);
	}
	
	this.next = function next()
	{
		this.viewContent(this.currentIndex + 1);
	}
	
	this.viewContent = function viewContent(index)
	{
		
		if(this.content.length>index)
		{
			this.currentIndex = index;
			//alert(this.content[this.currentIndex]['content']);
			window.parent.showLayer(this.content[this.currentIndex]['content']);
		}
	}
	
}


// definition du message a afficher
function message(txt)
{
	window.parent.showLayer(txt);
}

// afficher la fentre
function showLayer(content){
		// calcul de la taille de la fenetre de navigation
		size = truebody().scrollHeight;
		// insertion du texte a afficher
		document.getElementById('outerImageContainer').innerHTML=content;
		//alert(document.getElementById('outerImageContainer').offsetHeight + " < " + ( window.innerHeight ? window.innerHeight : truebody().clientHeight));
		// definition du style pour le fond noir transparent
		document.getElementById('overlay').className="overlayoff";
		// hauteur du fond noir
		document.getElementById('overlay').style.height= size + "px";
		document.getElementById('overlay').style.width= truebody().scrollWidth + "px";
		// centrage de la fenetre
		//larg = document.getElementById('outerImageContainer').offsetWidth;
		//document.getElementById('outerImageContainer').style.marginLeft = -(larg/2) + "px";
		centerScreen("outerImageContainer");
		
		// masque les éléments bug IE
		hideElementsByTagName('SELECT');
		
		// afichage general
		document.getElementById('popup_layer').style.visibility="visible";
		
		//Cacher le graphique flash si il est présent
		if(document.getElementById('cadreGraph'))
		{
			document.getElementById('icone_tab').className = 'icone_tab_on';
			document.getElementById('icone_graph').className = 'icone_graph';
			document.getElementById('cadreGraph').style.display = 'none';
			document.getElementById('cadreNotes').style.display = 'block';
		}
		
}

// masquer la fenetre
function hideLayer(){
		// cache general
		document.getElementById('popup_layer').style.visibility="hidden";
		// retour sur l'ancien style
		document.getElementById('overlay').className="overlay";
		// effacement du contenue du bloc
		document.getElementById('outerImageContainer').innerHTML= null;
		
		// affiche les éléments bug IE
		showElementsByTagName('SELECT');
}

//
function truebody(){
	//return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
	return document.documentElement ? document.documentElement : document.body;
}

// bloc message
if (document.getElementById || document.all){
	document.write('<div id="popup_layer"><div id="overlay" class="overlay"></div><div id="lightbox"><div id="outerImageContainer"></div></div></div>')
}

function centerScreen(id) 
{
	var obj = document.getElementById(id);
	if(obj)
	{
		if(document.getElementById('outerImageContainer').offsetHeight < ( window.innerHeight ? window.innerHeight : truebody().clientHeight))
		{
			offsetx = window.pageXOffset ? pageYOffset : (truebody().scrollLeft ? truebody().scrollLeft : 0);
			offsety = window.pageYOffset ? pageYOffset : (truebody().scrollTop ? truebody().scrollTop : 0);
			obj.style.left=(((( window.innerWidth ? window.innerWidth : truebody().clientWidth) - obj.offsetWidth)/2) + offsetx) + "px";
			obj.style.top=(((( window.innerHeight ? window.innerHeight : truebody().clientHeight) - obj.offsetHeight)/2) + offsety) + "px";
		}
		else{
			offsetx = window.pageXOffset ? pageYOffset : (truebody().scrollLeft ? truebody().scrollLeft : 0);
			offsety = window.pageYOffset ? pageYOffset : (truebody().scrollTop ? truebody().scrollTop : 0);
			obj.style.left=(((( window.innerWidth ? window.innerWidth : truebody().clientWidth) - obj.offsetWidth)/2) + offsetx) + "px";
			obj.style.top= 20 + "px";
		}
	}
}


function refreshStyle()
{
	
	//modifie la taille du fond
	//document.getElementById('overlay').style.height= truebody().scrollHeight + "px";
	//document.getElementById('overlay').style.width= truebody().scrollWidth + "px";
		
	// centre la fenetre à l'écran si le popup est affiché
	if(document.getElementById('popup_layer') && document.getElementById('popup_layer').style.visibility=="visible")
	{
		if(document.getElementById('outerImageContainer').offsetHeight < ( window.innerHeight ? window.innerHeight : truebody().clientHeight))
		{
			centerScreen("outerImageContainer");
		}
	}
}


function hideElementsByTagName(tagname,id)
{
	if(tagname==null)
		return;
	
	var elements = document.getElementsByTagName(tagname);
	
	for (i = 0; i < elements.length; i++)
	{
		if(elements[i].id != 'pays_ref')
			elements[i].style.visibility = 'hidden';
	}
}


function showElementsByTagName(tagname)
{
	if(tagname==null)
		return;
	
	var elements = document.getElementsByTagName(tagname);
	
	for (i = 0; i < elements.length; i++)
		elements[i].style.visibility = 'visible';
}



window.onscroll = refreshStyle;
window.onresize = refreshStyle;

