var popupClass = Class.create({
	
	popup:null,
	DEFAULT_LOADER_MESSAGE:'Chargement en cours ...',
	popup_id:null,
	closeCallBack:null,
	popupID:"popup",
	initialize:function(popup_id){
	
		if(popup_id==undefined)popup_id=this.popupID;
	this.popup_id=popup_id;
},
createPopup:function(container){
		
	if(this.popup){
		Event.observe(this.closeBtn,'click', this.closePopup.bindAsEventListener(this));
		return true;
		}
	
	if(!$(container))container=document.body;
	$(container).insert({'top':'<div id="'+this.popup_id+'" style="display: none;"></div>'});
	this.popup=$(this.popup_id);
	
	this.popup_fond=new Element('div',{id:this.popup_id+'-fond'})
	
	this.popup_fond.setOpacity(0.5);
	
	this.popup.appendChild(this.popup_fond);
	this.popup_front=new Element('div',{id:this.popup_id+'-front'});
	
	this.popup_cont=new Element('div',{id:this.popup_id+'-cont'});
	this.closeBtn=new Element('a',{className:'btn-fermer'});
	this.closeBtn.update("X");
	this.popup_cont.appendChild(this.closeBtn);
	this.contentDiv=new Element('div',{className:"popupContDiv"});	
	this.popup_cont.appendChild(this.contentDiv);
	this.ajaxLoader=new Element('div',{className:'popupLoader'});
		
	this.popup_cont.appendChild(this.ajaxLoader);
	this.popup_cont.insert({bottom:'<div class="clear"></div>'});
	this.popup_front.appendChild(this.popup_cont);
	this.popupContainer=new Element('div',{className:'popupMainCont'});
	//this.popupContainer.appendChild(this.popup_front);
	
	this.popup.appendChild(this.popup_front);

	Event.observe(this.closeBtn,'click', this.closePopup.bindAsEventListener(this));
//this.openPopup({modal:true});
	return true;
},

closePopup:function(e){
	if(e)Event.stop(e);

	if(this.onClose!=undefined&&typeof this.onClose=="function")this.onClose();
	this.onClose=null;
	
	new Effect.Opacity(this.popup,{duration:0.3,from:1,to:0, afterFinish:function(){
		Event.stopObserving(this.closeBtn,'click', this.closePopup.bindAsEventListener(this));
		Event.stopObserving(this.popup_fond,'click', this.closePopup.bindAsEventListener(this));
		if(this.closeCallBack) this.closeCallBack();
		this.closeCallBack=null;
		this.anchorElement=null;
		//this.popup_front.setStyle({top:'50%'});
		
		
		this.popup.hide();
		this.contentDiv.update();
	}.bind(this)});
	
		
	
	
},
openPopup:function(options){
	if(options==undefined) options={};
	this.createPopup();
	this.ajaxLoader.hide();
	this.contentDiv.update();
	
	this.anchorElement=null;
	
	this.closeBtn.hide();
	finishCallBack=null;
	this.popup.setOpacity(0);
	this.popup.show();
	if(options){
	
		this.setModal(options.modal);
		if(options.ajaxWait)this.showAjaxLoader(options.ajaxMessage);
		if(options.callBack&&typeof options.callBack=="function") finishCallBack=options.callBack;
		if(options.closeBtn)this.closeBtn.show();
		if(options.content)this.displayMessage(options.content,false);
		
		if(options.elt&&$(options.elt))this.anchorElement=options.elt;
		}
	
	
	
	//if(this.anchorElement)
	
	if(this.popup.style.display=='none'||this.popup.style.display.blank()){
		if(options.ajaxWait)this.contentDiv.hide();
		else this.contentDiv.show();
	new Effect.Opacity(this.popup,{duration:0.3,from:0,to:1, afterFinish:finishCallBack});
	}
	else {
		this.popup.setOpacity(1);
		if(finishCallBack)finishCallBack();
		
	}
},

showAjaxLoader:function(message){
	this.contentDiv.hide();
	this.closeBtn.hide();
	this.setModal(false);
	if(!message)message=this.DEFAULT_LOADER_MESSAGE;
	this.ajaxLoader.update(message);
	
	dim=this.ajaxLoader.getDimensions();
	dimPopup=this.popup_cont.getDimensions();
	this.popup_cont.setStyle({height:'auto'});
	
	//this.ajaxLoader.setStyle({left:(dimPopup.width-dim.width)/2+'px'})
	this.ajaxLoader.show();
	this.popup.show();
	this.setUpPosition();
},
displayMessage:function(message,display){
	
	this.ajaxLoader.hide();
	this.contentDiv.update(message);
	this.contentDiv.appendChild(new Element('div',{className:'clear'}));
	this.contentDiv.appendChild(new Element('p'));
	
	if(display==undefined||display==true)this.contentDiv.show();
	this.setUpPosition();
	
},
showContent:function(){

	this.contentDiv.show();
	this.ajaxLoader.hide();
	this.setModal(true);
	this.closeBtn.show();
	this.popup.setOpacity(1);
	this.popup.show();
	
	this.setUpPosition();
	
	
},
setUpPosition:function(){
	return;
	if(!this.anchorElement) var top = (document.viewport.getHeight()-this.popup_front.getDimensions().height)/2;//return this.popup_front.setStyle({top:(+"px"});
else var top=(this.anchorElement.viewportOffset().top-this.popup_front.getDimensions().height);

if(top<=0) top=10;
	
	
	this.popup_front.setStyle({top:top+"px"});
	
},

setModal:function(modal){
	
	Event.stopObserving(this.popup_fond,'click');
	if(modal==true)return Event.observe(this.popup_fond,'click',this.closePopup.bindAsEventListener(this));
	return;
	
	
}
	
	
});
