$(document).ready(function() {
  $('#jobsearch').submit(function(e){
    e.preventDefault();
    var base = $(this).attr('action') ;
    var data = new Array();
    $(this).find('div.search input.defaultvalue').each(function(){
      var val = $.trim($(this).val());
      var defval = $(this).next().val();
      if (val && defval != val)
        data.push($.trim($(this).attr('name')) + '/' + val);
    });
    $(this).find('#advancedSearch').find('select,input[name]').each(function(){
      var val = $.trim($(this).val());
      if (val)
        data.push($.trim($(this).attr('name')) + '/' + val);
    });
    $(this).find('.fancyoptions').each(function(){
      var options = [];
      $('span.checked', this).each(function(){
        options.push($.trim($(this).find('input').val()));
      });
      if (options.length > 0)
        data.push($.trim($('.urlname', this).val()) + '/' + options.join(','));
    });
    trackEvent('Search', 'Job search');
    // This is a bit ugly but it works for now...
    setTimeout(function() { 
      document.location.href = base + '/' + data.join('/');
    }, 200);
    return false;
  });

  $("#jobsearch_company").autocomplete('general', 'autocompletecompany',
  {
    //			delay : 10,
    minChars : 2,
    parseData : function (data) {
      return data.data;
    },
    convertValue : function(row, i, sum) {
      return i
    },
    convertText : function(row, i, sum) {
      return row.name
    },
    loadingClass : 'none',
    offsetLeft : 10,
    width : 144
  }
  );  //
  $("input.searchPlace").autocomplete('general', 'autocompletelocation',
  {
    //			delay : 10,
    minChars : 2,
    parseData : function (data) {
      return data.data;
    },
    convertValue : function(row, i, sum) {
      return row.value
    },
    convertText : function(row, i, sum) {
      return row.value;
    },
    onItemSelect : function(li) {
      $("input.searchPlace").val(li.selectValue).focus();
    },
    loadingClass : 'none',
    resultsClass : 'ac_results3',
    offsetLeft : 10,
    width : 237

  }
  );

  $("input.searchJob").autocomplete('general', 'autocompletecategoryforadjobsearch',
  {
   
    minChars : 2,
    parseData : function (data) {
      return data.data;
    },
    convertValue : function(row, i, sum) {
      return row.title
    },
    convertText : function(row, i, sum) {
      return row.title;
    },
    onItemSelect : function(li) {
      $("input.searchJob").val(li.selectValue).focus();
    },
    loadingClass : 'none',
    resultsClass : 'ac_results3',
    offsetLeft : 10,
    width : 237

  }
  );

});  

