
function search_form_type_changed(type) {
  type = $(type);
  if (!type) return;
  var price_start = $('price_start');
  var price_end = $('price_end');
  if (price_start && price_end) {
    price_start.disabled = (type.value >= 2) && (type.value < 4);
    price_end.disabled = (type.value >= 2) && (type.value < 4);
    
    price_start.setStyle('opacity', price_start.disabled ? 0.7: 1);
    price_end.setStyle('opacity', price_end.disabled ? 0.7: 1);
  }
}

var CitySelector = new Class({
  
  check: function(el) {
    el = $(el);
    this.element = el;
    if ((el.options.length - 1) != el.selectedIndex) return;
    
    this.closing = false;
    this.handlers = {
      cancel: this.cancel.bind(this),
      selectCity: this.selectCity.bind(this)
    };
    
    var html = '<div id="city_browser_view" class="browser" align="left" style="width: 485px"><ul id="city_browser" style="width: 242px"></ul></div>';
    this.box = new HtmlBox(html, {title: 'Výběr města', size: {width: 485, height: 275}, className: 'infobox' });
    
    this.hideSelects();
    
    this.box.addEvent('onClose', this.handlers.selectCity);
    this.box.button.disabled = true;
    
    this.cancel_button = new Element('input', {
      'id': 'htmlbox-cancel-button',
      'type': 'button',
      'value': '    Zavřít    '
    }).injectInside(this.box.buttons).addEvent('click', this.handlers.cancel);
    
    this.box.button.setStyle('margin-right', 5);
    this.cancel_button.setStyle('margin-left', 5);

    this.browser = $('city_browser');
    this.container = this.browser.getParent();
    this.url = '/cityselector/browse/';
    try {
      this.col_width = this.browser.getFirst().offsetWidth;
    } catch(e) {
      this.col_width = 242;
    }
    
    this.next(null, 0, 'country');
  },
  
  resetSelection: function(column) {
    $(column).getChildren().each(function(el) {
      el.removeClass('active');
    });
  },
  
  removeFollowing: function(column) {
    while (next = column.getNext()) {
      if (window.ie) {
        next.outerHTML = '';
      } else {
        next.remove();
      }
    }
    var w = this.browser.getChildren().length * this.col_width + (window.ie6 ? 1 : 0);
    if (window.ie7) {
      w -= 1;
    }
    this.browser.setStyle('width', w);
  },
  
  getColumn: function(li) {
    return $(li).getParent().getParent();
  },
  
  select: function(li, id, title) {
    var column = this.getColumn(li);
    this.removeFollowing(column);
    this.resetSelection(li.getParent());
    $(li).addClass('active');
    
    this.selected = {
      id: id,
      title: title
    }
    
    this.box.button.disabled = false;
  },
  
  next: function(li, id, parent_id) {
    if (li) {
      var column = this.getColumn(li);
      if (column.getNext()) {
        this.removeFollowing(column.getNext());
      }
      this.resetSelection(li.getParent());
      $(li).addClass('active');
      
      this.selected = {};
      this.box.button.disabled = true;
      
      if (!column.getNext()) {
        var col = this.create_column(parent_id);
      } else {
        var col = column.getNext();
        col.empty().adopt(
          new Element('ul', {'class': 'empty'})
        );
      }
    } else {
      var col = this.create_column(parent_id);
    }
    
    new Fx.Scroll(this.container, {
      wait: false,
      duration: 500,
      transition: Fx.Transitions.Quint.easeOut
    //}).toElement(this.browser.getChildren().getLast());
    }).toRight();

    new Ajax(this.url, {
      method: 'post',
      postBody: 'level=' + parent_id + '&id=' + id,
      update: col
    }).request();
  },
  
  create_column: function(id) {
    //this.browser.setStyle('width', this.browser.offsetWidth + this.col_width);
    var col = new Element('li', {
      'id': 'next_' + id,
      'class': 'category'
    }).injectInside(this.browser).adopt(
      new Element('ul', {'class': 'empty'})
    );
    
    this.browser.setStyle('width', this.browser.getChildren().length * this.col_width + (window.ie6 ? 1 : 0));
    
    /*new Fx.Scroll(this.container, {
      wait: false,
      duration: 500,
      transition: Fx.Transitions.Quint.easeOut
    }).toElement(this.browser.getChildren().getLast());*/
    
    return col;
  },

  selectCity: function() {
    this.showSelects();
    if (this.closing) return;

    if (this.element && this.selected.id) {
      /*this.element.options[this.element.options.length] = new Option(this.selected.title, this.selected.id, false, false);
      this.element.selectedIndex = this.element.options.length - 1;*/

      for (var i = 0; i < this.element.options.length; i++) {
        if (this.element.options[i].value == this.selected.id) {
          this.element.selectedIndex = i;
          return;
        }
      }

      var opt = document.createElement('option');
      opt.text = this.selected.title;
      opt.value = this.selected.id;
      var opt_before = this.element.options[this.element.options.length - 1];  
      try {
        this.element.add(opt, opt_before); // standards compliant; doesn't work in IE
      } catch(ex) {
        this.element.add(opt, this.element.options.length - 1); // IE only
      }
      this.element.selectedIndex = this.element.options.length - 2;
    }
  },
  
  cancel: function() {
    this.closing = true;
    this.box.close();
    if (this.element) {
      this.element.selectedIndex = 0;
    }
  },
  
  showSelects: function() {
    if (!window.ie6) return;
    
    $$('select').each(function(select) {
      select = $(select);
      state = select.getAttribute('prev-state');
      if (!state) state = 'visible';
      select.setStyle('visibility', state);
    });
  },

  hideSelects: function() {
    if (!window.ie6) return;
    
    $$('select').each(function(select) {
      select = $(select);
      select.setAttribute('prev-state', select.getStyle('visibility'));
      select.setStyle('visibility', 'hidden');
    });
  }
  
});

var cityhelper = new CitySelector();
var HtmlBox = new Class({

  options: {
    opacity: 0.7,
    title: '',
    size: {
      width: 380,
      height: 180
    },
    closeWithOverlay: false,
    className: '',
    zIndex: 1008
  },

  initialize: function(html, options) {
    this.html = html;
    this.setOptions(options);

    this.handlers = {
      key: this.key.bindWithEvent(this),
      resize: this.resize.bind(this),
      close: this.close.bind(this)
    };

    this.build();
    this.open();
  },

  resize: function() {
    var overlay = $('htmlbox-overlay');
    var htmlbox = $('htmlbox');
    if (overlay && htmlbox) {
      var sizes = window.getSize();
      if (window.ie6) {
        overlay.setStyles({
          'left': sizes.scroll.x,
          'top': sizes.scroll.y
        });
      }
      overlay.setStyles({
        'width': sizes.size.x,
        'height': sizes.size.y
      });
      var bounds = htmlbox.getSize().size;
      if (window.ie6) {
        this.iframe.setStyles({
          'left': window.getScrollLeft() + Math.round((sizes.size.x - bounds.x) / 2),
          'top': window.getScrollTop() + Math.round((sizes.size.y - bounds.y) / 2) - 20,
          'width': htmlbox.getStyle('width').toInt(),
          'height': htmlbox.getStyle('height').toInt()
        });
      }
      htmlbox.setStyles({
        'left': window.getScrollLeft() + Math.round((sizes.size.x - bounds.x) / 2),
        'top': window.getScrollTop() + Math.round((sizes.size.y - bounds.y) / 2) - 20
      });
    }
  },

  toggleListeners: function(state) {
    var task = state ? 'addEvent' : 'removeEvent';
    if (window.gecko) {
      window[task]('keydown', this.handlers.key);
    } else {
      document.body[task]('keydown', this.handlers.key);
    }
    window[task]('resize', this.handlers.resize);
    window[task]('scroll', this.handlers.resize);
  },

  showOverlay: function() {
    var overlay = $('htmlbox-overlay');
    if (!overlay) {
      overlay = new Element('div', {
        'id': 'htmlbox-overlay',
        'class': 'htmlbox-overlay',
        'styles': {
          'position': window.ie6 ? 'absolute' : 'fixed',
          'left': 0,
          'top': 0,
          'width': 1,
          'height': 1,
          'padding': 0,
          'margin': 0,
          'opacity': 0,
          'visibility': 'visible',
          'z-index': this.options.zIndex
        }
      }).injectInside(document.body);
      /*if (window.ie6) {    
        this.overlay.style.setExpression('left', 'document.body.scrollLeft || document.documentElement.scrollLeft');
        this.overlay.style.setExpression('top', 'document.body.scrollTop || document.documentElement.scrollTop');
      }*/
    } else {
      overlay.setStyles({
        'display': '',
        'opacity': 0
      });
    }
    this.resize();
    this.toggleListeners(true);
    if (this.options.closeWithOverlay) {
      overlay.addEvent('click', this.handlers.close);
    }
    overlay.effect('opacity', {duration: 250}).start(this.options.opacity);
  },

  hideOverlay: function() {
    var overlay = $('htmlbox-overlay');
    if (overlay) {
      overlay.effect('opacity', {duration: 250}).start(0).chain(function() {
        overlay.setStyle('display', 'none');
        this.toggleListeners(false);
        overlay.removeEvent('click', this.handlers.close);
        overlay.remove();
      }.bind(this));
    }
  },
  
  build: function() {
    var box = $('htmlbox');
    if (!box) {
      
      box = new Element('div', {
        'id': 'htmlbox',
        'class': 'htmlbox',
        'styles': {
          'position': 'absolute',
          'left': 0,
          'top': 0,
          'opacity': 0,
          'visibility': 'visible',
            'z-index': this.options.zIndex + 2
        }
      }).addClass(this.options.className).injectInside(document.body);

      if (window.ie6) {
        this.iframe = new Element('iframe', {
          'frameborder': 0,
          'src': 'about:blank',
          'styles': {
            'position': 'absolute',
            'left': 0,
            'top': 0,
            'width': 0,
            'height': 0,
            'z-index': this.options.zIndex + 1
          }
        }).injectBefore(box);
      }
      
      title = new Element('h1', {
        'id': 'htmlbox-title'
      }).injectInside(box);

      code = new Element('div', {
        'id': 'htmlbox-code',
        'class': 'htmlbox-code',
        'styles': {
          'overflow': 'hidden'
        }
      }).injectInside(box);

      this.buttons = new Element('div', {
        'id': 'htmlbox-buttons',
        'class': 'htmlbox-buttons',
        'align': 'center'
      }).injectInside(box);

      this.button = new Element('input', {
        'id': 'htmlbox-ok-button',
        'type': 'button',
        'value': '           OK           '
      }).injectInside(this.buttons).addEvent('click', this.handlers.close);
      
      this.resize();
    } else {
      title = $('htmlbox-title');
      code = $('htmlbox-code');
      buttons = $('htmlbox-buttons');
    }
    title.setHTML(this.options.title);
    code.setHTML(this.html);
    /*var height = this.options.size.height - title.getSize().size.y - buttons.getSize().size.y;
    height -= box.getStyle('padding-top').toInt() + box.getStyle('padding-bottom').toInt();
    height -= title.getStyle('padding-top').toInt() + title.getStyle('padding-bottom').toInt() +
      title.getStyle('margin-top').toInt() + title.getStyle('margin-bottom').toInt();
    height -= code.getStyle('padding-top').toInt() + code.getStyle('padding-bottom').toInt() +
      code.getStyle('margin-top').toInt() + code.getStyle('margin-bottom').toInt();
    height -= buttons.getStyle('padding-top').toInt() + code.getStyle('padding-bottom').toInt();
    code.setStyle('height', height);*/
    
    code.setStyles({
      'width': this.options.size.width,
      'height': this.options.size.height
    });
    
    box.effect('opacity', {duration: 250}).start(1);
  },
  
  open: function() {
    try {
      window.focus();
    } catch(e) {}
    this.showOverlay();
    this.fireEvent('onOpen');
  },
  
  close: function() {
    this.hideOverlay();
    var box = $('htmlbox');
    if (box) {
      box.effect('opacity', {duration: 250}).start(0).chain(function() {
        box.remove();
        
        if (window.ie6 && this.iframe) {
          this.iframe.remove();
        }

        this.fireEvent('onClose');
      }.bind(this));
    }
  },
  
  key: function(event) {
    if ((event.key == 'enter') || (event.key == 'esc')) {
      event.stop();
      this.close();
    }
  }

});

HtmlBox.implement(new Options, new Events);
