

/* -----------------------------------------------------------------------------
*
*  Funkce pro praci s popup okny   
*  - options viz this.defaults
* 
*  Last update: 2010-07-05  
*
*  v.1.2
*  added: nyni se pouziva trida Overlay na vytvoreni modalniho podkladu...
*
*  v.1.1.1
*  notic: code cleanup
*  added: refreshHostFrame(); 
*  
*  v.1.1.1
*  fixed: wierd webkit bug...
*
*  v1.1.0
*  added: shrink-to-fit iframe height (optional)  
*
*  v1.0.9
*  Minor fixes
*
*  v1.0.8
*  Fixed: opening fullsize window after a fixed width one
*  Fixed: Event.stopObserving(document, 'keydown', this.closeByEscEvent); - stopping nonexisting function...
*  Fixed: vieport scrolled down iframe height bug
*
*/

Dalen.InlinePopup = Class.create(Dalen, {
   initialize : function() {
      this.inserted = false;
      
      this.defaults = {
         escClose                   : true,
         showControls               : true,
         width                      : undefined,
         height                     : undefined,
         title                      : '>Není vyplněn titulek okna<',
         className                  : undefined,
         shrinkToFit                : false,
         refreshHostFrameOnClose    : false,
         onShow                     : undefined,
         onClose                    : undefined,
         
         // pokud je url stejne jako v _parent, prohlizece ho nechteji zobrazit
         url                        : './?' + new Date().getTime()          
      }

      this.options = {}
      
      // bounding cache
      this.bound = {
         close                      : this.close.bindAsEventListener(this),
         closeByEsc                 : this.closeByEsc.bindAsEventListener(this),
         setDimensions              : this.setDimensions.bindAsEventListener(this),
         checkIframeContentHeight   : this.checkIframeContentHeight.bindAsEventListener(this)
      }
      
      this.structure = {
         box         : new Element('div', {'class' : 'overlay-box'}),
         content     : new Element('div', {'class' : 'overlay-content', id : 'overlay-content'}),
         header      : new Element('div', {'class' : 'overlay-header'}),
         title       : new Element('h2',  {'class' : 'overlay-title'}),
         close       : new Element('a',   {
            'class'           : 'overlay-close',
            href              : '#',
            title             : 'Zavřít okno'
         }),
         closeSpan   : new Element('span').update('Zavřít'),         
         iframeBox   : new Element('div', {'class' : 'overlay-iframe-box'}),
         iframe      : new Element('iframe', {            
            width             : '100%',
            height            : '0',
            border            : '0',
            frameborder       : '0',
            allowtransparency : 'true'
         })
      }
      //IE6 horizontal scroll bugfix
      if (IE6) {
         this.structure.iframe.scrolling = 'yes';
      }      
      
      this.markup = this.structure.box.insert({
         bottom   : this.structure.content.insert({
            bottom   : this.structure.header.insert({
               bottom   : this.structure.title             
            }).insert({
               bottom   : this.structure.close.insert({
                  bottom   : this.structure.closeSpan
               })
            })
         }).insert({
            bottom   : this.structure.iframeBox.insert({
               bottom   : this.structure.iframe
            })
         })
      });
      
      this.overlay = new Overlay();
   },
      
   show : function(options, change) {
      if (!change) {
         Element.insert(document.body, {'bottom' : this.overlay.create()});
         Object.extend(this.options, this.defaults); // this.options naplnime defaultnimi hodnotami
      } 
      Object.extend(this.options, options); // this.options naplnime zmenami oproti defaultu nebo z change
      
      if (change && !options.url) {
         this.options.url = undefined;
      }
      
      if (this.options.url) { 
         this.structure.iframe.src = this.options.url;    
      }
      
      this.structure.title.update(this.options.title);
      
      if (this.options.className) {
         this.structure.box.addClassName(this.options.className);
      }
            
      if (!this.inserted && !change) {    
         Element.insert(document.body, {
            bottom   : this.markup
         });
      } else {
         this.structure.box.show();
      }
      
      false === this.options.showControls ? this.structure.header.hide() : this.structure.header.show();
      
      // pokud neoverujeme velikost obsahu v iframu, tak nastavime rozmery ihned
      // jinak se overi az na onload event iframu
      if (!this.options.shrinkToFit) {   
         this.setDimensions();
      }
      if (change && options.width) {
         this.setDimensions();
      }
      this.addEvents();
      
      if ('function' === typeof this.options.onShow) {
         this.options.onShow();
      }

   },
   
   change : function(changes) {
      this.show(changes, true);
   },
   
   close : function(e) {
      if (e && 'click' === e.type) {
         e.stop();
      }
   
      this.removeEvents(); // odstranit eventy
      this.structure.box.remove(); // odstranit okno
      
      if ('function' === typeof this.options.onClose) {
         this.options.onClose();
      }
      
      if (this.options.refreshHostFrameOnClose) {
         this.refreshHostFrame();
         return;
      }
      this.options = {} // vynulovat this.options
      this.overlay.destroy();
   },
   
   closeByEsc : function(e) {
      if (Event.KEY_ESC == e.keyCode) {
         this.close();
      }
   },
   
   closeAndRefresh : function() {
      this.close();
      this.refreshHostFrame();
   },
   
   closeWithoutRefresh : function() {
      this.close();
   },
   
   refreshHostFrame : function() {
      window.location.reload(true);
   },
   
   addEvents : function() {      
      Event.observe(window, 'resize', this.bound.setDimensions);
      Event.observe(this.structure.close, 'click', this.bound.close);
           
      if (true === this.options.escClose) {
         Event.observe(document, 'keydown', this.bound.closeByEsc);
      }
      
      if (true === this.options.shrinkToFit) {
         Event.observe(this.structure.iframe, 'load', this.bound.checkIframeContentHeight);
      }
      
   },
   
   removeEvents : function() {
      Event.stopObserving(window, 'resize', this.bound.setDimensions);
      Event.stopObserving(this.structure.close, 'click', this.bound.close);
      
      if (true === this.options.escClose) {
         Event.stopObserving(document, 'keydown', this.bound.closeByEsc);
      }
      
      if (true === this.options.shrinkToFit) {
         Event.stopObserving(this.structure.iframe, 'load', this.bound.checkIframeContentHeight);
      }

   },
   
   checkIframeContentHeight : function(e) {
      try {
         this.iframeContentHeight = this.getIframeContentHeight(this.structure.iframe, 20);
         this.setDimensions(); // nastavit rozmery az podle obsahu iframu...
      } 
      catch(error) {
         // do nothing...
      }
   },
   
   setDimensions : function() {
      var documentHeight      = Element.getHeight(document.body); // <IE7 
      var viewportWidth       = document.viewport.getWidth();
      var viewportHeight      = document.viewport.getHeight();
      var iframeHeight        = null;
      var iframeHeightCurrent = null;
      
      var contentWidth        = this.options.width && (this.options.width < (viewportWidth - this.getOtherDimensions('x'))) ? this.options.width : null;
      
      // nastaveni vysky - trosku slozitejsi...
      if (this.options.height && this.options.height < viewportHeight) {
         iframeHeight = this.options.height;
      } else {
         if (this.options.shrinkToFit) {
            if (this.iframeContentHeight < (viewportHeight - this.getOtherDimensions('y'))) {
               iframeHeight = this.iframeContentHeight;
            } else {
               iframeHeight = viewportHeight - this.getOtherDimensions('y');
            }
         } else {
            iframeHeight = viewportHeight - this.getOtherDimensions('y');
         } 
      }
         
      // pokud je specifikovana sirka a ta se do daneho prostoru vejde
      if (contentWidth) {
         this.structure.content.setStyle({
            width : contentWidth + 'px'
         })
         .addClassName('overlay-content-fixed-width')
         .removeClassName('overlay-content-fluid-width');
      } else {
         this.structure.content.setStyle({
            width : 'auto'
         })
         .addClassName('overlay-content-fluid-width')
         .removeClassName('overlay-content-fixed-width');
      }
      
      this.structure.iframe.height = iframeHeight;
      
   }, 
   
   getOtherDimensions : function(axis) {
      var Return = 0;
      
      // Kokot IE vraci null, pokud neni specifikovano, ostatni vraci 0px - nejak vymyslet lip
      // Cela tahle metoda stoji za vyliz prdel :)
      if ('x' === axis) {
         return 0;
         /*Return += parseInt(this.structure.content.getStyle('marginLeft') ? this.structure.content.getStyle('marginLeft') : 0);
         Return += parseInt(this.structure.content.getStyle('marginRight') ? this.structure.content.getStyle('marginRight') : 0);
         Return += parseInt(this.structure.content.getStyle('paddingLeft') ? this.structure.content.getStyle('paddingLeft') : 0);
         Return += parseInt(this.structure.content.getStyle('paddingRight') ? this.structure.content.getStyle('paddingRight') : 0);*/
      } else if ('y' === axis) {      
         Return += parseInt(this.structure.content.getStyle('marginTop') ? this.structure.content.getStyle('marginTop') : 0);
         Return += parseInt(this.structure.content.getStyle('marginBottom') ? this.structure.content.getStyle('marginBottom') : 0);
         Return += parseInt(this.structure.content.getStyle('paddingTop') ? this.structure.content.getStyle('paddingTop') : 0);
         Return += parseInt(this.structure.content.getStyle('paddingBottom') ? this.structure.content.getStyle('paddingBottom') : 0);
         if (this.options.showControls) {
            Return += this.structure.header.getHeight();
         }
      }
      return Return;
   }
});

var InlinePopup = new Dalen.InlinePopup();

/* backwards compatibility */
function openInlinePopup(url, title, height, width, popupId) {
   InlinePopup.show({
      url         : url,
      title       : title,
      height      : height,
      width       : width,
      className   : popupId   
   })
}

function destroyAllWindows() {
   InlinePopup.close();
   window.location.reload(true);
}
