var Popup=Class.create({
	                      id               : 'popup',
	                      top              : 0,
	                      left             : 0,
	                      width            : 0,
	                      height           : 0,
	                      container        : null,
	                      eventSrcContainer: null,
	                      initialized      : false,
	                      browser          : null,
	                      showOverlay      : true,
	                      bodyDim          : new Object(),
	                      
	                      initialize       : function(id)
	                                         {
	                                         	if(this.initialized) {return;}
	                                         	
	                                         	var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
	                                          var IE7 = false /*@cc_on || @_jscript_version >= 5.7 @*/;
	                                          this.browser=(IE6) ? 'IE6' : ((IE7) ? 'IE7' : 'MOZ');
	                                          
	                                         	if((id!='')&&(id!=null)) {this.id=id;}
	                                         	
	                                         	//Insert overlay if not exists
	                                         	if($('mouseTrap')==null)
                                            {
		                                         myOverlay=new Overlay();
		                                        }	                                         	
	                                         	
	                                         	var body=$$('body').first();
	                                         	
	                                         	//Insert popup if not exists         
		                                        if($(this.id)==null)
                                            {
		                                         var objPop=new Element('div',{id:this.id});
		                                         objPop.addClassName('popup');
		                                         body.insert(objPop);
		                                         this.container=objPop;
		                                        }
		                                        else
		                                        {
		                                         this.container=$(this.id);	
		                                        }                                       	
	                                         	
	                                         	var body=$$('body').first()
	                                         	
	                                         	if(this.browser.indexOf('MOZ')!=-1)
	                                         	{
	                                         	 this.bodyDim.width=body.clientWidth;
	                                         	 this.bodyDim.height=body.clientHeight;
	                                         	}
	                                         	else
	                                         	{
	                                         	 this.bodyDim.width=body.getWidth();
	                                         	 this.bodyDim.height=body.getHeight();
                                            }
	                                         	
	                                         	this.initialized=true;	
	                                          
	                                         },	
	                      show             : function(event)
	                                         {
	                                         	openedPopup=this;
	                                          if(this.showOverlay)
	                                          {
	                                           myOverlay.layerDiv.onclick=function(){openedPopup.end();};
	                                           myOverlay.start();
	                                          }
	                                          
	                                          //Empeche la popup de 'sortir' de la page
	                                          var body=$$('body').first();
	                                          
	                                          this.container.setStyle({display:'block'});
	                                          var popDim=this.container.getDimensions();
	                                          
	                                          if(this.left+popDim.width+10>this.bodyDim.width)
                                            {
                                             this.left=this.left-popDim.width;
                                            }
                                            
                                            if(this.top+popDim.height+10>this.bodyDim.height)
                                            {
                                             this.top=this.top-popDim.height;
                                            }                   
	                                          this.container.setStyle({top:this.top+body.scrollTop,
	                                          	                       left:this.left+body.scrollLeft});
	                                         },
	                      showClick        : function(event)
	                                         {
	                                         	this.top=Event.pointerY(event);
	                                         	this.left=Event.pointerX(event);
	                                         	this.show(event);
	                                         },
	                      showModal        : function(event,padding)
	                                         {
	                                          this.top=padding;
	                                         	this.left=padding;
	                                         	
	                                         	var width=this.bodyDim.width-(parseInt(padding)*2);
	                                         	var height=this.bodyDim.height-(parseInt(padding)*2)

	                                         	this.container.setStyle({width:width+'px',
                                                                     height:height+'px'});
                                            
                                            var desc=this.container.firstDescendant();
                                            
                                            if(desc.tagName.indexOf('IFRAME')!=-1)
                                            {
                                             var offset=(this.browser.indexOf('MOZ')!=-1) ? 0 : -7;
                                             desc.setStyle({width:width+offset+'px',
                                                            height:height+offset+'px'});	
                                            }	
                                            this.show(event);
	                                         },
	                                         
	                      showTooltip      : function(e)
	                                         {                   
	                                          this.top=Event.pointerY(event)+10;
	                                         	this.left=Event.pointerX(event)+10;
	                                         	this.showOverlay=false;
	                                         	this.show(event);
	                                         },
	                                        
	                      changeModalPos   : function(padding)
	                                         {
	                                          this.top=padding;
	                                         	this.left=padding;
	                                         	var dimensions=$$('body').first().getDimensions();
                                            this.container.setStyle({top:padding,
                                            	                       left:padding,
                                            	                       width:dimensions.width-(parseInt(padding)*2)+'px',
                                                                     height:dimensions.height-(parseInt(padding)*2)+'px'});
                                            this.container.descendants().each(function(p)
                                                                              {
                                                                               if(p.tagName=='IFRAME')
                                                                               {
                                                                                p.setStyle({width:dimensions.width-(parseInt(padding)*2)-7+'px',
                                                                                            height:dimensions.height-(parseInt(padding)*2)-7+'px'});	
                                                                               }	
                                                                              });
	                                         },
	                      showSized        : function(event,width,height)
	                                         {
	                                          this.top=Event.pointerY(event);
	                                         	this.left=Event.pointerX(event);
	                                          if(width!=null)
	                                          {
	                                           this.width=width;
	                                         	 this.container.setStyle({width:width+'px'});
	                                          }
	                                         
	                                         if(height!=null)
	                                         {
	                                          this.height=height;
	                                         	this.container.setStyle({height:height+'px'});
                                           }                          
                                            this.container.descendants().each(function(p)
                                                                              {
                                                                               if(p.tagName=='IFRAME')
                                                                               {
                                                                                var offset=(this.browser=='MOZ') ? 5 : -7;
                                                                                p.setStyle({width:p.up().getWidth()+offset+'px',
                                                                                            height:p.up().getHeight()+offset+'px'});	
                                                                               }	
                                                                              });                         
                                            this.show(event);
	                                         },                 
	                      end              : function(message)
	                                         {
	                                         	this.container.hide();
	                                         	myOverlay.end();
	                                         	document.fire('popup:end',{popupId:this.id,endMessage:message});
	                                          openedPopup=null;
	                                         },
	                      setContent       : function(elem)
	                                         {
	                                         	this.container.insert(elem);	                                         	
	                                         }                 
	                      
	                                        
	                     });
	                     
	  //Insert overlay.js
var s=new Element('script',{type : 'text/javascript',
	                          src  : './js/overlay.js'});
$$('head').first().insert(s);
openedPopup=null;
