// JavaScript Document

var Popup = {

  Ajax : true,
  width: 500,
  x: 0,
  y: 0,
  oldWidth: 0,
  oldHeight: 0,
  oldUri: '',
  iframe: null,
  onCloseReload: false,

  init: function(){

    /*
      <div id="popup">

        <div class="th" id="popup_handle"><div class="th"><div class="th"><span id="popup_head"></span></div></div></div>
        <div class="m" id="popup_content">

        </div>
        <div class="b"><div class="b"><div class="b"></div></div></div>

      </div>
     */

    pop = Builder.node('div', {id: 'popup', style: 'display:none;width:'+this.width+'px;position:absolute;'},
    [Builder.node('div', {className: 'ptr', id: 'popup_handle', style: 'cursor: move'},
      [Builder.node('img', {src: 'http://libs.abtera.cz/img/close.png', style: 'position:relative; top: 3px; left: -8px;float: right; cursor: pointer', onClick: 'Popup.close()'}),
        Builder.node('div', {className: 'ptl'},
        Builder.node('div', {className: 'pt'},
        Builder.node('span', {id: 'popup_head'}, 'head')
      )
      )
      ]
    ),
      Builder.node('div', {className: 'pr'},
      Builder.node('div', {className: 'pl'},
      Builder.node('div', {className: 'pm', id: 'popup_content', style: 'background-color: #ffffff;'})
    )
    ),
      Builder.node('div', {className: 'pbr'},
      Builder.node('div', {className: 'pbl'},
      Builder.node('div', {className: 'pb'})
    )
    )]
  );
    document.body.appendChild(pop);
    new Draggable('popup', { handle: 'handle' });
  },
  
  show: function(){
    Effect.Appear('popup');
  },
  hide: function(){
    Effect.Fade('popup');
  },
  close: function(){
    this.hide();
    BodyDisabler.stop();
    if(this.onCloseReload == true){
      location.reload();
    }
  },
  clear: function(){
    $('popup_head').innerHTML = '';
    $('popup_content').innerHTML = '';
  },

  resize: function(width, height){
    if(width == '-1'){
      if(Prototype.Browser.IE){
        width = document.body.clientWidth;
        height = document.body.clientHeight;
      } else {
        width = window.innerWidth-20;
        height = window.innerHeight;
      }
      
    }
    if(Prototype.Browser.IE){
      x = Math.round(document.body.clientWidth/2) - Math.round(width/2);
      y = Math.round(document.body.clientHeight/2) - Math.round((height-6)/2);
    } else {
      x = Math.round(window.innerWidth/2) - Math.round(width/2) -9;
      y = Math.round(window.innerHeight/2) + window.pageYOffset - Math.round((height-6)/2);
    }
    if(y<0){
      y = 0;
    }

    $('popup').setStyle({
      'width': width+'px',
      'height': height+'px',
      'top': y+'px',
      'left': x+'px'
    });
    
    h = height - 50;

    $('popup_content').setStyle({
      'height': h+'px'
    })
    //nastavení iframu
    if(iframe = $('popup_iframe')){
      iframe.setStyle({
        'width': width-25+'px',
        'height': h+'px'
      });
    }
  },

  openAjax: function(uri, width, head, disableBody, onCloseReload){
    if(disableBody==true){
      BodyDisabler.start();
    }

    if(onCloseReload==true){
      this.onCloseReload = true;
    } else {
      this.onCloseReload = false;
    }

    this.resize(width, 350);
    $('popup_head').innerHTML = head;
    new Ajax.Updater($('popup_content'), uri, {
      method: 'post'
    });
    this.show();
  },

  openIframe: function(uri, height, width, head, disableBody, onCloseReload){

    if(onCloseReload==true){
      this.onCloseReload = true;
    } else {
      this.onCloseReload = false;
    }

    if(disableBody==true){
      BodyDisabler.start();
    }
  
    $('popup_head').innerHTML = head;
    $('popup_content').innerHTML = '';
    iframe = Builder.node('iframe', {src: uri, id: 'popup_iframe', frameborder: '0', style: 'border: 0px;padding: 0px; margin: 0px; width:'+(width-25)+'px; height: '+height+'px'})
    $('popup_content').appendChild(iframe);

    this.resize(width, height);

    this.show();
  }
  
}

Event.observe(window, 'load', function(){Popup.init()});
