var Gallery = function(list) {
  this.photos = $$('#' + $(list).identify() + ' img');
  this.background = $(document.createElement('div'));
  this.background.setStyle({
    display: 'none',
    position: 'absolute',
    top: '0',
    left: '0',
    background: 'black',
    opacity: '0.5'
  });
  this.background.observe('click',(function(){ this.close() }).bind(this));
  this.container = $(document.createElement('div'));
  this.container.setStyle({
    display: 'none',
    position: 'absolute',
    top: '0',
    left: '0',
    padding: '10px',
    background: 'white',
    border: '1px solid silver'
  });
  this.container.parentGallery = this;
  this.container.innerHTML = '<p align="right"><a style="color:black" href="#" onclick="this.parentNode.parentNode.parentGallery.close();return false"> x Zavřít</a></p>' +
  '<div style="position:relative"><img src="' + this.photos[0].src.replace('_th200','') +
  '" onclick="this.parentNode.parentNode.parentGallery.next();return false" />' +
  '<a style="position:absolute;top:80px;left:0;text-decoration:none;font-weight:bold;font-size:30px;color:black;background:white" href="#" onclick="this.parentNode.parentNode.parentGallery.prev();return false"><</a>' +
  '<a style="position:absolute;top:80px;right:0;text-decoration:none;font-weight:bold;font-size:30px;color:black;background:white" href="#" onclick="this.parentNode.parentNode.parentGallery.next();return false">></a>' +
  '</div><center>' + this.photos[0].title + '</center>';
  document.body.appendChild(this.background);
  document.body.appendChild(this.container);
  this.image = $(this.container.childNodes[1].firstChild);
  this.image.observe('load',(function() {
    var temp = new Image();
    temp.src = this.image.src;
    this.image.removeAttribute('height');
    this.image.originalHeight = temp.height || this.image.height;
    this.photos.each((function(e,i){ if(e.src.replace('_th200','') == this.image.src) this.actualPhoto = i; }).bind(this));
    if(this.container.style.display != 'none') this.show();
  }).bind(this));
  window.setInterval((function(){ if(this.container.style.display != 'none') this.show(); }).bind(this),500);

  this.show = (function(src) {
    $$('embed,object').invoke('hide');
    var viewWidth = $(document).viewport.getWidth();
    var viewHeight = $(document).viewport.getHeight();
    if(this.image.originalHeight > viewHeight - 50) {
      this.image.height = viewHeight - 50;
    } else {
      this.image.height = this.image.originalHeight;
    }
    var height = this.container.getHeight();
  
    this.background.setStyle({
      display: 'block',
      width: $(document.body).getWidth() + 'px',
      height: $(document.body).getHeight() + 'px'
    });
    var top = parseInt((viewHeight - height)/2);
    
    if(src) this.image.src = src;
    
    this.container.setStyle({
      display: 'block',
      left: parseInt((viewWidth - this.container.getWidth())/2)+'px',
      top: ((top > 0)?top:0)+'px'
    });
  }).bind(this);
  this.close = (function() {
    this.background.hide();
    this.container.hide();
    $$('embed,object').invoke('show');
  }).bind(this);
  this.next = function() {
    if(this.actualPhoto == this.photos.length - 1) this.actualPhoto = -1;
    this.image.src = this.photos[++this.actualPhoto].src.replace('_th200','');
    this.container.lastChild.innerHTML = this.photos[this.actualPhoto].title;
  }.bind(this);
  this.prev = function() {
    if(this.actualPhoto == 0) this.actualPhoto = this.photos.length;
    this.image.src = this.photos[--this.actualPhoto].src.replace('_th200','');
    this.container.lastChild.innerHTML = this.photos[this.actualPhoto].title;
  }.bind(this);
}

var gal;
var start = 0;

function next() {
  if(++start > gal.photos.length - 4) start = gal.photos.length - 4;
  $$('.gal a.small_img').each(function(e,i){ if(i < start || i > start + 2) e.hide(); else e.show(); });
}
function prev() {
  if(--start < 0) start = 0;
  $$('.gal a.small_img').each(function(e,i){ if(i < start || i > start + 2) e.hide(); else e.show(); });
}

$(document).observe('dom:loaded',function() {
  window.lastHash = '';
  if(gal = $$('.gal')[0]) { 
    gal = new Gallery(gal);
    $$('.next')[0].observe('click',function(e){ next(); e.stop(); });
    $$('.prev')[0].observe('click',function(e){ prev(); e.stop(); });
  }
  $$('.gal a.small_img').each(function(e,i){ e.observe('click',function(e){ $$('.main_img')[0].src = e.element().src.replace('_th200',''); e.stop(); }); if(i >= 3) e.hide() });
  
  $$('#menu li,#footer a').invoke('observe','click',function(e){
    $$('#menu li,#footer a').invoke('removeClassName','active'); $$('a[href=' + e.element().href + ']').each(function(e) {
      var up = e.up('li'); if(up) up.addClassName('active'); else e.addClassName('active');
    })
  });
    
  setInterval(function() {
    if(location.hash != window.lastHash) {
      window.lastHash = location.hash;
      $('dynamic').innerHTML = '<h2>Načítám data...prosím čekejte</h2>';
      new Ajax.Updater('dynamic',document_root + location.hash.substr(1),{ method: 'get',evalScripts: true, onComplete: function() {
        $(document.body).scrollTo();
        $$('#dynamic a').each(function(e){ e.href = e.href.replace(document_root,document_root + '#') });

        var map_areas = $('map_areas');
        if(map_areas) {
          map_areas.observe('mouseover',function(e) {
            var map_img = $('map_img');
            var reg_img = $('reg_img');
            e = e.element();
            if(e.tagName.toLowerCase() == 'area') {
              reg_img.setStyle({ top: '-2px',left: '1px' });
              reg_img.src = map_img.src.replace('mapa.png',e.id.replace('reg_','') + '.gif');
            }
          });
        }
        $$('a.select').each(function(e) {
          var tr = e.up('tr').next('tr');
          tr.hide();
          e.observe('click',function(event) {
            tr.toggle();
            event.stop();
          });
        });
        $$('.top_ram a.title').each(function(e) { e.next('.window').hide(); e.observe('click',function(event) { $$('.top_ram .window').invoke('hide'); e.next('.window').toggle(); event.stop() }); });
        $(document.body).observe('click',function() { $$('.top_ram .window').invoke('hide'); });
      }});
    }
  },50);
  $$('a.select').each(function(e) {
    var tr = e.up('tr').next('tr');
    tr.hide();
    e.observe('click',function(event) {
      tr.toggle();
      event.stop();
    });
  });
  //$$('a').each(function(e){ e.href = e.href.replace(document_root,document_root + '#') });
  $$('#left div').invoke('setStyle',{ cursor: 'pointer' });
  $$('.top_ram a.title').each(function(e) { e.next('.window').hide(); e.observe('click',function(event) { $$('.top_ram .window').invoke('hide'); e.next('.window').toggle(); event.stop() }); });
  $(document.body).observe('click',function() { $$('.top_ram .window').invoke('hide'); });
});
