function bb1ItemListFiltering(parms) {
 if (parms) {
  for (var i in parms)
   this[i] = parms[i];
 }
 this.initialised = false;
 this.sorted_filtered_list = null;
 this.page = getParameter("page");
 this.page = (this.page&&this.page!="") ? parseInt(this.page) : 1;
 this.page_size = getParameter("pagesize");
 this.page_size = (this.page_size&&!isNaN(parseInt(this.page_size))) ? parseInt(this.page_size) : 6;
 this.price_min = getParameter("pricemin");
 this.price_min = (this.price_min&&!isNaN(parseInt(this.price_min))) ? parseInt(this.price_min) : 0;
 this.price_max = getParameter("pricemax");
 this.price_max = (this.price_max&&!isNaN(parseInt(this.price_max))) ? parseInt(this.price_max) : Infinity;
 this.page_count = 0;
 this.record_count = 0;
 this.d = document;
 this.page_options = [6,12,0];
 this.price_range_options = [{"title":"Below &pound;50","min":null,"max":50},{"title":"Between &pound;50-75","min":50,"max":75},{"title":"Between &pound;75-100","min":75,"max":100},{"title":"&pound;100 or more","min":100,"max":null},{"title":"Show All","min":null,"max":null}];
 this.itemlist_container = this.d.getElementById("item_list_container");
 return this;
}

bb1ItemListFiltering.prototype.init = function () {
 this.initItemListCache();
 this.displaySortedFiltered();
 this.displayPaginationOptions();
 this.displayPaginationLinks();
 this.displayFilterationLinks();
}

bb1ItemListFiltering.prototype.initItemListCache = function () {
 if (this.initialised==true) return;
 var c = this.itemlist_container;
 if (!c) return;
 c.style.display = "none";
 var item_list_cells = new Array();
 var divs = c.getElementsByTagName("DIV");
 for (var i=0; i < divs.length; i++) {
  if (divs[i].className=="item-list-cell") {
   item_list_cells[item_list_cells.length] = divs[i];
  }
 }
 switch (this.sortby) {
  case "name_asc":
   item_list_cells.sort(bb1SortItemListByAscName);
   break;
  default:
   item_list_cells.sort(bb1SortItemListByDescPrice);
 }
 this.item_list_cells = item_list_cells;
 this.initialised = true;
}

bb1ItemListFiltering.prototype.displaySortedFiltered = function () {
 this.clearSortedFiltered();
 var new_c = this.d.createElement("DIV");
 new_c.id = "item_list_filtered";
 var item_list_cells = this.item_list_cells;
 var start_index = (this.page_size>0) ? (this.page-1)*this.page_size : 0;
 var end_index = (this.page_size>0 && (this.page*this.page_size)<item_list_cells.length) ? this.page*this.page_size : item_list_cells.length;
 this.record_count = 0;
 var displayed_count = 0;
 for (var i=0; i < item_list_cells.length; i++) {
  if (this.isPriceWithinRange(item_list_cells[i])) {
   if (start_index<=this.record_count && this.record_count<end_index) {
    if (displayed_count!=0 && displayed_count%3==0) {
     var br = this.d.createElement("BR");
     br.className = "clear-both";
     new_c.appendChild(br);
    }
    var copy = item_list_cells[i].cloneNode(true);
    copy.className += (copy.className!=""?" ":"")+" float-left";
    new_c.appendChild(copy);
    displayed_count++;
   }
   this.record_count++;
  }
 }
 if (displayed_count==0) new_c.innerHTML = "<p>No items to display</p>";
 this.itemlist_container.parentNode.appendChild(new_c);
 this.sorted_filtered_list = new_c;
 this.page_count = (this.page_size==0) ? 1 : Math.ceil(this.record_count/this.page_size);
}

bb1ItemListFiltering.prototype.displayPaginationLinks = function () {
 var d = this.d, page = this.page, page_size = this.page_size, record_count = this.record_count, page_count = this.page_count, html = "", next_html = "", prev_html = "", index = 1;
 var url = removeParamFromURL(window.location.href, "page");
 if (page_count > 1) {
  if (page > 1) {
   var prev_link = addParamToURL(url, "page", page-1, true);
   prev_html = '<div class="prev"><a href="'+prev_link+'">previous</a></div>';
  }
  do {
   var divider = (index!=1 ? " | " : " ");
   if (index == page)
    html += divider+'<span class="active">'+index+'</span>';
   else {
    var i_link = addParamToURL(url, "page", index, true);
    html += divider+'<a href="'+i_link+'">'+index+'</a>';
   }
   index++;
  } while (index <= page_count)
  if (page < page_count) {
   var next_link = addParamToURL(url, "page", page+1, true);
   next_html = '<div class="next"><a href="'+next_link+'">next</a></div>';
  }
  html = prev_html+next_html+'<div class="page-index">'+html+'</div>';
 }
 var container = this.d.createElement('DIV');
 container.id = "item_list_pagenav";
 container.innerHTML = html;
 if (this.sorted_filtered_list) this.sorted_filtered_list.appendChild(container);
}

bb1ItemListFiltering.prototype.clearSortedFiltered = function (category) {
 if (this.sorted_filtered_list!=null) {
  this.sorted_filtered_list.parentNode.removeChild(this.sorted_filtered_list);
  this.sorted_filtered_list = null;
 }
}

bb1ItemListFiltering.prototype.isPriceWithinRange = function (cell) {
 if (!cell) return null;
 var price = parseFloat(cell.getAttribute("item_price").replace(/[^\d.]/g, ""));
 if (this.price_min <= price && price <= this.price_max)
  return true;
 else
  return false;
}

bb1ItemListFiltering.prototype.displayPaginationOptions = function () {
 var page_options = this.page_options;
 var html = "";
 for (var i=0; i < page_options.length; i++) {
  var divider = (i!=0 ? " | " : "");
  var link_title = (page_options[i]!=0 ? page_options[i]+" per page" : "Show all");
  var class_name = (page_options[i]==this.page_size ? ' class="active"' : '');
  html += divider+'<a href="#" onclick="return bb1ChangePaginationOption('+page_options[i]+');"'+class_name+'>'+link_title+'</a>';
 }
 if (html=="") return;
 var container = this.d.createElement('DIV');
 container.id = "page_size_selector";
 container.innerHTML = html;
 if (this.sorted_filtered_list) this.sorted_filtered_list.parentNode.insertBefore(container, this.sorted_filtered_list);
}

bb1ItemListFiltering.prototype.displayFilterationLinks = function () {
 var options = this.price_range_options;
 var html = "";
 for (var i=0; i < options.length; i++) {
  var class_name = (nvl(options[i].min,0)==this.price_min && nvl(options[i].max,Infinity)==this.price_max ? ' class="active"' : '');
  html += '<li><a href="#" onclick="return bb1ChangePriceRangeOption('+options[i].min+','+options[i].max+');"'+class_name+'>'+options[i].title+'</a></li>';
 }
 if (html=="") return;
 var container = this.d.createElement('DIV');
 container.id = "price_range_selector";
 container.innerHTML = "<h4>Show me Products...</h4><ul>"+html+"</ul>";
 var store_category_links = this.d.getElementById("store_category_links");
 if (store_category_links) store_category_links.parentNode.insertBefore(container, store_category_links);
}

function bb1ChangePaginationOption(page_size) {
 if (!isNaN(page_size)) {
  var url = removeParamFromURL(location.href,'page');
  location.href=addParamToURL(url,'pagesize',page_size,true);
 }
 return false;
}

function bb1ChangePriceRangeOption(price_min, price_max) {
 var url = removeParamFromURL(location.href,'page');
 if (price_min&&!isNaN(price_min))
  url = addParamToURL(url,'pricemin',price_min,true);
 else
  url = removeParamFromURL(url,'pricemin');
 if (price_max&&!isNaN(price_max))
  url = addParamToURL(url,'pricemax',price_max,true);
 else
  url = removeParamFromURL(url,'pricemax');
 location.href = url;
 return false;
}

function bb1SortItemListByDescPrice(a, b) {
 var a_price = parseFloat(a.getAttribute("item_price").replace(/[^\d.]/g, ""));
 var b_price = parseFloat(b.getAttribute("item_price").replace(/[^\d.]/g, ""));
 return b_price - a_price;
}

function bb1SortItemListByAscName(a, b) {
 try {
  var a_name = a.getElementsByTagName("H4")[0].getElementsByTagName("A")[0].innerHTML.toLowerCase();
  var b_name = b.getElementsByTagName("H4")[0].getElementsByTagName("A")[0].innerHTML.toLowerCase();
  if (a_name < b_name) return -1;
  if (a_name > b_name) return 1;
  return 0;
 }
 catch (e) {
  return 0;
 }
}

function nvl(val,val2) {
 return val == null ? val2 : val;
}