/* ----- ----- ----- Free software Foundation - Affero Licence ----- ----- -----

devant.js - display an iframe on other site contents
    Copyright (C) 2008  Thibault Garcia (thibault.garcia@revaweb.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

----- ----- ----- Free software Foundation - Affero Licence ----- ----- ----- */

var devantId;

/* ----- ----- devantInit ----- -----
*/
function devantInit(fct_deb,fct_fin) {
	devantId=document.createElement('div');

	/* ----- ----- fct ----- ------ */
	devantId.fct_deb = fct_deb || '';
	devantId.fct_fin = fct_fin || '';
	

	/* ----- ----- devantId ----- ----- */
	devantId.style.backgroundColor='#000';
	devantId.style.position='fixed';
	devantId.style.display='none';
	devantId.style.filter='alpha(opacity=0)';
	devantId.style.opacity=0;
	devantId.style.top='0';
	devantId.style.left='0';
	devantId.style.width='100%';
	devantId.style.height='100%';
	devantId.style.margin='0';
	devantId.style.padding='0';
	devantId.style.border='0';
	devantId.style.cssFloat='none';
	eventAddAfter(devantId,'onclick','devantFerme()')

	devantId.devantFactor=0.4;
	devantId.devantMinStep=0.05;
	devantId.devantDelay=25;
	devantId.devantValue=0;
	devantId.devantTimer=null;
	devantId.devantOpacityMax=0.60;

	/* ----- ----- node_div ----- ----- */
	var node_div=document.createElement('div');
	node_div.style.position='absolute';
	node_div.style.display='none';
	node_div.style.textAlign='center';
	node_div.style.top='60px';
	node_div.style.left='50%';
	node_div.style.marginRight='5px';
	node_div.style.marginBottom='5px';
	node_div.style.opacity='1';
	node_div.style.margin='0';
	node_div.style.padding='0';
	node_div.style.border='0';
	node_div.style.cssFloat='none';
	eventAddAfter(node_div,'onclick','devantFerme()');

	/* ----- ----- node_iframe ----- ----- */
	var node_iframe=document.createElement('iframe');
	node_iframe.setAttribute('allowTransparency','true');
	node_iframe.setAttribute('frameBorder','0');
	node_iframe.setAttribute('src','');
	node_iframe.style.width='0';
	node_iframe.style.height='0';
	node_iframe.style.background='transparent';

	/* ----- ----- finalisation ----- ----- */
	node_div.appendChild(node_iframe);

	devantId.devantTable=node_div;
	devantId.devantFrame=node_iframe;

	document.getElementsByTagName('body')[0].appendChild(devantId);
	document.getElementsByTagName('body')[0].appendChild(node_div);
}

/* ----- ----- devantOuvre ----- -----
*/
function devantOuvre(href,width,height) {
	if(devantId.DevantTimer) clearTimeout(devantId.DevantTimer);
	eval(devantId.fct_deb);
	devantId.devantFrame.src=href;
	devantId.devantTable.style.display='none';

	devantId.devantOpacity=devantId.devantOpacityMax;

	devantId.devantTable.style.top=(60+document.documentElement.scrollTop)+'px';
	devantId.devantFrame.style.width=width+'px';
	devantId.devantFrame.style.height=height+'px';

	devantId.devantTable.style.marginLeft='-'+(width/2)+'px';

	devantId.style.display='block';
	devantId.devantTimer=setTimeout('devantOpacityChange()',devantId.devantDelay);
	return false;
}

/* ----- ----- devantOpacityChange ----- -----
*/
function devantOpacityChange() {
	if(devantId.devantTimer) clearTimeout(devantId.devantTimer);

	var delta=devantId.devantFactor*(devantId.devantOpacity-devantId.devantValue);
	if(delta>0) delta=Math.max(devantId.devantMinStep,delta); else delta=Math.min(-devantId.devantMinStep,delta);

	if(Math.abs(devantId.devantOpacity-devantId.devantValue)>devantId.devantMinStep) {
		devantId.devantValue+=delta;
		devantId.style.opacity=devantId.devantValue;
		devantId.style.filter='alpha(opacity='+(100*devantId.devantValue)+')';

		devantId.devantTimer=setTimeout('devantOpacityChange()',devantId.devantDelay);
	} else {
		devantId.devantValue=devantId.devantOpacity;
		devantId.style.opacity=devantId.devantValue;
		devantId.style.filter='alpha(opacity='+(100*devantId.devantValue)+')';

		if(devantId.devantOpacity==0) {
			devantId.style.display='none';
			eval(devantId.fct_fin);
		} else {
			devantId.devantTable.style.display='inline';
		}
	}
}

/* ----- ----- devantFerme ----- -----
*/
function devantFerme() {
	if(devantId) {
		devantId.devantFrame.src='void.html';
		devantId.devantTable.style.display='none';
		devantId.devantOpacity=0;
		devantId.devantTimer=setTimeout('devantOpacityChange()',devantId.devantDelay);
	}
}

/* ----- ----- devantFin ----- -----
*/
function devantFin() {
	if ((window)&&(window.parent)&&(window.parent.devantFerme)) {
		window.parent.devantFerme();
	}
}