$(document).ready(function () {
    $('a.page,a.next,a.prev,a.view-all,a.filter').live('click', function() {
        var basepath = $(this).attr('href');
        var match_category = basepath.match(/catalog\/([0-9a-z_]+)/);
        if (!match_category) {
            match_category = basepath.match(/catalog\/([0-9a-z_]+)/);
        }
        if (!match_category) {
            match_category = basepath.match(/catalog/);
        }

        if (match_category) {
            var match_subcategory, match_page, category, subcategory, page, link;
            var new_products = false;
            
            category = match_category[1] || '';

            match_subcategory = basepath.match(/catalog\/(?:[0-9a-z_]+)\/([0-9a-z_]+)/);
            if (match_subcategory) subcategory = match_subcategory[1];

            match_page = $(this).attr('href').match(/page=([0-9]+)/);
            if (!match_page) match_page = $(this).attr('href').match(/(all)=true/);
            if (match_page) page = match_page[1];

            if (category == 'new_products') {
                category = subcategory || '';
                subcategory = '';
                new_products = true;
            } else if (category == '') {
                new_products = true;
            }

            link = '/catalog/product_list?category=' + category;
            if (subcategory) link += '&subcategory=' + subcategory;
            if (page) {
                if (page == 'all') link += '&all=true';
                else link += '&page=' + page;
            }
            if (new_products) link += '&new_products=true';

            $('#product_listing').load(link, function () {
                initContentPopups(); // reset dropdown code
            });

            return false;
        }
   });
});
