Event.observe(document, 'geolocalizer_form:loaded', function() {

  var form = $$('.geolocalizer_form')[0],
      radios = form.select('input[type="radio"]'),
      formTypes = form.select('.formType'),
      inputTexts = {
        'geolocalizer_type_0': 'koszenie trawnika',
        'geolocalizer_type_1': 'malowanie ścian'
      },
      inputTexts2 = 'Miasto, ulica',      
      input = $('geolocalizer_query'),
      input2 = $('geolocalizer_start'),
      actualBg, actualRadio;

  function change(radio) {

    var target = form.down('.' + radio.id);

    if (actualBg) {
      if (target == actualBg) {
        return;
      }
      target.setStyle({'zIndex': 3});
      new Effect.Opacity(target, {
        from: 0.0,
        to: 1.0,
        duration: 0.3,
        afterFinish: function(fx) {
          target = fx.element;
          hide(actualBg);
          target.setStyle({'zIndex': 2});
          afterChange(target, radio);
        }
      });
    } else {
      // first time change - no animation
      target.setStyle({
        'opacity': 1,
        'zIndex': 2
      });
      afterChange(target, radio);
    }
  }

  function afterChange(target, radio) {

    /* className for .geolocalizer_form element */
    if (actualRadio) {
      form.removeClassName(actualRadio.id);
    }
    actualBg = target;
    actualRadio = radio;
    form.addClassName(radio.id);

    /* handle query input texts */
    var
      value = input.value.strip(),
      isOneOfDefaultValues = (function() {
        for (var key in inputTexts) {
          if (value === inputTexts[key]) {
            return true;
          }
        }
        return false;
      })();

    if (isOneOfDefaultValues || value == '') {
      // user did not change value
      input.value = inputTexts[actualRadio.id];
    }

  }

  function check(radio) {
    if (radio.checked === true) {
      change(radio);
    }
  }

  function hide(node) {
    node.setStyle({
      'opacity': 0,
      'zIndex': 1
    });
  }

  /* INITIALIZATION */

  formTypes.each(hide);
  radios.each(function(radio) {
    check(radio);
    radio.observe('click', check.bind(this, radio));
  });

  input.observe('focus', function() {
    if (this.value.strip() == inputTexts[actualRadio.id]) {
      this.value = '';
    }
  });
  input.observe('blur', function() {
    if (this.value.strip() == '') {
      this.value = inputTexts[actualRadio.id];
    }
  });
  input2.observe('focus', function() {
    if (this.value.strip() == inputTexts2) {
      this.value = '';
    }
  });
  input2.observe('blur', function() {
    if (this.value.strip() == '') {
      this.value = inputTexts2;
    }
  });  
  

//  if (Prototype.Browser.IE || true) {
//
//    form.select('input[type="text"]', 'select').each(function(element) {
//      element.observe('mouseover', function(event) {
//        element.setStyle({
//          'backgroundColor': '#fbfff4',
//          'borderColor': '#d6e5b5'
//        });
//      }).observe('mouseout', function(event) {
//        var parent = event.relatedTarget;
//        while (parent && parent !== element) {
//          try { parent = parent.parentNode; }
//          catch(e) { parent = element; }
//        }
//
//        if (parent === element) return;
//
//        element.setStyle({
//          'backgroundColor': '',
//          'borderColor': ''
//        });
//      });
//    });
//  }

});
