$(document).ready(function(){
    var subtotal = 0;
    var total_selected_containers = 0;
    $(document).pngFix();
	$('#catcols').makeacolumnlists({cols: 4, colWidth: 0, equalHeight: 'ul', startN: 1});
    $(".details, .select-media").css("display", "none");
    $(".add-to-cart").removeAttr("disabled");
    $("#enroll").attr("disabled", "disabled").fadeTo("slow", 0.33)
    
    $(".add-to-cart").click(function(){
    
        var product = getSelectedProduct($(this));
        addToCart(product.id, product.container_id, product.title, product.price);
        $(this).fadeTo("slow", 0.33).attr("disabled", "disabled");
    });
    function getSelectedProduct(element){
        var product = $(element).parents(".item");
        var product_id = $(element).attr("id");
        var container_id = (product_id.split(":"))[1];
        var title = product.children("h3").html();
        var price = product.children(".meta").children(".price").children("span").html();
        return {
            id: product_id,
            container_id: container_id,
            title: title,
            price: price
        };
    }    
    function addToCart(id, container_id, title, price){
    
        var amount = parseFloat(price);
        var remove = "<button type=\"button\" class=\"remove\"><img src=\"/images/remove-course.jpg\" alt=\"Remove from cart\" /></button>";
        var selected_product = "<fieldset>\n" +
        "<h3>" +
        title +
        "</h3>\n" +
        "<span class=\"price\">$" +
        price +
        "</span>\n" +
        remove +
        "\n" +
        " <input type=\"hidden\" name=\"conainerId\" value=\"" +
        container_id +
        "\" />\n" +
        "</fieldset>";
        
        $(selected_product).insertBefore("#sub-totals");
        register("add", amount);
        $(".remove").click(function(){
        
            $(this).parents("fieldset").slideUp("slow").remove();
            register("subtract", amount);
            
            $(toId(id)).removeAttr("disabled").fadeTo("slow", 1);
        });
        
    }
    function register(todo, amount){
    
        subtotal = (subtotal * 100);
        amount = (amount * 100);
        
        switch (todo) {
            case "add":
                subtotal += amount;
                total_selected_containers++;
                break;
            case "subtract":
                subtotal -= amount;
                total_selected_containers--;
                break;
        }
        subtotal = Math.round(subtotal) / 100;
        
        $("#amount span").text(subtotal);
        if (total_selected_containers > 0) {
            $("#enroll").removeAttr("disabled").fadeTo("slow", 1);
            $("#enroll-note").slideUp("slow");
        }
        else {
        
            $("#enroll").attr("disabled", "disabled").fadeTo("slow", 0.33);
            $("#enroll-note").slideDown("slow");
        }
    }
    function toId(id){
        return '#' + id.replace(/(:|\.)/g, '\\$1');
    }
    function toClass(theclass){
        return '.' + theclass.replace(/(:|\.)/g, '\\$1');
    }
    
});
