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

function Popup()
{
	this.content = new Array();
	this.currentIndex = 0;
	this.alternativContent = new Array();
	
	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);
			return;
		}
		
		this.content.push(xhr_object.responseText);
		
	}
	
	this.show = function show()
	{
		this.viewContent(0);
	}
	
	this.hide = function hide()
	{
		for(var i=0; i < this.alternativContent.length; i++)
		{
			var elem = document.getElementById(this.alternativContent[i]['id']);
			if(elem)
			{
				elem.innerHTML = this.alternativContent[i]['content'];
			}
		}
		
		if(typeof switchBlocSWF == "function")
			switchBlocSWF();
		
		window.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]);
			
			for(var i=0; i < this.alternativContent.length; i++)
			{
				var elem = document.getElementById(this.alternativContent[i]['id']);
				if(elem)
				{
					if(typeof this.alternativContent[i]['content'] == "undefined")
						this.alternativContent[i]['content'] = elem.innerHTML;
					elem.innerHTML = this.alternativContent[i]['altcontent'];
				}
			}
			
			if(typeof switchBlocIMG == "function")
				switchBlocIMG();
			
			window.showLayer(this.content[this.currentIndex]);
		}
	}
	
	this.addAlternativContent = function addAlternativContent(id, contenu)
	{
		var newAlternativ = new Array();
		newAlternativ['id'] = id;
		newAlternativ['altcontent'] = contenu;
			
		this.alternativContent.push(newAlternativ);
	}
}


// 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;
		// 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");
		
		topScreen("outerImageContainer");
		
		// masque les éléments bug IE
		hideElementsByTagName('SELECT');
		
		// afichage general
		document.getElementById('popup_layer').style.visibility="visible";
		
}

// 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(obj)
{
	//return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
	if(obj)
		return obj.document.documentElement ? obj.document.documentElement : obj.document.body;
	else
		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 scrollPosition(obj)
{
	if(obj)
	{
		var coord = {};
		coord.x = obj.pageXOffset ? pageXOffset : (truebody(obj).scrollLeft ? truebody(obj).scrollLeft : 0);
		coord.y = obj.pageYOffset ? pageYOffset : (truebody(obj).scrollTop ? truebody(obj).scrollTop : 0);
		
		return coord;
	}
}


function visibleDimension(obj)
{
	if(obj)
	{
		var dim = {};
		dim.width = obj.innerWidth ? obj.innerWidth : truebody(obj).clientWidth;
		dim.height = obj.innerHeight ? obj.innerHeight : truebody(obj).clientHeight;
		
		return dim;
	}
}


function centerScreen(id) 
{
	var obj = document.getElementById(id);
	if(obj)
	{
		//offsetx = window.pageXOffset ? pageYOffset : (truebody().scrollLeft ? truebody().scrollLeft : 0);
		//offsety = window.pageYOffset ? pageYOffset : (truebody().scrollTop ? truebody().scrollTop : 0);
		
		//var windowScroll = scrollPosition(parent);
		//var windowDim = visibleDimension(parent);
		
		var iframeScroll = scrollPosition(window);
		var iframeDim = visibleDimension(window);
		
		/*obj.style.left=( ( ( (windowDim.width < iframeDim.width ? windowDim.width : iframeDim.width) - obj.offsetWidth) / 2 ) + iframeScroll.x + windowScroll.x) + "px";
		obj.style.top=( ( ( ( windowDim.height < iframeDim.height ? windowDim.height : iframeDim.height) - obj.offsetHeight) / 2 ) + iframeScroll.y  + windowScroll.y) + "px";*/
		
		
		obj.style.left=( ( ( iframeDim.width - obj.offsetWidth) / 2 ) + iframeScroll.x) + "px";
		obj.style.top=( ( ( iframeDim.height - obj.offsetHeight) / 2 ) + iframeScroll.y) + "px";
	}
}


function topScreen(id) 
{
	var obj = document.getElementById(id);
	if(obj)
	{
		var iframeScroll = scrollPosition(window);
		var iframeDim = visibleDimension(window);
		
		obj.style.left=( ( ( iframeDim.width - obj.offsetWidth) / 2 ) + iframeScroll.x) + "px";
		obj.style.top= "50px";
	}
}


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(typeof refreshTimeout != "undefined")
			window.clearTimeout(refreshTimeout);
		
		refreshTimeout = window.setTimeout("centerScreen('outerImageContainer');",100);
		
	}
}


function hideElementsByTagName(tagname,id)
{
	if(tagname==null)
		return;
	
	var elements = document.getElementsByTagName(tagname);
	
	for (i = 0; i < elements.length; i++)
	{
		if(elements[i].id != "rappelle")
			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;

/*
if(truebody(parent))
{
		
	truebody(parent).onscroll = refreshStyle;
}

if(truebody(parent))
{
	truebody(parent).onresize = refreshStyle;
}
*/
