$(document).ready(function(){
    var subtotal = 0;
    var credits = 0;
    var total_selected_containers = 0;
    
    //$("#packages").accordion();
    $("#packages").tabs({ fx: { opacity: 'toggle' } });
    $(".description").css("display", "none");
    $(".add-to-cart").removeAttr("disabled");
    $("#enroll").attr("disabled", "disabled").fadeTo("slow", 0.33)
    
    
    $(".close").click(function () {              
     $(this).parents(".description").toggle("slow");
     return false;
     });
    $("a[rel='description']").click(function(){
        $('+ .description', this).toggle('slow');
        return false;
    });
    $(".add-to-cart").click(function(){
    
        //var item = $(this).attr("id");
        var product = getSelectedItem($(this));
        addToCart(product.id, product.container_id, product.title, product.price, product.credit_hrs, product.exam_req, product.format);
        $(this).fadeTo("slow", 0.33).attr("disabled", "disabled");
        //alert(product.title);
    });
    $(".media").click(function(){
        var course = getSelectedCourse($(this));
        addToCart(course.collection_id, course.container_id, course.title, course.hrs, course.exam_req, course.format, course.price);
        $(this).parents(".select-media").slideUp("slow").fadeTo("normal", 0.33);
        
        return false;
    });
    function getSelectedItem(item){
        var product = $(item).parents(".item");
        var product_id = $(item).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();
        var credit_hrs = product.children(".meta").children(".credit-hours").children("span").html();
        var format = product.children(".meta").children(".format").children("span").html();
        var exam_req = product.children(".meta").children(".exam-required").children("span").html();
        return {
            id: product_id,
            container_id: container_id,
            title: title,
            price: price,
            credit_hrs: credit_hrs,
            exam_req: exam_req,
            format: format
        };
        //alert()
    }
    function addToCart(id, container_id, title, price, credit_hrs, exam_req, format){
    
        var amount = parseFloat(price);
        var hours = parseFloat(credit_hrs);
        // alert(hours);
        var remove = "<button type=\"button\" class=\"remove\"><img src=\"/images/remove-course.jpg\" alt=\"Remove from cart\" /></button>";
        /*var selected_product = "<div class=\"item\">\n" +
         "<div class=\"title\">" +
         title +
         "</div>\n" +
         "<span class=\"price\">$" +
         price +
         "</span>\n" +
         remove +
         "\n" +
         " <input type=\"hidden\" name=\"conainerId\" value=\"" +
         container_id +
         "\" />\n" +
         "</div>";*/
        var selected_product = "<div class=\"item\">" +
        "<div class=\"title\">" +
        title +
        "</div>" +
        "<div class=\"meta\">" +
        "<div class=\"credit-hours\">Credit Hours: <span>" +
        credit_hrs +
        "</span></div>" +
        "<div class=\"exam-required\">Exam Required: <span>" +
        exam_req +
        "</span></div>" +
        "<div class=\"format\">Format: <span>" +
        format +
        "</span></div>" +
        "<button type=\"button\" title=\"remove from cart\" class=\"remove-from-cart\">" +
        "<img src=\"/images/remove-course.jpg\" alt=\"Remove Course\">" +
        "</button>" +
        "<div class=\"price\">$<span>" +
        price +
        "</span></div>" +
        "</div>" +
        "<input name=\"conainerId\" value=\"" +
        container_id +
        "\" type=\"hidden\">" +
        "</div>"
        
        $(selected_product).insertBefore("#subtotals");
        register("add", amount, hours);
        
        $(".remove-from-cart").click(function(){
            $(this).parents(".item").slideUp("slow");//.fadeOut("slow");//.remove();
            //$(this).parents(".item").remove();
            register("subtract", amount, hours);
			$(toId(id)).removeAttr("disabled").fadeTo("slow", 1);
            $(this).parents(".item").remove();
        });
        
    }
    function register(todo, amount, hours){
    
        subtotal = (subtotal * 100);
        amount = (amount * 100);
        //hours= (hours * 100);
        //alert(hours);
        
        switch (todo) {
            case "add":
                subtotal += amount;
                credits += hours;
                total_selected_containers++;
                break;
            case "subtract":
                subtotal -= amount;
                credits -= hours;
                total_selected_containers--;
                break;
        }
        subtotal = Math.round(subtotal) / 100;
        //credits=Math.round(credits) / 100;
        $("#amount span").text(subtotal);
        $("#total-hours").text(credits);
        if (total_selected_containers > 0) {
            $("#enroll").removeAttr("disabled").fadeTo("slow", 1);// ("<img
            // src=\"/images/enroll-button.png\"
            // />");
            $("#enroll-note").slideUp("slow");
        }
        else {
            // change from button to input submit
            $("#enroll").attr("disabled", "disabled").fadeTo("slow", 0.33);// .html("<img
            // src=\"/images/enroll-disabled.png\"
            // />");
            $("#enroll-note").slideDown("slow");
        }
    }
    function toId(id){
        return '#' + id.replace(/(:|\.)/g, '\\$1');
    }
    function toClass(theclass){
        return '.' + theclass.replace(/(:|\.)/g, '\\$1');
    }
    
    
});

