// This class requires a global variable
var ProductSort = Class.create();

ProductSort.prototype = {

  initialize: function(selectID, base, page) {
    $(selectID).observe("change", function(ev) {
      var element = Event.element(ev);
      var param   = element.selectedIndex == 0 ? 'popularity' : 'name';
      var url     = base + param + '/';
      if(page !== null) {
        url += page;
      } 
      window.location = url;
    });  
  }

};

Event.observe(window, 'load', function() {
  var href    = window.location.href;
  var page    = href.match("/([0-9]+|all)");
  if(page !== null && page.length > 1) {
    page = page[1]; 
  }

  var baseAsArray = baseURL.toArray();
  if(baseAsArray[baseAsArray.length - 1] != '/') {
    baseAsArray.push('/');
    base = baseAsArray.join('');
  }

  var sorter  = new ProductSort('product_sort', base, page);
});
