// Sifacile2 Popup management

if(typeof(NC)=='undefined') NC={};
NC.popups= {

	memo:new Array(),	// stores some parameters used when popup opens, in order to close it the right way
	pre:200,			// default pre delay between displaying mask and popup
	post:150,			// default post delay between closing mask and popup
	fadeStep:25,		// default popup fade in/out step. 0 -> no fade, direct transition instead
	opacity:0.2,		// default mask opacity (0.01 .. 1)
	vars:new Array(),	// associative array = variables coming from calling process : object id or label, etc.

	// onresize (onscroll) ALL popups and masks update
	// called via an onresize event, therefore "this" does not exist...
	update:function() {
		var done=new Array(), i;
		for(i in NC.popups.memo) {
			if(NC.popups.memo[i]) {
				maskID=NC.popups.memo[i][0].id;
				if(!done[maskID]) {
					NC.popups.setMask({id:maskID});
					done[maskID]=true;
				}
				NC.tools.center(i);
			}
		}
	},

	/* Open the popup ID, if a mask exists -> set it first the push the popup Zindex above the mask, if this is not the case
	 * params :
	 *		popID = ID of the popup to open
	 *		keepAttachement: true / false (default) -> if false, check if popup is attached to body root, and if not move it to top 
	 *		keepPosition : true /false (default) -> if false, aucenter the popup
	 *		keepPosition : true /false (default) -> if false, aucenter the popup
	 *		maskColor, maskOpacity = background color ans opacity (0..1) of the mask (or nothing = default)
	 *		pre / post = ON and OFF delay between mask and popup change (mask is always changed first)
	 *		fadeStep = fade in/out step (0..100). 0 -> default step, 100 -> no fade in/out
	 *		vars = array of specific caller parameters (like : id:'xxx', etc.), sent to Ajax server script as POST id='xxx'
	 *		custom : array of custom areas in the popup (like : userID:'xxx') -> userID innerHTML will be replaced with xxx before opening the popup
	 */
	open:function(params) {
		var	popup=document.getElementById(params['popID']),
			zindex, mask, i, el;
		
		if(popup) {
			this.vars=params.vars ? params.vars : new Array();		// Specific caller parameters to send to Ajax server side script
			if(!params.keepAttachement && popup.parentNode.tagName!='BODY') document.body.appendChild(popup);		// move the popup attachement to body
			if(!params.keepPosition) NC.tools.center(popup);
			if(nc_nav.which=='IE' && nc_nav.majVersion<9) params.fadeStep=100;
			else {
				if(!params.fadeStep) params.fadeStep=this.fadeStep;
				if(params.fadeStep<1 || params.fadeStep>100) params.fadeStep=100;
			}
			if(!params.pre) params.pre=this.pre;
			if(!params.post) params.post=this.post;
			mask=this.setMask({ show:true, color:params.maskColor, opacity:params.maskOpacity });
			if(popup.style.zIndex<mask.style.zIndex) popup.style.zIndex=mask.style.zIndex+1;
			this.memo[popup.id]=new Array(mask,params.post,params.fadeStep);
			// Custom parameters to place inside the opup, before opening it?
			if(params.custom) {
				for(i in params.custom) {
					el=document.getElementById(i);
					if(el) el.innerHTML=params.custom[i];
				} 
			}
			// Popup open
			if(params.fadeStep==100) setTimeout("document.getElementById('" + popup.id + "').style.display='block'",params.pre);
			else NC.tools.fade({ id:popup.id, to:100, step:params.fadeStep, display:'block', delay:params.pre });
		}
	},

	// Close the popup popID and turn the mask off
	close:function(popID) {
		var	popup=document.getElementById(popID), post, params;
		if(popup && this.memo[popup.id]) {
			params=this.memo[popup.id];
			params[0].style.display='none';			// Extinction du masque
			if(params[2]==100) setTimeout("document.getElementById('" + popup.id + "').style.display='none'",params[1]);
			else NC.tools.fade({ id:popup.id, to:0, step:params[2], delay:params[1] });
			this.memo[popup.id]=false;
		}
		return false;
	},

	/* *************** BODY MASK MANAGEMENT ************************
	 * Set the mask size and opacity. Must be called after the document is ready (onload) and each time the window is resized
	 * if the ask does not exist it is automatically created
	 * params :
	 *		id = mask ID. If not set, 'sf_automask' used instead
	 *		show : true -> forces mask display and resize. Nothing or false -> do nothing if the mask is not visible (called through an "onresize" event)
	 *		opacity = mask opacity (0-1, default=0.4)
	 *		color = mask color (default = #000)
	 *		zindex : default=1000
	 */
	
	setMask:function(params) {
		if(!params['id']) params['id']='sf_automask';
		var mask=document.getElementById(params['id']),
			opacity=params['opacity'] ? params['opacity'] : this.opacity,
			color=params['color'] ? params['color'] : '#000';
		if(!mask) {
			// automatic mask creation
			var mask=document.createElement('div');
			mask.id='sf_automask';
			mask.style.display='none';
			mask.style.backgroundColor='#000';
			document.body.appendChild(mask);
		}
		if(mask && (params['show'] || mask.style.display!='none')) {
			if(mask.style.position!='absolute') mask.style.position='absolute';
			if(mask.parentNode.tagName!='BODY') document.body.appendChild(mask);	// rattache le masque à la racine du body
			if(params['color']) mask.style.backgroundColor=params['color'];
			smask=mask.style;
			smask.width=0;
			smask.height=0;
			var screen=NC.tools.winInfo();
			smask.width=screen.totalWidth+'px';
			smask.height=screen.totalHeight+'px';
			smask.left='0px';
			smask.top='0px';
			smask.opacity=opacity;
			smask.zIndex=params['zindex'] ? params['zindex'] : 1000;
			smask.filter='alpha(opacity=' + (opacity*100)+ ')';
			smask.display='block';
		}
		return mask;
	}


}
	
