var popup_simple = Class.create({
	initialize: function( element, popup_w, popup_h, contenu) {
		
		this.effet_actif = true;
		
		this.window_width = 0;
		this.window_height = 0;
		this.window_aff_height = 0;
		this.offset_x = 0;
		this.offset_y = 0;
		this.popup_width = popup_w;
		this.popup_height = popup_h;
		this.contenu_html = contenu;
		this.popup_top = 0;
		this.popup_left = 0;
		this.back_html = "";
		this.popup_html = "";
		this.id_elt_back = "background_" + element;
		this.id_elt = "contener_popup_simple_" + element;
		
		this.elt_body = document.getElementsByTagName('body')[0];
		
		//alert( "id : " + element);
		this.init_window_size();
		this.init_window_aff_height();
		this.calcul_popup_position();
		
		this.create_background();
		this.create_popup();
		
		this.show_popup();
		
		this.click_close = this._click_close.bindAsEventListener(this);
		this.active_bt_close();
		
	}, 
	init_window_size: function() {
		if (self.innerHeight) { // all except Explorer
			this.window_height = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			this.window_height = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			this.window_height = document.body.clientHeight;
		}
		this.window_width = $(this.elt_body).getWidth();
		
		var realOff = Position.realOffset(this.elt_body);
		var x, y;
		this.offset_x = realOff[0];
		this.offset_y = realOff[1];
		
	}, 
	init_window_aff_height: function() {
		
		var hauteur_page = 0;
		// recherche des hauteur des elements du body
		var elts_body = $(this.elt_body).immediateDescendants();
		elts_body.each( function( element) {
			hauteur_page += element.getHeight();
		});
		
		var hauteur_aff = this.window_height;
		if( hauteur_page > this.window_height ) {
			hauteur_aff = hauteur_page;
		}
		this.window_aff_height = hauteur_aff;
		
	}, 
	calcul_popup_position: function() {
		
		this.top = ( this.window_height - this.popup_height ) / 2;
		this.left = ( this.window_width - this.popup_width) / 2;
		
		this.left = parseInt( this.offset_x + this.left);
		this.top = parseInt( this.offset_y + this.top);
		
		if( this.window_height < this.popup_height ) this.top = 0;
		
	}, 
	create_background: function() {
		
		this.back_html += "<div id='"+this.id_elt_back+"' style=' background-color: #000000; position: absolute; top: 0px; left: 0px; width: 100%; height: "+this.window_aff_height+"px;'></div>";
		
	}, 
	create_popup: function() {
		
		this.popup_html = "<div id='"+this.id_elt+"' style=' position: absolute; top: "+this.top+"px; left: "+this.left+"px; width: "+this.popup_width+"px; height: "+this.popup_height+"px;'>"+this.contenu_html+"</div>";
		
	}, 
	show_popup: function() {
		
		Insertion.Bottom( document.body, this.back_html);
		Insertion.Bottom( document.body, this.popup_html);
		$( this.id_elt_back).setStyle({ opacity: 0.8 });
		$( this.id_elt).setStyle({ opacity: 1 });
		
	}, 
	active_bt_close: function() {
		
		var bt_close = $( this.id_elt).getElementsByClassName( "close_popup_simple");
		if( bt_close.length > 0 ) {
			for( var i = 0 ; i < bt_close.length ; i++ ) {
				bt_close[i].observe( "click", this.click_close);
				bt_close[i].setStyle({ cursor: "pointer" });
			}
		}
		
	}, 
	_click_close: function( obj) {
				
		if( this.effet_actif ) {
			var elt = this.id_elt;
			var elt_back = this.id_elt_back;
			new Effect.Fade( this.id_elt, { duration: 1.0, afterFinish: function( ) {
				$( elt).remove();
			}});
			new Effect.Fade( this.id_elt_back, { duration: 1.0, afterFinish: function() {
				$( elt_back).remove();
			}});
		} else {
			$( this.id_elt).remove();
			$( this.id_elt_back).remove();
		}
		
	}
});
