/**
* livrairies necessaires :
* 	prototype.js
*	effects.js
*/

var PopupInternal = function(pPopupRootId,pPopupContentId,pDisableAllScreenId) {
	this.popup_root_id = pPopupRootId;
	this.popup_content_id = pPopupContentId;
	this.loading = '';
	this.disable_all_screen_id = pDisableAllScreenId;
	this.isPopupModal = false;
}

PopupInternal.prototype = {
	/**  on construit un contenu comprenant l'image de loading plus un texte (veuillez patienter (en, fr)) */
	initLoading: function(picture, text) {
		var load_content = '<div id="inLoading">';
		load_content += '	<table style="width:100%; height:100%;">';
		load_content += '		<tr>';
		load_content += '			<td style="vertical-align:middle; text-align:right"><img src="' + picture + '"/></td>';
		load_content += '			<td style="vertical-align:middle; text-align:left">' + text + '</td>';
		load_content += '		</tr>';
		load_content += '	</table>';
		load_content += '</div>';
		
		this.loading = load_content;
	},

	/**
	 * fonction qui ouvre le formulaire de saisie pour l'envoi d'un email
	 * @param modal : si true alors la fenetre bloque tout le contenu du site (showModal)
	 */
	openPopup :function(modal, url_get_form, event) {
		var div = $(this.popup_root_id);
		// on charge le div disable_all_screen_id
		if(true==modal) {
		    this.disableScreen();    
		}
		var y = Event.pointerY(event) - 300;
		var styles = {position : 'absolute', top : y + 'px', zIndex:11};
		div.setStyle(styles);
		
		// on pose le div mail au meme niveau que le div disable_all_screen_id
		document.body.appendChild(div);
		
		var obj_content = $(this.popup_content_id);
		if ('IFRAME' != obj_content.tagName.toUpperCase()) {
			// on charge l'image de loading
			try{new Element.update(this.popup_content_id,this.loading);}catch(err) {}
		}
		
        // on fait apparaitre le div correspondant au popup_root_id
		// a la fin du hargement, on lance l'ajax qui reccupere le formulaire mail
		// si tout se passe bien alors on definit le div affiche actuellement
		new Effect.Appear(this.popup_root_id, {duration: 0.6,
			to:1.0, 
			afterFinish:function() {
				try {
					var obj_content = $(this.popup_content_id);
					if ('IFRAME' == obj_content.tagName.toUpperCase()) {
						obj_content.src=url_get_form;
					} else {
						new Ajax.Updater(this.popup_content_id, url_get_form, { method: 'get',
						asynchronous: true, 
						evalScripts: false,
						onSuccess : function() {
							/*this.popup_content_id;*/
						}.bind(this)});
					}
				}catch(err) {}
			}.bind(this) });
	},
	
    /**
	 * Fonction qui ferme le formulaire de saisie pour l'envoi d'un email
	 * on verifie si cette fenetre est modale avant de la fermer si elle l'est on 
	 * reactive l'ecran
	 */
	closePopup :function() {
	    // si on a choisit un popup modal alors on desactive le div disable_all_screen_id
		if(true==this.isPopupModal) {
	        this.enableScreen();
		}
		// on desctive le div popup_root_id
		new Effect.Fade(this.popup_root_id,{ duration: 0.6 });
		// on vide le contenu
		new Element.update(this.popup_content_id,'');
	},
	 
	
	/**
	 * fonction qui cree un div dont le but est de neutraliser l'ecran entier
	 */
	disableScreen : function() {
		// on appliqie les dimensions du body au div disable_all_screen_id 
		var nav_size  = Element.getDimensions(document.body);
		var div = $(this.disable_all_screen_id);
		var styles = { height: nav_size.height +'px', width: nav_size.width + 'px', zIndex:10};
		div.setStyle(styles);
		// on deplace le div directement sur le body
		document.body.appendChild(div);
		// on active le div
	    new Effect.Appear(this.disable_all_screen_id,{ duration: 0.6 ,to: 0.6});
		this.isPopupModal = true;
	},
	/**
	 * fonction qui supprime le div bloquant
	 */
	enableScreen : function() {
	    // on desactive le div (reactivation de l'ecran)
	    new Effect.Fade(this.disable_all_screen_id ,{ duration: 0.6});
		this.isPopupModal = false;
	}

}