var eng_controller;

$(document).ready(function() {
  engine_controller.hint();
  engine_controller.autocomplete();
  
  eng_controller = new EngineController();
});

var engine_controller = {
  
  hint: function() {

    if ($('#engine #keywords').val() == '') {
      $('#engine #keywords').val(engine_controller.hint_text);
    } else {
      $('#engine #keywords').css('color', 'black');
    }

    $('#engine #keywords').focus(function() {
      if ($(this).val() == engine_controller.hint_text) {
        $(this).val('');
        $(this).css('color', 'black');
      }
    });

    $('#engine #keywords').blur(function() {
      if ($(this).val() == '') {
        $(this).val(engine_controller.hint_text);
        $(this).css('color', '#aaa');
      }
    });
    
    // if #keywords is still set to hint_text, empty it
    $('form#engine').submit(function() {
      if ($('#engine #keywords').val() == engine_controller.hint_text)
        $('#engine #keywords').val('');
    });

  },

  autocomplete: function() {
    var options = {};
    options.max = 5;
    options.minChars = 2;
    options.selectFirst = false;
    $('#engine #keywords').autocomplete("/firms/completion", options);
  }
  
};

function EngineController() {
  
  $('.engine_help').dialog({
    autoOpen: false,
    modal: true,
    title: "Aide à la recherche",
    width: 700
  });
  
  $('.selector').dialog({ buttons: { "Ok": function() { $(this).dialog("close"); } } });
  
  
  $('#engine a.help').click(function() {
    $('.engine_help').dialog('open');
    return false;
  });
  
};