var timer;
var gallery;

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 Zavrieť</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 DetailPhotos = function() {
  this.range = { from: 0,to: 2 };
  this.photos = $$('#detail_photos_list li');
  if(this.photos.length > this.range.to + 1) $('detail_next_bt').setStyle({ visibility: 'visible' });
  $('detail_photos_list').setStyle({ overflow: 'hidden' });
  $('detail_photos_list').observe('click',(function(e) {
    var element = e.element();
    var id = (id = element.up('a'))?id.id:false;
    if(id && id.endsWith('bt')) {
      if(id == 'detail_prev_bt') this.prev();
      else if(id == 'detail_next_bt') this.next();
    } else {
      if(!element.src) return;
      $('detail_main_photo').down('img').src = element.src.replace('_th200','');
      $('detail_photos_bt').innerHTML = (id = parseInt(element.id.substr(4))) + 1 + ' / <strong>' + this.photos.length + '</strong>';
      this.photos.invoke('setStyle',{ borderColor: '' });
      this.photos[id].setStyle({ borderColor: '#D0142C' });
    }
    e.stop();
  }).bind(this));
  this.update();
  gallery = new Gallery('detail_photos_list');
}
DetailPhotos.prototype.update = function() {
  this.photos.each(function(e,i) {
    if(i < this.range.from || i > this.range.to) e.hide(); else e.show();
    e.setStyle({ borderColor: '' });
  },this);
  this.photos[this.range.from].setStyle({ borderColor: '#D0142C' });
  $('detail_main_photo').down('img').src = this.photos[this.range.from].down('img').src.replace('_th200','');
  $('detail_photos_bt').innerHTML = this.range.from + 1 + ' / <strong>' + this.photos.length + '</strong>';
}
DetailPhotos.prototype.prev = function() {
  if(this.range.from < 1) {
    $('detail_prev_bt').setStyle({ visibility: 'hidden' });
    return;
  } else $('detail_next_bt').setStyle({ visibility: 'visible' });
  --this.range.from;
  --this.range.to;
  this.update();
}
DetailPhotos.prototype.next = function() {
  if(this.range.to >= this.photos.length - 1) {
    $('detail_next_bt').setStyle({ visibility: 'hidden' });
    return;
  } else $('detail_prev_bt').setStyle({ visibility: 'visible' });
  ++this.range.from;
  ++this.range.to;
  this.update();
}

var Home = function() {
  var search_field = $('search_field');
  search_field.observe('click',function() {
    search_field.value = '';
    search_field.writeAttribute('send','1');
  }.bind(this));
  search_field.up('form').observe('submit',function() {
    if(!search_field.readAttribute('send')) search_field.value = '';
  });
  $('search_type').observe('click',function(e) {
    e = e.element();
    if(e.value == "nemovitosti-k-bydleni") this.selectTypes('bydleni');
    if(e.value == "komercni-nemovitosti") this.selectTypes('komercni');
    $$('#search_select option')[0].value = e.value;
  }.bind(this));
}
Home.prototype.selectTypes = function(types) {
  $$('#search_select option').invoke('hide');
  $$('#search_select option.' + types).invoke('show');
}

var Popup = {
  open: function(options)
  {
    this.options = {
      url: '#',
      width: 600,
      height: 500,
      name:"_blank",
      location:"no",
      menubar:"no",
      toolbar:"no",
      status:"yes",
      scrollbars:"yes",
      resizable:"yes",
      left:"",
      top:"",
      normal:false
    }
    Object.extend(this.options, options || {});

    if (this.options.normal){
        this.options.menubar = "yes";
        this.options.status = "yes";
        this.options.toolbar = "yes";
        this.options.location = "yes";
    }

    this.options.width = this.options.width < screen.availWidth?this.options.width:screen.availWidth;
    this.options.height=this.options.height < screen.availHeight?this.options.height:screen.availHeight;
    var openoptions = 'width='+this.options.width+',height='+this.options.height+',location='+this.options.location+',menubar='+this.options.menubar+',toolbar='+this.options.toolbar+',scrollbars='+this.options.scrollbars+',resizable='+this.options.resizable+',status='+this.options.status
    if (this.options.top!="")openoptions+=",top="+this.options.top;
    if (this.options.left!="")openoptions+=",left="+this.options.left;
    window.open(this.options.url, this.options.name,openoptions );
    return false;
  }
}

var tisk = function() {
try {
    this.window.print();
  }
  catch(err) {
    alert(err.description);
  }
}

document.observe('dom:loaded',function() {
  if($('search_field')) new Home();
  if($('detail_photos_list')) new DetailPhotos();
  if($('vyhledavani_podrobne')) $('vyhledavani_podrobne').observe('click',function(e) {
    new Ajax.Updater('podrobny-filtr',e.element().href,{ parameters: { ajax: 1 },method: 'get' });
    $('vyhledavani_filter_hd').hide();
    $('vyhledavani_filter').hide();
    $('vyhledavani_filter_ft').hide();
    $('vyhledavani_list').setStyle({ cssFloat: 'none', width: '698px' });
    location.hash = '#podrobny-filtr';
    timer = setInterval(function() {
      if(location.hash != '#podrobny-filtr') {
        $('podrobny-filtr').innerHTML = '';
        $('vyhledavani_list').setStyle({ cssFloat: '', width: '' });
        $('vyhledavani_filter_hd').show();
        $('vyhledavani_filter').show();
        $('vyhledavani_filter_ft').show();
        clearInterval(timer);
      }
    },200);
    e.stop();
  });
  if($('chci_koupit')) {
    $$('.sale')[0].observe('click',function(e) {
      e = e.element();
      if(e.type != 'checkbox') return;
      var span = e.next('span');
      span.setStyle({ fontWeight: span.style.fontWeight ? '' : 'bold' });
      var p = e.up('p').next('.price');
      if(p) p.toggle();
    });
    $$('.sale .price').each(function(e) {
      var input = e.previous('p').down('input');
      if(!input.checked) e.hide(); else input.next('span').setStyle({ fontWeight: 'bold' });
    });
    $$('.types')[0].observe('click',function(e) {
      e = e.element();
      var span = e.next('span');
      span.setStyle({ fontWeight: span.style.fontWeight ? '' : 'bold' });
      var ul = e.next('ul');
      if(ul) ul.toggle();
    });
    $$('li li ul').each(function(e) {
      var input = e.previous('input');
      if(!input.checked) e.hide(); else input.next('span').setStyle({ fontWeight: 'bold' });
    });
  }
  var per_page = $('per_page');
  if(per_page) {
    var onchange = function() {
      var per_page = $('per_page');
      var form = per_page.up('div').next('form');
      var input1 = document.createElement('input');
      var input2 = document.createElement('input');
      var input3 = document.createElement('input');
      input1.name = 'order_by';
      input1.type = 'hidden';
      input1.value = $('order_by').getValue();
      input2.name = 'per_page';
      input2.type = 'hidden';
      input2.value = per_page.getValue();
      input3.name = 'page';
      input3.type = 'hidden';
      input3.value = per_page.name;
      form.appendChild(input1);
      form.appendChild(input2);
      form.appendChild(input3);
      $$('#vyhledavani_filter input[type=text]').each(function(e){ if(isNaN(parseInt(e.value)) && e.name != 'text') e.value = 0 });
      $$('#vyhledavani_filter input[name=per_page]')[0].remove();
      form.submit();
    }
    $$('#per_page, #order_by').invoke('observe','change',onchange);
  }
});

