//jQuery = jQuery.noConflict(true);

// pozice mysi
var mouseXJ = 0;
var mouseYJ = 0;

if(typeof jQuery == 'function'){
  var gallery4u, panoramaGallery4u;
  jQuery(document).ready(function(){
     gallery4u = new Gallery4u; //inicializace
     
     panoramaGallery4u = new PanoramaGallery4u; //inicializace panoramatu    
       
     jQuery(document).mousemove(function(e){
        gallery4u.jMousePosX = e.pageX;
        gallery4u.jMousePosY = e.pageY;
     });  
  });
}

// panorama gallery - 24.11.2011

function PanoramaGallery4u() {
  //format rel - galleryPanorama[typ,obrID,rozmer2_x,rozmer2_y]
  this.rExp = /galleryPanorama\[(.*?),(.*?),(.*?),(.*?)\]/; //parsing regexp for params
  
  this.init();
}

PanoramaGallery4u.prototype.init = function() {
  var obj = this;
  jQuery('img.gallery_panorama').each(function(index) {
    if (jQuery(this).attr('rel')) {
      var rel = jQuery(this).attr('rel');
      rel = rel.replace(/\s/g, '');
      var matched = rel.match(obj.rExp);      
      if (matched) {
       
        var obr = matched[2];
        var w = parseInt(matched[3]);
        var h = parseInt(matched[4]); 
          //zobrazit ve strance
        if (matched[1] == '1') {
          var div = jQuery('<div>').insertBefore( jQuery(this) ).css('width',w).css('height',h);          
          div.load('http://'+location.host+'/galerie/obrazky/panorama.php?obr='+obr+'&w='+w+'&h='+h);
          jQuery(this).detach();       
            //vyskakovací okno 
        } else if(matched[1] == '2') {
          var a = jQuery('<a>').insertBefore( jQuery(this) );
          a.attr('href','javascript:;');
          a.click(function() {
            as4uPopup.nooverlay = false;
            as4uPopup.show('http://'+location.host+'/galerie/obrazky/panorama.php?obr='+obr+'&w='+w+'&h='+h, w+15, h+15);
            as4uPopup.nooverlay = true;
          }); 
          jQuery(this).appendTo(a);
          jQuery(this).attr('rel','');
        }        
        
      }
    }
  });
};




// gallery4u - galerie2 - 10.2010

function Gallery4u(rExp,onclosefocus) {
  this.galSizeH = 0;
  this.galSizeW = 0;
  this.galScroll = 0;
  this.galMiniH = 0;
  this.galTopDivH = 0;
  this.galId = 0;
  
  this.aktualFoto = 0;
  this.predesleFoto = 0;
  this.dalsiFoto = 0;
  this.oldFoto = 0;
  
  this.casovacBloku;
  this.casovacPrehravani;
  this.gallang = 'cs';
  
  this.jMousePosX = 0;
  this.jMousePosY = 0;
  
  this.jMousePosXold = 0;
  this.jMousePosYold = 0;
  
  this.fotos = new Array;
  
  this.baseAjaxUrl = '/galerie/ajax.php?';          
  
      //21.11.2011 - Novy parametr pro galleryID - all - pokud je all nacteme vsechny fotky z adresare      
    //gallery4u[obrID,adrID,minisize,lang,galleryID] 
  this.rExp = /gallery4u\[(.*?),(.*?),(.*?),(.*?),(.*?)\]/; //parsing regexp for params
  this.rExp2 = /gallery4u\[(.*?),(.*?),(.*?),(.*?)\]/; //parsing regexp for params
  this.rExpCompatibility = /showgalery\((.*?),(.*?),(.*?),(.*?)\)/;
  
  if (rExp)
    this.rExp = rExp;
  
  this.init(onclosefocus);  

}
//onclosefocus - after close gallery return focus to some element 
Gallery4u.prototype.init = function(onclosefocus) {
  var obj = this;  
  this.fotos = new Array;
  jQuery('a').each(function(index) {
      if (jQuery(this).attr('rel')) {
        //if (!jQuery(this).hasClass('gallery4u-initialized')) {
          var rel = jQuery(this).attr('rel');
          var matched = rel.match(obj.rExp); 
          if (!matched)  
            var matched = rel.match(obj.rExp2);        
          if (matched) {        
            if (matched[1]) {
              if (matched[5]) {
                var galleryID = matched[5];
              } else {
                var galleryID = 1;
              }
             
              if (typeof obj.fotos[galleryID] == "undefined") {
                obj.fotos[galleryID] = new Array;
              }           
            
              //obj.fotos[galleryID][obj.fotos[galleryID].length] = matched[1];
              obj.fotos[galleryID].push(matched[1]);
              
              jQuery(this).click(function(){  
                obj.showgallery(matched[1], matched[2], matched[3], matched[4], galleryID); 
                return false;   
              }); 
             // jQuery(this).addClass('gallery4u-initialized');           
            }         
          }
        //}
      } 
      //back compatibility
      if (jQuery(this).attr('onclick')) {
        //console.log('Hledame showgalery');
        var val = this.getAttributeNode('onclick').value;
        var matched = val.match(obj.rExpCompatibility);
      
        //console.log('Našli jsme něco?');
        //console.log(matched);
        if (matched) { 
          //console.log('Máme něco');          
          
          matched[1] = matched[1].replace("'", '').replace("'", '').replace(' ', '');
          matched[2] = matched[2].replace("'", '').replace("'", '').replace(' ', '');
          matched[3] = matched[3].replace("'", '').replace("'", '').replace(' ', '');
          matched[4] = matched[4].replace("'", '').replace("'", '').replace(' ', '');
          
          if (matched[5]) {
            matched[5] = matched[5].replace("'", '').replace("'", '').replace(' ', '');
            var galleryID = matched[5];
          } else {
            var galleryID = 1;
          }
          
          if (typeof obj.fotos[galleryID] == "undefined") {
            obj.fotos[galleryID] = new Array;
          } 
          
          //console.log('obrID - ' + matched[1]);
          
          obj.fotos[galleryID].push(matched[1]);
          
          jQuery(this).click(function(){  
            obj.showgallery(matched[1], matched[2], matched[3], matched[4], matched[2]); 
            return false;   
          });
          
          this.getAttributeNode('onclick').value = ''; 
        }
      }
      
      
    });
    if (onclosefocus) {
      this.onclosefocus = onclosefocus; 
    }
}

Gallery4u.prototype.initAndShow = function(element) {
  this.init();  
  if (typeof element.onclick == "function") {    
    element.onclick();
  }
  jQuery(element).attr('onmouseover', '');
}

Gallery4u.prototype.showgallery = 
  function(obr, gal, minisize, lang, galleryID) {
    var galeryDiv;
    minisize = 120;
    this.galMiniH = minisize;
    this.galId = gal;
    this.gallang = lang;
    this.galleryID = galleryID;   
    //21.11.2011 - Pokud galleryID = all - vypsat veschny fotky z daneho adresare
    if (galleryID) {
      if (galleryID.toLowerCase() == 'all') {
        var url = "http://"+location.host+"/galerie/obrazky/gallery.php?akce=getImagesFromGallery&adr="+gal;
        var toto = this;
        galleryID = gal+'_'+galleryID;
        this.galleryID = galleryID;
        if (typeof this.fotos[galleryID] == "undefined") {
          this.fotos[galleryID] = new Array;
          jQuery.ajax({
              url: url,
              async: false,
              dataType: 'xml',
              success: function(xml) {
                if (jQuery(xml).find('status').text() == "ok") {
                  jQuery(xml).find('image').each(function(index, element) {              
                    if (jQuery(this).text())
                      toto.fotos[galleryID].push(jQuery(this).text());  
                  });            
                }  
              } 
            });
        }     
      }
    }
    
     
    
   
    if (!document.getElementById('galery_view')) {
      jQuery('<div id="galery_view" onkeyup="checkKey(event)" style="position: absolute; display: none; top: 0px; left: 0px; z-index:11; width: 0px; height: 0px; padding: 0px; margin: 0px;"></div>').appendTo( jQuery('body') );  
    }
    if (!document.getElementById('galery_view_transparent')) { 
      jQuery('<div id="galery_view_transparent" onkeyup="checkKey(event)" style="position: absolute; display: none; top: 0px; left: 0px; z-index:10; background: black; width: 500px; height: 500px; padding: 0px; margin: 0px; filter:progid:DXImageTransform.Microsoft.Alpha(opacity=70); -moz-opacity: 0.7; opacity: 0.7;"></div>').appendTo( jQuery('body') );  
      jQuery('<div><img src="/images/gallery/loading.gif" alt="load" style="display: none; border: 0;" /></div>').appendTo( jQuery('body') );
    }
    
    document.getElementById('galery_view').style.display = '';
    document.getElementById('galery_view_transparent').style.display = '';
    
  
    galeryDiv = '<div style="width: 0px; height: ' + (minisize) + 'px; padding: 0px;	margin: 0px;" id="gal_minidiv"></div>';
    galeryDiv += '<div style="width: 0px; padding: 0px; margin: 0px; height: 0px;" id="gal_topdiv"></div>';
    
    this.minidiv = jQuery('<div>', {
                          id: "gal_minidiv",
                          css: {      
                            width: '0',
                            height: minisize,
                            padding: '0', 
                            margin: '0'}}).appendTo(jQuery('#galery_view'));
                                
    this.topdiv = jQuery('<div>', {id: "gal_topdiv", css: {width: 0, padding: 0, margin: 0, height: 0}}).appendTo(jQuery('#galery_view'));
    //document.getElementById('galery_view').innerHTML = galeryDiv;
    //jQuery('#galery_view').html(galeryDiv);
  
    
    this.galWinDim();  
    this.showObrGal(obr,galleryID);
    this.loadMini(galleryID);
    var obj = this;            
    //setTimeout(function(){obj.loadMiniWait();},100);
    
    this.showOvlGal();
      //kompatibilita s as4uPopup - potlaceni popup okna na pozadi
    try {
      jQuery('.as4uPopupDataContainer').css('zIndex',5);
    } catch(e) {
    
    }
  }




//zobrazeni galerie
Gallery4u.prototype.showOvlGal = 
  function(){
    var obj = this;
    if(jQuery('#galery_view_transparent').css("display") == ""){    
      if((this.jMousePosXold == this.jMousePosX) && (this.jMousePosYold == this.jMousePosY) && (this.oldFoto == this.aktualFoto)){
        
        jQuery("#gal_popis_transparent").hide(500);
        jQuery("#gal_popis").hide(10);
        jQuery("#gal_a_predchozi").hide(10);
        jQuery("#gal_a_dalsi").hide(10);
        jQuery("#gal_a_close").hide(10);
        
        setTimeout(function(){obj.showOvlGal();}, 100);
        
      } else {
        
        this.oldFoto = this.aktualFoto;
        
        jQuery("#gal_popis_transparent").show(200);
        jQuery("#gal_popis").show(10);
        jQuery("#gal_a_predchozi").show(10);
        jQuery("#gal_a_dalsi").show(10);
        jQuery("#gal_a_close").show(10);
        
        setTimeout(function(){obj.showOvlGal();}, 3000);
        
      }
      
      this.jMousePosXold = this.jMousePosX;
      this.jMousePosYold = this.jMousePosY;
    }    
  }  


Gallery4u.prototype.galWinDim = 
  function() { 
    this.galSizeH = jQuery(window).height();
    this.galSizeW = jQuery(window).width();
   // alert(jQuery(window).width());
    
    if(this.galSizeH == 0){
      alert('Error');
    } else {
      document.getElementById('galery_view_transparent').style.height = (this.galSizeH) + 'px';
      document.getElementById('galery_view').style.height = (this.galSizeH) + 'px';
      document.getElementById('galery_view_transparent').style.width = (this.galSizeW) + 'px';
      document.getElementById('galery_view').style.width = (this.galSizeW) + 'px';
      var obj = this;
      this.casovacBloku = setTimeout(function(){obj.galWinDim();},100);
    }
  
    if (document.documentElement && document.documentElement.scrollTop) {
      this.galScroll = document.documentElement.scrollTop;
    } else if (document.body) {
      this.galScroll = document.body.scrollTop;
    } else {
      this.galScroll = 0;
    }
    document.getElementById('galery_view_transparent').style.top = (this.galScroll) + 'px';
    document.getElementById('galery_view').style.top = (this.galScroll) + 'px';
    
    if(document.getElementById('gal_minidiv')){
       this.galTopDivH = this.galSizeH - this.galMiniH ;
      document.getElementById('gal_topdiv').style.height = (this.galTopDivH) + 'px';
      document.getElementById('gal_topdiv').style.width = (this.galSizeW) + 'px';
      document.getElementById('gal_minidiv').style.width = (this.galSizeW) + 'px';
    }
    if(document.getElementById('gal_miniobal')){
      document.getElementById('gal_miniobal').style.width = (this.galSizeW) + 'px';
    }  
  }


Gallery4u.prototype.showObrGal = 
  function(obr,galleryID){
    if (!galleryID && this.galleryID)
      var galleryID = this.galleryID;
  
    if(this.aktualFoto != obr){
      jQuery('#gallyery4uLoadingContDiv').children().css('visibility', 'hidden');
      if(document.getElementById('igal4u' + this.aktualFoto)){
        document.getElementById('igal4u' + this.aktualFoto).style.border = '2px solid black';
      }
      if(document.getElementById('igal4u' + obr)){
        document.getElementById('igal4u' + obr).style.border = '2px solid white';
      }
      
      this.aktualFoto = obr;
  
      var params = "&subakce=obrshow" +
                   "&obr=" + obr + 
                   "&galTopDivH=" + this.galTopDivH + 
                   "&galSizeW=" + this.galSizeW;
      var obj = this;
      jQuery("#gal_topdiv").load(this.baseAjaxUrl + 'akce=gal_mini&lanG=' + this.gallang + params, '', 
        function(){
          obj.afterLoadMini(galleryID);
        }
      );
  
    }
  }


Gallery4u.prototype.loadMini = 
  function(galleryID){   
    var obj = this; 
    var fotoids = this.fotos[galleryID].join(",");
    var params = "&subakce=mini" +
                 "&gal=" + this.galId + 
                 "&obrarr=" + fotoids +
                 "&galSizeW=" + this.galSizeW +
                 "&minih=" + this.galMiniH;
  
    jQuery("#gal_minidiv").load(this.baseAjaxUrl +'akce=gal_mini&lanG=' + gallang + params, '', 
      function(){
        obj.afterLoadMini(galleryID);
      }
    );  
  }



Gallery4u.prototype.imageStarSelected = 
  function(iStar){ 
    for (var i=1;i<=5;i++){
      if(i <= iStar){
        jQuery("#imageStar" + i).attr("src","/images/gallery/hvezdicka_zluta.png");
      } else {
        jQuery("#imageStar" + i).attr("src","/images/gallery/hvezdicka_cerna.png");
      }
  
    }
  }

Gallery4u.prototype.imgReviewsSet = 
  function(imgSet, score){      
    var params = "&imgSet=" + imgSet +
                 "&score=" + score +
                 "&subakce=set_score";   
    jQuery('#image_hodnoceni_div_star').load(this.baseAjaxUrl +'ajax.php?akce=gal_mini&lanG=' + gallang + params, function(){});
  
  }



Gallery4u.prototype.afterLoadMini = 
  function (galleryID){
    var obj = this;
    //set actions to buttons
    //next foto
    jQuery('.nextFotoClick').each(function(){
      jQuery(this).click(function(){
          obj.nextFoto();
        });    
    });
    jQuery('.prevFotoClick').each(function(){
      jQuery(this).click(function(){
          obj.prevFoto();
        });    
    });
    jQuery('.closeFotoClick').each(function(){
      jQuery(this).click(function(){
          obj.closeGalery();
        });    
    });
    jQuery('.showFotoClick').each(function(){
      jQuery(this).click(function(){
          if (jQuery(this).attr('rel')) {            
            obj.showObrGal(jQuery(this).attr('rel'),galleryID);
          }
        });    
    });
    
    jQuery('.g4u_stop').each(function(){
      jQuery(this).click(function(){
          obj.galPlayStop();
        });    
    });
    jQuery('.g4u_play').each(function(){
      jQuery(this).click(function(){
          obj.galPlay();
        });    
    });
    try {
    if(this.fotos[galleryID].length > 0 && this.aktualFoto){
      var minuleVpoli = 0;
      var nastavDalsi = 0;
      try {
        if (document.getElementById('agal4u' + this.aktualFoto)) {
          document.getElementById('agal4u' + this.aktualFoto).focus();
          jQuery('#agal4u' + this.aktualFoto).keydown(function(event) {
            obj.keyPressed(event.keyCode);
          });   
        }   
        if (document.getElementById('igal4u' + this.aktualFoto))       
          document.getElementById('igal4u' + this.aktualFoto).style.border = '2px solid white'; // style=\"border: 2px solid black;\"
      } catch(e) {}
      
      //var sznFoto = document.getElementById('seznam_fotek').innerHTML;
      var sznFotoArr = this.fotos[galleryID];
      
      this.predesleFoto = 0;
      this.dalsiFoto = 0;
      
      for (i=0;i<sznFotoArr.length;i++){
        
        if(this.aktualFoto == sznFotoArr[i]){
          this.predesleFoto = minuleVpoli;
          nastavDalsi = 1;
        } else if (nastavDalsi == 1){
          this.dalsiFoto = sznFotoArr[i];
          break;
        }
        minuleVpoli = sznFotoArr[i];
      }
      if (this.dalsiFoto == 0 && this.fotos[galleryID].length > 1) {
        this.dalsiFoto = this.fotos[galleryID][0];
      }
      if (this.aktualFoto == this.fotos[galleryID][0]) {
        this.predesleFoto =  this.fotos[galleryID][this.fotos[galleryID].length-1]; 
      }
      
      setTimeout(function(){obj.waitLoadPopis();},100);
    }
    } catch (e) {
      //alert('Chyba galleryID - '+galleryID);
    }
    
  }

Gallery4u.prototype.waitLoadPopis = 
  function(){
    var obj = this;
    if(document.getElementById('galIdPopisObr')){
      if(document.getElementById('galIdPopisObr').innerHTML == this.aktualFoto){
  
        var widthPop = document.getElementById('gal_popis').offsetWidth;
        var heightPop = document.getElementById('gal_popis').offsetHeight;
  
        document.getElementById('gal_popis_transparent').style.width = widthPop + 'px';
        document.getElementById('gal_popis_transparent').style.height = heightPop + 'px';
      
      } else {
        setTimeout(function(){obj.waitLoadPopis();},100);
      }
    } else {
      setTimeout(function(){obj.waitLoadPopis();},100);
    }
  }

Gallery4u.prototype.galPopisClose = 
  function(){
    document.getElementById('gal_popis').style.display = 'none';
    document.getElementById('gal_popis_transparent').style.display = 'none';
  }


Gallery4u.prototype.prevFoto = 
  function(){
    if(this.predesleFoto > 0){
      this.showObrGal(this.predesleFoto,this.galleryID);
    }
  }

Gallery4u.prototype.nextFoto = 
  function(){
    if(this.dalsiFoto > 0){
      this.showObrGal(this.dalsiFoto,this.galleryID);
    }
  }

Gallery4u.prototype.galPlay = 
  function(){
    if(this.dalsiFoto > 0 && this.stopSlideshow != true){
      this.nextFoto();
      this.slideshowIndicator();
      var obj = this;
      this.casovacPrehravani = setTimeout(function(){obj.galPlay();}, 5000);
    } else {
      clearTimeout(this.casovacPrehravani);
      clearInterval(this.loaderInterval);
      this.stopSlideshow = false;
    }
    //this.stopSlideshow = false;
  }

Gallery4u.prototype.slideshowIndicator = 
  function() {
    this.loaderW = 50;
    if (this.loaderInterval)
      clearInterval(this.loaderInterval);
    jQuery('#g4u_slideshow_loading').css({width:this.loaderW+'px',height: 15, display:'block',float:'left',background:'#666666', margin: '2px 0px'});
    var obj = this;
    this.loaderInterval = setInterval(function(){obj.loaderRewrite();}, 100);
  }

Gallery4u.prototype.loaderRewrite = 
  function() {
    if (this.loaderW-1 >= 0) {
      this.loaderW--;
      jQuery('#g4u_slideshow_loading').css({width: this.loaderW+'px'});
    } else {
      clearInterval(this.loaderInterval);  
    } 
  }

Gallery4u.prototype.galPlayStop = 
  function(){
    clearTimeout(this.casovacPrehravani);
    this.loaderW = 1;
    this.stopSlideshow = true;
  };

Gallery4u.prototype.closeGalery = 
  function(){
    clearTimeout(this.casovacPrehravani);
    document.getElementById('galery_view').style.display = 'none';
    document.getElementById('galery_view').style.height = '0px';
    document.getElementById('galery_view').style.width = '0px';
  
    document.getElementById('galery_view_transparent').style.display = 'none';
    document.getElementById('galery_view_transparent').style.height = '0px';
    document.getElementById('galery_view_transparent').style.width = '0px';
  
  
    document.getElementById('galery_view').style.innerHTML = '';
  
    this.aktualFoto = 0;
    this.predesleFoto = 0;
    this.dalsiFoto = 0;
    if (this.onclosefocus)
      jQuery(this.onclosefocus).focus();
    
      //kompatibilita s as4uPopup - potlaceni popup okna na pozadi
    try {
      jQuery('.as4uPopupDataContainer').css('zIndex','');
    } catch(e) {}
      
  }

Gallery4u.prototype.keyPressed = 
  function(keycode){
    //39 - right
    //37 - left
    //32 - space
    //27 - esc
    if (keycode == 39 || keycode == 32) {
      this.nextFoto();  
    }
    if (keycode == 37) {
      this.prevFoto();   
    }
    if (keycode == 27) {      
        this.closeGalery();  
    } else if(keycode == 83){
      this.galPlayStop();
    } else if(keycode == 80){
      this.galPlay();
    }
  
  }




// konec nove galsrie


