var ModalBox = Class.create({
    initialize: function(data,width,height,format,close){
		if(close===undefined) close = true;
        this.width = width;
        this.height = height;
        this.data = data;
        if(!format) this.format = 'ajax'; else this.format = format;
        //alert(url);
        this.createbox(close);  
       // this._top = 50;          
    },
    createbox: function(close){  
      if(Prototype.Browser.IE){$$('select').invoke('hide'); }    
      this.div = new Element('div',{'id':'modalbox', 'class':'modal'/*, 'style':'width:'+this.width+'px; height:'+this.height+'px;'*/});       
      this.divBg = new Element('div',{'id':'mb_bg'});    
      this.divWrap = new Element('div',{'id':'mb_wrap', 'style':'width:'+this.width+'px; height:'+this.height+'px;'});   
      if(close){ var aClose = new Element('a',{'class':'modalClose'});  aClose.addClassName('modalClose'); }
      this.divContent = new Element('div',{'id':'mb_content'});
      
      this.div.insert(this.divBg); 
      if(close) this.divWrap.insert(aClose);  
      this.divWrap.insert(this.divContent);    
      this.div.insert(this.divWrap);  
               
      this.divBg.setOpacity(0.6);
      $$('body')[0].insert(this.div);  
      var pos = $$('body')[0].cumulativeScrollOffset();  
      var top = pos[1];    
   //   this.div.writeAttribute('style','top:'+top+'px;'); 
    /*  if(navigator.appName!='Netscape'){  
          $$('html')[0].writeAttribute('style','overflow:hidden'); 
          $$('body')[0].writeAttribute('style','overflow:hidden');                      
      }else{   
     //     Event.observe(window,'scroll',this.onScroll.bind(this));  
      }    */     
      
      // Event
      if(close) this.div.observe('click',this.onBlur.bind(this));  
   //   this.divContent.observe('click',function(event){ Event.stop(event);});    
      if(close) aClose.observe('click',this.clickOnClose.bind(this));
      
      
      this.getContent(this.divContent);      
      
                      
    },
    getContent: function(contentElt){    
        switch(this.format){
         case 'ajax':
                 var data = this.data;  
                        new Ajax.Request(data, {                                                       
                              //parameters: {'format':'json','k':this.key,'c':c},
                              method: 'get',
                              onSuccess: function(transport){
                                var response = transport.responseText;
                                contentElt.update(response);
                              },
                              onLoading: function(){
                                 contentElt.update('<div style="position:absolute;"><img src="/public/images/ajaxloader.gif" /></div>'); 
                              }
                            }); 
         break;
         case 'frame':
              this.divContent.writeAttribute('style','overflow:hidden;');  
              var loader = new Element('img',{'src':'/public/images/ajaxloader.gif','style':'position:absolute;'});
              contentElt.update(loader);         
              var iframe = new Element('iframe',{'id':'frame','src':this.data,'frameborder':0}); 
              iframe.observe('load',function(){loader.remove(); });    
              contentElt.insert(iframe);  
         break;
         case 'text':
               var p = new Element('p',{'style':'padding:10px'});
               p.update(this.data);  
               contentElt.update(p);
         break;
         case 'html':
               contentElt.update(this.data);
         break;
        }
    },
    onScroll: function(event){
       var pos = $$('body')[0].cumulativeScrollOffset();  
       var top = pos[1]; 
       this.div.writeAttribute('style','top:'+top+'px;');  
    },                      
    close: function(){
      var div = this.div;
      var divBg = this.divBg;   
      
      if(navigator.appName!='Netscape'){ this.divContent.remove(); var op=0.4; var time=0.01} else{ var op=0.5; var time=0.001 }
       new PeriodicalExecuter(function(pe) {op-=0.2; div.setOpacity(op); if(op<=0){ div.remove(); pe.stop(); } }, 0.01); 
      if(Prototype.Browser.IE){$$('select').invoke('show'); }         
    // this.div.hide(); 
    },
    onBlur : function(event){ 
       var element = Event.element(event);
      if(!element.descendantOf('mb_wrap') && element.identify()!='mb_wrap'){ 
         this.close();
      }
    },
    clickOnClose: function(event){
      this.close();    
    }
    
});