// Init
$(document).ready(function(){
    // add parent class to left hand nav for styling and show after styling is done
    $('#SideCategoryList li:has(> ul)').addClass('parent');
    $('#SideCategoryList li').show();

    /*** Left Hand Nav functionality ***/
    // set up lefthand nav
    $("#SideCategoryList .parent ul").hide();

    $("#SideCategoryList .parent > a").click(function() {
        // if clicking on open tab, remove active class and slide up
        if ($(this).hasClass("Active")) {
            $("ul", $(this).parent()).slideUp("normal");
            $(this).removeClass("Active");
        // if clicking on closed tab, slide up all, slide down clicked, remove active class from all other items and add to clicked
        } else {
            $("#SideCategoryList .parent ul").slideUp("normal");
            $("ul", $(this).parent()).slideDown("normal");
            $("#SideCategoryList .parent > a").removeClass("Active");
            $(this).addClass("Active");
        }

        return false;
    });



    /*** Making fancy borders on the product listing page. ***/
    var total = $(".ProductList li").length;
    var last_row = Math.ceil(total/3);
    var n;
    var current_row;
    var link;

    $(".ProductList li").each(function(i){

        n = i+1;
        current_row = Math.ceil(n/3);

        // Get boxes in the last column.
        if( n%3 == 0 ){ 
        $(this).css("border-right","0 none"); 
        }

        // Get boxes in the last row
        if( current_row == last_row ){
        $(this).css("border-bottom","0 none"); 
        }

        // Add View Product Button
        link = $(".ProductImage a",$(this)).attr("href");
        $(this).append('<a href="'+link+'" class="view_product_button">View Product</a>');

    });



    /*** make all category page links ?sort=priceasc ***/
    // left hand nav
    $('.parent').each(function() {
        $('ul li', $(this)).each(function() {
            var catURL = $('a', $(this)).attr('href') + '?sort=priceasc';
            $('a', $(this)).attr('href', catURL);
        });
    });

});

