// JavaScript Document
/** Vyskakovaci okno 
 *  v 1.2
 */ 
function As4uPopup() {
  this.overlay = [];
  this.dataContainer = [];
  this.dataContainerClose = [];
  this.dataContainerIn = [];
  this.dataContainerLista = [];
  this.nooverlay = true;
  this.counter = 0;
  this.afterDataLoaded = "";
}


                                                       //pokud je volba frame vlozi se ulr do iframe
As4uPopup.prototype.show = function(url,cw,ch,callback,frame,win) {
    if (callback)
      this.afterDataLoaded = callback; //callback support - 31.5.2011
    
    var profi = this;          
    if (!this.overlay) {
      this.overlay = [];
    }
    this.counter++;   
    var counter = this.counter;
    //if (!this.overlay) {
    var isoverlay = this.nooverlay;
    
    for (var i=0; i < this.overlay.length;i++) {
      if (this.overlay[i]) {
        if (this.overlay[i].attr('id')) {
          isoverlay = true;
        }
      }
    }
    
    if (win) {
      console.log(win);
      var body = $(win.document).find('body');
    } else {
      var body = $('body');
    }
        
    if (!isoverlay) {
      var overlayheight = $(window).height() > $('body').height() ? $(window).height() : $('body').height();
      this.overlay[this.counter] = $('<div>', {
                      id: "as4uPopupOverlay"+this.counter,
                      className: "as4uPopupOverlay",
                      click: function(){
                        profi.close(counter);                        
                      }        
                    }).appendTo(body).css("width",$(body).width()).css("height",overlayheight).css("opacity",0.5);
    }
    var adurl = "";
      //novinka 6.6.2011 - mozno zadat velikost v procentech
    var percentage_w = 0.6;
    var percentage_h = 0.75;
    if (typeof cw == "string") {
      if (cw.indexOf('%') !== -1 ) {
        cw = cw.replace('%', '');
        percentage_w = parseInt(cw)/100;
        cw = false;  
      }
    }
    if (typeof ch == "string") {
      if (ch.indexOf('%') !== -1 ) {
        ch = ch.replace('%', '');
        percentage_w = parseInt(ch)/100;
        ch = false;  
      }
    }
    
    
    
    
    if (!cw) {
      var cw = Math.round($(window).width()*percentage_w);
      var cwidth = "";      
    } else {
        var cwidth = cw;
      }
    if (!ch) {
      var ch = Math.round($(window).height()*percentage_w);
      var cheight = "";
    } else {
      var cheight = ch;
    }
    if (ch+80 > $(window).height()) {      
      var ch = Math.round($(window).height()-105);
      var cheight = ch;
    } 
    
   
   
    
    if (url.indexOf('&w=') == -1)
      adurl += "&w="+cw;
    
    if (url.indexOf('&h=') == -1)
      adurl += "&h="+ch;
    //id okna do url
    adurl +='&thisPopupId='+this.counter;
    
    //create data container
    this.dataContainer[this.counter] = $('<div>',{ id: "as4uPopupDataContainer"+this.counter, className: "as4uPopupDataContainer"                    
              }).appendTo(body).css("width", cw).css("height",ch);  
    
    
    if (this.dataContainer[this.counter].css('position') == 'fixed' || 1==1) {
      var scrollTop = 0;
      var scrollLeft = 0;
    } else {
      var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
      var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
    }
    
    
     var top = Math.round($(window).height()/2 - (ch)/2 - 65/2) + scrollTop;
    var left = Math.round($(window).width()/2 - cw/2) + scrollLeft;
    
    this.dataContainer[this.counter].css("top",top).css("left",left);
    
    this.dataContainerLista[this.counter] = $('<div>',{ id: "as4uPopupLista"+this.counter,
                                        className: "as4uPopupLista"}).appendTo(this.dataContainer[this.counter]);
    
    //this.dataContainerLista[this.counter].html('<div class="inner"><div class="inner2"></div></div>'); 
    this.dataContainerLista[this.counter].html('<div class="inner"><div class="inner2"><span class="lista_title"></span></div></div>'); 
    
    this.dataContainerClose[this.counter] = $('<a>',{ id: "as4uPopupClose"+this.counter,
                                        className: "as4uPopupClose",
                                       href: "javascript:;",
                                       click: function() {
                                            profi.close(counter);
                                       }}).appendTo( this.dataContainerLista[this.counter].find('.inner2').eq(0) );
      
    this.dataContainerIn[this.counter] = $('<div>',{ id: "as4uPopupDataContainerIn"+this.counter, className: "as4uPopupDataContainerIn"                    
              }).appendTo(this.dataContainer[this.counter]);
    
    //nastaveni dragable
    this.dataContainer[this.counter].draggable({handle:profi.dataContainerLista[profi.counter]});
     
    this.rozmery = [];
    this.casovac = [];
    this.rozmery[this.counter] = {width:$(window).width(), height:$(window).height(), bwidth:$('body').width(), bheight:$('body').height()}; 
    this.casovac[this.counter] = setTimeout(function () {
      profi.sizeCounter(profi.counter,cwidth,cheight,frame);  
    }, 500);
    
    //z-index 
    //log(this.dataContainer[this.counter].css('zIndex'));
    this.dataContainer[this.counter].css('zIndex',parseInt(this.dataContainer[this.counter].css('zIndex'))+this.counter); 
    if (frame) { //vlozeni obsahu do iframu
      var iframe = '<iframe border="0" frameBorder="0" name="profiPopupDataIframe'+this.counter+'" style="width:100%;height:'+parseInt(ch-10)+'px;border:0px solid #fff;padding:0;margin:0;" id="profiPopupDataIframe'+this.counter+'" src="'+url + (url.indexOf('?') == -1 ? '?' : '&') + 'frameheight='+parseInt(ch-10)+'"></iframe>';      
      this.dataContainerIn[this.counter].html(iframe);
      if (typeof callback == "function") { //bude fungovat ? Otestovat  !!
        $('#profiPopupDataIframe'+this.counter).load(callback);
      }
    } else {          
      if (url) {
        url += adurl;               
        this.loadData(url);
      } else {
        return this.counter;
      }
    }
           
};

As4uPopup.prototype.sizeCounter = function(id,cw,ch,frame) {
  /*console.log('Resize kontrola');
  console.log('Stavajici rozmery');
  console.log(this.rozmery[id]);
  console.log(this.rozmery[id].width);*/
  if (!this.dataContainer[id]) {
    //console.log('Neni okno, konec casovace');
    return;
  }
  var cwidth = cw; 
  var cheight = ch; 
  if (this.rozmery[id].width != $(window).width() || this.rozmery[id].height != $(window).height() || this.rozmery[id].bwidth != $('body').width() || this.rozmery[id].bheight != $('body').height()) {
    //console.log('Detekovana změna');
    
    
    if (!cw) {
      var cw = Math.round($(window).width()*0.6);
      var cwidth = "";      
    } else {
        var cwidth = cw;
      }
    if (!ch) {
      var ch = Math.round($(window).height()*0.75);
      var cheight = "";
    } else {
      var cheight = ch;
    }
    if (ch+80 > $(window).height()) {      
      var ch = Math.round($(window).height()-105);
      var cheight = ch;
    }
    
    //pozici okna odpocteme z puvodnich souradnic
    var oldtop = parseInt(this.dataContainer[id].css('top'), 10);
    var oldleft = parseInt(this.dataContainer[id].css('left'), 10);
    if (this.dataContainer[id].css('position') == 'fixed' || 1==1 ) {
      var scrollTop = 0;
      var scrollLeft = 0;
    } else {
      var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
      var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
    }
    
        
    var top = Math.round(((oldtop-scrollTop) / this.rozmery[id].height) * $(window).height()) + scrollTop;
    var left = Math.round(((oldleft-scrollLeft) / this.rozmery[id].width) * $(window).width()) + scrollLeft;
    
     
    this.dataContainer[id].css('top', top);
    this.dataContainer[id].css('left', left);
    
    if (cw != this.dataContainer[id].width()) {
      this.dataContainer[id].css('width', cw); 
      
    } 
    if (ch != this.dataContainer[id].height()) {
      this.dataContainer[id].css('height', ch); 
      if (frame) {
        var listah = 0;
        try {
          if (this.dataContainerLista[id]) {
            listah = this.dataContainerLista[id].height();
          }
        } catch (e) {}
        $('#profiPopupDataIframe'+id).css('height', ch-10 - listah);  
      }  
    }   
    
    if (this.overlay[id]) {
      this.overlay[id].css('width', $('body').width());
      this.overlay[id].css('height', $('body').height());
    }
    this.rozmery[id] = {width:$(window).width(), height:$(window).height(), bwidth:$('body').width(), bheight:$('body').height()};
  }
  var profi = this;
  this.casovac[id] = setTimeout(function () {
    profi.sizeCounter(id,cwidth,cheight,frame);  
  }, 500);
};
  
As4uPopup.prototype.close = function(id) {      
    try {
      if (!id) {
        var id = this.counter; 
      } 
      if (this.overlay[id])
        this.overlay[id].detach(); 
        this.overlay[id] = '';        
      this.dataContainer[id].detach();
      $('#as4uPopupDataContainer'+[id]).detach();   
      clearTimeout(this.casovac[id]);    
    } catch(e) {}
};
  
As4uPopup.prototype.loadData = function(url,plus,dataType) {
    var profi = this;
    var counter = this.counter;
    if (!dataType) 
      var dataType = 'xml';  
       
    this.dataContainerIn[this.counter].css("background",'url(/images/gallery/loading.gif) no-repeat scroll center center #FFFFFF');
    
    profi.dataContainerIn[counter].load(url,
          function(){                
            $(profi.dataContainerIn[counter]).css("backgroundImage", "");
              
            try { //inicializace galerie
              if (!this.gallery4u) {          
                this.gallery4u = new Gallery4u(''); //inicializace
                var obj = this; 
                $(document).mousemove(function(e){
                  obj.gallery4u.jMousePosX = e.pageX;
                  obj.gallery4u.jMousePosY = e.pageY;
                });                   
              } else { 
                this.gallery4u.init();
              }   
            } catch(e) {            
            }
            if (typeof profi.afterDataLoaded == "function") { //callback
              profi.afterDataLoaded();
              profi.afterDataLoaded = "";              
              //alert(profi.dataContainerIn[counter]);               
            } 
          }); 
    
    /*$.ajax({ url: url, 
      dataType: dataType,
      success: function(html){                
        profi.dataContainerIn[counter].css("background",'transparent');
        if (dataType == 'xml') {
          if (!plus) {
            console.log("já jsem zápis do konzole vole");
            try {
              //console.log($(html).find('response').get().data);                            
              console.log(html.documentElement);
              console.log(html.responseXML);
              console.log(html.documentElement.innerHTML);
              console.log(html.documentElement.responseText);
              console.log(html.documentElement.firstChild.data.responseText);
              profi.dataContainerIn[counter].html(html.documentElement);
              
              //$(html).find('response').appendTo(profi.dataContainerIn[counter]);              
              
            } catch (e) {
              console.log(e);
            }
            console.log("A je to asi tam, snad.");
          } else {
            profi.dataContainerIn[counter].html(profi.dataContainerIn[counter].html()+$(html).find('response').html());
          }   
        } else {
          if (!plus) {       
            profi.dataContainerIn[counter].html(html);
          } else {
            profi.dataContainerIn[counter].html(html+profi.dataContainerIn[counter].html());
          }
        }
        if (typeof profi.afterDataLoaded == "function") { //callback
          profi.afterDataLoaded();
          profi.afterDataLoaded = "";
        }                 
     }});*/  
};   

//29.8.2011 - set title - moznost zadat title okna
    //- pokud nezadam id, beru posledni vytvoreny popup
As4uPopup.prototype.setTitle = function(title, id) {
  if (!id) {
    var id = this.counter;   
  }
  //lista_title      
  this.dataContainer[id].find('.lista_title').eq(0).html(title);    
};

var as4uPopup = new As4uPopup();
