function initInventory() {
            $('#filter_category').change(handleCatChange);
            $('#filter_subcategory').change(handleSubCatChange);
            $('#button_search').click(handleSearch);
            $('#filter_search').change(handleSearch);
            $('#filter_price').change(handlePriceChange);
            $('.filterBox').hover(hoverFilter, hoverFilterOut);
            $('.filterBox').click(removeFilter);
            $('.itemDisplay').click(gotoDetail);
            $('.thumblink').lightBox();
            $('#sortBy').change(sort);
            $('#sortDir').change(sort);
            populateSubCat($('#filter_category').val());
}

function sort(e) {
            $sortBy = $('#sortBy').val();
            $sortDir = $('#sortDir').val();
            postData = { action: "addFilterRefresh", by: $sortBy, dir: $sortDir };
            refreshList(postData);
}

function removeFilter(e) {
    id = $(this).attr('id');
    if (id == 'search') {
                postData = { action: "addFilterRefresh", activesearch: "true" };
    }
    if (id == 'category') {
                postData = { action: "addFilterRefresh", catId: "-1" };
                $('#filter_category').val('-1');
                $('#filter_subcategory').val('-1');
                $('#filter_subcategory').css('display', 'none');
    }
    if (id == 'subcategory') {
                postData = { action: "addFilterRefresh", subCatId: "-1" };
                $('#filter_subcategory').val('-1');
    }
    if (id == 'price') {
                postData = { action: "addFilterRefresh", priceId: "-1" };
                $('#filter_price').val('-1');
    }
    refreshList(postData);
}

function handleCatChange(e) {
    populateSubCat($(this).val());
    postData = { action: "addFilterRefresh", catId: $(this).val() };
    refreshList(postData);
}

function handlePriceChange(e) {
    postData = { action: "addFilterRefresh", priceId: $(this).val() };
    refreshList(postData);
}

function handleSearch(e) {
    postData = { action: "addFilterRefresh", activesearch: "true", search: $('#filter_search').val() };
    refreshList(postData);
}

function subCatVisible(visible) {
    if (!visible) {
        $('#filter_subcategory').css('display','none');
    } else {
        if ($('#filter_subcategory').css('display') == 'none') {
            $('#filter_subcategory').css('display','inline');    
        }
        return true;
    }
    return false;
}

function handleSubCatChange(e) {
    postData = { action: "addFilterRefresh", subCatId: $(this).val() };
    refreshList(postData);
}

function populateSubCat(catId) {
    if (subCatVisible(catId!=-1)) {
        $.post(
            "/adm/php/process.php",
            { action: "populatesubcat",
              cat_id: catId },
              function(data){
                if (data.returnValue != "LessThan2") {
                    $('#filter_subcategory').html(data.returnValue);
                } else {
                    subCatVisible(false);
                }
              }, 
            "json"
        );
    }
}

function refreshList(postData) {
    $('#tier2_right').html("<h3>Loading...</h3>");
    $.post(
        "/adm/php/process.php",
        postData,
        function(data) {
            $('#tier2_right').html(data.returnValue);
            $('#updateFilters').html(data.filters);
            $('.filterBox').hover(hoverFilter, hoverFilterOut);
            $('.filterBox').click(removeFilter);
            $('.itemDisplay').click(gotoDetail);
        },
        "json"
    );
}

var filterHtml;
function hoverFilter(e) {
    filterHtml = $(this).html();
    $(this).html("<img src='/adm/images/filter_x.gif' />Remove?");
}

function hoverFilterOut(e) {
    $(this).html(filterHtml);
}

function gotoDetail(e) {
    location.href='/inventory/detail.php?id=' + $(this).attr('id');
}

