
window.addEvent('domready', function()
{
  $$('input.autopopulate').each(function(input)
  {
    /*@cc_on
    if (input.type == 'password') {
      return;
    }
    @*/
    var type  = input.type;
    
    if (input.value == '')
    {
      if (type == 'password') {
          input.type = 'text';
      }
      input.addClass('label');
      input.value = input.getAttribute('title');
    }
    
    input.addEvent('focus', function()
    {
      if (input.value == input.getAttribute('title'))
      {
        input.removeClass('label');
        input.value = '';
        if (type == 'password' && input.type != 'password')
        {
          input.type = 'password';
          setInterval(input.focus, 20);
        }
      }
    });
    
    input.addEvent('blur', function()
    {
      if (input.value.match(/^\s*$/))
      {
        if (type == 'password') {
          input.type = 'text';
        }
        input.addClass('label');
        input.value = input.getAttribute('title');
      }
    });
    
    var form = input.parentNode;
    while(form && form.tagName && form.tagName.toUpperCase() != 'FORM' && form.parentNode) {
      form = form.parentNode;
    }
    $(form).addEvent('submit', function(e)
    {
      if (input.value == input.getAttribute('title')) {
        input.value = '';
      }
    });
    
    input.changeLabel = function(label)
    {
      if (input.hasClass('label')) {
        input.value = label;
      }
      input.setAttribute('title', label);
    }
  });
  
  function setLabelFromSelect()
  {
    var select = $('recherche-culturelle-select');
    $('recherche-culturelle-input').changeLabel(select.options[select.selectedIndex].getAttribute('title'));
  }
  setLabelFromSelect();
  
  $('recherche-culturelle-select').addEvent('change', function(e) {
    setLabelFromSelect();
  });
  
  $('recherche-culturelle').addEvent('submit', function(e)
  {
    var input  = $('recherche-culturelle-input');
    var select = $('recherche-culturelle-select');
    var url, value = (input.value == '') ? ' ' : input.value;
    
    switch(select.value)
    {
      case 'agenda': url = '/agenda.html?pays_lieu=1&id_region=toute_la_france&temps=0&word='; break;
      case 'pseudo': url = '/recherches.php?type=pseudos&pseudocherche='; break;
      default:
        url = '/search/' + select.value + '/';
        break;
    }
    window.location = url + (input.value != '' ? encodeURIComponent(input.value).replace('%2F', '%252F').replace('%5C', '%255C') : '%20');
    
    e.stop();
  }.bindWithEvent(window));
});

