
  $(function() {
    var filters = [];
    $("#products a").click(function() {
      var returnStr = "";
      $("#filters a.selected").each(function() {
        if($(this).parent().hasClass("second")) {
          if($(this).parent().find("a.selected").length == 1) {
            returnStr += "." + this.id;
          }
        } else if($(this).parent().hasClass("third")) {
          returnStr += "." + this.id;
        } else if($(this).parent().parent().find("a.selected").length == 1) {
          returnStr += "." + this.id;
        }
      });
      this.href = this.href + "&return=" + returnStr;
      return true;
    });
    $("#filters a").click(function() {
      if(filters[this.id] || $(this).hasClass("selected")) {
        filters[this.id] = null;
        $(this).removeClass("selected");
        $(this).parent().removeClass("selected");
        filterRemoved(this);
      } else {
        filters[this.id] = generateFilter(this);
        $(this).addClass("selected");
        $(this).parent().addClass("selected");
      }

      if(filtersNotEmpty()) {
        var show = [], hide = [];
        $("#products .product").each(function(){
          if(hasAnyFilter(this)) {
            if($(this).css("opacity") != 1 || $(this).css("display") == "none") {
              $(this).css("opacity", 0);
              $(this).css("height", "0px");
              $(this).css("width", "0px");
              show.push(this);
            }
          } else if($(this).css("opacity") != 0 && $(this).css("display") != "none") {
            hide.push(this);
          }
        });

        $(hide).animate({ width: "0px", height: "0px", opacity: 0 }, 750, "swing", function() { $(this).hide(); });
        $(show).show();
        $(show).animate({ width: "135px", height: "165px", opacity: 1 }, 750, "swing");
      } else {
        $("#products .product").animate({ width: "135px", height: "165px", opacity: 1 }, 750, "swing");
      }
      return false;
    });
    var generateFilter = function(node) {
      if($(node).parent().hasClass("first") && $(node).parent().parent().find("a.selected").length == 0) { // only select first level if nothign else is selectd
        return "." + node.id;
      } else if($(node).parent().hasClass("second")) {
        $(node).parent().parent().parent().find("a:first").addClass("selected");
        $(node).parent().parent().parent().find("div:first").addClass("selected");
        filters[$(node).parent().parent().parent().find("a:first").attr("id")] = null; // ensure the more generic filter is null
        return "." + $(node).parent().parent().parent().find("a:first").attr("id") + "." + node.id;
      } else if($(node).parent().hasClass("third")) {
        $(node).parent().parent().parent().find("a:first").addClass("selected");
        $(node).parent().parent().parent().addClass("selected");
        $(node).parent().parent().parent().parent().parent().find("a:first").addClass("selected");
        $(node).parent().parent().parent().parent().parent().find("div:first").addClass("selected");
        filters[$(node).parent().parent().parent().find("a:first").attr("id")] = null; // ensure the more generic filter is null
        return "." + $(node).parent().parent().parent().parent().parent().find("a:first").attr("id") + "." + $(node).parent().parent().parent().find("a:first").attr("id") + "." + node.id;
      }
    };
    var filterRemoved = function(node) {
      if($(node).parent().hasClass("first")) {
        $(node).parent().parent().find("a.selected").each(function() {
          filters[this.id] = null;
          $(this).removeClass("selected");
          $(this).parent().removeClass("selected");
        });
      } else if($(node).parent().hasClass("second")) {
        $(node).parent().find("a.selected").each(function() {
          // remove all 3rd level selected nodes
          filters[this.id] = null;
          $(this).removeClass("selected");
        });
        if($(node).parent().parent().parent().find("a.selected").length == 1 && $(node).parent().parent().parent().find("a:first").hasClass("selected")) {
          // nothing else is selected, ensure the top level filter is active if its checked
          var parentId = $(node).parent().parent().parent().find("a:first").attr("id");
          filters[parentId] = "." + parentId;
        }
      } else if($(node).parent().hasClass("third")) {
        if($(node).parent().parent().find("a.selected").length == 0) {
          // no other 3rd level selected, ensure 2nd level is
          var parentId = $(node).parent().parent().parent().find("a:first").attr("id");
          filters[parentId] = "." + parentId;
        }
      }
    };
    var hasAnyFilter = function(node) {
      for(id in filters) {
        if(filters[id]) {
          var classes = filters[id].split(".");
          var hasAll = true;
          for(var j=0; j<classes.length; j++) {
            if(!$(node).hasClass(classes[j])) {
              hasAll = false;
            }
          }
          if(hasAll) { return true; }
        }
      }
      return false;
    };
    var filtersNotEmpty = function() {
      for(id in filters) {
        if(filters[id]) {
          return true;
        }
      }
      return false;
    }

    $("#sortBy").change(function() {
      var sortBy = $(this).val();
      $("#products").hide();
      var products = [];
      $("#products .product").each(function() { products.push(this); });
      products.sort(function(a, b) {
        if(sortBy == "price") {
          return parseFloat($(a).find(".price:first").html()) - parseFloat($(b).find(".price:first").html());
        } else {
          return parseInt($(a).find(".order:first").val(), 10) - parseInt($(b).find(".order:first").val(), 10);
        }
      });
      $("#products").html("");
      $(products).each(function() {
        $("#products").append(this);
      });
      $("#products").show();
    });
    $("#filters a.selected").each(function(){
      // page load filtering
      if($(this).hasClass("selected")) {
        filters[this.id] = generateFilter(this);
      }
    });
  });
