function toggleSubattrs(name) {
  jQuery('#subattr_' + name).toggle('slow');
}

function searchAttr(type, val, count, seo) {
  if (count > 0) {
    toggleSubattrs(seo);
    return false;
  }
  return true;
}

function searchPage() {

  var params = {};
  var i = 0;
  while (i < searchPage.arguments.length) {
    var k = searchPage.arguments[i];
    var v = searchPage.arguments[i+1];
    params[k] = v;
    i += 2;
  }

  var url = '/discount-fabrics/search';
  var attributes = context['attributes'].split(',');
  if (params['attribute'] && params['attribute'].length) {
    attributes.push(params['attribute'].split(','));
  }
  var uniqAttribs = {};
  for (var i=0; i<attributes.length; i++) {
    if (attributes[i].length) {
      var type = context['attributetypes'][attributes[i]];
      uniqAttribs[type] = attributes[i];
    }
  }
  for (var type in uniqAttribs) {
    url += '/' + encodeURIComponent(uniqAttribs[type]);
  }
  var p = {keywords: 1, low: 1, hi: 1};
  for (var i in p) {
    if (typeof(params[i]) == 'undefined' && typeof(context[i]) != 'undefined') {
      params[i] = context[i];
    }
  }
  if (params['keywords']) {
    url += '/keywords-' + encodeURIComponent(encodeURIComponent(params['keywords']));
  }
  if (params['low'] && params['low'] != 0 || params['hi'] && params['hi'] != 50) {
    url += '/' + encodeURIComponent(params['low']) + '-' + encodeURIComponent(params['hi']) + '-price';
  }
  if (url != window.location.href) {
    window.location.href = url;
  }
}

function searchKeywords(event) {
  if (!event) {
    event = window.event;
  }
  if (event && event.keyCode === 13) {
    var value = jQuery('#keywords').get(0).value;
    if (value.replace(/\s*/g, '').length == 0) {
      alert("Please type a search term.");
      jQuery('#keywords').get(0).focus();
      return false;
    }
    //searchPage('keywords', value);
    var url = '/discount-fabrics/search';
    url += '/keywords-' + encodeURIComponent(encodeURIComponent(value));
    if (url != window.location.href) {
      window.location.href = url;
    }
  }
}
