$(document).ready(function() {

  $('input[name=\'search\']').keydown(function(e) {
    if (e.keyCode == 13) {
      $('#button-search').trigger('click');
    }
  });

  $('select[name=\'category_id\']').bind('change', function() {
    if (this.value == '0') {
      $('input[name=\'sub_category\']').attr('disabled', 'disabled');
      $('input[name=\'sub_category\']').removeAttr('checked');
    } else {
      $('input[name=\'sub_category\']').removeAttr('disabled');
    }
  });

  $('select[name=\'category_id\']').trigger('change');

  $('#button-search').bind('click', function() {
    url = 'index.php?route=product/search';

    var search = $('#content-search input[name=\'search\']').attr('value');

    if (!search.trim().length > 0) {
      return;
    }

    if (search) {
      url += '&search=' + encodeURIComponent(search);
    }

    var category_id = $('#content-search select[name=\'category_id\']').attr('value');

    if (category_id > 0) {
      url += '&category_id=' + encodeURIComponent(category_id);
    }

    var sub_category = $('#content-search input[name=\'sub_category\']:checked').attr('value');

    if (sub_category) {
      url += '&sub_category=true';
    }

    var filter_description = $('#content-search input[name=\'description\']:checked').attr('value');

    if (filter_description) {
      url += '&description=true';
    }
    
    location = url;
  });
});
