popup_compteur_iframe = 0;
var Popup = new Class({
	Implements: Options,
	
	options: {
		titre: 'sans titre',
		largeur: '400',
		hauteur: '350',
		background_page: true,
		
		// type de contenu
		iframe: false, // url de l'iframe
		html: null,// contenu html de la popup
		ajax_url: null,// url ajax de la popup
		ajax_loader: null,// loader de chargement ajax de la popup
		ajax_params: null,// parametres ajax de la popup
		
		// autoriser le fond ( overlay ) à se comptorter comme le bouton fermer
		overlay_close_ok:true,

		onBeforeClose: $empty,
		onClose: $empty
	},

	initialize: function(idcible,options){	
		this.idcible = idcible;
		this.idcontenu = idcible+'_contenu';
		this.setOptions(options);
		
		if(!options.onBeforeClose)this.options.onBeforeClose = function(){return true;};
		if(!options.onClose)this.options.onClose = function(){this.close();}.bind(this);
		
		this.open();
	},

	// fonction d'ouverture de la popup
	open: function (){	
		// on cache les liste deroulantes ( bug )
		this.hide_select();

		if(!$(this.idcible)) {
			// Création de ma structure
			var nBox1 = new Element('div', {'id': this.idcible+'_main'});
			var nBox3 = new Element('div', {'id': this.idcible, 'class': 'neftisbox'});
			var nBox4 = new Element('a', {'id': this.idcible+'_close', 'class': 'close', 'href': 'javascript:void(0);', 'html': 'FERMER'});
			var nBox5 = new Element('div', {'id': this.idcible+'_titre','class': 'titre'});
			var nBox5 = new Element('div', {'id': this.idcible+'_titre','class': 'titre'});
			var nBox6 = new Element('div', {'id': this.idcible+'_contenu', 'class': 'encadre'});

			// Injection des données
			nBox4.inject(nBox3);
			nBox5.inject(nBox3);
			nBox6.inject(nBox3);
			nBox3.inject(nBox1);			
			nBox1.inject($(document.body));
			
			// J'injecte le reste des données
			if(this.options.background_page == true)this.add_background();
			
			// on positionne la popup
			$(this.idcible).setStyle('width',this.options.largeur+"px");
			$(this.idcible).setStyle('height',this.options.hauteur+"px");
			$(this.idcible).setStyle('margin-left', "-"+Math.round(this.options.largeur/2)+"px");
			margintop = (eval($(document.body).getSize().y)-eval(this.options.hauteur))/2+eval($(document.body).getScroll().y);
			$(this.idcible).setStyle('margin-top', Math.round(margintop)+"px");
			
			// bouton fermer + fond
			$(this.idcible+"_close").addEvent('click', function(){
				this.options.onClose();
			}.bind(this));
			
			if(this.options.overlay_close_ok==true){
				$(this.idcible+"_fond").addEvent('click', function(){
					this.options.onClose();
				}.bind(this));
			}

		} else if(!$(this.idcible+'_contenu')) {
			alert("L'id : "+this.idcible+", que vous cherchez à utiliser existe déjà");
		}

		// recherche de la hauteur du contenu
		this.hauteurContenu = this.get_height_contenu();

		// on ajoute le titre
		this.set_titre(this.options.titre);

		// Je force la taille du contenu par rapport a la hauteur du titre.
		$(this.idcible+'_contenu').setStyle('height', this.hauteurContenu);

		// gestion du type de contenu		
		if(this.options.iframe){// version avec iframe
			this.add_iframe();
			
		} else if(this.options.contenu){ // version avec contenu html
			$(this.idcontenu).set('html',this.options.contenu);
			
		} else if(this.options.ajax_url){// version ajax
			new Request.HTML({
				method: 'post',
				url: this.options.ajax_url,
				data: this.options.ajax_params,
				onRequest: function(){
					$(this.idcontenu).set('html',this.options.ajax_loader);
				}.bind(this),
				update: $(this.idcontenu)
			}).send();
			
		}
		
	},

	// ajoute un titre à la popup
	set_titre: function(titre){
		$(this.idcible+"_titre").set('html',titre);
	},

	// calcul la hauteur du contenu
	get_height_contenu: function(){
		hauteurContenu = this.options.hauteur - ($(this.idcible+'_titre').getStyle('height','').toInt() + $(this.idcible+'_titre').getStyle('padding-top','').toInt() + $(this.idcible+'_titre').getStyle('padding-bottom','').toInt());
		return hauteurContenu;
	},

	// ajoute un fond gris sur tout le body
	add_background: function(){
		hauteur_scroll_body = $(document.body).getScrollSize().y;
		var nBox2 = new Element('div', {'id': this.idcible+'_fond', 'class': 'neftisbox_fond','styles':{'height':hauteur_scroll_body}});
		nBox2.inject(this.idcible+'_main');		
	},

	// ajoute une iframe dans le contenu de la popup
	add_iframe: function(){
		popup_compteur_iframe++;
		this.nameiframe = this.idcible+"_iframe_"+popup_compteur_iframe;
		
		var iframe1 = new Element('iframe', {'id': this.idcible+'_iframe','name':this.nameiframe,'src': this.options.iframe,'class':'iframe'});
		iframe1.set('height', this.hauteurContenu);
		iframe1.inject(this.idcontenu);

		$(this.idcible+'_iframe').set('src',this.options.iframe);
	},
	
	// function de fermeture de la popup
	close: function (){
		// on execute la fonction beforeclose avnt de fermer
		if(this.options.onBeforeClose)this.options.onBeforeClose();
		
		var liste = $$(".neftisbox");

		nb = liste.length;
		if(nb==1){
			// on affiche les liste derouantes ( bug )
			this.show_select();
		} else if(nb>1){
			var calID = liste[(nb-2)].getProperty('id');
			// on affiche les liste derouantes ( bug )
			this.show_select(calID);
		}
		
		// on supprime la popup
		if($(this.idcible+'_main'))$(this.idcible+'_main').destroy();
	},
	
	
	hide_select: function(){
		oSelects = document.getElementsByTagName('select');
		if (oSelects.length > 0) {
			for (i = 0; i < oSelects.length; i++) {
				oSelects[i].style.visibility = 'hidden';
			}
		}		
	},
	
	show_select: function(element_id){
		if(!document.getElementById(element_id)){
			oSelects = document.getElementsByTagName('select');
		} else {
			oSelects = document.getElementById(element_id).getElementsByTagName('select');
		}
		if (oSelects.length > 0) {
			for (i = 0; i < oSelects.length; i++) {
				oSelects[i].style.visibility = 'visible';
			}
		}		
	}	

});
