var age=0;
var ageLow=0;
var ageHigh=0;
var priceLow=0;
var priceHigh=0;

function changeCategory(el) {
    var categoryURL = el.value;
    window.location = categoryURL;
}

function changeAge(el, productsPerRow) {
    ageRange = el.value;
    ageRangeParts = ageRange.split("-");
    ageLow = ageRangeParts[0];
    ageHigh = ageRangeParts[1];
    filterProductList(productsPerRow);
}

function changePrice(el, productsPerRow) {
    priceRange = el.value;
    priceRangeParts = priceRange.split("-");
    priceLow = priceRangeParts[0];
    priceHigh = priceRangeParts[1];
    filterProductList(productsPerRow);
}

function filterProductList(productsPerRow) {
    var listingEl = $(".listing");
    var listings = listingEl.children("li");
    var enabledProductCount = 0;

    listings.each(
        // The "indIndex" is the
        // loop iteration index on the current element.
        function( intIndex ){
         
            productAge = parseInt($( this ).children("#age")[0].innerHTML);
            productPrice = parseFloat($( this ).children("#price")[0].innerHTML);
            
            doPriceFilter = false;
            doAgeFilter = false;
            
            if (priceLow > 0 && priceHigh > 0) {
                doPriceFilter = true;
            }
            
            if (ageLow > 0 && ageHigh > 0) {
                doAgeFilter = true;
            }
            
            
            if (doPriceFilter && doAgeFilter) {
               // alert( "index: " + intIndex + " " + age);
                if (productPrice <= priceHigh && productPrice >= priceLow && productAge <= ageHigh && productAge >= ageLow) {
                    $( this ).css("display", "block");
                    enabledProductCount=enabledProductCount+1;
                } else {
                    $( this ).css("display", "none");
                }
            } else if (doPriceFilter) {
                //alert("productPrice = [" + productPrice + "]\npriceHigh=[" + priceHigh + "]\npriceLow=[" + priceLow + "]");
                
                if (productPrice <= priceHigh && productPrice >= priceLow) {
                    $( this ).css("display", "block");
                    enabledProductCount=enabledProductCount+1;
                } else {
                    $( this ).css("display", "none");
                }
            
            } else if (doAgeFilter) {
                if (productAge <= ageHigh && productAge >= ageLow) {
                    $( this ).css("display", "block");
                    enabledProductCount=enabledProductCount+1;
                } else {
                    $( this ).css("display", "none");
                }
            } else {
                //No filter - display all...
                $( this ).css("display", "block");
                enabledProductCount=enabledProductCount+1;
            }
            
            if (enabledProductCount % productsPerRow == 0) {
                $( this ).addClass("last");
            } else {
                $( this ).removeClass("last");
            }

            
        }
        );
        
        if (enabledProductCount == 0) {
            $("#noResults").css("display", "block");
        } else {
            $("#noResults").css("display", "none");
        }
}


