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, wlow: 1, whi: 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'] != 1 || params['hi'] && params['hi'] != 60) {
    url += '/price-' + encodeURIComponent(params['low']) + '-' + encodeURIComponent(params['hi']);
  }
  if (params['wlow'] && params['wlow'] != 1 || params['whi'] && params['whi'] != 120) {
    url += '/width-' + encodeURIComponent(params['wlow']) + '-' + encodeURIComponent(params['whi']);
  }
  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;
    }
    var url;
    if (typeof(context) != 'undefined' && context.pagetype === 'fashion') {
      url = '/fashion.html?keywords=' + encodeURIComponent(value);
    } else {
      //searchPage('keywords', value);
      url = '/discount-fabrics/search';
      url += '/keywords-' + encodeURIComponent(encodeURIComponent(value));
    }
    if (url != window.location.href) {
      window.location.href = url;
    }
  }
}


