/*
v1.01 Copyright (c) 2007 Julien Roussel
Date : 2007-09-02 - Email : julien@carpediem.fr
Website : http://www.julien.roussel.pagesperso.org
Doc	: http://www.julien.roussel.pagesperso.org/DHTMLpop/

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial 
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
IN THE SOFTWARE.
*/

var DHTMLpop = {};
var scrollDiff = 0;
function initDHTMLpop(Obj) 
{
	if (!Obj)
	{
		Obj = {};
		if (!Obj.autorun) Obj.autorun = true;
	}
	if (typeof(Obj.opacity)=="undefined") Obj.opacity = 6;
	if (!Obj.height) Obj.height	= 300;
	if (!Obj.width)	Obj.width = 500;
	if (!Obj.url) Obj.url = 'http://www.google.fr';
	DHTMLpop = Obj;
	if (DHTMLpop.autorun) showHideInnerPopUp();
	return DHTMLpop;
}
function runDHTMLpop(obj) { 
	obj.autorun=true; 
	DHTMLpop = initDHTMLpop(obj); 
	return false;
}
function debugDHTMLpop() {
	var d = dimension_detect();
	var output = 
	' viewW='+d.viewW+' viewH='+d.viewH+' docH='+d.docH+' docW='+d.docW+
	' left='+d.left+' top='+d.top+' scrollDiff='+scrollDiff+'<hr>';
	var bod = document.getElementsByTagName('body')[0];
	if (document.getElementById('debugSpan')) {
		document.getElementById('debugSpan').innerHTML+=output;
	} else {
		var div = document.createElement('div');
		div.id = "debugSpan";
		div.style.position = "absolute";
		div.style.display="block";
		div.style.top="0px";
		div.style.width="500px";
		div.style.backgroundColor='lightgreen';
		div.innerHTML = output;
		bod.appendChild(div);
	}
}
function replaceInnerPopUp(width,height)
{
	window.scrollTo(0,0);
	if (width)	DHTMLpop.width = width;
	if (height) DHTMLpop.height = height;
	var d 		= dimension_detect();
	var newTop 	= (d.viewH-DHTMLpop.height)/2 ;
	var newLeft = (d.viewW-DHTMLpop.width)/2;
	// on repositionne le div au centre de la page
	var innerPopUp = document.getElementById("innerpopup_iframe");
	innerPopUp.allowTransparency = true;
	innerPopUp.style.top 	= parseInt(newTop)+"px";
	innerPopUp.style.left 	= parseInt(newLeft)+"px";
	innerPopUp.style.width 	= DHTMLpop.width+"px";
	innerPopUp.style.height = DHTMLpop.height+"px";
	// on réagrandit le background	
	if (document.body.style.overflow!='hidden') {
		var back = document.getElementById("innerpopup_background");
		back.style.width = '1px';
		back.style.height = '1px';
	} else {
		var back = document.getElementById("innerpopup_background");
		back.style.width = (d.docW>d.viewW)?d.docW+'px':d.viewW+'px';
		back.style.height = (d.docH>d.viewH)?d.docH+'px':d.viewH+'px';
	}
	scrollDiff = d.viewW-d.docW;
	if (scrollDiff<1) document.body.style.width=d.viewW;
};
function showHideInnerPopUp()
{
	window.scrollTo(0,0);
	// On applique une opacité
	var back = document.getElementById("innerpopup_background");

	back.style.filter = 'alpha(opacity='+DHTMLpop.opacity*10+')';
	back.style.MozOpacity = DHTMLpop.opacity/100;
	back.style.KHTMLOpacity = DHTMLpop.opacity/100;
	back.style.opacity = DHTMLpop.opacity/10;

	// Chargement du contenu de l'iframe
	var innerpopup_iframe = document.getElementById("innerpopup_iframe");
	frames['innerpopup_iframe'].location.href = DHTMLpop.url;
	innerpopup_iframe.style.width=DHTMLpop.width+'px';
	innerpopup_iframe.style.height=DHTMLpop.height+'px';

	// On vérifie l'etat de l'innerPopUp pour determiner si on doit l'afficher ou la cacher
	var innerpopup = document.getElementById("innerpopup");
	// si caché on affiche
	if (innerpopup.style.visibility == 'hidden' || DHTMLpop.showonly)
	{
		var os=document.getElementsByTagName('select');
		for (var i=0,o;o=os[i];i++) o.parentNode.style.visibility = 'hidden';
		document.body.style.overflow = 'hidden';
		document.getElementsByTagName('html')[0].style.overflow = 'hidden';
		innerpopup.style.visibility = 'visible';
	// si affiché on cache
	} else {
		innerpopup.style.width='1px';
		var os=document.getElementsByTagName('select');
		for (var i=0,o;o=os[i];i++) o.parentNode.style.visibility = 'visible';
		document.body.style.overflow = 'auto';
		document.getElementsByTagName('html')[0].style.overflow = 'auto';
		innerpopup.style.visibility = 'hidden';
	}
	replaceInnerPopUp();
	if (DHTMLpop.debug) debugDHTMLpop();
}
// Dimension_detect
function dimension_detect(){
	var d={'viewW':0,'viewH':0,'docH':0,'docW':0,'left':0,'top':0};
	if (document.body.scrollHeight>document.body.offsetHeight){
		d.docW=document.body.scrollWidth;
		d.docH=document.body.scrollHeight;
	} else {
		d.docW=document.body.offsetWidth;
		d.docH=document.body.offsetHeight;
	}
	if (self.innerWidth){
		d.viewW=self.innerWidth;
		d.viewH=self.innerHeight;
		d.left=window.pageXOffset;
		d.top=window.pageYOffset;
	} else {
		var ie=(document.compatMode&&document.compatMode!='BackCompat')?document.documentElement:document.body;
		d.viewW=ie.clientWidth;
		d.viewH=ie.clientHeight;
		d.left=ie.scrollLeft;
		d.top=ie.scrollTop;
	}
	return d;
};
window.onresize=function(){
	var innerpopup_iframe = document.getElementById("innerpopup_iframe");
	replaceInnerPopUp( 
		parseInt(innerpopup_iframe.style.width,10), 
		parseInt(innerpopup_iframe.style.height,10) 
	);
};
document.write('<style type="text/css">');
document.write('#innerpopup_iframe { overflow:hidden; z-index:500; position:absolute; top:0; left:0; border:0; }');
document.write('#innerpopup_background { z-index:498; position:absolute; top:0; left:0; background-color:#000000; width:640px; height:480px; -moz-opacity:.7; opacity:.7; }');
document.write('</style>');
document.write('<div id="innerpopup" style="visibility:hidden;">');
document.write('	<div id="innerpopup_background">&nbsp;</div>');
document.write('	<iframe id="innerpopup_iframe" name="innerpopup_iframe" frameborder="0" scrolling="no" src="about:blank" style="width:5px;height:5px" autocomplete="Off"></iframe>');
document.write('</div>');
